annotate src/c/urweb.c @ 1118:87b0a9d08e73

uw_close()
author Adam Chlipala <adamc@hcoop.net>
date Tue, 05 Jan 2010 14:57:35 -0500
parents 31ef567fa772
children 951fced704d6
rev   line source
adamc@1094 1 #define _XOPEN_SOURCE 600
adamc@436 2
adamc@117 3 #include <stdlib.h>
adamc@102 4 #include <stdio.h>
adamc@117 5 #include <string.h>
adamc@457 6 #include <strings.h>
adamc@106 7 #include <ctype.h>
adamc@167 8 #include <setjmp.h>
adamc@167 9 #include <stdarg.h>
adamc@682 10 #include <assert.h>
adamc@741 11 #include <ctype.h>
adamc@1114 12 #include <limits.h>
adamc@1113 13 #include <stdint.h>
adamc@1094 14 #include <sys/types.h>
adamc@1094 15 #include <sys/socket.h>
adamc@102 16
adamc@667 17 #include <pthread.h>
adamc@667 18
adamc@102 19 #include "types.h"
adamc@102 20
adamc@311 21 uw_unit uw_unit_v = {};
adamc@102 22
adamc@667 23
adamc@667 24 // Socket extras
adamc@667 25
adamc@863 26 int uw_really_send(int sock, const void *buf, ssize_t len) {
adamc@667 27 while (len > 0) {
adamc@856 28 ssize_t n = send(sock, buf, len, 0);
adamc@856 29
adamc@856 30 if (n < 0)
adamc@856 31 return n;
adamc@856 32
adamc@856 33 buf += n;
adamc@856 34 len -= n;
adamc@856 35 }
adamc@856 36
adamc@856 37 return 0;
adamc@856 38 }
adamc@856 39
adamc@856 40 int uw_really_write(int fd, const void *buf, size_t len) {
adamc@856 41 while (len > 0) {
adamc@856 42 ssize_t n = write(fd, buf, len);
adamc@667 43
adamc@667 44 if (n < 0)
adamc@667 45 return n;
adamc@667 46
adamc@667 47 buf += n;
adamc@667 48 len -= n;
adamc@667 49 }
adamc@667 50
adamc@667 51 return 0;
adamc@667 52 }
adamc@667 53
adamc@667 54
adamc@667 55 // Buffers
adamc@667 56
adamc@667 57 typedef struct {
adamc@1113 58 size_t max;
adamc@667 59 char *start, *front, *back;
adamc@667 60 } buf;
adamc@667 61
adamc@1113 62 static void buf_init(size_t max, buf *b, size_t s) {
adamc@1113 63 b->max = max;
adamc@667 64 b->front = b->start = malloc(s);
adamc@667 65 b->back = b->front + s;
adamc@667 66 }
adamc@667 67
adamc@667 68 static void buf_free(buf *b) {
adamc@667 69 free(b->start);
adamc@667 70 }
adamc@667 71
adamc@667 72 static void buf_reset(buf *b) {
adamc@667 73 b->front = b->start;
adamc@667 74 }
adamc@667 75
adamc@1113 76 static int buf_check(buf *b, size_t extra) {
adamc@667 77 if (b->back - b->front < extra) {
adamc@667 78 size_t desired = b->front - b->start + extra, next;
adamc@667 79 char *new_heap;
adamc@667 80
adamc@667 81 next = b->back - b->start;
adamc@667 82 if (next == 0)
adamc@667 83 next = 1;
adamc@667 84 for (; next < desired; next *= 2);
adamc@667 85
adamc@1113 86 if (next > b->max)
adamc@1113 87 if (desired <= b->max)
adamc@1113 88 next = desired;
adamc@1113 89 else
adamc@1113 90 return 1;
adamc@1113 91
adamc@667 92 new_heap = realloc(b->start, next);
adamc@667 93 b->front = new_heap + (b->front - b->start);
adamc@667 94 b->back = new_heap + next;
adamc@667 95 b->start = new_heap;
adamc@667 96 }
adamc@1113 97
adamc@1113 98 return 0;
adamc@1113 99 }
adamc@1113 100
adamc@1113 101 __attribute__((noreturn)) void uw_error(uw_context, failure_kind, const char *, ...);
adamc@1113 102
adamc@1113 103 static void ctx_buf_check(uw_context ctx, const char *kind, buf *b, size_t extra) {
adamc@1113 104 if (buf_check(b, extra))
adamc@1113 105 uw_error(ctx, FATAL, "Memory limit exceeded (%s)", kind);
adamc@667 106 }
adamc@667 107
adamc@667 108 static size_t buf_used(buf *b) {
adamc@667 109 return b->front - b->start;
adamc@667 110 }
adamc@667 111
adamc@667 112 static size_t buf_avail(buf *b) {
adamc@667 113 return b->back - b->start;
adamc@667 114 }
adamc@667 115
adamc@1113 116 static int buf_append(buf *b, const char *s, size_t len) {
adamc@1113 117 if (buf_check(b, len+1))
adamc@1113 118 return 1;
adamc@1113 119
adamc@1113 120 memcpy(b->front, s, len);
adamc@1113 121 b->front += len;
adamc@1113 122 *b->front = 0;
adamc@1113 123
adamc@1113 124 return 0;
adamc@1113 125 }
adamc@1113 126
adamc@1113 127 static void ctx_buf_append(uw_context ctx, const char *kind, buf *b, const char *s, size_t len) {
adamc@1113 128 ctx_buf_check(ctx, kind, b, len+1);
adamc@1113 129
adamc@668 130 memcpy(b->front, s, len);
adamc@668 131 b->front += len;
adamc@672 132 *b->front = 0;
adamc@668 133 }
adamc@667 134
adamc@668 135
adamc@672 136 // Persistent state types
adamc@667 137
adamc@667 138 typedef enum { UNUSED, USED } usage;
adamc@667 139
adamc@667 140 typedef struct client {
adamc@682 141 unsigned id;
adamc@667 142 usage mode;
adamc@682 143 int pass;
adamc@682 144 struct client *next;
adamc@689 145 pthread_mutex_t lock, pull_lock;
adamc@682 146 buf msgs;
adamc@682 147 int sock;
adamc@863 148 int (*send)(int sockfd, const void *buf, ssize_t len);
adamc@863 149 int (*close)(int fd);
adamc@682 150 time_t last_contact;
adamc@682 151 unsigned n_channels;
adamc@685 152 unsigned refcount;
adamc@667 153 } client;
adamc@667 154
adamc@672 155
adamc@672 156 // Persistent client state
adamc@672 157
adamc@682 158 static client **clients, *clients_free, *clients_used;
adamc@682 159 static unsigned n_clients;
adamc@667 160
adamc@667 161 static pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER;
adamc@667 162
adamc@1113 163 size_t uw_messages_max = SIZE_MAX;
adamc@1113 164 size_t uw_clients_max = SIZE_MAX;
adamc@1113 165
adamc@682 166 static client *new_client() {
adamc@667 167 client *c;
adamc@667 168
adamc@667 169 pthread_mutex_lock(&clients_mutex);
adamc@667 170
adamc@667 171 if (clients_free) {
adamc@667 172 c = clients_free;
adamc@682 173 clients_free = clients_free->next;
adamc@667 174 }
adamc@1113 175 else if (n_clients >= uw_clients_max)
adamc@1113 176 return NULL;
adamc@667 177 else {
adamc@667 178 ++n_clients;
adamc@667 179 clients = realloc(clients, sizeof(client) * n_clients);
adamc@667 180 c = malloc(sizeof(client));
adamc@667 181 c->id = n_clients-1;
adamc@682 182 pthread_mutex_init(&c->lock, NULL);
adamc@689 183 pthread_mutex_init(&c->pull_lock, NULL);
adamc@1113 184 buf_init(uw_messages_max, &c->msgs, 0);
adamc@667 185 clients[n_clients-1] = c;
adamc@667 186 }
adamc@667 187
adamc@682 188 pthread_mutex_lock(&c->lock);
adamc@667 189 c->mode = USED;
adamc@682 190 c->pass = rand();
adamc@682 191 c->sock = -1;
adamc@682 192 c->last_contact = time(NULL);
adamc@682 193 buf_reset(&c->msgs);
adamc@682 194 c->n_channels = 0;
adamc@685 195 c->refcount = 0;
adamc@682 196 pthread_mutex_unlock(&c->lock);
adamc@682 197
adamc@682 198 c->next = clients_used;
adamc@682 199 clients_used = c;
adamc@667 200
adamc@667 201 pthread_mutex_unlock(&clients_mutex);
adamc@667 202
adamc@667 203 return c;
adamc@667 204 }
adamc@667 205
adamc@685 206 static void use_client(client *c) {
adamc@685 207 pthread_mutex_lock(&c->lock);
adamc@685 208 ++c->refcount;
adamc@685 209 pthread_mutex_unlock(&c->lock);
adamc@689 210 pthread_mutex_lock(&c->pull_lock);
adamc@685 211 }
adamc@685 212
adamc@685 213 static void release_client(client *c) {
adamc@689 214 pthread_mutex_unlock(&c->pull_lock);
adamc@685 215 pthread_mutex_lock(&c->lock);
adamc@685 216 --c->refcount;
adamc@685 217 pthread_mutex_unlock(&c->lock);
adamc@689 218
adamc@685 219 }
adamc@685 220
adamc@856 221 static const char begin_msgs[] = "Content-type: text/plain\r\n\r\n";
adamc@667 222
adamc@682 223 static client *find_client(unsigned id) {
adamc@667 224 client *c;
adamc@667 225
adamc@667 226 pthread_mutex_lock(&clients_mutex);
adamc@667 227
adamc@667 228 if (id >= n_clients) {
adamc@667 229 pthread_mutex_unlock(&clients_mutex);
adamc@668 230 return NULL;
adamc@667 231 }
adamc@667 232
adamc@667 233 c = clients[id];
adamc@667 234
adamc@668 235 pthread_mutex_unlock(&clients_mutex);
adamc@668 236 return c;
adamc@668 237 }
adamc@668 238
adamc@856 239 static char *on_success = "HTTP/1.1 200 OK\r\n";
adamc@1065 240 static char *on_redirect = "HTTP/1.1 303 See Other\r\n";
adamc@856 241
adamc@856 242 void uw_set_on_success(char *s) {
adamc@856 243 on_success = s;
adamc@856 244 }
adamc@856 245
adamc@863 246 void uw_client_connect(unsigned id, int pass, int sock,
adamc@863 247 int (*send)(int sockfd, const void *buf, ssize_t len),
adamc@864 248 int (*close)(int fd),
adamc@864 249 void *logger_data, uw_logger log_error) {
adamc@682 250 client *c = find_client(id);
adamc@668 251
adamc@668 252 if (c == NULL) {
adamc@667 253 close(sock);
adamc@864 254 log_error(logger_data, "Out-of-bounds client request (%u)\n", id);
adamc@667 255 return;
adamc@667 256 }
adamc@667 257
adamc@682 258 pthread_mutex_lock(&c->lock);
adamc@671 259
adamc@682 260 if (c->mode != USED) {
adamc@682 261 pthread_mutex_unlock(&c->lock);
adamc@667 262 close(sock);
adamc@864 263 log_error(logger_data, "Client request for unused slot (%u)\n", id);
adamc@667 264 return;
adamc@667 265 }
adamc@667 266
adamc@682 267 if (pass != c->pass) {
adamc@682 268 pthread_mutex_unlock(&c->lock);
adamc@682 269 close(sock);
adamc@864 270 log_error(logger_data, "Wrong client password (%u, %d)\n", id, pass);
adamc@682 271 return;
adamc@667 272 }
adamc@667 273
adamc@682 274 if (c->sock != -1) {
adamc@863 275 c->close(c->sock);
adamc@682 276 c->sock = -1;
adamc@682 277 }
adamc@667 278
adamc@682 279 c->last_contact = time(NULL);
adamc@682 280
adamc@682 281 if (buf_used(&c->msgs) > 0) {
adamc@863 282 send(sock, on_success, strlen(on_success));
adamc@863 283 send(sock, begin_msgs, sizeof(begin_msgs) - 1);
adamc@863 284 send(sock, c->msgs.start, buf_used(&c->msgs));
adamc@682 285 buf_reset(&c->msgs);
adamc@667 286 close(sock);
adamc@667 287 }
adamc@863 288 else {
adamc@682 289 c->sock = sock;
adamc@863 290 c->send = send;
adamc@863 291 c->close = close;
adamc@863 292 }
adamc@667 293
adamc@682 294 pthread_mutex_unlock(&c->lock);
adamc@667 295 }
adamc@667 296
adamc@682 297 static void free_client(client *c) {
adamc@682 298 c->mode = UNUSED;
adamc@682 299 c->pass = -1;
adamc@667 300
adamc@682 301 c->next = clients_free;
adamc@682 302 clients_free = c;
adamc@667 303 }
adamc@667 304
adamc@682 305 static uw_Basis_channel new_channel(client *c) {
adamc@682 306 uw_Basis_channel ch = {c->id, c->n_channels++};
adamc@668 307 return ch;
adamc@668 308 }
adamc@668 309
adamc@685 310 static void client_send(client *c, buf *msg) {
adamc@685 311 pthread_mutex_lock(&c->lock);
adamc@671 312
adamc@682 313 if (c->sock != -1) {
adamc@863 314 c->send(c->sock, on_success, strlen(on_success));
adamc@863 315 c->send(c->sock, begin_msgs, sizeof(begin_msgs) - 1);
adamc@863 316 c->send(c->sock, msg->start, buf_used(msg));
adamc@863 317 c->close(c->sock);
adamc@682 318 c->sock = -1;
adamc@1113 319 } else if (buf_append(&c->msgs, msg->start, buf_used(msg)))
adamc@1113 320 fprintf(stderr, "Client message buffer size exceeded");
adamc@671 321
adamc@685 322 pthread_mutex_unlock(&c->lock);
adamc@668 323 }
adamc@668 324
adamc@668 325
adamc@668 326 // Global entry points
adamc@668 327
adamc@1104 328 extern void uw_init_crypto();
adamc@1104 329
adamc@668 330 void uw_global_init() {
adamc@668 331 srand(time(NULL) ^ getpid());
adamc@668 332
adamc@668 333 clients = malloc(0);
adamc@1104 334
adamc@1104 335 uw_init_crypto();
adamc@1094 336 }
adamc@1094 337
adamc@1094 338 void uw_app_init(uw_app *app) {
adamc@1094 339 app->client_init();
adamc@668 340 }
adamc@668 341
adamc@1114 342 int uw_time = 0;
adamc@1114 343
adamc@668 344
adamc@667 345 // Single-request state
adamc@667 346
adamc@167 347 #define ERROR_BUF_LEN 1024
adamc@167 348
adamc@323 349 typedef struct regions {
adamc@323 350 struct regions *next;
adamc@323 351 } regions;
adamc@323 352
adamc@425 353 typedef struct {
adamc@425 354 void (*func)(void*);
adamc@425 355 void *arg;
adamc@425 356 } cleanup;
adamc@425 357
adamc@671 358 typedef struct {
adamc@682 359 unsigned client;
adamc@671 360 buf msgs;
adamc@682 361 } delta;
adamc@671 362
adamc@737 363 typedef enum {
adamc@759 364 UNSET, NORMAL, FIL, SUBFORM, SUBFORMS, ENTRY
adamc@737 365 } input_kind;
adamc@737 366
adamc@756 367 typedef struct input {
adamc@737 368 input_kind kind;
adamc@737 369 union {
adamc@737 370 char *normal;
adamc@739 371 uw_Basis_file file;
adamc@756 372 struct {
adamc@759 373 struct input *fields, *parent;
adamc@756 374 } subform;
adamc@759 375 struct {
adamc@759 376 struct input *entries, *parent;
adamc@759 377 } subforms;
adamc@759 378 struct {
adamc@759 379 struct input *fields, *next, *parent;
adamc@759 380 } entry;
adamc@737 381 } data;
adamc@737 382 } input;
adamc@737 383
adamc@771 384 typedef struct {
adamc@771 385 void *data;
adamc@771 386 uw_callback commit, rollback, free;
adamc@771 387 } transactional;
adamc@771 388
adamc@1061 389 typedef struct {
adamc@1061 390 char *name;
adamc@1061 391 void *data;
adamc@1061 392 void (*free)(void*);
adamc@1061 393 } global;
adamc@1061 394
adamc@311 395 struct uw_context {
adamc@1094 396 uw_app *app;
adamc@1094 397
adamc@854 398 char *(*get_header)(void *, const char *);
adamc@854 399 void *get_header_data;
adamc@457 400
adamc@666 401 buf outHeaders, page, heap, script;
adamc@1065 402 int returning_indirectly;
adamc@759 403 input *inputs, *subinputs, *cur_container;
adamc@1094 404 size_t sz_inputs, n_subinputs, used_subinputs;
adamc@167 405
adamc@565 406 int source_count;
adamc@562 407
adamc@272 408 void *db;
adamc@272 409
adamc@167 410 jmp_buf jmp_buf;
adamc@167 411
adamc@323 412 regions *regions;
adamc@323 413
adamc@425 414 cleanup *cleanup, *cleanup_front, *cleanup_back;
adamc@425 415
adamc@1094 416 const char *script_header;
adamc@643 417
adamc@736 418 int needs_push, needs_sig;
adamc@693 419
adamc@682 420 size_t n_deltas, used_deltas;
adamc@682 421 delta *deltas;
adamc@671 422
adamc@682 423 client *client;
adamc@682 424
adamc@771 425 transactional *transactionals;
adamc@771 426 size_t n_transactionals, used_transactionals;
adamc@771 427
adamc@1061 428 global *globals;
adamc@1061 429 size_t n_globals;
adamc@1061 430
adamc@1066 431 char *current_url;
adamc@1066 432
adamc@1114 433 int deadline;
adamc@1114 434
adamc@167 435 char error_message[ERROR_BUF_LEN];
adamc@117 436 };
adamc@117 437
adamc@1113 438 size_t uw_headers_max = SIZE_MAX;
adamc@1113 439 size_t uw_page_max = SIZE_MAX;
adamc@1113 440 size_t uw_heap_max = SIZE_MAX;
adamc@1113 441 size_t uw_script_max = SIZE_MAX;
adamc@1113 442
adamc@687 443 uw_context uw_init() {
adamc@311 444 uw_context ctx = malloc(sizeof(struct uw_context));
adamc@136 445
adamc@1094 446 ctx->app = NULL;
adamc@1094 447
adamc@854 448 ctx->get_header = NULL;
adamc@854 449 ctx->get_header_data = NULL;
adamc@457 450
adamc@1113 451 buf_init(uw_headers_max, &ctx->outHeaders, 0);
adamc@1113 452 buf_init(uw_page_max, &ctx->page, 0);
adamc@1065 453 ctx->returning_indirectly = 0;
adamc@1113 454 buf_init(uw_heap_max, &ctx->heap, 0);
adamc@1113 455 buf_init(uw_script_max, &ctx->script, 1);
adamc@667 456 ctx->script.start[0] = 0;
adamc@136 457
adamc@1094 458 ctx->inputs = malloc(0);
adamc@759 459 ctx->cur_container = NULL;
adamc@756 460 ctx->subinputs = malloc(0);
adamc@1094 461 ctx->sz_inputs = ctx->n_subinputs = ctx->used_subinputs = 0;
adamc@144 462
adamc@272 463 ctx->db = NULL;
adamc@272 464
adamc@323 465 ctx->regions = NULL;
adamc@323 466
adamc@425 467 ctx->cleanup_front = ctx->cleanup_back = ctx->cleanup = malloc(0);
adamc@425 468
adamc@643 469 ctx->script_header = "";
adamc@693 470 ctx->needs_push = 0;
adamc@736 471 ctx->needs_sig = 0;
adamc@1104 472
adamc@565 473 ctx->source_count = 0;
adamc@562 474
adamc@682 475 ctx->n_deltas = ctx->used_deltas = 0;
adamc@671 476 ctx->deltas = malloc(0);
adamc@671 477
adamc@700 478 ctx->client = NULL;
adamc@700 479
adamc@700 480 ctx->error_message[0] = 0;
adamc@700 481
adamc@771 482 ctx->transactionals = malloc(0);
adamc@771 483 ctx->n_transactionals = ctx->used_transactionals = 0;
adamc@771 484
adamc@1061 485 ctx->globals = malloc(0);
adamc@1061 486 ctx->n_globals = 0;
adamc@1061 487
adamc@1066 488 ctx->current_url = "";
adamc@1066 489
adamc@1114 490 ctx->deadline = INT_MAX;
adamc@1114 491
adamc@117 492 return ctx;
adamc@106 493 }
adamc@106 494
adamc@1113 495 size_t uw_inputs_max = SIZE_MAX;
adamc@1113 496
adamc@1113 497 int uw_set_app(uw_context ctx, uw_app *app) {
adamc@1094 498 ctx->app = app;
adamc@1094 499
adamc@1105 500 if (app && app->inputs_len > ctx->sz_inputs) {
adamc@1113 501 if (app->inputs_len > uw_inputs_max)
adamc@1113 502 return 1;
adamc@1113 503
adamc@1094 504 ctx->sz_inputs = app->inputs_len;
adamc@1094 505 ctx->inputs = realloc(ctx->inputs, ctx->sz_inputs * sizeof(input));
adamc@1094 506 memset(ctx->inputs, 0, ctx->sz_inputs * sizeof(input));
adamc@1094 507 }
adamc@1094 508 }
adamc@1094 509
adamc@311 510 void uw_set_db(uw_context ctx, void *db) {
adamc@272 511 ctx->db = db;
adamc@272 512 }
adamc@272 513
adamc@311 514 void *uw_get_db(uw_context ctx) {
adamc@272 515 return ctx->db;
adamc@272 516 }
adamc@272 517
adamc@311 518 void uw_free(uw_context ctx) {
adamc@671 519 size_t i;
adamc@671 520
adamc@666 521 buf_free(&ctx->outHeaders);
adamc@666 522 buf_free(&ctx->script);
adamc@666 523 buf_free(&ctx->page);
adamc@666 524 buf_free(&ctx->heap);
adamc@144 525 free(ctx->inputs);
adamc@756 526 free(ctx->subinputs);
adamc@425 527 free(ctx->cleanup);
adamc@771 528 free(ctx->transactionals);
adamc@671 529
adamc@682 530 for (i = 0; i < ctx->n_deltas; ++i)
adamc@671 531 buf_free(&ctx->deltas[i].msgs);
adamc@671 532
adamc@1061 533 for (i = 0; i < ctx->n_globals; ++i)
adamc@1089 534 if (ctx->globals[i].free)
adamc@1089 535 ctx->globals[i].free(ctx->globals[i].data);
adamc@1061 536
adamc@136 537 free(ctx);
adamc@136 538 }
adamc@136 539
adamc@562 540 void uw_reset_keep_error_message(uw_context ctx) {
adamc@756 541 size_t i;
adamc@756 542
adamc@666 543 buf_reset(&ctx->outHeaders);
adamc@666 544 buf_reset(&ctx->script);
adamc@667 545 ctx->script.start[0] = 0;
adamc@666 546 buf_reset(&ctx->page);
adamc@1065 547 ctx->returning_indirectly = 0;
adamc@666 548 buf_reset(&ctx->heap);
adamc@323 549 ctx->regions = NULL;
adamc@425 550 ctx->cleanup_front = ctx->cleanup;
adamc@565 551 ctx->source_count = 0;
adamc@682 552 ctx->used_deltas = 0;
adamc@682 553 ctx->client = NULL;
adamc@759 554 ctx->cur_container = NULL;
adamc@771 555 ctx->used_transactionals = 0;
adamc@1117 556 ctx->script_header = "";
adamc@167 557 }
adamc@167 558
adamc@562 559 void uw_reset_keep_request(uw_context ctx) {
adamc@562 560 uw_reset_keep_error_message(ctx);
adamc@562 561 ctx->error_message[0] = 0;
adamc@167 562 }
adamc@167 563
adamc@311 564 void uw_reset(uw_context ctx) {
adamc@311 565 uw_reset_keep_request(ctx);
adamc@1105 566 if (ctx->app)
adamc@1105 567 memset(ctx->inputs, 0, ctx->app->inputs_len * sizeof(input));
adamc@756 568 memset(ctx->subinputs, 0, ctx->n_subinputs * sizeof(input));
adamc@756 569 ctx->used_subinputs = 0;
adamc@144 570 }
adamc@144 571
adamc@311 572 failure_kind uw_begin_init(uw_context ctx) {
adamc@272 573 int r = setjmp(ctx->jmp_buf);
adamc@272 574
adamc@1105 575 if (r == 0 && ctx->app)
adamc@1094 576 ctx->app->db_init(ctx);
adamc@272 577
adamc@272 578 return r;
adamc@272 579 }
adamc@272 580
adamc@1118 581 void uw_close(uw_context ctx) {
adamc@1118 582 ctx->app->db_close(ctx);
adamc@1118 583 }
adamc@1118 584
adamc@854 585 void uw_set_headers(uw_context ctx, char *(*get_header)(void *, const char *), void *get_header_data) {
adamc@854 586 ctx->get_header = get_header;
adamc@854 587 ctx->get_header_data = get_header_data;
adamc@742 588 }
adamc@742 589
adamc@801 590 static void uw_set_error(uw_context ctx, const char *fmt, ...) {
adamc@801 591 va_list ap;
adamc@801 592 va_start(ap, fmt);
adamc@801 593
adamc@801 594 vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap);
adamc@801 595 }
adamc@801 596
adamc@311 597 __attribute__((noreturn)) void uw_error(uw_context ctx, failure_kind fk, const char *fmt, ...) {
adamc@425 598 cleanup *cl;
adamc@425 599
adamc@167 600 va_list ap;
adamc@167 601 va_start(ap, fmt);
adamc@167 602
adamc@167 603 vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap);
adamc@167 604
adamc@425 605 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@425 606 cl->func(cl->arg);
adamc@425 607
adamc@425 608 ctx->cleanup_front = ctx->cleanup;
adamc@425 609
adamc@190 610 longjmp(ctx->jmp_buf, fk);
adamc@167 611 }
adamc@167 612
adamc@1113 613 size_t uw_cleanup_max = SIZE_MAX;
adamc@1113 614
adamc@425 615 void uw_push_cleanup(uw_context ctx, void (*func)(void *), void *arg) {
adamc@425 616 if (ctx->cleanup_front >= ctx->cleanup_back) {
adamc@425 617 int len = ctx->cleanup_back - ctx->cleanup, newLen;
adamc@425 618 if (len == 0)
adamc@425 619 newLen = 1;
adamc@425 620 else
adamc@428 621 newLen = len * 2;
adamc@1113 622
adamc@1113 623 if (newLen > uw_cleanup_max)
adamc@1113 624 if (len+1 <= uw_cleanup_max)
adamc@1113 625 newLen = uw_cleanup_max;
adamc@1113 626 else
adamc@1113 627 uw_error(ctx, FATAL, "Exceeded limit on number of cleanup handlers");
adamc@1113 628
adamc@470 629 ctx->cleanup = realloc(ctx->cleanup, newLen * sizeof(cleanup));
adamc@425 630 ctx->cleanup_front = ctx->cleanup + len;
adamc@425 631 ctx->cleanup_back = ctx->cleanup + newLen;
adamc@425 632 }
adamc@425 633
adamc@425 634 ctx->cleanup_front->func = func;
adamc@425 635 ctx->cleanup_front->arg = arg;
adamc@425 636 ++ctx->cleanup_front;
adamc@425 637 }
adamc@425 638
adamc@682 639 uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) {
adamc@854 640 return ctx->get_header(ctx->get_header_data, h);
adamc@682 641 }
adamc@682 642
adamc@682 643 void uw_login(uw_context ctx) {
adamc@693 644 if (ctx->needs_push) {
adamc@682 645 char *id_s, *pass_s;
adamc@682 646
adamc@682 647 if ((id_s = uw_Basis_requestHeader(ctx, "UrWeb-Client"))
adamc@682 648 && (pass_s = uw_Basis_requestHeader(ctx, "UrWeb-Pass"))) {
adamc@682 649 unsigned id = atoi(id_s);
adamc@682 650 int pass = atoi(pass_s);
adamc@682 651 client *c = find_client(id);
adamc@682 652
adamc@682 653 if (c == NULL)
adamc@682 654 uw_error(ctx, FATAL, "Unknown client ID in HTTP headers (%s, %s)", id_s, pass_s);
adamc@682 655 else {
adamc@685 656 use_client(c);
adamc@682 657 ctx->client = c;
adamc@682 658
adamc@682 659 if (c->mode != USED)
adamc@682 660 uw_error(ctx, FATAL, "Stale client ID (%u) in subscription request", id);
adamc@682 661 if (c->pass != pass)
adamc@682 662 uw_error(ctx, FATAL, "Wrong client password (%u, %d) in subscription request", id, pass);
adamc@682 663 }
adamc@682 664 } else {
adamc@682 665 client *c = new_client();
adamc@1113 666
adamc@1113 667 if (c == NULL)
adamc@1113 668 uw_error(ctx, FATAL, "Limit exceeded on number of message-passing clients");
adamc@1113 669
adamc@685 670 use_client(c);
adamc@682 671 ctx->client = c;
adamc@864 672 }
adamc@682 673 }
adamc@682 674 }
adamc@682 675
adamc@671 676 failure_kind uw_begin(uw_context ctx, char *path) {
adamc@671 677 int r = setjmp(ctx->jmp_buf);
adamc@671 678
adamc@671 679 if (r == 0) {
adamc@1094 680 if (ctx->app->db_begin(ctx))
adamc@671 681 uw_error(ctx, BOUNDED_RETRY, "Error running SQL BEGIN");
adamc@682 682
adamc@1094 683 ctx->app->handle(ctx, path);
adamc@671 684 }
adamc@671 685
adamc@671 686 return r;
adamc@671 687 }
adamc@671 688
adamc@772 689 uw_Basis_client uw_Basis_self(uw_context ctx) {
adamc@682 690 if (ctx->client == NULL)
adamc@682 691 uw_error(ctx, FATAL, "Call to Basis.self() from page that has only server-side code");
adamc@682 692
adamc@682 693 return ctx->client->id;
adamc@682 694 }
adamc@682 695
adamc@425 696 void uw_pop_cleanup(uw_context ctx) {
adamc@425 697 if (ctx->cleanup_front == ctx->cleanup)
adamc@425 698 uw_error(ctx, FATAL, "Attempt to pop from empty cleanup action stack");
adamc@425 699
adamc@425 700 --ctx->cleanup_front;
adamc@425 701 ctx->cleanup_front->func(ctx->cleanup_front->arg);
adamc@425 702 }
adamc@425 703
adamc@311 704 char *uw_error_message(uw_context ctx) {
adamc@167 705 return ctx->error_message;
adamc@167 706 }
adamc@167 707
adamc@1115 708 void uw_set_error_message(uw_context ctx, const char *msg) {
adamc@1115 709 strncpy(ctx->error_message, msg, sizeof(ctx->error_message));
adamc@1115 710 ctx->error_message[sizeof(ctx->error_message)-1] = 0;
adamc@1115 711 }
adamc@1115 712
adamc@759 713 static input *INP(uw_context ctx) {
adamc@759 714 if (ctx->cur_container == NULL)
adamc@759 715 return ctx->inputs;
adamc@759 716 else if (ctx->cur_container->kind == SUBFORM)
adamc@759 717 return ctx->cur_container->data.subform.fields;
adamc@759 718 else if (ctx->cur_container->kind == ENTRY)
adamc@759 719 return ctx->cur_container->data.entry.fields;
adamc@759 720 else
adamc@760 721 uw_error(ctx, FATAL, "INP: Wrong kind (%d, %p)", ctx->cur_container->kind, ctx->cur_container);
adamc@759 722 }
adamc@759 723
adamc@760 724 static void adjust_pointer(input **ptr, input *old_start, input *new_start, size_t len) {
adamc@760 725 if (*ptr != NULL && *ptr >= old_start && *ptr < old_start + len)
adamc@760 726 *ptr += new_start - old_start;
adamc@760 727 }
adamc@760 728
adamc@760 729 static void adjust_input(input *x, input *old_start, input *new_start, size_t len) {
adamc@759 730 switch (x->kind) {
adamc@759 731 case SUBFORM:
adamc@760 732 adjust_pointer(&x->data.subform.fields, old_start, new_start, len);
adamc@760 733 adjust_pointer(&x->data.subform.parent, old_start, new_start, len);
adamc@759 734 break;
adamc@759 735 case SUBFORMS:
adamc@760 736 adjust_pointer(&x->data.subforms.entries, old_start, new_start, len);
adamc@760 737 adjust_pointer(&x->data.subforms.parent, old_start, new_start, len);
adamc@759 738 break;
adamc@759 739 case ENTRY:
adamc@760 740 adjust_pointer(&x->data.entry.fields, old_start, new_start, len);
adamc@760 741 adjust_pointer(&x->data.entry.next, old_start, new_start, len);
adamc@760 742 adjust_pointer(&x->data.entry.parent, old_start, new_start, len);
adamc@759 743 }
adamc@759 744 }
adamc@759 745
adamc@1113 746 size_t uw_subinputs_max = SIZE_MAX;
adamc@1113 747
adamc@759 748 static input *check_input_space(uw_context ctx, size_t len) {
adamc@759 749 size_t i;
adamc@759 750 input *r;
adamc@759 751
adamc@759 752 if (ctx->used_subinputs + len >= ctx->n_subinputs) {
adamc@1113 753 if (ctx->used_subinputs + len > uw_subinputs_max)
adamc@1113 754 uw_error(ctx, FATAL, "Exceeded limit on number of subinputs");
adamc@1113 755
adamc@759 756 input *new_subinputs = realloc(ctx->subinputs, sizeof(input) * (ctx->used_subinputs + len));
adamc@759 757 size_t offset = new_subinputs - ctx->subinputs;
adamc@759 758
adamc@760 759 if (ctx->subinputs != new_subinputs) {
adamc@760 760 for (i = 0; i < ctx->used_subinputs; ++i)
adamc@760 761 adjust_input(&new_subinputs[i], ctx->subinputs, new_subinputs, ctx->used_subinputs);
adamc@1094 762 for (i = 0; i < ctx->app->inputs_len; ++i)
adamc@760 763 adjust_input(&ctx->inputs[i], ctx->subinputs, new_subinputs, ctx->used_subinputs);
adamc@760 764
adamc@760 765 adjust_pointer(&ctx->cur_container, ctx->subinputs, new_subinputs, ctx->used_subinputs);
adamc@760 766
adamc@760 767 ctx->n_subinputs = ctx->used_subinputs + len;
adamc@760 768 ctx->subinputs = new_subinputs;
adamc@760 769 }
adamc@759 770 }
adamc@759 771
adamc@759 772 r = &ctx->subinputs[ctx->used_subinputs];
adamc@759 773
adamc@759 774 for (i = 0; i < len; ++i)
adamc@759 775 ctx->subinputs[ctx->used_subinputs++].kind = UNUSED;
adamc@759 776
adamc@759 777 return r;
adamc@759 778 }
adamc@756 779
adamc@801 780 int uw_set_input(uw_context ctx, const char *name, char *value) {
adamc@1050 781 //printf("Input name %s\n", name);
adamc@1038 782
adamc@756 783 if (!strcasecmp(name, ".b")) {
adamc@1094 784 int n = ctx->app->input_num(value);
adamc@756 785 input *inps;
adamc@756 786
adamc@801 787 if (n < 0) {
adamc@801 788 uw_set_error(ctx, "Bad subform name %s", value);
adamc@801 789 return -1;
adamc@801 790 }
adamc@801 791
adamc@1094 792 if (n >= ctx->app->inputs_len) {
adamc@801 793 uw_set_error(ctx, "For subform name %s, index %d is out of range", value, n);
adamc@801 794 return -1;
adamc@801 795 }
adamc@756 796
adamc@1094 797 inps = check_input_space(ctx, ctx->app->inputs_len);
adamc@759 798
adamc@759 799 INP(ctx)[n].kind = SUBFORM;
adamc@759 800 INP(ctx)[n].data.subform.parent = ctx->cur_container;
adamc@759 801 INP(ctx)[n].data.subform.fields = inps;
adamc@759 802 ctx->cur_container = &INP(ctx)[n];
adamc@756 803 } else if (!strcasecmp(name, ".e")) {
adamc@756 804 input *tmp;
adamc@756 805
adamc@801 806 if (ctx->cur_container == NULL) {
adamc@801 807 uw_set_error(ctx, "Unmatched subform closer");
adamc@801 808 return -1;
adamc@801 809 }
adamc@756 810
adamc@759 811 tmp = ctx->cur_container;
adamc@759 812 switch (tmp->kind) {
adamc@759 813 case SUBFORM:
adamc@759 814 ctx->cur_container = tmp->data.subform.parent;
adamc@759 815 tmp->data.subform.parent = NULL;
adamc@759 816 break;
adamc@759 817 case SUBFORMS:
adamc@759 818 ctx->cur_container = tmp->data.subforms.parent;
adamc@759 819 tmp->data.subforms.parent = NULL;
adamc@759 820 break;
adamc@759 821 case ENTRY:
adamc@759 822 ctx->cur_container = tmp->data.entry.parent;
adamc@759 823 break;
adamc@759 824 default:
adamc@801 825 uw_set_error(ctx, "uw_set_input: Wrong kind");
adamc@801 826 return -1;
adamc@759 827 }
adamc@759 828 } else if (!strcasecmp(name, ".s")) {
adamc@1094 829 int n = ctx->app->input_num(value);
adamc@759 830
adamc@801 831 if (n < 0) {
adamc@801 832 uw_set_error(ctx, "Bad subforms name %s", value);
adamc@801 833 return -1;
adamc@801 834 }
adamc@801 835
adamc@1094 836 if (n >= ctx->app->inputs_len) {
adamc@801 837 uw_set_error(ctx, "For subforms name %s, index %d is out of range", value, n);
adamc@801 838 return -1;
adamc@801 839 }
adamc@759 840
adamc@759 841 INP(ctx)[n].kind = SUBFORMS;
adamc@759 842 INP(ctx)[n].data.subforms.parent = ctx->cur_container;
adamc@759 843 INP(ctx)[n].data.subforms.entries = NULL;
adamc@759 844 ctx->cur_container = &INP(ctx)[n];
adamc@759 845 } else if (!strcasecmp(name, ".i")) {
adamc@759 846 input *inps;
adamc@759 847
adamc@801 848 if (!ctx->cur_container) {
adamc@801 849 uw_set_error(ctx, "New entry without container");
adamc@801 850 return -1;
adamc@801 851 }
adamc@801 852
adamc@801 853 if (ctx->cur_container->kind != SUBFORMS) {
adamc@801 854 uw_set_error(ctx, "Bad kind for entry parent");
adamc@801 855 return -1;
adamc@801 856 }
adamc@759 857
adamc@1094 858 inps = check_input_space(ctx, ctx->app->inputs_len + 1);
adamc@759 859
adamc@759 860 inps->kind = ENTRY;
adamc@759 861 inps->data.entry.parent = ctx->cur_container;
adamc@759 862 inps->data.entry.next = ctx->cur_container->data.subforms.entries;
adamc@759 863 ctx->cur_container->data.subforms.entries = inps;
adamc@759 864
adamc@759 865 inps->data.entry.fields = inps+1;
adamc@759 866 ctx->cur_container = inps;
adamc@756 867 } else {
adamc@1094 868 int n = ctx->app->input_num(name);
adamc@756 869
adamc@801 870 if (n < 0) {
adamc@1038 871 if (!strcmp(name, "null"))
adamc@1038 872 return 0;
adamc@801 873 uw_set_error(ctx, "Bad input name %s", name);
adamc@801 874 return -1;
adamc@801 875 }
adamc@801 876
adamc@1094 877 if (n >= ctx->app->inputs_len) {
adamc@801 878 uw_set_error(ctx, "For input name %s, index %d is out of range", name, n);
adamc@801 879 return -1;
adamc@801 880 }
adamc@756 881
adamc@756 882 INP(ctx)[n].kind = NORMAL;
adamc@756 883 INP(ctx)[n].data.normal = value;
adamc@756 884 }
adamc@801 885
adamc@801 886 return 0;
adamc@144 887 }
adamc@144 888
adamc@311 889 char *uw_get_input(uw_context ctx, int n) {
adamc@169 890 if (n < 0)
adamc@311 891 uw_error(ctx, FATAL, "Negative input index %d", n);
adamc@1094 892 if (n >= ctx->app->inputs_len)
adamc@311 893 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
adamc@737 894
adamc@756 895 switch (INP(ctx)[n].kind) {
adamc@737 896 case UNSET:
adamc@737 897 return NULL;
adamc@739 898 case FIL:
adamc@739 899 uw_error(ctx, FATAL, "Tried to read a file form input as normal");
adamc@756 900 case SUBFORM:
adamc@756 901 uw_error(ctx, FATAL, "Tried to read a subform form input as normal");
adamc@759 902 case SUBFORMS:
adamc@759 903 uw_error(ctx, FATAL, "Tried to read a subforms form input as normal");
adamc@759 904 case ENTRY:
adamc@759 905 uw_error(ctx, FATAL, "Tried to read an entry form input as normal");
adamc@737 906 case NORMAL:
adamc@756 907 return INP(ctx)[n].data.normal;
adamc@737 908 default:
adamc@737 909 uw_error(ctx, FATAL, "Impossible input kind");
adamc@737 910 }
adamc@138 911 }
adamc@138 912
adamc@311 913 char *uw_get_optional_input(uw_context ctx, int n) {
adamc@190 914 if (n < 0)
adamc@311 915 uw_error(ctx, FATAL, "Negative input index %d", n);
adamc@1094 916 if (n >= ctx->app->inputs_len)
adamc@311 917 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
adamc@737 918
adamc@756 919 switch (INP(ctx)[n].kind) {
adamc@737 920 case UNSET:
adamc@737 921 return "";
adamc@739 922 case FIL:
adamc@739 923 uw_error(ctx, FATAL, "Tried to read a file form input as normal");
adamc@756 924 case SUBFORM:
adamc@756 925 uw_error(ctx, FATAL, "Tried to read a subform form input as normal");
adamc@759 926 case SUBFORMS:
adamc@759 927 uw_error(ctx, FATAL, "Tried to read a subforms form input as normal");
adamc@759 928 case ENTRY:
adamc@759 929 uw_error(ctx, FATAL, "Tried to read an entry form input as normal");
adamc@737 930 case NORMAL:
adamc@756 931 return INP(ctx)[n].data.normal;
adamc@737 932 default:
adamc@737 933 uw_error(ctx, FATAL, "Impossible input kind");
adamc@737 934 }
adamc@737 935 }
adamc@737 936
adamc@801 937 int uw_set_file_input(uw_context ctx, const char *name, uw_Basis_file f) {
adamc@1094 938 int n = ctx->app->input_num(name);
adamc@737 939
adamc@801 940 if (n < 0) {
adamc@801 941 uw_set_error(ctx, "Bad file input name %s", name);
adamc@801 942 return -1;
adamc@801 943 }
adamc@801 944
adamc@1094 945 if (n >= ctx->app->inputs_len) {
adamc@801 946 uw_set_error(ctx, "For file input name %s, index %d is out of range", name, n);
adamc@801 947 return -1;
adamc@801 948 }
adamc@737 949
adamc@739 950 ctx->inputs[n].kind = FIL;
adamc@739 951 ctx->inputs[n].data.file = f;
adamc@801 952
adamc@801 953 return 0;
adamc@737 954 }
adamc@737 955
adamc@739 956 void *uw_malloc(uw_context ctx, size_t len);
adamc@739 957
adamc@760 958 static void parents(input *inp) {
adamc@760 959 printf("Stack: %p\n", inp);
adamc@760 960 while (inp) {
adamc@760 961 switch (inp->kind) {
adamc@760 962 case NORMAL:
adamc@760 963 printf("Normal(%p)\n", inp);
adamc@760 964 break;
adamc@760 965 case FIL:
adamc@760 966 printf("File(%p)\n", inp);
adamc@760 967 break;
adamc@760 968 case SUBFORM:
adamc@760 969 printf("Subform; fields = %p\n", inp->data.subform.fields);
adamc@760 970 inp = inp->data.subform.parent;
adamc@760 971 break;
adamc@760 972 case SUBFORMS:
adamc@760 973 printf("Subforms; entries = %p\n", inp->data.subforms.entries);
adamc@760 974 inp = inp->data.subforms.parent;
adamc@760 975 break;
adamc@760 976 case ENTRY:
adamc@760 977 printf("Entry; fields = %p; next = %p\n", inp->data.entry.fields, inp->data.entry.next);
adamc@760 978 inp = inp->data.entry.parent;
adamc@760 979 break;
adamc@760 980 default:
adamc@760 981 inp = NULL;
adamc@760 982 }
adamc@760 983 }
adamc@760 984 }
adamc@760 985
adamc@739 986 uw_Basis_file uw_get_file_input(uw_context ctx, int n) {
adamc@737 987 if (n < 0)
adamc@737 988 uw_error(ctx, FATAL, "Negative file input index %d", n);
adamc@1094 989 if (n >= ctx->app->inputs_len)
adamc@737 990 uw_error(ctx, FATAL, "Out-of-bounds file input index %d", n);
adamc@737 991
adamc@756 992 switch (INP(ctx)[n].kind) {
adamc@737 993 case UNSET:
adamc@737 994 {
adamc@739 995 char *data = uw_malloc(ctx, 0);
adamc@740 996 uw_Basis_file f = {NULL, "", {0, data}};
adamc@739 997 return f;
adamc@737 998 }
adamc@739 999 case FIL:
adamc@756 1000 return INP(ctx)[n].data.file;
adamc@737 1001 case NORMAL:
adamc@737 1002 uw_error(ctx, FATAL, "Tried to read a normal form input as files");
adamc@756 1003 case SUBFORM:
adamc@756 1004 uw_error(ctx, FATAL, "Tried to read a subform form input as files");
adamc@759 1005 case SUBFORMS:
adamc@759 1006 uw_error(ctx, FATAL, "Tried to read a subforms form input as files");
adamc@759 1007 case ENTRY:
adamc@759 1008 uw_error(ctx, FATAL, "Tried to read an entry form input as files");
adamc@737 1009 default:
adamc@737 1010 uw_error(ctx, FATAL, "Impossible input kind");
adamc@737 1011 }
adamc@190 1012 }
adamc@190 1013
adamc@756 1014 void uw_enter_subform(uw_context ctx, int n) {
adamc@756 1015 if (n < 0)
adamc@756 1016 uw_error(ctx, FATAL, "Negative subform index %d", n);
adamc@1094 1017 if (n >= ctx->app->inputs_len)
adamc@756 1018 uw_error(ctx, FATAL, "Out-of-bounds subform index %d", n);
adamc@756 1019
adamc@756 1020 switch (INP(ctx)[n].kind) {
adamc@756 1021 case UNSET:
adamc@756 1022 uw_error(ctx, FATAL, "Missing subform");
adamc@756 1023 case FIL:
adamc@756 1024 uw_error(ctx, FATAL, "Tried to read a file form input as subform");
adamc@756 1025 case NORMAL:
adamc@756 1026 uw_error(ctx, FATAL, "Tried to read a normal form input as subform");
adamc@759 1027 case SUBFORMS:
adamc@759 1028 uw_error(ctx, FATAL, "Tried to read a subforms form input as subform");
adamc@759 1029 case ENTRY:
adamc@759 1030 uw_error(ctx, FATAL, "Tried to read an entry form input as subform");
adamc@756 1031 case SUBFORM:
adamc@759 1032 INP(ctx)[n].data.subform.parent = ctx->cur_container;
adamc@760 1033 ctx->cur_container = &INP(ctx)[n];
adamc@756 1034 return;
adamc@756 1035 default:
adamc@756 1036 uw_error(ctx, FATAL, "Impossible input kind");
adamc@756 1037 }
adamc@756 1038 }
adamc@756 1039
adamc@756 1040 void uw_leave_subform(uw_context ctx) {
adamc@756 1041 input *tmp;
adamc@756 1042
adamc@759 1043 if (ctx->cur_container == NULL)
adamc@756 1044 uw_error(ctx, FATAL, "Unmatched uw_leave_subform");
adamc@756 1045
adamc@759 1046 tmp = ctx->cur_container;
adamc@759 1047 ctx->cur_container = tmp->data.subform.parent;
adamc@759 1048 tmp->data.subform.parent = NULL;
adamc@759 1049 }
adamc@759 1050
adamc@759 1051 int uw_enter_subforms(uw_context ctx, int n) {
adamc@759 1052 input *inps;
adamc@759 1053
adamc@759 1054 if (n < 0)
adamc@759 1055 uw_error(ctx, FATAL, "Negative subforms index %d", n);
adamc@1094 1056 if (n >= ctx->app->inputs_len)
adamc@759 1057 uw_error(ctx, FATAL, "Out-of-bounds subforms index %d", n);
adamc@759 1058
adamc@759 1059 switch (INP(ctx)[n].kind) {
adamc@759 1060 case UNSET:
adamc@759 1061 uw_error(ctx, FATAL, "Missing subforms");
adamc@759 1062 case FIL:
adamc@759 1063 uw_error(ctx, FATAL, "Tried to read a file form input as subforms");
adamc@759 1064 case NORMAL:
adamc@759 1065 uw_error(ctx, FATAL, "Tried to read a normal form input %p as subforms", &INP(ctx)[n]);
adamc@759 1066 case SUBFORM:
adamc@759 1067 uw_error(ctx, FATAL, "Tried to read a subform form input as subforms");
adamc@759 1068 case ENTRY:
adamc@759 1069 uw_error(ctx, FATAL, "Tried to read an entry form input as subforms");
adamc@759 1070 case SUBFORMS:
adamc@759 1071 inps = INP(ctx)[n].data.subforms.entries;
adamc@759 1072 if (inps) {
adamc@759 1073 INP(ctx)[n].data.subforms.parent = ctx->cur_container;
adamc@759 1074 ctx->cur_container = INP(ctx)[n].data.subforms.entries;
adamc@759 1075 return 1;
adamc@759 1076 } else
adamc@759 1077 return 0;
adamc@759 1078 default:
adamc@759 1079 uw_error(ctx, FATAL, "Impossible input kind");
adamc@759 1080 }
adamc@759 1081 }
adamc@759 1082
adamc@759 1083 int uw_next_entry(uw_context ctx) {
adamc@759 1084 if (ctx->cur_container == NULL)
adamc@759 1085 uw_error(ctx, FATAL, "uw_next_entry(NULL)");
adamc@759 1086
adamc@759 1087 switch (ctx->cur_container->kind) {
adamc@759 1088 case UNSET:
adamc@759 1089 uw_error(ctx, FATAL, "Missing entry");
adamc@759 1090 case FIL:
adamc@759 1091 uw_error(ctx, FATAL, "Tried to read a file form input as entry");
adamc@759 1092 case NORMAL:
adamc@759 1093 uw_error(ctx, FATAL, "Tried to read a normal form input as entry");
adamc@759 1094 case SUBFORM:
adamc@759 1095 uw_error(ctx, FATAL, "Tried to read a subform form input as entry");
adamc@759 1096 case SUBFORMS:
adamc@759 1097 uw_error(ctx, FATAL, "Tried to read a subforms form input as entry");
adamc@759 1098 case ENTRY:
adamc@759 1099 if (ctx->cur_container->data.entry.next) {
adamc@759 1100 ctx->cur_container = ctx->cur_container->data.entry.next;
adamc@759 1101 return 1;
adamc@759 1102 } else {
adamc@759 1103 ctx->cur_container = ctx->cur_container->data.entry.parent->data.subforms.parent;
adamc@759 1104 return 0;
adamc@759 1105 }
adamc@759 1106 default:
adamc@759 1107 uw_error(ctx, FATAL, "Impossible input kind");
adamc@759 1108 }
adamc@756 1109 }
adamc@756 1110
adamc@643 1111 void uw_set_script_header(uw_context ctx, const char *s) {
adamc@643 1112 ctx->script_header = s;
adamc@643 1113 }
adamc@643 1114
adamc@1094 1115 const char *uw_get_url_prefix(uw_context ctx) {
adamc@1094 1116 return ctx->app->url_prefix;
adamc@667 1117 }
adamc@667 1118
adamc@693 1119 void uw_set_needs_push(uw_context ctx, int n) {
adamc@693 1120 ctx->needs_push = n;
adamc@693 1121 }
adamc@693 1122
adamc@736 1123 void uw_set_needs_sig(uw_context ctx, int n) {
adamc@736 1124 ctx->needs_sig = n;
adamc@736 1125 }
adamc@736 1126
adamc@667 1127
adamc@1113 1128 static void buf_check_ctx(uw_context ctx, const char *kind, buf *b, size_t extra, const char *desc) {
adamc@666 1129 if (b->back - b->front < extra) {
adamc@666 1130 size_t desired = b->front - b->start + extra, next;
adamc@136 1131 char *new_heap;
adamc@136 1132
adamc@666 1133 next = b->back - b->start;
adamc@317 1134 if (next == 0)
adamc@317 1135 next = 1;
adamc@317 1136 for (; next < desired; next *= 2);
adamc@136 1137
adamc@1113 1138 if (next > b->max)
adamc@1113 1139 if (desired <= b->max)
adamc@1113 1140 next = desired;
adamc@1113 1141 else
adamc@1113 1142 uw_error(ctx, FATAL, "Memory limit exceeded (%s)", kind);
adamc@1113 1143
adamc@666 1144 new_heap = realloc(b->start, next);
adamc@666 1145 b->front = new_heap + (b->front - b->start);
adamc@666 1146 b->back = new_heap + next;
adamc@136 1147
adamc@667 1148 if (new_heap != b->start) {
adamc@666 1149 b->start = new_heap;
adamc@666 1150 uw_error(ctx, UNLIMITED_RETRY, "Couldn't allocate new %s contiguously", desc);
adamc@136 1151 }
adamc@136 1152
adamc@666 1153 b->start = new_heap;
adamc@136 1154 }
adamc@136 1155 }
adamc@136 1156
adamc@832 1157 void uw_check_heap(uw_context ctx, size_t extra) {
adamc@1113 1158 buf_check_ctx(ctx, "heap", &ctx->heap, extra, "heap chunk");
adamc@666 1159 }
adamc@666 1160
adamc@832 1161 char *uw_heap_front(uw_context ctx) {
adamc@832 1162 return ctx->heap.front;
adamc@832 1163 }
adamc@832 1164
adamc@832 1165 void uw_set_heap_front(uw_context ctx, char *fr) {
adamc@832 1166 ctx->heap.front = fr;
adamc@832 1167 }
adamc@832 1168
adamc@311 1169 void *uw_malloc(uw_context ctx, size_t len) {
adamc@136 1170 void *result;
adamc@136 1171
adamc@311 1172 uw_check_heap(ctx, len);
adamc@136 1173
adamc@666 1174 result = ctx->heap.front;
adamc@666 1175 ctx->heap.front += len;
adamc@136 1176 return result;
adamc@117 1177 }
adamc@117 1178
adamc@323 1179 void uw_begin_region(uw_context ctx) {
adamc@666 1180 regions *r = (regions *) ctx->heap.front;
adamc@323 1181
adamc@323 1182 uw_check_heap(ctx, sizeof(regions));
adamc@323 1183
adamc@666 1184 ctx->heap.front += sizeof(regions);
adamc@323 1185
adamc@323 1186 r->next = ctx->regions;
adamc@323 1187 ctx->regions = r;
adamc@323 1188 }
adamc@323 1189
adamc@323 1190 void uw_end_region(uw_context ctx) {
adamc@323 1191 regions *r = ctx->regions;
adamc@323 1192
adamc@323 1193 if (r == NULL)
adamc@323 1194 uw_error(ctx, FATAL, "Region stack underflow");
adamc@323 1195
adamc@666 1196 ctx->heap.front = (char *) r;
adamc@323 1197 ctx->regions = r->next;
adamc@323 1198 }
adamc@323 1199
adamc@324 1200 void uw_memstats(uw_context ctx) {
adamc@666 1201 printf("Headers: %d/%d\n", buf_used(&ctx->outHeaders), buf_avail(&ctx->outHeaders));
adamc@666 1202 printf("Script: %d/%d\n", buf_used(&ctx->script), buf_avail(&ctx->script));
adamc@666 1203 printf("Page: %d/%d\n", buf_used(&ctx->page), buf_avail(&ctx->page));
adamc@666 1204 printf("Heap: %d/%d\n", buf_used(&ctx->heap), buf_avail(&ctx->heap));
adamc@324 1205 }
adamc@324 1206
adamc@311 1207 int uw_send(uw_context ctx, int sock) {
adamc@666 1208 int n = uw_really_send(sock, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);
adamc@462 1209
adamc@462 1210 if (n < 0)
adamc@462 1211 return n;
adamc@462 1212
adamc@462 1213 n = uw_really_send(sock, "\r\n", 2);
adamc@462 1214
adamc@462 1215 if (n < 0)
adamc@462 1216 return n;
adamc@462 1217
adamc@666 1218 return uw_really_send(sock, ctx->page.start, ctx->page.front - ctx->page.start);
adamc@462 1219 }
adamc@462 1220
adamc@856 1221 int uw_print(uw_context ctx, int fd) {
adamc@856 1222 int n = uw_really_write(fd, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);
adamc@856 1223
adamc@856 1224 if (n < 0)
adamc@856 1225 return n;
adamc@856 1226
adamc@856 1227 n = uw_really_write(fd, "\r\n", 2);
adamc@856 1228
adamc@856 1229 if (n < 0)
adamc@856 1230 return n;
adamc@856 1231
adamc@856 1232 return uw_really_write(fd, ctx->page.start, ctx->page.front - ctx->page.start);
adamc@856 1233 }
adamc@856 1234
adamc@860 1235 int uw_output(uw_context ctx, int (*output)(void *data, char *buf, size_t len), void *data) {
adamc@860 1236 int n = output(data, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);
adamc@860 1237
adamc@860 1238 if (n < 0)
adamc@860 1239 return n;
adamc@860 1240
adamc@860 1241 n = output(data, "\r\n", 2);
adamc@860 1242
adamc@860 1243 if (n < 0)
adamc@860 1244 return n;
adamc@860 1245
adamc@860 1246 return output(data, ctx->page.start, ctx->page.front - ctx->page.start);
adamc@860 1247 }
adamc@860 1248
adamc@462 1249 static void uw_check_headers(uw_context ctx, size_t extra) {
adamc@1113 1250 ctx_buf_check(ctx, "headers", &ctx->outHeaders, extra);
adamc@462 1251 }
adamc@462 1252
adamc@462 1253 void uw_write_header(uw_context ctx, uw_Basis_string s) {
adamc@462 1254 int len = strlen(s);
adamc@462 1255
adamc@462 1256 uw_check_headers(ctx, len + 1);
adamc@666 1257 strcpy(ctx->outHeaders.front, s);
adamc@666 1258 ctx->outHeaders.front += len;
adamc@562 1259 }
adamc@562 1260
adamc@1110 1261 void uw_clear_headers(uw_context ctx) {
adamc@1110 1262 buf_reset(&ctx->outHeaders);
adamc@1110 1263 }
adamc@1110 1264
adamc@562 1265 static void uw_check_script(uw_context ctx, size_t extra) {
adamc@1113 1266 ctx_buf_check(ctx, "script", &ctx->script, extra);
adamc@562 1267 }
adamc@562 1268
adamc@562 1269 void uw_write_script(uw_context ctx, uw_Basis_string s) {
adamc@562 1270 int len = strlen(s);
adamc@562 1271
adamc@562 1272 uw_check_script(ctx, len + 1);
adamc@666 1273 strcpy(ctx->script.front, s);
adamc@666 1274 ctx->script.front += len;
adamc@562 1275 }
adamc@562 1276
adamc@645 1277 const char *uw_Basis_get_script(uw_context ctx, uw_unit u) {
adamc@916 1278 return "<sc>";
adamc@565 1279 }
adamc@565 1280
adamc@695 1281 uw_Basis_string uw_Basis_maybe_onload(uw_context ctx, uw_Basis_string s) {
adamc@695 1282 if (s[0] == 0)
adamc@695 1283 return "";
adamc@695 1284 else {
adamc@695 1285 char *r = uw_malloc(ctx, 11 + strlen(s));
adamc@695 1286 sprintf(r, " onload='%s'", s);
adamc@695 1287 return r;
adamc@695 1288 }
adamc@695 1289 }
adamc@695 1290
adamc@1042 1291 uw_Basis_string uw_Basis_maybe_onunload(uw_context ctx, uw_Basis_string s) {
adamc@1042 1292 if (ctx->script_header[0] == 0)
adamc@1042 1293 return "";
adamc@1042 1294 else {
adamc@1042 1295 char *r = uw_malloc(ctx, 22 + strlen(s));
adamc@1042 1296 sprintf(r, " onunload='unload();%s'", s);
adamc@1042 1297 return r;
adamc@1042 1298 }
adamc@1042 1299 }
adamc@1042 1300
adamc@694 1301 const char *uw_Basis_get_settings(uw_context ctx, uw_unit u) {
adamc@736 1302 if (ctx->client == NULL) {
adamc@736 1303 if (ctx->needs_sig) {
adamc@1094 1304 char *sig = ctx->app->cookie_sig(ctx);
adamc@736 1305 char *r = uw_malloc(ctx, strlen(sig) + 8);
adamc@736 1306 sprintf(r, "sig=\"%s\";", sig);
adamc@736 1307 return r;
adamc@736 1308 }
adamc@736 1309 else
adamc@736 1310 return "";
adamc@736 1311 } else {
adamc@1094 1312 char *sig = ctx->needs_sig ? ctx->app->cookie_sig(ctx) : "";
adamc@1094 1313 char *r = uw_malloc(ctx, 59 + 3 * INTS_MAX + strlen(ctx->app->url_prefix)
adamc@736 1314 + (ctx->needs_sig ? strlen(sig) + 7 : 0));
adamc@736 1315 sprintf(r, "client_id=%u;client_pass=%d;url_prefix=\"%s\";timeout=%d;%s%s%slistener();",
adamc@682 1316 ctx->client->id,
adamc@682 1317 ctx->client->pass,
adamc@1094 1318 ctx->app->url_prefix,
adamc@1094 1319 ctx->app->timeout,
adamc@736 1320 ctx->needs_sig ? "sig=\"" : "",
adamc@736 1321 sig,
adamc@736 1322 ctx->needs_sig ? "\";" : "");
adamc@679 1323 return r;
adamc@668 1324 }
adamc@667 1325 }
adamc@667 1326
adamc@574 1327 uw_Basis_string uw_Basis_jsifyString(uw_context ctx, uw_Basis_string s) {
adamc@574 1328 char *r, *s2;
adamc@574 1329
adamc@574 1330 uw_check_heap(ctx, strlen(s) * 4 + 2);
adamc@574 1331
adamc@666 1332 r = s2 = ctx->heap.front;
adamc@574 1333 *s2++ = '"';
adamc@574 1334
adamc@574 1335 for (; *s; s++) {
adamc@574 1336 char c = *s;
adamc@574 1337
adamc@574 1338 switch (c) {
adamc@574 1339 case '"':
adamc@574 1340 strcpy(s2, "\\\"");
adamc@574 1341 s2 += 2;
adamc@574 1342 break;
adamc@1027 1343 case '\'':
adamc@1027 1344 strcpy(s2, "\\047");
adamc@1027 1345 s2 += 4;
adamc@1027 1346 break;
adamc@574 1347 case '\\':
adamc@574 1348 strcpy(s2, "\\\\");
adamc@574 1349 s2 += 2;
adamc@574 1350 break;
adamc@574 1351 default:
adamc@574 1352 if (isprint(c))
adamc@574 1353 *s2++ = c;
adamc@574 1354 else {
adamc@574 1355 sprintf(s2, "\\%3o", c);
adamc@574 1356 s2 += 4;
adamc@574 1357 }
adamc@574 1358 }
adamc@574 1359 }
adamc@574 1360
adamc@574 1361 strcpy(s2, "\"");
adamc@910 1362 ctx->heap.front = s2 + 2;
adamc@574 1363 return r;
adamc@574 1364 }
adamc@574 1365
adamc@1025 1366 uw_Basis_string uw_Basis_jsifyChar(uw_context ctx, uw_Basis_char c) {
adamc@1025 1367 char *r, *s2;
adamc@1025 1368
adamc@1025 1369 uw_check_heap(ctx, 6);
adamc@1025 1370
adamc@1025 1371 r = s2 = ctx->heap.front;
adamc@1025 1372 *s2++ = '"';
adamc@1025 1373
adamc@1025 1374 switch (c) {
adamc@1025 1375 case '"':
adamc@1025 1376 strcpy(s2, "\\\"");
adamc@1025 1377 s2 += 2;
adamc@1025 1378 break;
adamc@1027 1379 case '\'':
adamc@1027 1380 strcpy(s2, "\\047");
adamc@1027 1381 s2 += 4;
adamc@1027 1382 break;
adamc@1025 1383 case '\\':
adamc@1025 1384 strcpy(s2, "\\\\");
adamc@1025 1385 s2 += 2;
adamc@1025 1386 break;
adamc@1025 1387 default:
adamc@1025 1388 if (isprint(c))
adamc@1025 1389 *s2++ = c;
adamc@1025 1390 else {
adamc@1025 1391 sprintf(s2, "\\%3o", c);
adamc@1025 1392 s2 += 4;
adamc@1025 1393 }
adamc@1025 1394 }
adamc@1025 1395
adamc@1025 1396 strcpy(s2, "\"");
adamc@1025 1397 ctx->heap.front = s2 + 2;
adamc@1025 1398 return r;
adamc@1025 1399 }
adamc@1025 1400
adamc@574 1401 uw_Basis_string uw_Basis_jsifyString_ws(uw_context ctx, uw_Basis_string s) {
adamc@574 1402 char *r, *s2;
adamc@574 1403
adamc@574 1404 uw_check_script(ctx, strlen(s) * 4 + 2);
adamc@574 1405
adamc@666 1406 r = s2 = ctx->script.front;
adamc@574 1407 *s2++ = '"';
adamc@574 1408
adamc@574 1409 for (; *s; s++) {
adamc@574 1410 char c = *s;
adamc@574 1411
adamc@574 1412 switch (c) {
adamc@577 1413 case '\'':
adamc@1027 1414 strcpy(s2, "\\");
adamc@574 1415 s2 += 2;
adamc@574 1416 break;
adamc@574 1417 case '\\':
adamc@574 1418 strcpy(s2, "\\\\");
adamc@574 1419 s2 += 2;
adamc@574 1420 break;
adamc@574 1421 default:
adamc@574 1422 if (isprint(c))
adamc@574 1423 *s2++ = c;
adamc@574 1424 else {
adamc@574 1425 sprintf(s2, "\\%3o", c);
adamc@574 1426 s2 += 4;
adamc@574 1427 }
adamc@574 1428 }
adamc@574 1429 }
adamc@574 1430
adamc@574 1431 strcpy(s2, "\"");
adamc@666 1432 ctx->script.front = s2 + 1;
adamc@574 1433 return r;
adamc@574 1434 }
adamc@574 1435
adamc@682 1436 char *uw_Basis_jsifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 1437 if (ctx->client == NULL || chn.cli != ctx->client->id)
adamc@682 1438 return "null";
adamc@682 1439 else {
adamc@682 1440 int len;
adamc@682 1441 char *r;
adamc@682 1442
adamc@682 1443 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 1444 r = ctx->heap.front;
adamc@682 1445 sprintf(r, "%u%n", chn.chn, &len);
adamc@682 1446 ctx->heap.front += len+1;
adamc@682 1447 return r;
adamc@682 1448 }
adamc@682 1449 }
adamc@682 1450
adamc@577 1451 uw_Basis_int uw_Basis_new_client_source(uw_context ctx, uw_Basis_string s) {
adamc@577 1452 int len;
adamc@577 1453 size_t s_len = strlen(s);
adamc@562 1454
adamc@970 1455 uw_check_script(ctx, 18 + INTS_MAX + s_len);
adamc@970 1456 sprintf(ctx->script.front, "var s%d=sc(exec(%n", ctx->source_count, &len);
adamc@666 1457 ctx->script.front += len;
adamc@666 1458 strcpy(ctx->script.front, s);
adamc@666 1459 ctx->script.front += s_len;
adamc@970 1460 strcpy(ctx->script.front, "));");
adamc@972 1461 ctx->script.front += 3;
adamc@562 1462
adamc@565 1463 return ctx->source_count++;
adamc@117 1464 }
adamc@117 1465
adamc@577 1466 uw_unit uw_Basis_set_client_source(uw_context ctx, uw_Basis_int n, uw_Basis_string s) {
adamc@577 1467 int len;
adamc@577 1468 size_t s_len = strlen(s);
adamc@577 1469
adamc@970 1470 uw_check_script(ctx, 12 + INTS_MAX + s_len);
adamc@970 1471 sprintf(ctx->script.front, "sv(s%d,exec(%n", (int)n, &len);
adamc@666 1472 ctx->script.front += len;
adamc@666 1473 strcpy(ctx->script.front, s);
adamc@666 1474 ctx->script.front += s_len;
adamc@970 1475 strcpy(ctx->script.front, "));");
adamc@972 1476 ctx->script.front += 3;
adamc@577 1477
adamc@577 1478 return uw_unit_v;
adamc@577 1479 }
adamc@577 1480
adamc@311 1481 static void uw_check(uw_context ctx, size_t extra) {
adamc@1113 1482 ctx_buf_check(ctx, "page", &ctx->page, extra);
adamc@117 1483 }
adamc@117 1484
adamc@311 1485 static void uw_writec_unsafe(uw_context ctx, char c) {
adamc@666 1486 *(ctx->page.front)++ = c;
adamc@117 1487 }
adamc@117 1488
adamc@311 1489 void uw_writec(uw_context ctx, char c) {
adamc@311 1490 uw_check(ctx, 1);
adamc@311 1491 uw_writec_unsafe(ctx, c);
adamc@117 1492 }
adamc@117 1493
adamc@311 1494 static void uw_write_unsafe(uw_context ctx, const char* s) {
adamc@117 1495 int len = strlen(s);
adamc@666 1496 memcpy(ctx->page.front, s, len);
adamc@666 1497 ctx->page.front += len;
adamc@117 1498 }
adamc@117 1499
adamc@311 1500 void uw_write(uw_context ctx, const char* s) {
adamc@311 1501 uw_check(ctx, strlen(s) + 1);
adamc@311 1502 uw_write_unsafe(ctx, s);
adamc@666 1503 *ctx->page.front = 0;
adamc@102 1504 }
adamc@106 1505
adamc@311 1506 char *uw_Basis_attrifyInt(uw_context ctx, uw_Basis_int n) {
adamc@136 1507 char *result;
adamc@136 1508 int len;
adamc@311 1509 uw_check_heap(ctx, INTS_MAX);
adamc@666 1510 result = ctx->heap.front;
adamc@276 1511 sprintf(result, "%lld%n", n, &len);
adamc@666 1512 ctx->heap.front += len+1;
adamc@136 1513 return result;
adamc@106 1514 }
adamc@106 1515
adamc@311 1516 char *uw_Basis_attrifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@136 1517 char *result;
adamc@136 1518 int len;
adamc@311 1519 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 1520 result = ctx->heap.front;
adamc@136 1521 sprintf(result, "%g%n", n, &len);
adamc@666 1522 ctx->heap.front += len+1;
adamc@136 1523 return result;
adamc@106 1524 }
adamc@106 1525
adamc@1054 1526 static int isCont(unsigned char ch) {
adamc@1054 1527 return ch / 64 == 2;
adamc@1054 1528 }
adamc@1054 1529
adamc@311 1530 char *uw_Basis_attrifyString(uw_context ctx, uw_Basis_string s) {
adamc@136 1531 int len = strlen(s);
adamc@136 1532 char *result, *p;
adamc@311 1533 uw_check_heap(ctx, len * 6 + 1);
adamc@136 1534
adamc@666 1535 result = p = ctx->heap.front;
adamc@136 1536
adamc@136 1537 for (; *s; s++) {
adamc@1054 1538 unsigned char c = *s;
adamc@136 1539
adamc@136 1540 if (c == '"') {
adamc@136 1541 strcpy(p, "&quot;");
adamc@136 1542 p += 6;
adamc@136 1543 } else if (c == '&') {
adamc@136 1544 strcpy(p, "&amp;");
adamc@136 1545 p += 5;
adamc@136 1546 }
adamc@1059 1547 else
adamc@136 1548 *p++ = c;
adamc@136 1549 }
adamc@136 1550
adamc@137 1551 *p++ = 0;
adamc@666 1552 ctx->heap.front = p;
adamc@136 1553 return result;
adamc@106 1554 }
adamc@106 1555
adamc@1011 1556 char *uw_Basis_attrifyChar(uw_context ctx, uw_Basis_char c) {
adamc@1011 1557 char *result, *p;
adamc@1011 1558 uw_check_heap(ctx, 7);
adamc@1011 1559
adamc@1011 1560 result = p = ctx->heap.front;
adamc@1011 1561
adamc@1011 1562 if (c == '"') {
adamc@1011 1563 strcpy(p, "&quot;");
adamc@1011 1564 p += 6;
adamc@1011 1565 } else if (c == '&') {
adamc@1011 1566 strcpy(p, "&amp;");
adamc@1011 1567 p += 5;
adamc@1011 1568 }
adamc@1059 1569 else
adamc@1011 1570 *p++ = c;
adamc@1011 1571
adamc@1011 1572 *p++ = 0;
adamc@1011 1573 ctx->heap.front = p;
adamc@1011 1574 return result;
adamc@1011 1575 }
adamc@1011 1576
adamc@721 1577 char *uw_Basis_attrifyCss_class(uw_context ctx, uw_Basis_css_class s) {
adamc@721 1578 return s;
adamc@721 1579 }
adamc@721 1580
adamc@311 1581 static void uw_Basis_attrifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
adamc@117 1582 int len;
adamc@117 1583
adamc@666 1584 sprintf(ctx->page.front, "%lld%n", n, &len);
adamc@666 1585 ctx->page.front += len;
adamc@106 1586 }
adamc@106 1587
adamc@428 1588 uw_unit uw_Basis_attrifyInt_w(uw_context ctx, uw_Basis_int n) {
adamc@311 1589 uw_check(ctx, INTS_MAX);
adamc@311 1590 uw_Basis_attrifyInt_w_unsafe(ctx, n);
adamc@428 1591
adamc@428 1592 return uw_unit_v;
adamc@106 1593 }
adamc@106 1594
adamc@428 1595 uw_unit uw_Basis_attrifyFloat_w(uw_context ctx, uw_Basis_float n) {
adamc@117 1596 int len;
adamc@117 1597
adamc@311 1598 uw_check(ctx, FLOATS_MAX);
adamc@666 1599 sprintf(ctx->page.front, "%g%n", n, &len);
adamc@666 1600 ctx->page.front += len;
adamc@428 1601
adamc@428 1602 return uw_unit_v;
adamc@117 1603 }
adamc@117 1604
adamc@428 1605 uw_unit uw_Basis_attrifyString_w(uw_context ctx, uw_Basis_string s) {
adamc@311 1606 uw_check(ctx, strlen(s) * 6);
adamc@117 1607
adamc@106 1608 for (; *s; s++) {
adamc@1054 1609 unsigned char c = *s;
adamc@106 1610
adamc@106 1611 if (c == '"')
adamc@311 1612 uw_write_unsafe(ctx, "&quot;");
adamc@136 1613 else if (c == '&')
adamc@311 1614 uw_write_unsafe(ctx, "&amp;");
adamc@1059 1615 else
adamc@311 1616 uw_writec_unsafe(ctx, c);
adamc@106 1617 }
adamc@428 1618
adamc@428 1619 return uw_unit_v;
adamc@106 1620 }
adamc@120 1621
adamc@1011 1622 uw_unit uw_Basis_attrifyChar_w(uw_context ctx, uw_Basis_char c) {
adamc@1011 1623 uw_check(ctx, 6);
adamc@1011 1624
adamc@1011 1625 if (c == '"')
adamc@1011 1626 uw_write_unsafe(ctx, "&quot;");
adamc@1011 1627 else if (c == '&')
adamc@1011 1628 uw_write_unsafe(ctx, "&amp;");
adamc@1059 1629 else
adamc@1011 1630 uw_writec_unsafe(ctx, c);
adamc@1011 1631
adamc@1011 1632 return uw_unit_v;
adamc@1011 1633 }
adamc@1011 1634
adamc@120 1635
adamc@311 1636 char *uw_Basis_urlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@137 1637 int len;
adamc@137 1638 char *r;
adamc@137 1639
adamc@311 1640 uw_check_heap(ctx, INTS_MAX);
adamc@666 1641 r = ctx->heap.front;
adamc@276 1642 sprintf(r, "%lld%n", n, &len);
adamc@666 1643 ctx->heap.front += len+1;
adamc@137 1644 return r;
adamc@120 1645 }
adamc@120 1646
adamc@682 1647 char *uw_Basis_urlifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 1648 if (ctx->client == NULL || chn.cli != ctx->client->id)
adamc@682 1649 return "";
adamc@682 1650 else {
adamc@682 1651 int len;
adamc@682 1652 char *r;
adamc@679 1653
adamc@682 1654 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 1655 r = ctx->heap.front;
adamc@682 1656 sprintf(r, "%u%n", chn.chn, &len);
adamc@682 1657 ctx->heap.front += len+1;
adamc@682 1658 return r;
adamc@682 1659 }
adamc@679 1660 }
adamc@679 1661
adamc@311 1662 char *uw_Basis_urlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@137 1663 int len;
adamc@137 1664 char *r;
adamc@137 1665
adamc@311 1666 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 1667 r = ctx->heap.front;
adamc@137 1668 sprintf(r, "%g%n", n, &len);
adamc@666 1669 ctx->heap.front += len+1;
adamc@137 1670 return r;
adamc@120 1671 }
adamc@120 1672
adamc@311 1673 char *uw_Basis_urlifyString(uw_context ctx, uw_Basis_string s) {
adamc@137 1674 char *r, *p;
adamc@137 1675
adamc@925 1676 if (s[0] == '\0')
adamc@925 1677 return "_";
adamc@925 1678
adamc@925 1679 uw_check_heap(ctx, strlen(s) * 3 + 1 + !!(s[0] == '_'));
adamc@925 1680
adamc@925 1681 r = p = ctx->heap.front;
adamc@925 1682 if (s[0] == '_')
adamc@925 1683 *p++ = '_';
adamc@925 1684
adamc@925 1685 for (; *s; s++) {
adamc@1087 1686 unsigned char c = *s;
adamc@137 1687
adamc@137 1688 if (c == ' ')
adamc@137 1689 *p++ = '+';
adamc@137 1690 else if (isalnum(c))
adamc@137 1691 *p++ = c;
adamc@137 1692 else {
adamc@137 1693 sprintf(p, "%%%02X", c);
adamc@137 1694 p += 3;
adamc@137 1695 }
adamc@137 1696 }
adamc@137 1697
adamc@137 1698 *p++ = 0;
adamc@666 1699 ctx->heap.front = p;
adamc@137 1700 return r;
adamc@120 1701 }
adamc@120 1702
adamc@311 1703 char *uw_Basis_urlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 1704 if (b == uw_Basis_False)
adamc@186 1705 return "0";
adamc@186 1706 else
adamc@186 1707 return "1";
adamc@186 1708 }
adamc@186 1709
adamc@311 1710 static void uw_Basis_urlifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
adamc@120 1711 int len;
adamc@120 1712
adamc@666 1713 sprintf(ctx->page.front, "%lld%n", n, &len);
adamc@666 1714 ctx->page.front += len;
adamc@120 1715 }
adamc@120 1716
adamc@428 1717 uw_unit uw_Basis_urlifyInt_w(uw_context ctx, uw_Basis_int n) {
adamc@311 1718 uw_check(ctx, INTS_MAX);
adamc@311 1719 uw_Basis_urlifyInt_w_unsafe(ctx, n);
adamc@428 1720
adamc@428 1721 return uw_unit_v;
adamc@120 1722 }
adamc@120 1723
adamc@682 1724 uw_unit uw_Basis_urlifyChannel_w(uw_context ctx, uw_Basis_channel chn) {
adamc@682 1725 if (ctx->client != NULL && chn.cli == ctx->client->id) {
adamc@682 1726 int len;
adamc@682 1727
adamc@682 1728 uw_check(ctx, INTS_MAX + 1);
adamc@682 1729 sprintf(ctx->page.front, "%u%n", chn.chn, &len);
adamc@682 1730 ctx->page.front += len;
adamc@682 1731 }
adamc@682 1732
adamc@679 1733 return uw_unit_v;
adamc@679 1734 }
adamc@679 1735
adamc@428 1736 uw_unit uw_Basis_urlifyFloat_w(uw_context ctx, uw_Basis_float n) {
adamc@120 1737 int len;
adamc@120 1738
adamc@311 1739 uw_check(ctx, FLOATS_MAX);
adamc@666 1740 sprintf(ctx->page.front, "%g%n", n, &len);
adamc@666 1741 ctx->page.front += len;
adamc@428 1742
adamc@428 1743 return uw_unit_v;
adamc@120 1744 }
adamc@120 1745
adamc@488 1746 uw_Basis_string uw_Basis_urlifyTime(uw_context ctx, uw_Basis_time t) {
adamc@488 1747 return uw_Basis_urlifyInt(ctx, t);
adamc@488 1748 }
adamc@488 1749
adamc@428 1750 uw_unit uw_Basis_urlifyString_w(uw_context ctx, uw_Basis_string s) {
adamc@925 1751 if (s[0] == '\0') {
adamc@925 1752 uw_check(ctx, 1);
adamc@925 1753 uw_writec_unsafe(ctx, '_');
adamc@925 1754 return uw_unit_v;
adamc@925 1755 }
adamc@925 1756
adamc@925 1757 uw_check(ctx, strlen(s) * 3 + !!(s[0] == '_'));
adamc@925 1758
adamc@925 1759 if (s[0] == '_')
adamc@925 1760 uw_writec_unsafe(ctx, '_');
adamc@120 1761
adamc@120 1762 for (; *s; s++) {
adamc@1087 1763 unsigned char c = *s;
adamc@120 1764
adamc@120 1765 if (c == ' ')
adamc@311 1766 uw_writec_unsafe(ctx, '+');
adamc@120 1767 else if (isalnum(c))
adamc@311 1768 uw_writec_unsafe(ctx, c);
adamc@120 1769 else {
adamc@666 1770 sprintf(ctx->page.front, "%%%02X", c);
adamc@666 1771 ctx->page.front += 3;
adamc@120 1772 }
adamc@120 1773 }
adamc@428 1774
adamc@428 1775 return uw_unit_v;
adamc@120 1776 }
adamc@120 1777
adamc@428 1778 uw_unit uw_Basis_urlifyBool_w(uw_context ctx, uw_Basis_bool b) {
adamc@311 1779 if (b == uw_Basis_False)
adamc@311 1780 uw_writec(ctx, '0');
adamc@186 1781 else
adamc@311 1782 uw_writec(ctx, '1');
adamc@428 1783
adamc@428 1784 return uw_unit_v;
adamc@186 1785 }
adamc@186 1786
adamc@120 1787
adamc@311 1788 static char *uw_unurlify_advance(char *s) {
adamc@144 1789 char *new_s = strchr(s, '/');
adamc@120 1790
adamc@120 1791 if (new_s)
adamc@120 1792 *new_s++ = 0;
adamc@120 1793 else
adamc@144 1794 new_s = strchr(s, 0);
adamc@144 1795
adamc@144 1796 return new_s;
adamc@144 1797 }
adamc@144 1798
adamc@311 1799 uw_Basis_int uw_Basis_unurlifyInt(uw_context ctx, char **s) {
adamc@311 1800 char *new_s = uw_unurlify_advance(*s);
adamc@311 1801 uw_Basis_int r;
adamc@120 1802
adamc@276 1803 r = atoll(*s);
adamc@120 1804 *s = new_s;
adamc@120 1805 return r;
adamc@120 1806 }
adamc@120 1807
adamc@311 1808 uw_Basis_float uw_Basis_unurlifyFloat(uw_context ctx, char **s) {
adamc@311 1809 char *new_s = uw_unurlify_advance(*s);
adamc@311 1810 uw_Basis_float r;
adamc@120 1811
adamc@120 1812 r = atof(*s);
adamc@120 1813 *s = new_s;
adamc@120 1814 return r;
adamc@120 1815 }
adamc@120 1816
adamc@488 1817 uw_Basis_time uw_Basis_unurlifyTime(uw_context ctx, char **s) {
adamc@488 1818 return uw_Basis_unurlifyInt(ctx, s);
adamc@488 1819 }
adamc@488 1820
adamc@1023 1821 static uw_Basis_string uw_unurlifyString_to(int fromClient, uw_context ctx, char *r, char *s) {
adamc@927 1822 char *s1, *s2 = s;
adamc@144 1823 int n;
adamc@136 1824
adamc@1023 1825 if (!fromClient) {
adamc@1023 1826 if (*s2 == '_')
adamc@1023 1827 ++s2;
adamc@1023 1828 else if (s2[0] == '%' && s2[1] == '5' && (s2[2] == 'f' || s2[2] == 'F'))
adamc@1023 1829 s2 += 3;
adamc@1023 1830 }
adamc@925 1831
adamc@927 1832 for (s1 = r; *s2; ++s1, ++s2) {
adamc@1087 1833 unsigned char c = *s2;
adamc@136 1834
adamc@136 1835 switch (c) {
adamc@136 1836 case '+':
adamc@136 1837 *s1 = ' ';
adamc@136 1838 break;
adamc@136 1839 case '%':
adamc@169 1840 if (s2[1] == 0)
adamc@311 1841 uw_error(ctx, FATAL, "Missing first character of escaped URL byte");
adamc@169 1842 if (s2[2] == 0)
adamc@311 1843 uw_error(ctx, FATAL, "Missing second character of escaped URL byte");
adamc@169 1844 if (sscanf(s2+1, "%02X", &n) != 1)
adamc@311 1845 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", s2);
adamc@136 1846 *s1 = n;
adamc@136 1847 s2 += 2;
adamc@136 1848 break;
adamc@136 1849 default:
adamc@136 1850 *s1 = c;
adamc@136 1851 }
adamc@136 1852 }
adamc@136 1853 *s1++ = 0;
adamc@144 1854 return s1;
adamc@144 1855 }
adamc@144 1856
adamc@311 1857 uw_Basis_bool uw_Basis_unurlifyBool(uw_context ctx, char **s) {
adamc@311 1858 char *new_s = uw_unurlify_advance(*s);
adamc@311 1859 uw_Basis_bool r;
adamc@186 1860
adamc@186 1861 if (*s[0] == 0 || !strcmp(*s, "0") || !strcmp(*s, "off"))
adamc@311 1862 r = uw_Basis_False;
adamc@186 1863 else
adamc@311 1864 r = uw_Basis_True;
adamc@186 1865
adamc@186 1866 *s = new_s;
adamc@186 1867 return r;
adamc@186 1868 }
adamc@186 1869
adamc@311 1870 uw_Basis_string uw_Basis_unurlifyString(uw_context ctx, char **s) {
adamc@311 1871 char *new_s = uw_unurlify_advance(*s);
adamc@144 1872 char *r, *s1, *s2;
adamc@144 1873 int len, n;
adamc@144 1874
adamc@200 1875 len = strlen(*s);
adamc@311 1876 uw_check_heap(ctx, len + 1);
adamc@144 1877
adamc@666 1878 r = ctx->heap.front;
adamc@1023 1879 ctx->heap.front = uw_unurlifyString_to(0, ctx, ctx->heap.front, *s);
adamc@1023 1880 *s = new_s;
adamc@1023 1881 return r;
adamc@1023 1882 }
adamc@1023 1883
adamc@1109 1884 uw_Basis_unit uw_Basis_unurlifyUnit(uw_context ctx, char **s) {
adamc@1109 1885 *s = uw_unurlify_advance(*s);
adamc@1109 1886 return uw_unit_v;
adamc@1109 1887 }
adamc@1109 1888
adamc@1023 1889 uw_Basis_string uw_Basis_unurlifyString_fromClient(uw_context ctx, char **s) {
adamc@1023 1890 char *new_s = uw_unurlify_advance(*s);
adamc@1023 1891 char *r, *s1, *s2;
adamc@1023 1892 int len, n;
adamc@1023 1893
adamc@1023 1894 len = strlen(*s);
adamc@1023 1895 uw_check_heap(ctx, len + 1);
adamc@1023 1896
adamc@1023 1897 r = ctx->heap.front;
adamc@1023 1898 ctx->heap.front = uw_unurlifyString_to(1, ctx, ctx->heap.front, *s);
adamc@136 1899 *s = new_s;
adamc@136 1900 return r;
adamc@120 1901 }
adamc@135 1902
adamc@135 1903
adamc@311 1904 char *uw_Basis_htmlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@286 1905 int len;
adamc@286 1906 char *r;
adamc@286 1907
adamc@311 1908 uw_check_heap(ctx, INTS_MAX);
adamc@666 1909 r = ctx->heap.front;
adamc@286 1910 sprintf(r, "%lld%n", n, &len);
adamc@666 1911 ctx->heap.front += len+1;
adamc@286 1912 return r;
adamc@286 1913 }
adamc@286 1914
adamc@428 1915 uw_unit uw_Basis_htmlifyInt_w(uw_context ctx, uw_Basis_int n) {
adamc@286 1916 int len;
adamc@286 1917
adamc@311 1918 uw_check(ctx, INTS_MAX);
adamc@666 1919 sprintf(ctx->page.front, "%lld%n", n, &len);
adamc@666 1920 ctx->page.front += len;
adamc@428 1921
adamc@428 1922 return uw_unit_v;
adamc@286 1923 }
adamc@286 1924
adamc@311 1925 char *uw_Basis_htmlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@286 1926 int len;
adamc@286 1927 char *r;
adamc@286 1928
adamc@311 1929 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 1930 r = ctx->heap.front;
adamc@286 1931 sprintf(r, "%g%n", n, &len);
adamc@666 1932 ctx->heap.front += len+1;
adamc@286 1933 return r;
adamc@286 1934 }
adamc@286 1935
adamc@428 1936 uw_unit uw_Basis_htmlifyFloat_w(uw_context ctx, uw_Basis_float n) {
adamc@286 1937 int len;
adamc@286 1938
adamc@311 1939 uw_check(ctx, FLOATS_MAX);
adamc@666 1940 sprintf(ctx->page.front, "%g%n", n, &len);
adamc@666 1941 ctx->page.front += len;
adamc@428 1942
adamc@428 1943 return uw_unit_v;
adamc@286 1944 }
adamc@286 1945
adamc@311 1946 char *uw_Basis_htmlifyString(uw_context ctx, uw_Basis_string s) {
adamc@137 1947 char *r, *s2;
adamc@137 1948
adamc@311 1949 uw_check_heap(ctx, strlen(s) * 5 + 1);
adamc@137 1950
adamc@666 1951 for (r = s2 = ctx->heap.front; *s; s++) {
adamc@1054 1952 unsigned char c = *s;
adamc@137 1953
adamc@137 1954 switch (c) {
adamc@137 1955 case '<':
adamc@137 1956 strcpy(s2, "&lt;");
adamc@137 1957 s2 += 4;
adamc@137 1958 break;
adamc@137 1959 case '&':
adamc@137 1960 strcpy(s2, "&amp;");
adamc@137 1961 s2 += 5;
adamc@137 1962 break;
adamc@137 1963 default:
adamc@1059 1964 *s2++ = c;
adamc@137 1965 }
adamc@137 1966 }
adamc@137 1967
adamc@137 1968 *s2++ = 0;
adamc@666 1969 ctx->heap.front = s2;
adamc@137 1970 return r;
adamc@135 1971 }
adamc@135 1972
adamc@428 1973 uw_unit uw_Basis_htmlifyString_w(uw_context ctx, uw_Basis_string s) {
adamc@321 1974 uw_check(ctx, strlen(s) * 6);
adamc@135 1975
adamc@135 1976 for (; *s; s++) {
adamc@1054 1977 unsigned char c = *s;
adamc@135 1978
adamc@135 1979 switch (c) {
adamc@135 1980 case '<':
adamc@311 1981 uw_write_unsafe(ctx, "&lt;");
adamc@135 1982 break;
adamc@135 1983 case '&':
adamc@311 1984 uw_write_unsafe(ctx, "&amp;");
adamc@135 1985 break;
adamc@135 1986 default:
adamc@1059 1987 uw_writec_unsafe(ctx, c);
adamc@135 1988 }
adamc@135 1989 }
adamc@428 1990
adamc@428 1991 return uw_unit_v;
adamc@135 1992 }
adamc@180 1993
adamc@311 1994 uw_Basis_string uw_Basis_htmlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 1995 if (b == uw_Basis_False)
adamc@286 1996 return "False";
adamc@286 1997 else
adamc@286 1998 return "True";
adamc@286 1999 }
adamc@286 2000
adamc@428 2001 uw_unit uw_Basis_htmlifyBool_w(uw_context ctx, uw_Basis_bool b) {
adamc@311 2002 if (b == uw_Basis_False) {
adamc@311 2003 uw_check(ctx, 6);
adamc@666 2004 strcpy(ctx->page.front, "False");
adamc@666 2005 ctx->page.front += 5;
adamc@286 2006 } else {
adamc@311 2007 uw_check(ctx, 5);
adamc@666 2008 strcpy(ctx->page.front, "True");
adamc@666 2009 ctx->page.front += 4;
adamc@286 2010 }
adamc@428 2011
adamc@428 2012 return uw_unit_v;
adamc@286 2013 }
adamc@286 2014
adamc@436 2015 #define TIME_FMT "%x %X"
adamc@438 2016 #define TIME_FMT_PG "%Y-%m-%d %T"
adamc@436 2017
adamc@436 2018 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) {
adamc@436 2019 size_t len;
adamc@436 2020 char *r;
adamc@436 2021 struct tm stm;
adamc@436 2022
adamc@436 2023 if (localtime_r(&t, &stm)) {
adamc@436 2024 uw_check_heap(ctx, TIMES_MAX);
adamc@666 2025 r = ctx->heap.front;
adamc@436 2026 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 2027 ctx->heap.front += len+1;
adamc@436 2028 return r;
adamc@436 2029 } else
adamc@436 2030 return "<i>Invalid time</i>";
adamc@436 2031 }
adamc@436 2032
adamc@436 2033 uw_unit uw_Basis_htmlifyTime_w(uw_context ctx, uw_Basis_time t) {
adamc@436 2034 size_t len;
adamc@436 2035 char *r;
adamc@436 2036 struct tm stm;
adamc@436 2037
adamc@436 2038 if (localtime_r(&t, &stm)) {
adamc@436 2039 uw_check(ctx, TIMES_MAX);
adamc@666 2040 r = ctx->page.front;
adamc@436 2041 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 2042 ctx->page.front += len;
adamc@436 2043 } else {
adamc@436 2044 uw_check(ctx, 20);
adamc@666 2045 strcpy(ctx->page.front, "<i>Invalid time</i>");
adamc@666 2046 ctx->page.front += 19;
adamc@436 2047 }
adamc@436 2048
adamc@436 2049 return uw_unit_v;
adamc@436 2050 }
adamc@436 2051
adamc@821 2052 uw_Basis_char uw_Basis_strsub(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
adamc@821 2053 if (n >= 0 && n < strlen(s))
adamc@821 2054 return s[n];
adamc@821 2055 else
adamc@821 2056 uw_error(ctx, FATAL, "Out-of-bounds strsub");
adamc@821 2057 }
adamc@821 2058
adamc@821 2059 uw_Basis_string uw_Basis_strsuffix(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
adamc@821 2060 if (n >= 0 && n < strlen(s))
adamc@821 2061 return &s[n];
adamc@821 2062 else
adamc@821 2063 uw_error(ctx, FATAL, "Out-of-bounds strsuffix");
adamc@821 2064 }
adamc@821 2065
adamc@828 2066 uw_Basis_int uw_Basis_strlen(uw_context ctx, uw_Basis_string s) {
adamc@828 2067 return strlen(s);
adamc@828 2068 }
adamc@828 2069
adamc@829 2070 uw_Basis_string uw_Basis_strchr(uw_context ctx, uw_Basis_string s, uw_Basis_char ch) {
adamc@829 2071 return strchr(s, ch);
adamc@829 2072 }
adamc@829 2073
adamc@831 2074 uw_Basis_int uw_Basis_strcspn(uw_context ctx, uw_Basis_string s, uw_Basis_string chs) {
adamc@831 2075 return strcspn(s, chs);
adamc@831 2076 }
adamc@831 2077
adamc@829 2078 uw_Basis_int *uw_Basis_strindex(uw_context ctx, uw_Basis_string s, uw_Basis_char ch) {
adamc@829 2079 uw_Basis_string r = strchr(s, ch);
adamc@829 2080 if (r == NULL)
adamc@829 2081 return NULL;
adamc@829 2082 else {
adamc@829 2083 uw_Basis_int *nr = uw_malloc(ctx, sizeof(uw_Basis_int));
adamc@829 2084 *nr = r - s;
adamc@829 2085 return nr;
adamc@829 2086 }
adamc@829 2087 }
adamc@829 2088
adamc@311 2089 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) {
adamc@828 2090 int len = uw_Basis_strlen(ctx, s1) + uw_Basis_strlen(ctx, s2) + 1;
adamc@180 2091 char *s;
adamc@180 2092
adamc@311 2093 uw_check_heap(ctx, len);
adamc@180 2094
adamc@666 2095 s = ctx->heap.front;
adamc@180 2096
adamc@180 2097 strcpy(s, s1);
adamc@180 2098 strcat(s, s2);
adamc@666 2099 ctx->heap.front += len;
adamc@180 2100
adamc@180 2101 return s;
adamc@180 2102 }
adamc@278 2103
adamc@829 2104 uw_Basis_string uw_Basis_substring(uw_context ctx, uw_Basis_string s, uw_Basis_int start, uw_Basis_int len) {
adamc@829 2105 size_t full_len = uw_Basis_strlen(ctx, s);
adamc@829 2106
adamc@829 2107 if (start < 0)
adamc@829 2108 uw_error(ctx, FATAL, "substring: Negative start index");
adamc@829 2109 if (len < 0)
adamc@829 2110 uw_error(ctx, FATAL, "substring: Negative length");
adamc@829 2111 if (start + len > full_len)
adamc@829 2112 uw_error(ctx, FATAL, "substring: Start index plus length is too large");
adamc@829 2113
adamc@829 2114 if (start + len == full_len)
adamc@829 2115 return &s[start];
adamc@829 2116 else {
adamc@829 2117 uw_Basis_string r = uw_malloc(ctx, len+1);
adamc@959 2118 memcpy(r, s+start, len);
adamc@829 2119 r[len] = 0;
adamc@829 2120 return r;
adamc@829 2121 }
adamc@829 2122
adamc@829 2123 }
adamc@829 2124
adamc@1023 2125 uw_Basis_string uw_Basis_str1(uw_context ctx, uw_Basis_char ch) {
adamc@1023 2126 char *r;
adamc@1023 2127
adamc@1023 2128 uw_check_heap(ctx, 2);
adamc@1023 2129 r = ctx->heap.front;
adamc@1023 2130 r[0] = ch;
adamc@1023 2131 r[1] = 0;
adamc@1023 2132
adamc@1023 2133 ctx->heap.front += 2;
adamc@1023 2134
adamc@1023 2135 return r;
adamc@1023 2136 }
adamc@1023 2137
adamc@737 2138 uw_Basis_string uw_strdup(uw_context ctx, uw_Basis_string s1) {
adamc@828 2139 int len = uw_Basis_strlen(ctx, s1) + 1;
adamc@278 2140 char *s;
adamc@278 2141
adamc@311 2142 uw_check_heap(ctx, len);
adamc@278 2143
adamc@666 2144 s = ctx->heap.front;
adamc@278 2145
adamc@278 2146 strcpy(s, s1);
adamc@666 2147 ctx->heap.front += len;
adamc@278 2148
adamc@278 2149 return s;
adamc@278 2150 }
adamc@280 2151
adamc@737 2152 uw_Basis_string uw_maybe_strdup(uw_context ctx, uw_Basis_string s1) {
adamc@493 2153 if (s1)
adamc@737 2154 return uw_strdup(ctx, s1);
adamc@493 2155 else
adamc@493 2156 return NULL;
adamc@493 2157 }
adamc@493 2158
adamc@742 2159 char *uw_memdup(uw_context ctx, const char *p, size_t len) {
adamc@742 2160 char *r = uw_malloc(ctx, len);
adamc@742 2161 memcpy(r, p, len);
adamc@742 2162 return r;
adamc@742 2163 }
adamc@280 2164
adamc@879 2165 char *uw_sqlfmtInt = "%lld::int8%n";
adamc@879 2166
adamc@311 2167 char *uw_Basis_sqlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@281 2168 int len;
adamc@281 2169 char *r;
adamc@281 2170
adamc@311 2171 uw_check_heap(ctx, INTS_MAX + 6);
adamc@666 2172 r = ctx->heap.front;
adamc@879 2173 sprintf(r, uw_sqlfmtInt, n, &len);
adamc@666 2174 ctx->heap.front += len+1;
adamc@281 2175 return r;
adamc@281 2176 }
adamc@281 2177
adamc@467 2178 char *uw_Basis_sqlifyIntN(uw_context ctx, uw_Basis_int *n) {
adamc@467 2179 if (n == NULL)
adamc@467 2180 return "NULL";
adamc@467 2181 else
adamc@467 2182 return uw_Basis_sqlifyInt(ctx, *n);
adamc@467 2183 }
adamc@467 2184
adamc@879 2185 char *uw_sqlfmtFloat = "%g::float8%n";
adamc@879 2186
adamc@311 2187 char *uw_Basis_sqlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@281 2188 int len;
adamc@281 2189 char *r;
adamc@281 2190
adamc@311 2191 uw_check_heap(ctx, FLOATS_MAX + 8);
adamc@666 2192 r = ctx->heap.front;
adamc@879 2193 sprintf(r, uw_sqlfmtFloat, n, &len);
adamc@666 2194 ctx->heap.front += len+1;
adamc@281 2195 return r;
adamc@281 2196 }
adamc@281 2197
adamc@467 2198 char *uw_Basis_sqlifyFloatN(uw_context ctx, uw_Basis_float *n) {
adamc@467 2199 if (n == NULL)
adamc@467 2200 return "NULL";
adamc@467 2201 else
adamc@467 2202 return uw_Basis_sqlifyFloat(ctx, *n);
adamc@467 2203 }
adamc@467 2204
adamc@879 2205 int uw_Estrings = 1;
adamc@879 2206 char *uw_sqlsuffixString = "::text";
adamc@1011 2207 char *uw_sqlsuffixChar = "::char";
adamc@281 2208
adamc@311 2209 uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) {
adamc@280 2210 char *r, *s2;
adamc@280 2211
adamc@879 2212 uw_check_heap(ctx, strlen(s) * 2 + 3 + uw_Estrings + strlen(uw_sqlsuffixString));
adamc@280 2213
adamc@666 2214 r = s2 = ctx->heap.front;
adamc@879 2215 if (uw_Estrings)
adamc@879 2216 *s2++ = 'E';
adamc@280 2217 *s2++ = '\'';
adamc@280 2218
adamc@280 2219 for (; *s; s++) {
adamc@280 2220 char c = *s;
adamc@280 2221
adamc@280 2222 switch (c) {
adamc@280 2223 case '\'':
adamc@885 2224 if (uw_Estrings)
adamc@885 2225 strcpy(s2, "\\'");
adamc@885 2226 else
adamc@885 2227 strcpy(s2, "''");
adamc@280 2228 s2 += 2;
adamc@280 2229 break;
adamc@280 2230 case '\\':
adamc@885 2231 if (uw_Estrings) {
adamc@885 2232 strcpy(s2, "\\\\");
adamc@885 2233 s2 += 2;
adamc@885 2234 } else
adamc@885 2235 *s2++ = '\\';
adamc@280 2236 break;
adamc@280 2237 default:
adamc@280 2238 if (isprint(c))
adamc@280 2239 *s2++ = c;
adamc@879 2240 else if (uw_Estrings) {
adamc@737 2241 sprintf(s2, "\\%03o", c);
adamc@280 2242 s2 += 4;
adamc@280 2243 }
adamc@879 2244 else
adamc@879 2245 uw_error(ctx, FATAL, "Non-printable character %u in string to SQLify", c);
adamc@280 2246 }
adamc@280 2247 }
adamc@280 2248
adamc@879 2249 *s2++ = '\'';
adamc@879 2250 strcpy(s2, uw_sqlsuffixString);
adamc@879 2251 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixString);
adamc@280 2252 return r;
adamc@280 2253 }
adamc@281 2254
adamc@1011 2255 uw_Basis_string uw_Basis_sqlifyChar(uw_context ctx, uw_Basis_char c) {
adamc@1011 2256 char *r, *s2;
adamc@1011 2257
adamc@1011 2258 uw_check_heap(ctx, 5 + uw_Estrings + strlen(uw_sqlsuffixChar));
adamc@1011 2259
adamc@1011 2260 r = s2 = ctx->heap.front;
adamc@1011 2261 if (uw_Estrings)
adamc@1011 2262 *s2++ = 'E';
adamc@1011 2263 *s2++ = '\'';
adamc@1011 2264
adamc@1011 2265 switch (c) {
adamc@1011 2266 case '\'':
adamc@1011 2267 if (uw_Estrings)
adamc@1011 2268 strcpy(s2, "\\'");
adamc@1011 2269 else
adamc@1011 2270 strcpy(s2, "''");
adamc@1011 2271 s2 += 2;
adamc@1011 2272 break;
adamc@1011 2273 case '\\':
adamc@1011 2274 if (uw_Estrings) {
adamc@1011 2275 strcpy(s2, "\\\\");
adamc@1011 2276 s2 += 2;
adamc@1011 2277 } else
adamc@1011 2278 *s2++ = '\\';
adamc@1011 2279 break;
adamc@1011 2280 default:
adamc@1011 2281 if (isprint(c))
adamc@1011 2282 *s2++ = c;
adamc@1011 2283 else if (uw_Estrings) {
adamc@1011 2284 sprintf(s2, "\\%03o", c);
adamc@1011 2285 s2 += 4;
adamc@1011 2286 }
adamc@1011 2287 else
adamc@1011 2288 uw_error(ctx, FATAL, "Non-printable character %u in char to SQLify", c);
adamc@1011 2289 }
adamc@1011 2290
adamc@1011 2291 *s2++ = '\'';
adamc@1011 2292 strcpy(s2, uw_sqlsuffixChar);
adamc@1011 2293 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixChar);
adamc@1011 2294 return r;
adamc@1011 2295 }
adamc@1011 2296
adamc@879 2297 char *uw_sqlsuffixBlob = "::bytea";
adamc@879 2298
adamc@737 2299 uw_Basis_string uw_Basis_sqlifyBlob(uw_context ctx, uw_Basis_blob b) {
adamc@737 2300 char *r, *s2;
adamc@737 2301 size_t i;
adamc@737 2302
adamc@879 2303 uw_check_heap(ctx, b.size * 5 + 3 + uw_Estrings + strlen(uw_sqlsuffixBlob));
adamc@737 2304
adamc@737 2305 r = s2 = ctx->heap.front;
adamc@879 2306 if (uw_Estrings)
adamc@879 2307 *s2++ = 'E';
adamc@737 2308 *s2++ = '\'';
adamc@737 2309
adamc@737 2310 for (i = 0; i < b.size; ++i) {
adamc@737 2311 char c = b.data[i];
adamc@737 2312
adamc@737 2313 switch (c) {
adamc@737 2314 case '\'':
adamc@885 2315 if (uw_Estrings)
adamc@885 2316 strcpy(s2, "\\'");
adamc@885 2317 else
adamc@885 2318 strcpy(s2, "''");
adamc@737 2319 s2 += 2;
adamc@737 2320 break;
adamc@737 2321 case '\\':
adamc@885 2322 if (uw_Estrings) {
adamc@885 2323 strcpy(s2, "\\\\\\\\");
adamc@885 2324 s2 += 4;
adamc@885 2325 } else
adamc@885 2326 *s2++ = '\\';
adamc@737 2327 break;
adamc@737 2328 default:
adamc@737 2329 if (isprint(c))
adamc@737 2330 *s2++ = c;
adamc@879 2331 else if (uw_Estrings) {
adamc@737 2332 sprintf(s2, "\\\\%03o", c);
adamc@737 2333 s2 += 5;
adamc@737 2334 }
adamc@879 2335 else
adamc@879 2336 uw_error(ctx, FATAL, "Non-printable character %u in blob to SQLify", c);
adamc@737 2337 }
adamc@737 2338 }
adamc@737 2339
adamc@879 2340 *s2++ = '\'';
adamc@879 2341 strcpy(s2, uw_sqlsuffixBlob);
adamc@879 2342 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixBlob);
adamc@737 2343 return r;
adamc@737 2344 }
adamc@737 2345
adamc@682 2346 char *uw_Basis_sqlifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 2347 int len;
adamc@682 2348 char *r;
adamc@682 2349 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
adamc@682 2350
adamc@682 2351 uw_check_heap(ctx, INTS_MAX + 7);
adamc@682 2352 r = ctx->heap.front;
adamc@879 2353 sprintf(r, uw_sqlfmtInt, combo, &len);
adamc@682 2354 ctx->heap.front += len+1;
adamc@682 2355 return r;
adamc@682 2356 }
adamc@682 2357
adamc@682 2358 char *uw_Basis_attrifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 2359 int len;
adamc@682 2360 char *r;
adamc@682 2361 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
adamc@682 2362
adamc@682 2363 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 2364 r = ctx->heap.front;
adamc@682 2365 sprintf(r, "%lld%n", combo, &len);
adamc@682 2366 ctx->heap.front += len+1;
adamc@682 2367 return r;
adamc@682 2368 }
adamc@682 2369
adamc@879 2370 char *uw_sqlfmtUint4 = "%u::int4%n";
adamc@879 2371
adamc@682 2372 char *uw_Basis_sqlifyClient(uw_context ctx, uw_Basis_client cli) {
adamc@682 2373 int len;
adamc@682 2374 char *r;
adamc@682 2375
adamc@682 2376 uw_check_heap(ctx, INTS_MAX + 7);
adamc@682 2377 r = ctx->heap.front;
adamc@879 2378 sprintf(r, uw_sqlfmtUint4, cli, &len);
adamc@682 2379 ctx->heap.front += len+1;
adamc@682 2380 return r;
adamc@682 2381 }
adamc@682 2382
adamc@682 2383 char *uw_Basis_attrifyClient(uw_context ctx, uw_Basis_client cli) {
adamc@682 2384 int len;
adamc@682 2385 char *r;
adamc@682 2386
adamc@682 2387 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 2388 r = ctx->heap.front;
adamc@682 2389 sprintf(r, "%u%n", cli, &len);
adamc@682 2390 ctx->heap.front += len+1;
adamc@682 2391 return r;
adamc@682 2392 }
adamc@682 2393
adamc@467 2394 uw_Basis_string uw_Basis_sqlifyStringN(uw_context ctx, uw_Basis_string s) {
adamc@467 2395 if (s == NULL)
adamc@467 2396 return "NULL";
adamc@467 2397 else
adamc@467 2398 return uw_Basis_sqlifyString(ctx, s);
adamc@467 2399 }
adamc@467 2400
adamc@311 2401 char *uw_Basis_sqlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 2402 if (b == uw_Basis_False)
adamc@281 2403 return "FALSE";
adamc@281 2404 else
adamc@281 2405 return "TRUE";
adamc@281 2406 }
adamc@282 2407
adamc@467 2408 char *uw_Basis_sqlifyBoolN(uw_context ctx, uw_Basis_bool *b) {
adamc@467 2409 if (b == NULL)
adamc@467 2410 return "NULL";
adamc@467 2411 else
adamc@467 2412 return uw_Basis_sqlifyBool(ctx, *b);
adamc@467 2413 }
adamc@467 2414
adamc@439 2415 char *uw_Basis_sqlifyTime(uw_context ctx, uw_Basis_time t) {
adamc@439 2416 size_t len;
adamc@682 2417 char *r, *s;
adamc@682 2418 struct tm stm;
adamc@682 2419
adamc@682 2420 if (localtime_r(&t, &stm)) {
adamc@682 2421 s = uw_malloc(ctx, TIMES_MAX);
adamc@995 2422 --stm.tm_hour;
adamc@682 2423 len = strftime(s, TIMES_MAX, TIME_FMT, &stm);
adamc@682 2424 r = uw_malloc(ctx, len + 14);
adamc@682 2425 sprintf(r, "'%s'::timestamp", s);
adamc@682 2426 return r;
adamc@682 2427 } else
adamc@682 2428 return "<Invalid time>";
adamc@682 2429 }
adamc@682 2430
adamc@682 2431 char *uw_Basis_attrifyTime(uw_context ctx, uw_Basis_time t) {
adamc@682 2432 size_t len;
adamc@439 2433 char *r;
adamc@439 2434 struct tm stm;
adamc@439 2435
adamc@439 2436 if (localtime_r(&t, &stm)) {
adamc@439 2437 uw_check_heap(ctx, TIMES_MAX);
adamc@666 2438 r = ctx->heap.front;
adamc@439 2439 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 2440 ctx->heap.front += len+1;
adamc@439 2441 return r;
adamc@439 2442 } else
adamc@439 2443 return "<Invalid time>";
adamc@439 2444 }
adamc@439 2445
adamc@467 2446 char *uw_Basis_sqlifyTimeN(uw_context ctx, uw_Basis_time *t) {
adamc@467 2447 if (t == NULL)
adamc@467 2448 return "NULL";
adamc@467 2449 else
adamc@467 2450 return uw_Basis_sqlifyTime(ctx, *t);
adamc@467 2451 }
adamc@467 2452
adamc@311 2453 char *uw_Basis_ensqlBool(uw_Basis_bool b) {
adamc@311 2454 static uw_Basis_int true = 1;
adamc@311 2455 static uw_Basis_int false = 0;
adamc@282 2456
adamc@311 2457 if (b == uw_Basis_False)
adamc@282 2458 return (char *)&false;
adamc@282 2459 else
adamc@282 2460 return (char *)&true;
adamc@282 2461 }
adamc@284 2462
adamc@311 2463 uw_Basis_string uw_Basis_intToString(uw_context ctx, uw_Basis_int n) {
adamc@284 2464 int len;
adamc@284 2465 char *r;
adamc@284 2466
adamc@311 2467 uw_check_heap(ctx, INTS_MAX);
adamc@666 2468 r = ctx->heap.front;
adamc@284 2469 sprintf(r, "%lld%n", n, &len);
adamc@666 2470 ctx->heap.front += len+1;
adamc@284 2471 return r;
adamc@284 2472 }
adamc@285 2473
adamc@311 2474 uw_Basis_string uw_Basis_floatToString(uw_context ctx, uw_Basis_float n) {
adamc@285 2475 int len;
adamc@285 2476 char *r;
adamc@285 2477
adamc@311 2478 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 2479 r = ctx->heap.front;
adamc@285 2480 sprintf(r, "%g%n", n, &len);
adamc@666 2481 ctx->heap.front += len+1;
adamc@285 2482 return r;
adamc@285 2483 }
adamc@285 2484
adamc@821 2485 uw_Basis_string uw_Basis_charToString(uw_context ctx, uw_Basis_char ch) {
adamc@821 2486 char *r = uw_malloc(ctx, 2);
adamc@821 2487 r[0] = ch;
adamc@821 2488 r[1] = 0;
adamc@821 2489 return r;
adamc@821 2490 }
adamc@821 2491
adamc@311 2492 uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) {
adamc@311 2493 if (b == uw_Basis_False)
adamc@285 2494 return "False";
adamc@285 2495 else
adamc@285 2496 return "True";
adamc@285 2497 }
adamc@288 2498
adamc@436 2499 uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) {
adamc@436 2500 size_t len;
adamc@436 2501 char *r;
adamc@436 2502 struct tm stm;
adamc@436 2503
adamc@436 2504 if (localtime_r(&t, &stm)) {
adamc@436 2505 uw_check_heap(ctx, TIMES_MAX);
adamc@666 2506 r = ctx->heap.front;
adamc@436 2507 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 2508 ctx->heap.front += len+1;
adamc@436 2509 return r;
adamc@436 2510 } else
adamc@436 2511 return "<Invalid time>";
adamc@436 2512 }
adamc@288 2513
adamc@311 2514 uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) {
adamc@288 2515 char *endptr;
adamc@311 2516 uw_Basis_int n = strtoll(s, &endptr, 10);
adamc@288 2517
adamc@288 2518 if (*s != '\0' && *endptr == '\0') {
adamc@311 2519 uw_Basis_int *r = uw_malloc(ctx, sizeof(uw_Basis_int));
adamc@288 2520 *r = n;
adamc@288 2521 return r;
adamc@288 2522 } else
adamc@288 2523 return NULL;
adamc@288 2524 }
adamc@289 2525
adamc@311 2526 uw_Basis_float *uw_Basis_stringToFloat(uw_context ctx, uw_Basis_string s) {
adamc@289 2527 char *endptr;
adamc@311 2528 uw_Basis_float n = strtod(s, &endptr);
adamc@289 2529
adamc@289 2530 if (*s != '\0' && *endptr == '\0') {
adamc@311 2531 uw_Basis_float *r = uw_malloc(ctx, sizeof(uw_Basis_float));
adamc@289 2532 *r = n;
adamc@289 2533 return r;
adamc@289 2534 } else
adamc@289 2535 return NULL;
adamc@289 2536 }
adamc@289 2537
adamc@821 2538 uw_Basis_char *uw_Basis_stringToChar(uw_context ctx, uw_Basis_string s) {
adamc@821 2539 if (s[0] == 0) {
adamc@821 2540 uw_Basis_char *r = uw_malloc(ctx, 1);
adamc@821 2541 r[0] = 0;
adamc@821 2542 return r;
adamc@821 2543 } else if (s[1] != 0)
adamc@821 2544 return NULL;
adamc@821 2545 else {
adamc@821 2546 uw_Basis_char *r = uw_malloc(ctx, 1);
adamc@821 2547 r[0] = s[0];
adamc@821 2548 return r;
adamc@821 2549 }
adamc@821 2550 }
adamc@821 2551
adamc@311 2552 uw_Basis_bool *uw_Basis_stringToBool(uw_context ctx, uw_Basis_string s) {
adamc@311 2553 static uw_Basis_bool true = uw_Basis_True;
adamc@311 2554 static uw_Basis_bool false = uw_Basis_False;
adamc@289 2555
adamc@289 2556 if (!strcasecmp (s, "True"))
adamc@289 2557 return &true;
adamc@289 2558 else if (!strcasecmp (s, "False"))
adamc@289 2559 return &false;
adamc@289 2560 else
adamc@289 2561 return NULL;
adamc@289 2562 }
adamc@292 2563
adamc@436 2564 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) {
adamc@438 2565 char *dot = strchr(s, '.'), *end = strchr(s, 0);
adamc@436 2566 struct tm stm;
adamc@436 2567
adamc@439 2568 if (dot) {
adamc@439 2569 *dot = 0;
adamc@439 2570 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adamc@439 2571 *dot = '.';
adamc@439 2572 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adamc@439 2573 *r = mktime(&stm);
adamc@439 2574 return r;
adamc@439 2575 }
adamc@439 2576 else {
adamc@439 2577 *dot = '.';
adamc@439 2578 return NULL;
adamc@439 2579 }
adamc@436 2580 }
adamc@439 2581 else {
adamc@439 2582 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adamc@439 2583 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adamc@439 2584 *r = mktime(&stm);
adamc@439 2585 return r;
adamc@439 2586 }
adamc@439 2587 else if (strptime(s, TIME_FMT, &stm) == end) {
adamc@439 2588 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adamc@439 2589 *r = mktime(&stm);
adamc@439 2590 return r;
adamc@439 2591 }
adamc@439 2592 else
adamc@439 2593 return NULL;
adamc@439 2594 }
adamc@436 2595 }
adamc@436 2596
adamc@311 2597 uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) {
adamc@292 2598 char *endptr;
adamc@311 2599 uw_Basis_int n = strtoll(s, &endptr, 10);
adamc@292 2600
adamc@292 2601 if (*s != '\0' && *endptr == '\0')
adamc@292 2602 return n;
adamc@292 2603 else
adamc@311 2604 uw_error(ctx, FATAL, "Can't parse int: %s", s);
adamc@292 2605 }
adamc@293 2606
adamc@682 2607 #include <errno.h>
adamc@682 2608
adamc@678 2609 uw_Basis_channel uw_Basis_stringToChannel_error(uw_context ctx, uw_Basis_string s) {
adamc@682 2610 unsigned long long n;
adamc@682 2611
adamc@682 2612 if (sscanf(s, "%llu", &n) < 1)
adamc@682 2613 uw_error(ctx, FATAL, "Can't parse channel: %s", s);
adamc@682 2614 else {
adamc@682 2615 uw_Basis_channel ch = {n >> 32, n & ((1ull << 32) - 1)};
adamc@682 2616 return ch;
adamc@682 2617 }
adamc@682 2618 }
adamc@682 2619
adamc@682 2620 uw_Basis_client uw_Basis_stringToClient_error(uw_context ctx, uw_Basis_string s) {
adamc@678 2621 char *endptr;
adamc@682 2622 unsigned long n = strtoul(s, &endptr, 10);
adamc@678 2623
adamc@678 2624 if (*s != '\0' && *endptr == '\0')
adamc@678 2625 return n;
adamc@678 2626 else
adamc@682 2627 uw_error(ctx, FATAL, "Can't parse client: %s", s);
adamc@678 2628 }
adamc@678 2629
adamc@311 2630 uw_Basis_float uw_Basis_stringToFloat_error(uw_context ctx, uw_Basis_string s) {
adamc@293 2631 char *endptr;
adamc@311 2632 uw_Basis_float n = strtod(s, &endptr);
adamc@293 2633
adamc@293 2634 if (*s != '\0' && *endptr == '\0')
adamc@293 2635 return n;
adamc@293 2636 else
adamc@311 2637 uw_error(ctx, FATAL, "Can't parse float: %s", s);
adamc@293 2638 }
adamc@293 2639
adamc@821 2640 uw_Basis_char uw_Basis_stringToChar_error(uw_context ctx, uw_Basis_string s) {
adamc@821 2641 if (s[0] == 0)
adamc@821 2642 return 0;
adamc@821 2643 else if (s[1] != 0)
adamc@821 2644 uw_error(ctx, FATAL, "Can't parse char: %s", s);
adamc@821 2645 else
adamc@821 2646 return s[0];
adamc@821 2647 }
adamc@821 2648
adamc@311 2649 uw_Basis_bool uw_Basis_stringToBool_error(uw_context ctx, uw_Basis_string s) {
adamc@296 2650 if (!strcasecmp(s, "T") || !strcasecmp (s, "True"))
adamc@311 2651 return uw_Basis_True;
adamc@296 2652 else if (!strcasecmp(s, "F") || !strcasecmp (s, "False"))
adamc@311 2653 return uw_Basis_False;
adamc@293 2654 else
adamc@311 2655 uw_error(ctx, FATAL, "Can't parse bool: %s", s);
adamc@293 2656 }
adamc@436 2657
adamc@436 2658 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) {
adamc@438 2659 char *dot = strchr(s, '.'), *end = strchr(s, 0);
adamc@436 2660 struct tm stm = {};
adamc@436 2661
adamc@438 2662 if (dot) {
adamc@438 2663 *dot = 0;
adamc@438 2664 if (strptime(s, TIME_FMT_PG, &stm)) {
adamc@438 2665 *dot = '.';
adamc@438 2666 return mktime(&stm);
adamc@438 2667 }
adamc@438 2668 else {
adamc@438 2669 *dot = '.';
adamc@438 2670 uw_error(ctx, FATAL, "Can't parse time: %s", s);
adamc@438 2671 }
adamc@438 2672 }
adamc@438 2673 else {
adamc@439 2674 if (strptime(s, TIME_FMT_PG, &stm) == end)
adamc@439 2675 return mktime(&stm);
adamc@439 2676 else if (strptime(s, TIME_FMT, &stm) == end)
adamc@438 2677 return mktime(&stm);
adamc@438 2678 else
adamc@438 2679 uw_error(ctx, FATAL, "Can't parse time: %s", s);
adamc@438 2680 }
adamc@436 2681 }
adamc@457 2682
adamc@939 2683 uw_Basis_time uw_Basis_unsqlTime(uw_context ctx, uw_Basis_string s) {
adamc@939 2684 char *dot = strchr(s, '.'), *end = strchr(s, 0);
adamc@939 2685 struct tm stm = {};
adamc@939 2686
adamc@939 2687 if (dot) {
adamc@939 2688 *dot = 0;
adamc@939 2689 if (strptime(s, TIME_FMT_PG, &stm)) {
adamc@939 2690 *dot = '.';
adamc@939 2691 return mktime(&stm);
adamc@939 2692 }
adamc@939 2693 else {
adamc@939 2694 *dot = '.';
adamc@939 2695 uw_error(ctx, FATAL, "Can't parse time: %s", s);
adamc@939 2696 }
adamc@939 2697 }
adamc@939 2698 else {
adamc@939 2699 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adamc@939 2700 return mktime(&stm);
adamc@939 2701 } else if (strptime(s, TIME_FMT, &stm) == end) {
adamc@939 2702 return mktime(&stm);
adamc@939 2703 } else
adamc@939 2704 uw_error(ctx, FATAL, "Can't parse time: %s", s);
adamc@939 2705 }
adamc@939 2706 }
adamc@939 2707
adamc@742 2708 uw_Basis_blob uw_Basis_stringToBlob_error(uw_context ctx, uw_Basis_string s, size_t len) {
adamc@742 2709 char *r = ctx->heap.front;
adamc@742 2710 uw_Basis_blob b = {len, r};
adamc@742 2711
adamc@742 2712 uw_check_heap(ctx, len);
adamc@742 2713
adamc@742 2714 while (*s) {
adamc@742 2715 if (s[0] == '\\') {
adamc@742 2716 if (s[1] == '\\') {
adamc@742 2717 *r++ = '\\';
adamc@742 2718 s += 2;
adamc@742 2719 } else if (isdigit(s[1]) && isdigit(s[2]) && isdigit(s[3])) {
adamc@742 2720 *r++ = (s[1] - '0') * 8 * 8 + ((s[2] - '0') * 8) + (s[3] - '0');
adamc@742 2721 s += 4;
adamc@742 2722 }
adamc@742 2723 else {
adamc@742 2724 *r++ = '\\';
adamc@742 2725 ++s;
adamc@742 2726 }
adamc@742 2727 } else {
adamc@742 2728 *r++ = s[0];
adamc@742 2729 ++s;
adamc@742 2730 }
adamc@742 2731 }
adamc@742 2732
adamc@742 2733 b.size = r - ctx->heap.front;
adamc@742 2734 ctx->heap.front = r;
adamc@742 2735 return b;
adamc@742 2736 }
adamc@742 2737
adamc@1052 2738 #define THE_PAST "expires=Mon, 01-01-1970 00:00:00 GMT"
adamc@1052 2739
adamc@463 2740 uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) {
adamc@463 2741 int len = strlen(c);
adamc@854 2742 char *p = ctx->outHeaders.start;
adamc@474 2743
adamc@474 2744 while (p = strstr(p, "\nSet-Cookie: ")) {
adamc@474 2745 char *p2;
adamc@474 2746 p += 13;
adamc@474 2747 p2 = strchr(p, '=');
adamc@474 2748
adamc@474 2749 if (p2) {
adamc@474 2750 size_t sz = strcspn(p2+1, ";\r\n");
adamc@474 2751
adamc@474 2752 if (!strncasecmp(p, c, p2 - p)) {
adamc@1052 2753 if (sz == 0 && strstr(p2+2, THE_PAST))
adamc@1052 2754 return NULL;
adamc@1052 2755 else {
adamc@1052 2756 char *ret = uw_malloc(ctx, sz + 1);
adamc@1052 2757 memcpy(ret, p2+1, sz);
adamc@1052 2758 ret[sz] = 0;
adamc@1052 2759 return ret;
adamc@1052 2760 }
adamc@474 2761 }
adamc@474 2762 }
adamc@474 2763 }
adamc@463 2764
adamc@854 2765 if (p = uw_Basis_requestHeader(ctx, "Cookie")) {
adamc@1051 2766 char *p2;
adamc@1051 2767
adamc@854 2768 while (1) {
adamc@1051 2769 if (!strncmp(p, c, len) && p[len] == '=') {
adamc@1051 2770 if (p2 = strchr(p, ';')) {
adamc@1051 2771 size_t n = p2 - (p + len);
adamc@1051 2772 char *r = uw_malloc(ctx, n);
adamc@1051 2773 memcpy(r, p + 1 + len, n-1);
adamc@1051 2774 r[n-1] = 0;
adamc@1051 2775 return r;
adamc@1051 2776 } else
adamc@1051 2777 return p + 1 + len;
adamc@1051 2778 } else if (p = strchr(p, ';'))
adamc@854 2779 p += 2;
adamc@463 2780 else
adamc@463 2781 return NULL;
adamc@463 2782 }
adamc@463 2783 }
adamc@792 2784
adamc@792 2785 return NULL;
adamc@457 2786 }
adamc@462 2787
adamc@1050 2788 uw_unit uw_Basis_set_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c, uw_Basis_string v, uw_Basis_time *expires, uw_Basis_bool secure) {
adamc@462 2789 uw_write_header(ctx, "Set-Cookie: ");
adamc@462 2790 uw_write_header(ctx, c);
adamc@462 2791 uw_write_header(ctx, "=");
adamc@462 2792 uw_write_header(ctx, v);
adamc@466 2793 uw_write_header(ctx, "; path=");
adamc@466 2794 uw_write_header(ctx, prefix);
adamc@1050 2795 if (expires) {
adamc@1050 2796 char formatted[30];
adamc@1050 2797 struct tm tm;
adamc@1050 2798
adamc@1050 2799 gmtime_r(expires, &tm);
adamc@1050 2800
adamc@1050 2801 strftime(formatted, sizeof formatted, "%a, %d-%b-%Y %T GMT", &tm);
adamc@1050 2802
adamc@1050 2803 uw_write_header(ctx, "; expires=");
adamc@1050 2804 uw_write_header(ctx, formatted);
adamc@1050 2805 }
adamc@1050 2806 if (secure)
adamc@1050 2807 uw_write_header(ctx, "; secure");
adamc@462 2808 uw_write_header(ctx, "\r\n");
adamc@462 2809
adamc@462 2810 return uw_unit_v;
adamc@462 2811 }
adamc@667 2812
adamc@1050 2813 uw_unit uw_Basis_clear_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c) {
adamc@1050 2814 uw_write_header(ctx, "Set-Cookie: ");
adamc@1050 2815 uw_write_header(ctx, c);
adamc@1050 2816 uw_write_header(ctx, "=; path=");
adamc@1050 2817 uw_write_header(ctx, prefix);
adamc@1052 2818 uw_write_header(ctx, "; " THE_PAST "\r\n");
adamc@1050 2819
adamc@1050 2820 return uw_unit_v;
adamc@1050 2821 }
adamc@1050 2822
adamc@1113 2823 size_t uw_deltas_max = SIZE_MAX;
adamc@1113 2824
adamc@682 2825 static delta *allocate_delta(uw_context ctx, unsigned client) {
adamc@682 2826 unsigned i;
adamc@682 2827 delta *d;
adamc@671 2828
adamc@682 2829 for (i = 0; i < ctx->used_deltas; ++i)
adamc@682 2830 if (ctx->deltas[i].client == client)
adamc@682 2831 return &ctx->deltas[i];
adamc@671 2832
adamc@682 2833 if (ctx->used_deltas >= ctx->n_deltas) {
adamc@1113 2834 if (ctx->n_deltas + 1 > uw_deltas_max)
adamc@1113 2835 uw_error(ctx, FATAL, "Exceeded limit on number of deltas");
adamc@1113 2836
adamc@682 2837 ctx->deltas = realloc(ctx->deltas, sizeof(delta) * ++ctx->n_deltas);
adamc@1113 2838 buf_init(uw_messages_max, &ctx->deltas[ctx->n_deltas-1].msgs, 0);
adamc@682 2839 }
adamc@671 2840
adamc@682 2841 d = &ctx->deltas[ctx->used_deltas++];
adamc@682 2842 d->client = client;
adamc@682 2843 buf_reset(&d->msgs);
adamc@682 2844 return d;
adamc@671 2845 }
adamc@671 2846
adamc@668 2847 uw_Basis_channel uw_Basis_new_channel(uw_context ctx, uw_unit u) {
adamc@682 2848 if (ctx->client == NULL)
adamc@682 2849 uw_error(ctx, FATAL, "Attempt to create channel on request not associated with a persistent connection");
adamc@671 2850
adamc@682 2851 return new_channel(ctx->client);
adamc@668 2852 }
adamc@668 2853
adamc@668 2854 uw_unit uw_Basis_send(uw_context ctx, uw_Basis_channel chn, uw_Basis_string msg) {
adamc@682 2855 delta *d = allocate_delta(ctx, chn.cli);
adamc@682 2856 size_t len;
adamc@682 2857 int preLen;
adamc@682 2858 char pre[INTS_MAX + 2];
adamc@668 2859
adamc@682 2860 len = strlen(msg);
adamc@682 2861
adamc@682 2862 sprintf(pre, "%u\n%n", chn.chn, &preLen);
adamc@682 2863
adamc@1113 2864 ctx_buf_append(ctx, "messages", &d->msgs, pre, preLen);
adamc@1113 2865 ctx_buf_append(ctx, "messages", &d->msgs, msg, len);
adamc@1113 2866 ctx_buf_append(ctx, "messages", &d->msgs, "\n", 1);
adamc@671 2867
adamc@671 2868 return uw_unit_v;
adamc@668 2869 }
adamc@671 2870
adamc@671 2871 void uw_commit(uw_context ctx) {
adamc@1104 2872 int i;
adamc@1104 2873
adamc@1104 2874 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@771 2875 if (ctx->transactionals[i].rollback != NULL)
adamc@1083 2876 if (ctx->transactionals[i].commit)
adamc@1083 2877 ctx->transactionals[i].commit(ctx->transactionals[i].data);
adamc@771 2878
adamc@1104 2879 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@771 2880 if (ctx->transactionals[i].rollback == NULL)
adamc@1083 2881 if (ctx->transactionals[i].commit)
adamc@1083 2882 ctx->transactionals[i].commit(ctx->transactionals[i].data);
adamc@771 2883
adamc@1094 2884 if (ctx->app->db_commit(ctx))
adamc@671 2885 uw_error(ctx, FATAL, "Error running SQL COMMIT");
adamc@682 2886
adamc@682 2887 for (i = 0; i < ctx->used_deltas; ++i) {
adamc@682 2888 delta *d = &ctx->deltas[i];
adamc@682 2889 client *c = find_client(d->client);
adamc@682 2890
adamc@682 2891 assert (c != NULL && c->mode == USED);
adamc@682 2892
adamc@685 2893 client_send(c, &d->msgs);
adamc@682 2894 }
adamc@682 2895
adamc@682 2896 if (ctx->client)
adamc@685 2897 release_client(ctx->client);
adamc@771 2898
adamc@1104 2899 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@1083 2900 if (ctx->transactionals[i].free)
adamc@1083 2901 ctx->transactionals[i].free(ctx->transactionals[i].data);
adamc@916 2902
adamc@916 2903 // Splice script data into appropriate part of page
adamc@1065 2904 if (ctx->returning_indirectly || ctx->script_header[0] == 0) {
adamc@995 2905 char *start = strstr(ctx->page.start, "<sc>");
adamc@995 2906 if (start) {
adamc@995 2907 memmove(start, start + 4, buf_used(&ctx->page) - (start - ctx->page.start) - 4);
adamc@995 2908 ctx->page.front -= 4;
adamc@995 2909 }
adamc@995 2910 } else if (buf_used(&ctx->script) == 0) {
adamc@916 2911 size_t len = strlen(ctx->script_header);
adamc@916 2912 char *start = strstr(ctx->page.start, "<sc>");
adamc@916 2913 if (start) {
adamc@1113 2914 ctx_buf_check(ctx, "page", &ctx->page, buf_used(&ctx->page) - 4 + len);
adamc@917 2915 start = strstr(ctx->page.start, "<sc>");
adamc@916 2916 memmove(start + len, start + 4, buf_used(&ctx->page) - (start - ctx->page.start) - 3);
adamc@916 2917 ctx->page.front += len - 4;
adamc@916 2918 memcpy(start, ctx->script_header, len);
adamc@916 2919 }
adamc@916 2920 } else {
adamc@916 2921 size_t lenH = strlen(ctx->script_header), len = buf_used(&ctx->script);
adamc@916 2922 size_t lenP = lenH + 40 + len;
adamc@916 2923 char *start = strstr(ctx->page.start, "<sc>");
adamc@916 2924 if (start) {
adamc@1113 2925 ctx_buf_check(ctx, "page", &ctx->page, buf_used(&ctx->page) - 4 + lenP);
adamc@917 2926 start = strstr(ctx->page.start, "<sc>");
adamc@916 2927 memmove(start + lenP, start + 4, buf_used(&ctx->page) - (start - ctx->page.start) - 3);
adamc@916 2928 ctx->page.front += lenP - 4;
adamc@916 2929 memcpy(start, ctx->script_header, lenH);
adamc@916 2930 memcpy(start + lenH, "<script type=\"text/javascript\">", 31);
adamc@916 2931 memcpy(start + lenH + 31, ctx->script.start, len);
adamc@916 2932 memcpy(start + lenH + 31 + len, "</script>", 9);
adamc@916 2933 }
adamc@916 2934 }
adamc@671 2935 }
adamc@671 2936
adamc@671 2937 int uw_rollback(uw_context ctx) {
adamc@1104 2938 int i;
adamc@885 2939 cleanup *cl;
adamc@771 2940
adamc@682 2941 if (ctx->client)
adamc@685 2942 release_client(ctx->client);
adamc@671 2943
adamc@885 2944 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@885 2945 cl->func(cl->arg);
adamc@885 2946
adamc@885 2947 ctx->cleanup_front = ctx->cleanup;
adamc@885 2948
adamc@1104 2949 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@771 2950 if (ctx->transactionals[i].rollback != NULL)
adamc@771 2951 ctx->transactionals[i].rollback(ctx->transactionals[i].data);
adamc@771 2952
adamc@1104 2953 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@1084 2954 if (ctx->transactionals[i].free)
adamc@1084 2955 ctx->transactionals[i].free(ctx->transactionals[i].data);
adamc@771 2956
adamc@1094 2957 return ctx->app->db_rollback(ctx);
adamc@671 2958 }
adamc@683 2959
adamc@1113 2960 size_t uw_transactionals_max = SIZE_MAX;
adamc@1113 2961
adamc@771 2962 void uw_register_transactional(uw_context ctx, void *data, uw_callback commit, uw_callback rollback,
adamc@771 2963 uw_callback free) {
adamc@771 2964 if (ctx->used_transactionals >= ctx->n_transactionals) {
adamc@1113 2965 if (ctx->used_transactionals+1 > uw_transactionals_max)
adamc@1113 2966 uw_error(ctx, FATAL, "Exceeded limit on number of transactionals");
adamc@1084 2967 ctx->transactionals = realloc(ctx->transactionals, sizeof(transactional) * (ctx->used_transactionals+1));
adamc@771 2968 ++ctx->n_transactionals;
adamc@771 2969 }
adamc@771 2970
adamc@771 2971 ctx->transactionals[ctx->used_transactionals].data = data;
adamc@771 2972 ctx->transactionals[ctx->used_transactionals].commit = commit;
adamc@771 2973 ctx->transactionals[ctx->used_transactionals].rollback = rollback;
adamc@771 2974 ctx->transactionals[ctx->used_transactionals++].free = free;
adamc@771 2975 }
adamc@771 2976
adamc@683 2977
adamc@683 2978 // "Garbage collection"
adamc@683 2979
adamc@683 2980 static failure_kind uw_expunge(uw_context ctx, uw_Basis_client cli) {
adamc@683 2981 int r = setjmp(ctx->jmp_buf);
adamc@683 2982
adamc@684 2983 if (r == 0) {
adamc@1094 2984 if (ctx->app->db_begin(ctx))
adamc@684 2985 uw_error(ctx, FATAL, "Error running SQL BEGIN");
adamc@1094 2986 ctx->app->expunger(ctx, cli);
adamc@1094 2987 if (ctx->app->db_commit(ctx))
adamc@684 2988 uw_error(ctx, FATAL, "Error running SQL COMMIT");
adamc@684 2989 }
adamc@683 2990
adamc@683 2991 return r;
adamc@683 2992 }
adamc@683 2993
adamc@683 2994 void uw_prune_clients(uw_context ctx) {
adamc@683 2995 client *c, *next, *prev = NULL;
adamc@683 2996 time_t cutoff;
adamc@683 2997
adamc@1094 2998 cutoff = time(NULL) - ctx->app->timeout;
adamc@683 2999
adamc@683 3000 pthread_mutex_lock(&clients_mutex);
adamc@683 3001
adamc@683 3002 for (c = clients_used; c; c = next) {
adamc@683 3003 next = c->next;
adamc@683 3004 pthread_mutex_lock(&c->lock);
adamc@685 3005 if (c->last_contact < cutoff && c->refcount == 0) {
adamc@683 3006 failure_kind fk = UNLIMITED_RETRY;
adamc@683 3007 if (prev)
adamc@683 3008 prev->next = next;
adamc@683 3009 else
adamc@683 3010 clients_used = next;
adamc@684 3011 uw_reset(ctx);
adamc@683 3012 while (fk == UNLIMITED_RETRY) {
adamc@683 3013 fk = uw_expunge(ctx, c->id);
adamc@684 3014 if (fk == UNLIMITED_RETRY) {
adamc@1094 3015 ctx->app->db_rollback(ctx);
adamc@684 3016 printf("Unlimited retry during expunge: %s\n", uw_error_message(ctx));
adamc@683 3017 }
adamc@683 3018 }
adamc@684 3019 if (fk == SUCCESS)
adamc@684 3020 free_client(c);
adamc@684 3021 else {
adamc@1094 3022 ctx->app->db_rollback(ctx);
adamc@856 3023 fprintf(stderr, "Expunge blocked by error: %s\n", uw_error_message(ctx));
adamc@684 3024 }
adamc@683 3025 }
adamc@683 3026 else
adamc@683 3027 prev = c;
adamc@683 3028 pthread_mutex_unlock(&c->lock);
adamc@683 3029 }
adamc@683 3030
adamc@683 3031 pthread_mutex_unlock(&clients_mutex);
adamc@683 3032 }
adamc@687 3033
adamc@687 3034 failure_kind uw_initialize(uw_context ctx) {
adamc@687 3035 int r = setjmp(ctx->jmp_buf);
adamc@687 3036
adamc@687 3037 if (r == 0) {
adamc@1094 3038 if (ctx->app->db_begin(ctx))
adamc@687 3039 uw_error(ctx, FATAL, "Error running SQL BEGIN");
adamc@1094 3040 ctx->app->initializer(ctx);
adamc@1094 3041 if (ctx->app->db_commit(ctx))
adamc@687 3042 uw_error(ctx, FATAL, "Error running SQL COMMIT");
adamc@687 3043 }
adamc@687 3044
adamc@687 3045 return r;
adamc@687 3046 }
adamc@717 3047
adamc@1065 3048 static int url_bad(uw_Basis_string s) {
adamc@1065 3049 for (; *s; ++s)
adamc@1065 3050 if (!isgraph(*s))
adamc@1065 3051 return 1;
adamc@1065 3052
adamc@1065 3053 return 0;
adamc@1065 3054 }
adamc@1065 3055
adamc@717 3056 uw_Basis_string uw_Basis_bless(uw_context ctx, uw_Basis_string s) {
adamc@1065 3057 if (url_bad(s))
adamc@1065 3058 uw_error(ctx, FATAL, "Invalid URL %s", uw_Basis_htmlifyString(ctx, s));
adamc@1094 3059 if (ctx->app->check_url(s))
adamc@770 3060 return s;
adamc@770 3061 else
adamc@770 3062 uw_error(ctx, FATAL, "Disallowed URL %s", uw_Basis_htmlifyString(ctx, s));
adamc@717 3063 }
adamc@734 3064
adamc@770 3065 uw_Basis_string uw_Basis_checkUrl(uw_context ctx, uw_Basis_string s) {
adamc@1065 3066 if (url_bad(s))
adamc@1065 3067 return NULL;
adamc@1094 3068 if (ctx->app->check_url(s))
adamc@770 3069 return s;
adamc@770 3070 else
adamc@770 3071 return NULL;
adamc@770 3072 }
adamc@770 3073
adamc@770 3074 int mime_format(const char *s) {
adamc@770 3075 for (; *s; ++s)
adamc@770 3076 if (!isalnum(*s) && *s != '/' && *s != '-' && *s != '.')
adamc@770 3077 return 0;
adamc@770 3078
adamc@770 3079 return 1;
adamc@770 3080 }
adamc@770 3081
adamc@741 3082 uw_Basis_string uw_Basis_blessMime(uw_context ctx, uw_Basis_string s) {
adamc@770 3083 if (!mime_format(s))
adamc@770 3084 uw_error(ctx, FATAL, "MIME type \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s));
adamc@770 3085
adamc@1094 3086 if (ctx->app->check_mime(s))
adamc@770 3087 return s;
adamc@770 3088 else
adamc@770 3089 uw_error(ctx, FATAL, "Disallowed MIME type %s", uw_Basis_htmlifyString(ctx, s));
adamc@770 3090 }
adamc@770 3091
adamc@770 3092 uw_Basis_string uw_Basis_checkMime(uw_context ctx, uw_Basis_string s) {
adamc@770 3093 if (!mime_format(s))
adamc@770 3094 return NULL;
adamc@770 3095
adamc@1094 3096 if (ctx->app->check_mime(s))
adamc@770 3097 return s;
adamc@770 3098 else
adamc@770 3099 return NULL;
adamc@741 3100 }
adamc@741 3101
adamc@734 3102 uw_Basis_string uw_unnull(uw_Basis_string s) {
adamc@734 3103 return s ? s : "";
adamc@734 3104 }
adamc@734 3105
adamc@734 3106 extern int uw_hash_blocksize;
adamc@734 3107
adamc@734 3108 uw_Basis_string uw_Basis_makeSigString(uw_context ctx, uw_Basis_string sig) {
adamc@734 3109 uw_Basis_string r = uw_malloc(ctx, 2 * uw_hash_blocksize + 1);
adamc@734 3110 int i;
adamc@734 3111
adamc@734 3112 for (i = 0; i < uw_hash_blocksize; ++i)
adamc@734 3113 sprintf(&r[2*i], "%.02X", ((unsigned char *)sig)[i]);
adamc@734 3114
adamc@734 3115 return r;
adamc@734 3116 }
adamc@734 3117
adamc@734 3118 uw_Basis_string uw_Basis_sigString(uw_context ctx, uw_unit u) {
adamc@1094 3119 return ctx->app->cookie_sig(ctx);
adamc@734 3120 }
adamc@737 3121
adamc@737 3122 uw_Basis_string uw_Basis_fileName(uw_context ctx, uw_Basis_file f) {
adamc@737 3123 return f.name;
adamc@737 3124 }
adamc@737 3125
adamc@740 3126 uw_Basis_string uw_Basis_fileMimeType(uw_context ctx, uw_Basis_file f) {
adamc@740 3127 return f.type;
adamc@740 3128 }
adamc@740 3129
adamc@745 3130 uw_Basis_int uw_Basis_blobSize(uw_context ctx, uw_Basis_blob b) {
adamc@745 3131 return b.size;
adamc@745 3132 }
adamc@745 3133
adamc@737 3134 uw_Basis_blob uw_Basis_fileData(uw_context ctx, uw_Basis_file f) {
adamc@737 3135 return f.data;
adamc@737 3136 }
adamc@741 3137
adamc@741 3138 __attribute__((noreturn)) void uw_return_blob(uw_context ctx, uw_Basis_blob b, uw_Basis_string mimeType) {
adamc@741 3139 cleanup *cl;
adamc@741 3140 int len;
adamc@741 3141
adamc@1065 3142 ctx->returning_indirectly = 1;
adamc@741 3143 buf_reset(&ctx->outHeaders);
adamc@741 3144 buf_reset(&ctx->page);
adamc@741 3145
adamc@856 3146 uw_write_header(ctx, on_success);
adamc@856 3147 uw_write_header(ctx, "Content-Type: ");
adamc@741 3148 uw_write_header(ctx, mimeType);
adamc@741 3149 uw_write_header(ctx, "\r\nContent-Length: ");
adamc@1113 3150 ctx_buf_check(ctx, "headers", &ctx->outHeaders, INTS_MAX);
adamc@741 3151 sprintf(ctx->outHeaders.front, "%d%n", b.size, &len);
adamc@741 3152 ctx->outHeaders.front += len;
adamc@1066 3153 uw_write_header(ctx, "\r\n");
adamc@741 3154
adamc@1113 3155 ctx_buf_append(ctx, "page", &ctx->page, b.data, b.size);
adamc@741 3156
adamc@741 3157 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@741 3158 cl->func(cl->arg);
adamc@741 3159
adamc@741 3160 ctx->cleanup_front = ctx->cleanup;
adamc@741 3161
adamc@1065 3162 longjmp(ctx->jmp_buf, RETURN_INDIRECTLY);
adamc@1065 3163 }
adamc@1065 3164
adamc@1065 3165 __attribute__((noreturn)) void uw_redirect(uw_context ctx, uw_Basis_string url) {
adamc@1065 3166 cleanup *cl;
adamc@1065 3167 int len;
adamc@1066 3168 char *s;
adamc@1065 3169
adamc@1065 3170 ctx->returning_indirectly = 1;
adamc@1066 3171 buf_reset(&ctx->page);
adamc@1113 3172 ctx_buf_check(ctx, "page", &ctx->page, buf_used(&ctx->outHeaders)+1);
adamc@1066 3173 memcpy(ctx->page.start, ctx->outHeaders.start, buf_used(&ctx->outHeaders));
adamc@1066 3174 ctx->page.start[buf_used(&ctx->outHeaders)] = 0;
adamc@1065 3175 buf_reset(&ctx->outHeaders);
adamc@1065 3176
adamc@1065 3177 uw_write_header(ctx, on_redirect);
adamc@1066 3178
adamc@1066 3179 s = strchr(ctx->page.start, '\n');
adamc@1066 3180 if (s) {
adamc@1066 3181 char *s2;
adamc@1066 3182 for (++s; s2 = strchr(s, '\n'); s = s2+1) {
adamc@1066 3183 *s2 = 0;
adamc@1066 3184 if (!strncmp(s, "Set-Cookie: ", 12)) {
adamc@1066 3185 uw_write_header(ctx, s);
adamc@1066 3186 uw_write_header(ctx, "\n");
adamc@1066 3187 }
adamc@1066 3188 }
adamc@1066 3189 }
adamc@1066 3190
adamc@1065 3191 uw_write_header(ctx, "Location: ");
adamc@1065 3192 uw_write_header(ctx, url);
adamc@1065 3193 uw_write_header(ctx, "\r\n\r\n");
adamc@1065 3194
adamc@1065 3195 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@1065 3196 cl->func(cl->arg);
adamc@1065 3197
adamc@1065 3198 ctx->cleanup_front = ctx->cleanup;
adamc@1065 3199
adamc@1065 3200 longjmp(ctx->jmp_buf, RETURN_INDIRECTLY);
adamc@741 3201 }
adamc@874 3202
adamc@874 3203 uw_Basis_string uw_Basis_unAs(uw_context ctx, uw_Basis_string s) {
adamc@874 3204 uw_Basis_string r = uw_malloc(ctx, strlen(s) + 1);
adamc@874 3205
adamc@874 3206 for (; *s; ++s) {
adamc@874 3207 if (s[0] == '\'') {
adamc@874 3208 *r++ = '\'';
adamc@874 3209 for (++s; *s; ++s) {
adamc@874 3210 if (s[0] == '\'') {
adamc@874 3211 *r++ = '\'';
adamc@874 3212 break;
adamc@874 3213 } else if (s[0] == '\\') {
adamc@874 3214 if (s[1] == '\\') {
adamc@874 3215 *r++ = '\\';
adamc@874 3216 *r++ = '\\';
adamc@874 3217 ++s;
adamc@874 3218 } else if (s[1] == '\'') {
adamc@874 3219 *r++ = '\\';
adamc@874 3220 *r++ = '\'';
adamc@874 3221 ++s;
adamc@874 3222 } else
adamc@874 3223 *r++ = '\'';
adamc@874 3224 } else
adamc@874 3225 *r++ = s[0];
adamc@874 3226 }
adamc@874 3227 if (*s == 0) break;
adamc@874 3228 } else if (s[0] == 'T' && s[1] == '.')
adamc@874 3229 ++s;
adamc@874 3230 else
adamc@874 3231 *r++ = s[0];
adamc@874 3232 }
adamc@874 3233
adamc@874 3234 return r;
adamc@874 3235 }
adamc@874 3236
adamc@922 3237 uw_Basis_string uw_Basis_mstrcat(uw_context ctx, ...) {
adamc@922 3238 va_list ap;
adamc@922 3239 size_t len = 1;
adamc@922 3240 char *s, *r, *s2;
adamc@922 3241
adamc@922 3242 va_start(ap, ctx);
adamc@922 3243 for (s = va_arg(ap, char*); s; s = va_arg(ap, char*))
adamc@922 3244 len += strlen(s);
adamc@922 3245 va_end(ap);
adamc@922 3246
adamc@922 3247 r = uw_malloc(ctx, len);
adamc@922 3248 va_start(ap, ctx);
adamc@922 3249 for (s2 = r, s = va_arg(ap, char*); s; s = va_arg(ap, char*))
adamc@922 3250 while (*s)
adamc@922 3251 *s2++ = *s++;
adamc@922 3252 va_end(ap);
adamc@922 3253 *s2 = 0;
adamc@922 3254
adamc@922 3255 return r;
adamc@922 3256 }
adamc@1006 3257
adamc@1050 3258 const uw_Basis_time minTime = 0;
adamc@1050 3259
adamc@1006 3260 uw_Basis_time uw_Basis_now(uw_context ctx) {
adamc@1006 3261 return time(NULL);
adamc@1006 3262 }
adamc@1061 3263
adamc@1061 3264 void *uw_get_global(uw_context ctx, char *name) {
adamc@1061 3265 int i;
adamc@1061 3266
adamc@1061 3267 for (i = 0; i < ctx->n_globals; ++i)
adamc@1061 3268 if (!strcmp(name, ctx->globals[i].name))
adamc@1061 3269 return ctx->globals[i].data;
adamc@1061 3270
adamc@1061 3271 return NULL;
adamc@1061 3272 }
adamc@1061 3273
adamc@1113 3274 size_t uw_globals_max = SIZE_MAX;
adamc@1113 3275
adamc@1061 3276 void uw_set_global(uw_context ctx, char *name, void *data, void (*free)(void*)) {
adamc@1061 3277 int i;
adamc@1061 3278
adamc@1061 3279 for (i = 0; i < ctx->n_globals; ++i)
adamc@1061 3280 if (!strcmp(name, ctx->globals[i].name)) {
adamc@1089 3281 if (ctx->globals[i].free)
adamc@1061 3282 ctx->globals[i].free(ctx->globals[i].data);
adamc@1061 3283 ctx->globals[i].data = data;
adamc@1061 3284 ctx->globals[i].free = free;
adamc@1061 3285 return;
adamc@1061 3286 }
adamc@1089 3287
adamc@1113 3288 if (ctx->n_globals+1 > uw_globals_max)
adamc@1113 3289 uw_error(ctx, FATAL, "Exceeded limit on number of globals");
adamc@1113 3290
adamc@1061 3291 ++ctx->n_globals;
adamc@1061 3292 ctx->globals = realloc(ctx->globals, ctx->n_globals * sizeof(global));
adamc@1061 3293 ctx->globals[ctx->n_globals-1].name = name;
adamc@1061 3294 ctx->globals[ctx->n_globals-1].data = data;
adamc@1061 3295 ctx->globals[ctx->n_globals-1].free = free;
adamc@1061 3296 }
adamc@1061 3297
adamc@1061 3298 uw_Basis_bool uw_Basis_isalnum(uw_context ctx, uw_Basis_char c) {
adamc@1063 3299 return !!isalnum(c);
adamc@1061 3300 }
adamc@1061 3301
adamc@1061 3302 uw_Basis_bool uw_Basis_isalpha(uw_context ctx, uw_Basis_char c) {
adamc@1063 3303 return !!isalpha(c);
adamc@1061 3304 }
adamc@1061 3305
adamc@1061 3306 uw_Basis_bool uw_Basis_isblank(uw_context ctx, uw_Basis_char c) {
adamc@1063 3307 return !!isblank(c);
adamc@1061 3308 }
adamc@1061 3309
adamc@1061 3310 uw_Basis_bool uw_Basis_iscntrl(uw_context ctx, uw_Basis_char c) {
adamc@1063 3311 return !!iscntrl(c);
adamc@1061 3312 }
adamc@1061 3313
adamc@1061 3314 uw_Basis_bool uw_Basis_isdigit(uw_context ctx, uw_Basis_char c) {
adamc@1063 3315 return !!isdigit(c);
adamc@1061 3316 }
adamc@1061 3317
adamc@1061 3318 uw_Basis_bool uw_Basis_isgraph(uw_context ctx, uw_Basis_char c) {
adamc@1063 3319 return !!isgraph(c);
adamc@1061 3320 }
adamc@1061 3321
adamc@1061 3322 uw_Basis_bool uw_Basis_islower(uw_context ctx, uw_Basis_char c) {
adamc@1063 3323 return !!islower(c);
adamc@1061 3324 }
adamc@1061 3325
adamc@1061 3326 uw_Basis_bool uw_Basis_isprint(uw_context ctx, uw_Basis_char c) {
adamc@1063 3327 return !!isprint(c);
adamc@1061 3328 }
adamc@1061 3329
adamc@1061 3330 uw_Basis_bool uw_Basis_ispunct(uw_context ctx, uw_Basis_char c) {
adamc@1063 3331 return !!ispunct(c);
adamc@1061 3332 }
adamc@1061 3333
adamc@1061 3334 uw_Basis_bool uw_Basis_isspace(uw_context ctx, uw_Basis_char c) {
adamc@1063 3335 return !!isspace(c);
adamc@1061 3336 }
adamc@1063 3337
adamc@1061 3338 uw_Basis_bool uw_Basis_isupper(uw_context ctx, uw_Basis_char c) {
adamc@1063 3339 return !!isupper(c);
adamc@1061 3340 }
adamc@1061 3341
adamc@1061 3342 uw_Basis_bool uw_Basis_isxdigit(uw_context ctx, uw_Basis_char c) {
adamc@1063 3343 return !!isxdigit(c);
adamc@1061 3344 }
adamc@1061 3345
adamc@1061 3346 uw_Basis_char uw_Basis_tolower(uw_context ctx, uw_Basis_char c) {
adamc@1061 3347 return tolower(c);
adamc@1061 3348 }
adamc@1061 3349
adamc@1061 3350 uw_Basis_char uw_Basis_toupper(uw_context ctx, uw_Basis_char c) {
adamc@1061 3351 return toupper(c);
adamc@1061 3352 }
adamc@1066 3353
adamc@1066 3354 uw_Basis_string uw_Basis_currentUrl(uw_context ctx) {
adamc@1066 3355 return ctx->current_url;
adamc@1066 3356 }
adamc@1066 3357
adamc@1066 3358 void uw_set_currentUrl(uw_context ctx, char *s) {
adamc@1066 3359 ctx->current_url = s;
adamc@1066 3360 }
adamc@1114 3361
adamc@1114 3362 void uw_set_deadline(uw_context ctx, int n) {
adamc@1114 3363 ctx->deadline = n;
adamc@1114 3364 }
adamc@1114 3365
adamc@1114 3366 void uw_check_deadline(uw_context ctx) {
adamc@1114 3367 if (uw_time > ctx->deadline)
adamc@1114 3368 uw_error(ctx, FATAL, "Maximum running time exceeded");
adamc@1114 3369 }
adamc@1115 3370
adamc@1115 3371 size_t uw_database_max = SIZE_MAX;