annotate src/c/urweb.c @ 1294:b4480a56cab7

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