annotate src/c/urweb.c @ 835:b0a85cbefed2

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