annotate src/c/urweb.c @ 1665:f9ffe8497742

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