annotate src/c/urweb.c @ 1459:156b8e8c25d8

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