annotate src/c/urweb.c @ 874:3c7b48040dcf

MySQL demo/sql succeeds in reading no rows
author Adam Chlipala <adamc@hcoop.net>
date Sun, 12 Jul 2009 15:05:40 -0400
parents 03e7f111fe99
children b2a175a0f2ef
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@666 1238 ctx->heap.front = s2 + 1;
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@311 1935 char *uw_Basis_sqlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@281 1936 int len;
adamc@281 1937 char *r;
adamc@281 1938
adamc@311 1939 uw_check_heap(ctx, INTS_MAX + 6);
adamc@666 1940 r = ctx->heap.front;
adamc@281 1941 sprintf(r, "%lld::int8%n", n, &len);
adamc@666 1942 ctx->heap.front += len+1;
adamc@281 1943 return r;
adamc@281 1944 }
adamc@281 1945
adamc@467 1946 char *uw_Basis_sqlifyIntN(uw_context ctx, uw_Basis_int *n) {
adamc@467 1947 if (n == NULL)
adamc@467 1948 return "NULL";
adamc@467 1949 else
adamc@467 1950 return uw_Basis_sqlifyInt(ctx, *n);
adamc@467 1951 }
adamc@467 1952
adamc@311 1953 char *uw_Basis_sqlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@281 1954 int len;
adamc@281 1955 char *r;
adamc@281 1956
adamc@311 1957 uw_check_heap(ctx, FLOATS_MAX + 8);
adamc@666 1958 r = ctx->heap.front;
adamc@281 1959 sprintf(r, "%g::float8%n", n, &len);
adamc@666 1960 ctx->heap.front += len+1;
adamc@281 1961 return r;
adamc@281 1962 }
adamc@281 1963
adamc@467 1964 char *uw_Basis_sqlifyFloatN(uw_context ctx, uw_Basis_float *n) {
adamc@467 1965 if (n == NULL)
adamc@467 1966 return "NULL";
adamc@467 1967 else
adamc@467 1968 return uw_Basis_sqlifyFloat(ctx, *n);
adamc@467 1969 }
adamc@467 1970
adamc@281 1971
adamc@311 1972 uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) {
adamc@280 1973 char *r, *s2;
adamc@280 1974
adamc@311 1975 uw_check_heap(ctx, strlen(s) * 2 + 10);
adamc@280 1976
adamc@666 1977 r = s2 = ctx->heap.front;
adamc@280 1978 *s2++ = 'E';
adamc@280 1979 *s2++ = '\'';
adamc@280 1980
adamc@280 1981 for (; *s; s++) {
adamc@280 1982 char c = *s;
adamc@280 1983
adamc@280 1984 switch (c) {
adamc@280 1985 case '\'':
adamc@280 1986 strcpy(s2, "\\'");
adamc@280 1987 s2 += 2;
adamc@280 1988 break;
adamc@280 1989 case '\\':
adamc@280 1990 strcpy(s2, "\\\\");
adamc@280 1991 s2 += 2;
adamc@280 1992 break;
adamc@280 1993 default:
adamc@280 1994 if (isprint(c))
adamc@280 1995 *s2++ = c;
adamc@280 1996 else {
adamc@737 1997 sprintf(s2, "\\%03o", c);
adamc@280 1998 s2 += 4;
adamc@280 1999 }
adamc@280 2000 }
adamc@280 2001 }
adamc@280 2002
adamc@281 2003 strcpy(s2, "'::text");
adamc@666 2004 ctx->heap.front = s2 + 8;
adamc@280 2005 return r;
adamc@280 2006 }
adamc@281 2007
adamc@737 2008 uw_Basis_string uw_Basis_sqlifyBlob(uw_context ctx, uw_Basis_blob b) {
adamc@737 2009 char *r, *s2;
adamc@737 2010 size_t i;
adamc@737 2011
adamc@737 2012 uw_check_heap(ctx, b.size * 5 + 11);
adamc@737 2013
adamc@737 2014 r = s2 = ctx->heap.front;
adamc@737 2015 *s2++ = 'E';
adamc@737 2016 *s2++ = '\'';
adamc@737 2017
adamc@737 2018 for (i = 0; i < b.size; ++i) {
adamc@737 2019 char c = b.data[i];
adamc@737 2020
adamc@737 2021 switch (c) {
adamc@737 2022 case '\'':
adamc@737 2023 strcpy(s2, "\\'");
adamc@737 2024 s2 += 2;
adamc@737 2025 break;
adamc@737 2026 case '\\':
adamc@737 2027 strcpy(s2, "\\\\\\\\");
adamc@737 2028 s2 += 4;
adamc@737 2029 break;
adamc@737 2030 default:
adamc@737 2031 if (isprint(c))
adamc@737 2032 *s2++ = c;
adamc@737 2033 else {
adamc@737 2034 sprintf(s2, "\\\\%03o", c);
adamc@737 2035 s2 += 5;
adamc@737 2036 }
adamc@737 2037 }
adamc@737 2038 }
adamc@737 2039
adamc@737 2040 strcpy(s2, "'::bytea");
adamc@737 2041 ctx->heap.front = s2 + 9;
adamc@737 2042 return r;
adamc@737 2043 }
adamc@737 2044
adamc@682 2045 char *uw_Basis_sqlifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 2046 int len;
adamc@682 2047 char *r;
adamc@682 2048 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
adamc@682 2049
adamc@682 2050 uw_check_heap(ctx, INTS_MAX + 7);
adamc@682 2051 r = ctx->heap.front;
adamc@682 2052 sprintf(r, "%lld::int8%n", combo, &len);
adamc@682 2053 ctx->heap.front += len+1;
adamc@682 2054 return r;
adamc@682 2055 }
adamc@682 2056
adamc@682 2057 char *uw_Basis_attrifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 2058 int len;
adamc@682 2059 char *r;
adamc@682 2060 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
adamc@682 2061
adamc@682 2062 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 2063 r = ctx->heap.front;
adamc@682 2064 sprintf(r, "%lld%n", combo, &len);
adamc@682 2065 ctx->heap.front += len+1;
adamc@682 2066 return r;
adamc@682 2067 }
adamc@682 2068
adamc@682 2069 char *uw_Basis_sqlifyClient(uw_context ctx, uw_Basis_client cli) {
adamc@682 2070 int len;
adamc@682 2071 char *r;
adamc@682 2072
adamc@682 2073 uw_check_heap(ctx, INTS_MAX + 7);
adamc@682 2074 r = ctx->heap.front;
adamc@682 2075 sprintf(r, "%u::int4%n", cli, &len);
adamc@682 2076 ctx->heap.front += len+1;
adamc@682 2077 return r;
adamc@682 2078 }
adamc@682 2079
adamc@682 2080 char *uw_Basis_attrifyClient(uw_context ctx, uw_Basis_client cli) {
adamc@682 2081 int len;
adamc@682 2082 char *r;
adamc@682 2083
adamc@682 2084 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 2085 r = ctx->heap.front;
adamc@682 2086 sprintf(r, "%u%n", cli, &len);
adamc@682 2087 ctx->heap.front += len+1;
adamc@682 2088 return r;
adamc@682 2089 }
adamc@682 2090
adamc@467 2091 uw_Basis_string uw_Basis_sqlifyStringN(uw_context ctx, uw_Basis_string s) {
adamc@467 2092 if (s == NULL)
adamc@467 2093 return "NULL";
adamc@467 2094 else
adamc@467 2095 return uw_Basis_sqlifyString(ctx, s);
adamc@467 2096 }
adamc@467 2097
adamc@311 2098 char *uw_Basis_sqlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 2099 if (b == uw_Basis_False)
adamc@281 2100 return "FALSE";
adamc@281 2101 else
adamc@281 2102 return "TRUE";
adamc@281 2103 }
adamc@282 2104
adamc@467 2105 char *uw_Basis_sqlifyBoolN(uw_context ctx, uw_Basis_bool *b) {
adamc@467 2106 if (b == NULL)
adamc@467 2107 return "NULL";
adamc@467 2108 else
adamc@467 2109 return uw_Basis_sqlifyBool(ctx, *b);
adamc@467 2110 }
adamc@467 2111
adamc@439 2112 char *uw_Basis_sqlifyTime(uw_context ctx, uw_Basis_time t) {
adamc@439 2113 size_t len;
adamc@682 2114 char *r, *s;
adamc@682 2115 struct tm stm;
adamc@682 2116
adamc@682 2117 if (localtime_r(&t, &stm)) {
adamc@682 2118 s = uw_malloc(ctx, TIMES_MAX);
adamc@682 2119 len = strftime(s, TIMES_MAX, TIME_FMT, &stm);
adamc@682 2120 r = uw_malloc(ctx, len + 14);
adamc@682 2121 sprintf(r, "'%s'::timestamp", s);
adamc@682 2122 return r;
adamc@682 2123 } else
adamc@682 2124 return "<Invalid time>";
adamc@682 2125 }
adamc@682 2126
adamc@682 2127 char *uw_Basis_attrifyTime(uw_context ctx, uw_Basis_time t) {
adamc@682 2128 size_t len;
adamc@439 2129 char *r;
adamc@439 2130 struct tm stm;
adamc@439 2131
adamc@439 2132 if (localtime_r(&t, &stm)) {
adamc@439 2133 uw_check_heap(ctx, TIMES_MAX);
adamc@666 2134 r = ctx->heap.front;
adamc@439 2135 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 2136 ctx->heap.front += len+1;
adamc@439 2137 return r;
adamc@439 2138 } else
adamc@439 2139 return "<Invalid time>";
adamc@439 2140 }
adamc@439 2141
adamc@467 2142 char *uw_Basis_sqlifyTimeN(uw_context ctx, uw_Basis_time *t) {
adamc@467 2143 if (t == NULL)
adamc@467 2144 return "NULL";
adamc@467 2145 else
adamc@467 2146 return uw_Basis_sqlifyTime(ctx, *t);
adamc@467 2147 }
adamc@467 2148
adamc@311 2149 char *uw_Basis_ensqlBool(uw_Basis_bool b) {
adamc@311 2150 static uw_Basis_int true = 1;
adamc@311 2151 static uw_Basis_int false = 0;
adamc@282 2152
adamc@311 2153 if (b == uw_Basis_False)
adamc@282 2154 return (char *)&false;
adamc@282 2155 else
adamc@282 2156 return (char *)&true;
adamc@282 2157 }
adamc@284 2158
adamc@311 2159 uw_Basis_string uw_Basis_intToString(uw_context ctx, uw_Basis_int n) {
adamc@284 2160 int len;
adamc@284 2161 char *r;
adamc@284 2162
adamc@311 2163 uw_check_heap(ctx, INTS_MAX);
adamc@666 2164 r = ctx->heap.front;
adamc@284 2165 sprintf(r, "%lld%n", n, &len);
adamc@666 2166 ctx->heap.front += len+1;
adamc@284 2167 return r;
adamc@284 2168 }
adamc@285 2169
adamc@311 2170 uw_Basis_string uw_Basis_floatToString(uw_context ctx, uw_Basis_float n) {
adamc@285 2171 int len;
adamc@285 2172 char *r;
adamc@285 2173
adamc@311 2174 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 2175 r = ctx->heap.front;
adamc@285 2176 sprintf(r, "%g%n", n, &len);
adamc@666 2177 ctx->heap.front += len+1;
adamc@285 2178 return r;
adamc@285 2179 }
adamc@285 2180
adamc@821 2181 uw_Basis_string uw_Basis_charToString(uw_context ctx, uw_Basis_char ch) {
adamc@821 2182 char *r = uw_malloc(ctx, 2);
adamc@821 2183 r[0] = ch;
adamc@821 2184 r[1] = 0;
adamc@821 2185 return r;
adamc@821 2186 }
adamc@821 2187
adamc@311 2188 uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) {
adamc@311 2189 if (b == uw_Basis_False)
adamc@285 2190 return "False";
adamc@285 2191 else
adamc@285 2192 return "True";
adamc@285 2193 }
adamc@288 2194
adamc@436 2195 uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) {
adamc@436 2196 size_t len;
adamc@436 2197 char *r;
adamc@436 2198 struct tm stm;
adamc@436 2199
adamc@436 2200 if (localtime_r(&t, &stm)) {
adamc@436 2201 uw_check_heap(ctx, TIMES_MAX);
adamc@666 2202 r = ctx->heap.front;
adamc@436 2203 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 2204 ctx->heap.front += len+1;
adamc@436 2205 return r;
adamc@436 2206 } else
adamc@436 2207 return "<Invalid time>";
adamc@436 2208 }
adamc@288 2209
adamc@311 2210 uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) {
adamc@288 2211 char *endptr;
adamc@311 2212 uw_Basis_int n = strtoll(s, &endptr, 10);
adamc@288 2213
adamc@288 2214 if (*s != '\0' && *endptr == '\0') {
adamc@311 2215 uw_Basis_int *r = uw_malloc(ctx, sizeof(uw_Basis_int));
adamc@288 2216 *r = n;
adamc@288 2217 return r;
adamc@288 2218 } else
adamc@288 2219 return NULL;
adamc@288 2220 }
adamc@289 2221
adamc@311 2222 uw_Basis_float *uw_Basis_stringToFloat(uw_context ctx, uw_Basis_string s) {
adamc@289 2223 char *endptr;
adamc@311 2224 uw_Basis_float n = strtod(s, &endptr);
adamc@289 2225
adamc@289 2226 if (*s != '\0' && *endptr == '\0') {
adamc@311 2227 uw_Basis_float *r = uw_malloc(ctx, sizeof(uw_Basis_float));
adamc@289 2228 *r = n;
adamc@289 2229 return r;
adamc@289 2230 } else
adamc@289 2231 return NULL;
adamc@289 2232 }
adamc@289 2233
adamc@821 2234 uw_Basis_char *uw_Basis_stringToChar(uw_context ctx, uw_Basis_string s) {
adamc@821 2235 if (s[0] == 0) {
adamc@821 2236 uw_Basis_char *r = uw_malloc(ctx, 1);
adamc@821 2237 r[0] = 0;
adamc@821 2238 return r;
adamc@821 2239 } else if (s[1] != 0)
adamc@821 2240 return NULL;
adamc@821 2241 else {
adamc@821 2242 uw_Basis_char *r = uw_malloc(ctx, 1);
adamc@821 2243 r[0] = s[0];
adamc@821 2244 return r;
adamc@821 2245 }
adamc@821 2246 }
adamc@821 2247
adamc@311 2248 uw_Basis_bool *uw_Basis_stringToBool(uw_context ctx, uw_Basis_string s) {
adamc@311 2249 static uw_Basis_bool true = uw_Basis_True;
adamc@311 2250 static uw_Basis_bool false = uw_Basis_False;
adamc@289 2251
adamc@289 2252 if (!strcasecmp (s, "True"))
adamc@289 2253 return &true;
adamc@289 2254 else if (!strcasecmp (s, "False"))
adamc@289 2255 return &false;
adamc@289 2256 else
adamc@289 2257 return NULL;
adamc@289 2258 }
adamc@292 2259
adamc@436 2260 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) {
adamc@438 2261 char *dot = strchr(s, '.'), *end = strchr(s, 0);
adamc@436 2262 struct tm stm;
adamc@436 2263
adamc@439 2264 if (dot) {
adamc@439 2265 *dot = 0;
adamc@439 2266 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adamc@439 2267 *dot = '.';
adamc@439 2268 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adamc@439 2269 *r = mktime(&stm);
adamc@439 2270 return r;
adamc@439 2271 }
adamc@439 2272 else {
adamc@439 2273 *dot = '.';
adamc@439 2274 return NULL;
adamc@439 2275 }
adamc@436 2276 }
adamc@439 2277 else {
adamc@439 2278 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adamc@439 2279 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adamc@439 2280 *r = mktime(&stm);
adamc@439 2281 return r;
adamc@439 2282 }
adamc@439 2283 else if (strptime(s, TIME_FMT, &stm) == end) {
adamc@439 2284 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adamc@439 2285 *r = mktime(&stm);
adamc@439 2286 return r;
adamc@439 2287 }
adamc@439 2288 else
adamc@439 2289 return NULL;
adamc@439 2290 }
adamc@436 2291 }
adamc@436 2292
adamc@311 2293 uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) {
adamc@292 2294 char *endptr;
adamc@311 2295 uw_Basis_int n = strtoll(s, &endptr, 10);
adamc@292 2296
adamc@292 2297 if (*s != '\0' && *endptr == '\0')
adamc@292 2298 return n;
adamc@292 2299 else
adamc@311 2300 uw_error(ctx, FATAL, "Can't parse int: %s", s);
adamc@292 2301 }
adamc@293 2302
adamc@682 2303 #include <errno.h>
adamc@682 2304
adamc@678 2305 uw_Basis_channel uw_Basis_stringToChannel_error(uw_context ctx, uw_Basis_string s) {
adamc@682 2306 unsigned long long n;
adamc@682 2307
adamc@682 2308 if (sscanf(s, "%llu", &n) < 1)
adamc@682 2309 uw_error(ctx, FATAL, "Can't parse channel: %s", s);
adamc@682 2310 else {
adamc@682 2311 uw_Basis_channel ch = {n >> 32, n & ((1ull << 32) - 1)};
adamc@682 2312 return ch;
adamc@682 2313 }
adamc@682 2314 }
adamc@682 2315
adamc@682 2316 uw_Basis_client uw_Basis_stringToClient_error(uw_context ctx, uw_Basis_string s) {
adamc@678 2317 char *endptr;
adamc@682 2318 unsigned long n = strtoul(s, &endptr, 10);
adamc@678 2319
adamc@678 2320 if (*s != '\0' && *endptr == '\0')
adamc@678 2321 return n;
adamc@678 2322 else
adamc@682 2323 uw_error(ctx, FATAL, "Can't parse client: %s", s);
adamc@678 2324 }
adamc@678 2325
adamc@311 2326 uw_Basis_float uw_Basis_stringToFloat_error(uw_context ctx, uw_Basis_string s) {
adamc@293 2327 char *endptr;
adamc@311 2328 uw_Basis_float n = strtod(s, &endptr);
adamc@293 2329
adamc@293 2330 if (*s != '\0' && *endptr == '\0')
adamc@293 2331 return n;
adamc@293 2332 else
adamc@311 2333 uw_error(ctx, FATAL, "Can't parse float: %s", s);
adamc@293 2334 }
adamc@293 2335
adamc@821 2336 uw_Basis_char uw_Basis_stringToChar_error(uw_context ctx, uw_Basis_string s) {
adamc@821 2337 if (s[0] == 0)
adamc@821 2338 return 0;
adamc@821 2339 else if (s[1] != 0)
adamc@821 2340 uw_error(ctx, FATAL, "Can't parse char: %s", s);
adamc@821 2341 else
adamc@821 2342 return s[0];
adamc@821 2343 }
adamc@821 2344
adamc@311 2345 uw_Basis_bool uw_Basis_stringToBool_error(uw_context ctx, uw_Basis_string s) {
adamc@296 2346 if (!strcasecmp(s, "T") || !strcasecmp (s, "True"))
adamc@311 2347 return uw_Basis_True;
adamc@296 2348 else if (!strcasecmp(s, "F") || !strcasecmp (s, "False"))
adamc@311 2349 return uw_Basis_False;
adamc@293 2350 else
adamc@311 2351 uw_error(ctx, FATAL, "Can't parse bool: %s", s);
adamc@293 2352 }
adamc@436 2353
adamc@436 2354 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) {
adamc@438 2355 char *dot = strchr(s, '.'), *end = strchr(s, 0);
adamc@436 2356 struct tm stm = {};
adamc@436 2357
adamc@438 2358 if (dot) {
adamc@438 2359 *dot = 0;
adamc@438 2360 if (strptime(s, TIME_FMT_PG, &stm)) {
adamc@438 2361 *dot = '.';
adamc@438 2362 return mktime(&stm);
adamc@438 2363 }
adamc@438 2364 else {
adamc@438 2365 *dot = '.';
adamc@438 2366 uw_error(ctx, FATAL, "Can't parse time: %s", s);
adamc@438 2367 }
adamc@438 2368 }
adamc@438 2369 else {
adamc@439 2370 if (strptime(s, TIME_FMT_PG, &stm) == end)
adamc@439 2371 return mktime(&stm);
adamc@439 2372 else if (strptime(s, TIME_FMT, &stm) == end)
adamc@438 2373 return mktime(&stm);
adamc@438 2374 else
adamc@438 2375 uw_error(ctx, FATAL, "Can't parse time: %s", s);
adamc@438 2376 }
adamc@436 2377 }
adamc@457 2378
adamc@742 2379 uw_Basis_blob uw_Basis_stringToBlob_error(uw_context ctx, uw_Basis_string s, size_t len) {
adamc@742 2380 char *r = ctx->heap.front;
adamc@742 2381 uw_Basis_blob b = {len, r};
adamc@742 2382
adamc@742 2383 uw_check_heap(ctx, len);
adamc@742 2384
adamc@742 2385 while (*s) {
adamc@742 2386 if (s[0] == '\\') {
adamc@742 2387 if (s[1] == '\\') {
adamc@742 2388 *r++ = '\\';
adamc@742 2389 s += 2;
adamc@742 2390 } else if (isdigit(s[1]) && isdigit(s[2]) && isdigit(s[3])) {
adamc@742 2391 *r++ = (s[1] - '0') * 8 * 8 + ((s[2] - '0') * 8) + (s[3] - '0');
adamc@742 2392 s += 4;
adamc@742 2393 }
adamc@742 2394 else {
adamc@742 2395 *r++ = '\\';
adamc@742 2396 ++s;
adamc@742 2397 }
adamc@742 2398 } else {
adamc@742 2399 *r++ = s[0];
adamc@742 2400 ++s;
adamc@742 2401 }
adamc@742 2402 }
adamc@742 2403
adamc@742 2404 b.size = r - ctx->heap.front;
adamc@742 2405 ctx->heap.front = r;
adamc@742 2406 return b;
adamc@742 2407 }
adamc@742 2408
adamc@463 2409 uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) {
adamc@463 2410 int len = strlen(c);
adamc@854 2411 char *p = ctx->outHeaders.start;
adamc@474 2412
adamc@474 2413 while (p = strstr(p, "\nSet-Cookie: ")) {
adamc@474 2414 char *p2;
adamc@474 2415 p += 13;
adamc@474 2416 p2 = strchr(p, '=');
adamc@474 2417
adamc@474 2418 if (p2) {
adamc@474 2419 size_t sz = strcspn(p2+1, ";\r\n");
adamc@474 2420
adamc@474 2421 if (!strncasecmp(p, c, p2 - p)) {
adamc@474 2422 char *ret = uw_malloc(ctx, sz + 1);
adamc@474 2423 memcpy(ret, p2+1, sz);
adamc@474 2424 ret[sz] = 0;
adamc@474 2425 return ret;
adamc@474 2426 }
adamc@474 2427 }
adamc@474 2428 }
adamc@463 2429
adamc@854 2430 if (p = uw_Basis_requestHeader(ctx, "Cookie")) {
adamc@854 2431 while (1) {
adamc@854 2432 if (!strncmp(p, c, len) && p[len] == '=')
adamc@854 2433 return p + 1 + len;
adamc@854 2434 else if (p = strchr(p, ';'))
adamc@854 2435 p += 2;
adamc@463 2436 else
adamc@463 2437 return NULL;
adamc@463 2438 }
adamc@463 2439 }
adamc@792 2440
adamc@792 2441 return NULL;
adamc@457 2442 }
adamc@462 2443
adamc@466 2444 uw_unit uw_Basis_set_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c, uw_Basis_string v) {
adamc@462 2445 uw_write_header(ctx, "Set-Cookie: ");
adamc@462 2446 uw_write_header(ctx, c);
adamc@462 2447 uw_write_header(ctx, "=");
adamc@462 2448 uw_write_header(ctx, v);
adamc@466 2449 uw_write_header(ctx, "; path=");
adamc@466 2450 uw_write_header(ctx, prefix);
adamc@462 2451 uw_write_header(ctx, "\r\n");
adamc@462 2452
adamc@462 2453 return uw_unit_v;
adamc@462 2454 }
adamc@667 2455
adamc@682 2456 static delta *allocate_delta(uw_context ctx, unsigned client) {
adamc@682 2457 unsigned i;
adamc@682 2458 delta *d;
adamc@671 2459
adamc@682 2460 for (i = 0; i < ctx->used_deltas; ++i)
adamc@682 2461 if (ctx->deltas[i].client == client)
adamc@682 2462 return &ctx->deltas[i];
adamc@671 2463
adamc@682 2464 if (ctx->used_deltas >= ctx->n_deltas) {
adamc@682 2465 ctx->deltas = realloc(ctx->deltas, sizeof(delta) * ++ctx->n_deltas);
adamc@682 2466 buf_init(&ctx->deltas[ctx->n_deltas-1].msgs, 0);
adamc@682 2467 }
adamc@671 2468
adamc@682 2469 d = &ctx->deltas[ctx->used_deltas++];
adamc@682 2470 d->client = client;
adamc@682 2471 buf_reset(&d->msgs);
adamc@682 2472 return d;
adamc@671 2473 }
adamc@671 2474
adamc@668 2475 uw_Basis_channel uw_Basis_new_channel(uw_context ctx, uw_unit u) {
adamc@682 2476 if (ctx->client == NULL)
adamc@682 2477 uw_error(ctx, FATAL, "Attempt to create channel on request not associated with a persistent connection");
adamc@671 2478
adamc@682 2479 return new_channel(ctx->client);
adamc@668 2480 }
adamc@668 2481
adamc@668 2482 uw_unit uw_Basis_send(uw_context ctx, uw_Basis_channel chn, uw_Basis_string msg) {
adamc@682 2483 delta *d = allocate_delta(ctx, chn.cli);
adamc@682 2484 size_t len;
adamc@682 2485 int preLen;
adamc@682 2486 char pre[INTS_MAX + 2];
adamc@668 2487
adamc@682 2488 len = strlen(msg);
adamc@682 2489
adamc@682 2490 sprintf(pre, "%u\n%n", chn.chn, &preLen);
adamc@682 2491
adamc@682 2492 buf_append(&d->msgs, pre, preLen);
adamc@682 2493 buf_append(&d->msgs, msg, len);
adamc@682 2494 buf_append(&d->msgs, "\n", 1);
adamc@671 2495
adamc@671 2496 return uw_unit_v;
adamc@668 2497 }
adamc@671 2498
adamc@671 2499 int uw_db_commit(uw_context);
adamc@671 2500 int uw_db_rollback(uw_context);
adamc@671 2501
adamc@671 2502 void uw_commit(uw_context ctx) {
adamc@682 2503 unsigned i;
adamc@671 2504
adamc@771 2505 for (i = 0; i < ctx->used_transactionals; ++i)
adamc@771 2506 if (ctx->transactionals[i].rollback != NULL)
adamc@771 2507 ctx->transactionals[i].commit(ctx->transactionals[i].data);
adamc@771 2508
adamc@771 2509 for (i = 0; i < ctx->used_transactionals; ++i)
adamc@771 2510 if (ctx->transactionals[i].rollback == NULL)
adamc@771 2511 ctx->transactionals[i].commit(ctx->transactionals[i].data);
adamc@771 2512
adamc@771 2513 if (uw_db_commit(ctx))
adamc@671 2514 uw_error(ctx, FATAL, "Error running SQL COMMIT");
adamc@682 2515
adamc@682 2516 for (i = 0; i < ctx->used_deltas; ++i) {
adamc@682 2517 delta *d = &ctx->deltas[i];
adamc@682 2518 client *c = find_client(d->client);
adamc@682 2519
adamc@682 2520 assert (c != NULL && c->mode == USED);
adamc@682 2521
adamc@685 2522 client_send(c, &d->msgs);
adamc@682 2523 }
adamc@682 2524
adamc@682 2525 if (ctx->client)
adamc@685 2526 release_client(ctx->client);
adamc@771 2527
adamc@771 2528 for (i = 0; i < ctx->used_transactionals; ++i)
adamc@771 2529 ctx->transactionals[i].free(ctx->transactionals[i].data);
adamc@671 2530 }
adamc@671 2531
adamc@671 2532 int uw_rollback(uw_context ctx) {
adamc@771 2533 size_t i;
adamc@771 2534
adamc@682 2535 if (ctx->client)
adamc@685 2536 release_client(ctx->client);
adamc@671 2537
adamc@771 2538 for (i = 0; i < ctx->used_transactionals; ++i)
adamc@771 2539 if (ctx->transactionals[i].rollback != NULL)
adamc@771 2540 ctx->transactionals[i].rollback(ctx->transactionals[i].data);
adamc@771 2541
adamc@771 2542 for (i = 0; i < ctx->used_transactionals; ++i)
adamc@771 2543 ctx->transactionals[i].free(ctx->transactionals[i].data);
adamc@771 2544
adamc@671 2545 return uw_db_rollback(ctx);
adamc@671 2546 }
adamc@683 2547
adamc@771 2548 void uw_register_transactional(uw_context ctx, void *data, uw_callback commit, uw_callback rollback,
adamc@771 2549 uw_callback free) {
adamc@771 2550 if (commit == NULL)
adamc@771 2551 uw_error(ctx, FATAL, "uw_register_transactional: NULL commit callback");
adamc@771 2552
adamc@771 2553 if (ctx->used_transactionals >= ctx->n_transactionals) {
adamc@771 2554 ctx->transactionals = realloc(ctx->transactionals, ctx->used_transactionals+1);
adamc@771 2555 ++ctx->n_transactionals;
adamc@771 2556 }
adamc@771 2557
adamc@771 2558 ctx->transactionals[ctx->used_transactionals].data = data;
adamc@771 2559 ctx->transactionals[ctx->used_transactionals].commit = commit;
adamc@771 2560 ctx->transactionals[ctx->used_transactionals].rollback = rollback;
adamc@771 2561 ctx->transactionals[ctx->used_transactionals++].free = free;
adamc@771 2562 }
adamc@771 2563
adamc@683 2564
adamc@683 2565 // "Garbage collection"
adamc@683 2566
adamc@683 2567 void uw_expunger(uw_context ctx, uw_Basis_client cli);
adamc@683 2568
adamc@683 2569 static failure_kind uw_expunge(uw_context ctx, uw_Basis_client cli) {
adamc@683 2570 int r = setjmp(ctx->jmp_buf);
adamc@683 2571
adamc@684 2572 if (r == 0) {
adamc@684 2573 if (uw_db_begin(ctx))
adamc@684 2574 uw_error(ctx, FATAL, "Error running SQL BEGIN");
adamc@683 2575 uw_expunger(ctx, cli);
adamc@684 2576 if (uw_db_commit(ctx))
adamc@684 2577 uw_error(ctx, FATAL, "Error running SQL COMMIT");
adamc@684 2578 }
adamc@683 2579
adamc@683 2580 return r;
adamc@683 2581 }
adamc@683 2582
adamc@683 2583 void uw_prune_clients(uw_context ctx) {
adamc@683 2584 client *c, *next, *prev = NULL;
adamc@683 2585 time_t cutoff;
adamc@683 2586
adamc@683 2587 cutoff = time(NULL) - uw_timeout;
adamc@683 2588
adamc@683 2589 pthread_mutex_lock(&clients_mutex);
adamc@683 2590
adamc@683 2591 for (c = clients_used; c; c = next) {
adamc@683 2592 next = c->next;
adamc@683 2593 pthread_mutex_lock(&c->lock);
adamc@685 2594 if (c->last_contact < cutoff && c->refcount == 0) {
adamc@683 2595 failure_kind fk = UNLIMITED_RETRY;
adamc@683 2596 if (prev)
adamc@683 2597 prev->next = next;
adamc@683 2598 else
adamc@683 2599 clients_used = next;
adamc@684 2600 uw_reset(ctx);
adamc@683 2601 while (fk == UNLIMITED_RETRY) {
adamc@683 2602 fk = uw_expunge(ctx, c->id);
adamc@684 2603 if (fk == UNLIMITED_RETRY) {
adamc@684 2604 uw_db_rollback(ctx);
adamc@684 2605 printf("Unlimited retry during expunge: %s\n", uw_error_message(ctx));
adamc@683 2606 }
adamc@683 2607 }
adamc@684 2608 if (fk == SUCCESS)
adamc@684 2609 free_client(c);
adamc@684 2610 else {
adamc@684 2611 uw_db_rollback(ctx);
adamc@856 2612 fprintf(stderr, "Expunge blocked by error: %s\n", uw_error_message(ctx));
adamc@684 2613 }
adamc@683 2614 }
adamc@683 2615 else
adamc@683 2616 prev = c;
adamc@683 2617 pthread_mutex_unlock(&c->lock);
adamc@683 2618 }
adamc@683 2619
adamc@683 2620 pthread_mutex_unlock(&clients_mutex);
adamc@683 2621 }
adamc@687 2622
adamc@687 2623 void uw_initializer(uw_context ctx);
adamc@687 2624
adamc@687 2625 failure_kind uw_initialize(uw_context ctx) {
adamc@687 2626 int r = setjmp(ctx->jmp_buf);
adamc@687 2627
adamc@687 2628 if (r == 0) {
adamc@687 2629 if (uw_db_begin(ctx))
adamc@687 2630 uw_error(ctx, FATAL, "Error running SQL BEGIN");
adamc@687 2631 uw_initializer(ctx);
adamc@687 2632 if (uw_db_commit(ctx))
adamc@687 2633 uw_error(ctx, FATAL, "Error running SQL COMMIT");
adamc@687 2634 }
adamc@687 2635
adamc@687 2636 return r;
adamc@687 2637 }
adamc@717 2638
adamc@770 2639 extern int uw_check_url(const char *);
adamc@770 2640 extern int uw_check_mime(const char *);
adamc@770 2641
adamc@717 2642 uw_Basis_string uw_Basis_bless(uw_context ctx, uw_Basis_string s) {
adamc@770 2643 if (uw_check_url(s))
adamc@770 2644 return s;
adamc@770 2645 else
adamc@770 2646 uw_error(ctx, FATAL, "Disallowed URL %s", uw_Basis_htmlifyString(ctx, s));
adamc@717 2647 }
adamc@734 2648
adamc@770 2649 uw_Basis_string uw_Basis_checkUrl(uw_context ctx, uw_Basis_string s) {
adamc@770 2650 if (uw_check_url(s))
adamc@770 2651 return s;
adamc@770 2652 else
adamc@770 2653 return NULL;
adamc@770 2654 }
adamc@770 2655
adamc@770 2656 int mime_format(const char *s) {
adamc@770 2657 for (; *s; ++s)
adamc@770 2658 if (!isalnum(*s) && *s != '/' && *s != '-' && *s != '.')
adamc@770 2659 return 0;
adamc@770 2660
adamc@770 2661 return 1;
adamc@770 2662 }
adamc@770 2663
adamc@741 2664 uw_Basis_string uw_Basis_blessMime(uw_context ctx, uw_Basis_string s) {
adamc@770 2665 if (!mime_format(s))
adamc@770 2666 uw_error(ctx, FATAL, "MIME type \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s));
adamc@770 2667
adamc@770 2668 if (uw_check_mime(s))
adamc@770 2669 return s;
adamc@770 2670 else
adamc@770 2671 uw_error(ctx, FATAL, "Disallowed MIME type %s", uw_Basis_htmlifyString(ctx, s));
adamc@770 2672 }
adamc@770 2673
adamc@770 2674 uw_Basis_string uw_Basis_checkMime(uw_context ctx, uw_Basis_string s) {
adamc@770 2675 if (!mime_format(s))
adamc@770 2676 return NULL;
adamc@770 2677
adamc@770 2678 if (uw_check_mime(s))
adamc@770 2679 return s;
adamc@770 2680 else
adamc@770 2681 return NULL;
adamc@741 2682 }
adamc@741 2683
adamc@734 2684 uw_Basis_string uw_unnull(uw_Basis_string s) {
adamc@734 2685 return s ? s : "";
adamc@734 2686 }
adamc@734 2687
adamc@734 2688 extern int uw_hash_blocksize;
adamc@734 2689
adamc@734 2690 uw_Basis_string uw_Basis_makeSigString(uw_context ctx, uw_Basis_string sig) {
adamc@734 2691 uw_Basis_string r = uw_malloc(ctx, 2 * uw_hash_blocksize + 1);
adamc@734 2692 int i;
adamc@734 2693
adamc@734 2694 for (i = 0; i < uw_hash_blocksize; ++i)
adamc@734 2695 sprintf(&r[2*i], "%.02X", ((unsigned char *)sig)[i]);
adamc@734 2696
adamc@734 2697 return r;
adamc@734 2698 }
adamc@734 2699
adamc@734 2700 uw_Basis_string uw_Basis_sigString(uw_context ctx, uw_unit u) {
adamc@734 2701 return uw_cookie_sig(ctx);
adamc@734 2702 }
adamc@737 2703
adamc@737 2704 uw_Basis_string uw_Basis_fileName(uw_context ctx, uw_Basis_file f) {
adamc@737 2705 return f.name;
adamc@737 2706 }
adamc@737 2707
adamc@740 2708 uw_Basis_string uw_Basis_fileMimeType(uw_context ctx, uw_Basis_file f) {
adamc@740 2709 return f.type;
adamc@740 2710 }
adamc@740 2711
adamc@745 2712 uw_Basis_int uw_Basis_blobSize(uw_context ctx, uw_Basis_blob b) {
adamc@745 2713 return b.size;
adamc@745 2714 }
adamc@745 2715
adamc@737 2716 uw_Basis_blob uw_Basis_fileData(uw_context ctx, uw_Basis_file f) {
adamc@737 2717 return f.data;
adamc@737 2718 }
adamc@741 2719
adamc@741 2720 __attribute__((noreturn)) void uw_return_blob(uw_context ctx, uw_Basis_blob b, uw_Basis_string mimeType) {
adamc@741 2721 cleanup *cl;
adamc@741 2722 int len;
adamc@741 2723
adamc@741 2724 buf_reset(&ctx->outHeaders);
adamc@741 2725 buf_reset(&ctx->page);
adamc@741 2726
adamc@856 2727 uw_write_header(ctx, on_success);
adamc@856 2728 uw_write_header(ctx, "Content-Type: ");
adamc@741 2729 uw_write_header(ctx, mimeType);
adamc@741 2730 uw_write_header(ctx, "\r\nContent-Length: ");
adamc@741 2731 buf_check(&ctx->outHeaders, INTS_MAX);
adamc@741 2732 sprintf(ctx->outHeaders.front, "%d%n", b.size, &len);
adamc@741 2733 ctx->outHeaders.front += len;
adamc@741 2734 uw_write_header(ctx, "\r\n");
adamc@741 2735
adamc@741 2736 buf_append(&ctx->page, b.data, b.size);
adamc@741 2737
adamc@741 2738 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@741 2739 cl->func(cl->arg);
adamc@741 2740
adamc@741 2741 ctx->cleanup_front = ctx->cleanup;
adamc@741 2742
adamc@741 2743 longjmp(ctx->jmp_buf, RETURN_BLOB);
adamc@741 2744 }
adamc@874 2745
adamc@874 2746 uw_Basis_string uw_Basis_unAs(uw_context ctx, uw_Basis_string s) {
adamc@874 2747 uw_Basis_string r = uw_malloc(ctx, strlen(s) + 1);
adamc@874 2748
adamc@874 2749 for (; *s; ++s) {
adamc@874 2750 if (s[0] == '\'') {
adamc@874 2751 *r++ = '\'';
adamc@874 2752 for (++s; *s; ++s) {
adamc@874 2753 if (s[0] == '\'') {
adamc@874 2754 *r++ = '\'';
adamc@874 2755 break;
adamc@874 2756 } else if (s[0] == '\\') {
adamc@874 2757 if (s[1] == '\\') {
adamc@874 2758 *r++ = '\\';
adamc@874 2759 *r++ = '\\';
adamc@874 2760 ++s;
adamc@874 2761 } else if (s[1] == '\'') {
adamc@874 2762 *r++ = '\\';
adamc@874 2763 *r++ = '\'';
adamc@874 2764 ++s;
adamc@874 2765 } else
adamc@874 2766 *r++ = '\'';
adamc@874 2767 } else
adamc@874 2768 *r++ = s[0];
adamc@874 2769 }
adamc@874 2770 if (*s == 0) break;
adamc@874 2771 } else if (s[0] == 'T' && s[1] == '.')
adamc@874 2772 ++s;
adamc@874 2773 else
adamc@874 2774 *r++ = s[0];
adamc@874 2775 }
adamc@874 2776
adamc@874 2777 return r;
adamc@874 2778 }
adamc@874 2779