annotate src/c/urweb.c @ 863:305bc0a431de

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