annotate src/c/urweb.c @ 1131:94e83c5533d2

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