annotate src/c/urweb.c @ 912:771449d8b411

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