annotate src/c/urweb.c @ 1115:150465f2895c

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