annotate src/c/urweb.c @ 1446:36f7d1debb37

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