annotate src/c/urweb.c @ 2280:985c8016b592

Merge.
author Ziv Scully <ziv@mit.edu>
date Thu, 12 Nov 2015 08:46:51 -0500
parents 32a407902d3b 14c45a0b6362
children 75cb60a7f6f1
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
ziv@2234 25 #include "uthash.h"
ziv@2234 26
adam@1431 27 uw_unit uw_unit_v = 0;
adamc@102 28
adamc@667 29
adamc@667 30 // Socket extras
adamc@667 31
adamc@863 32 int uw_really_send(int sock, const void *buf, ssize_t len) {
adamc@667 33 while (len > 0) {
adamc@856 34 ssize_t n = send(sock, buf, len, 0);
adamc@856 35
adamc@856 36 if (n < 0)
adamc@856 37 return n;
adamc@856 38
adamc@856 39 buf += n;
adamc@856 40 len -= n;
adamc@856 41 }
adamc@856 42
adamc@856 43 return 0;
adamc@856 44 }
adamc@856 45
adamc@856 46 int uw_really_write(int fd, const void *buf, size_t len) {
adamc@856 47 while (len > 0) {
adamc@856 48 ssize_t n = write(fd, buf, len);
adamc@667 49
adamc@667 50 if (n < 0)
adamc@667 51 return n;
adamc@667 52
adamc@667 53 buf += n;
adamc@667 54 len -= n;
adamc@667 55 }
adamc@667 56
adamc@667 57 return 0;
adamc@667 58 }
adamc@667 59
adamc@667 60
adamc@667 61 // Buffers
adamc@667 62
adam@1369 63 void uw_buffer_init(size_t max, uw_buffer *b, size_t s) {
adamc@1113 64 b->max = max;
adamc@667 65 b->front = b->start = malloc(s);
adamc@667 66 b->back = b->front + s;
adamc@667 67 }
adamc@667 68
adam@1369 69 void uw_buffer_free(uw_buffer *b) {
adamc@667 70 free(b->start);
adamc@667 71 }
adamc@667 72
adam@1369 73 void uw_buffer_reset(uw_buffer *b) {
adamc@667 74 b->front = b->start;
ziv@2262 75 if (b->front != b->back) {
ziv@2262 76 *b->front = 0;
ziv@2262 77 }
adamc@667 78 }
adamc@667 79
adam@1369 80 int uw_buffer_check(uw_buffer *b, size_t extra) {
adamc@667 81 if (b->back - b->front < extra) {
adamc@667 82 size_t desired = b->front - b->start + extra, next;
adamc@667 83 char *new_heap;
adamc@667 84
adamc@667 85 next = b->back - b->start;
adamc@667 86 if (next == 0)
adamc@667 87 next = 1;
adamc@667 88 for (; next < desired; next *= 2);
adamc@667 89
adamc@1134 90 if (next > b->max) {
adamc@1113 91 if (desired <= b->max)
adamc@1113 92 next = desired;
adamc@1113 93 else
adamc@1113 94 return 1;
adamc@1134 95 }
adamc@1113 96
adamc@667 97 new_heap = realloc(b->start, next);
adamc@667 98 b->front = new_heap + (b->front - b->start);
adamc@667 99 b->back = new_heap + next;
adamc@667 100 b->start = new_heap;
adamc@667 101 }
adamc@1113 102
adamc@1113 103 return 0;
adamc@1113 104 }
adamc@1113 105
adamc@1113 106 __attribute__((noreturn)) void uw_error(uw_context, failure_kind, const char *, ...);
adamc@1113 107
adam@1369 108 static void ctx_uw_buffer_check(uw_context ctx, const char *kind, uw_buffer *b, size_t extra) {
adam@1369 109 if (uw_buffer_check(b, extra))
adamc@1113 110 uw_error(ctx, FATAL, "Memory limit exceeded (%s)", kind);
adamc@667 111 }
adamc@667 112
adam@1369 113 size_t uw_buffer_used(uw_buffer *b) {
adamc@667 114 return b->front - b->start;
adamc@667 115 }
adamc@667 116
adam@1369 117 size_t uw_buffer_avail(uw_buffer *b) {
adamc@667 118 return b->back - b->start;
adamc@667 119 }
adamc@667 120
adam@1369 121 int uw_buffer_append(uw_buffer *b, const char *s, size_t len) {
adam@1369 122 if (uw_buffer_check(b, len+1))
adamc@1113 123 return 1;
adamc@1113 124
adamc@1113 125 memcpy(b->front, s, len);
adamc@1113 126 b->front += len;
adamc@1113 127 *b->front = 0;
adamc@1113 128
adamc@1113 129 return 0;
adamc@1113 130 }
adamc@1113 131
adam@1369 132 static void ctx_uw_buffer_append(uw_context ctx, const char *kind, uw_buffer *b, const char *s, size_t len) {
adam@1369 133 ctx_uw_buffer_check(ctx, kind, b, len+1);
adamc@1113 134
adamc@668 135 memcpy(b->front, s, len);
adamc@668 136 b->front += len;
adamc@672 137 *b->front = 0;
adamc@668 138 }
adamc@667 139
adamc@668 140
adamc@672 141 // Persistent state types
adamc@667 142
adamc@667 143 typedef enum { UNUSED, USED } usage;
adamc@667 144
adamc@667 145 typedef struct client {
adamc@682 146 unsigned id;
adamc@667 147 usage mode;
adamc@682 148 int pass;
adamc@682 149 struct client *next;
adamc@689 150 pthread_mutex_t lock, pull_lock;
adam@1369 151 uw_buffer msgs;
adamc@682 152 int sock;
adamc@863 153 int (*send)(int sockfd, const void *buf, ssize_t len);
adamc@863 154 int (*close)(int fd);
adamc@682 155 time_t last_contact;
adamc@682 156 unsigned n_channels;
adamc@685 157 unsigned refcount;
adamc@1121 158 void *data;
adamc@667 159 } client;
adamc@667 160
adamc@672 161
adamc@672 162 // Persistent client state
adamc@672 163
adamc@682 164 static client **clients, *clients_free, *clients_used;
adamc@682 165 static unsigned n_clients;
adamc@667 166
adam@1707 167 static pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER;
adamc@1113 168 size_t uw_messages_max = SIZE_MAX;
adamc@1113 169 size_t uw_clients_max = SIZE_MAX;
adamc@1113 170
adamc@1121 171 void *uw_init_client_data();
adamc@1121 172 void uw_free_client_data(void *);
adamc@1121 173 void uw_copy_client_data(void *dst, void *src);
adamc@1121 174
adam@2104 175 static uw_Basis_int my_rand() {
adam@2105 176 int ret, r = RAND_bytes((unsigned char *)&ret, sizeof ret);
adam@2104 177 if (r)
adam@2105 178 return abs(ret);
adam@2104 179 else
adam@2104 180 return -1;
adam@2104 181 }
adam@2104 182
adamc@682 183 static client *new_client() {
adamc@667 184 client *c;
adamc@667 185
adamc@667 186 pthread_mutex_lock(&clients_mutex);
adamc@667 187
adamc@667 188 if (clients_free) {
adamc@667 189 c = clients_free;
adamc@682 190 clients_free = clients_free->next;
adamc@667 191 }
adam@1672 192 else if (n_clients >= uw_clients_max) {
adam@1672 193 pthread_mutex_unlock(&clients_mutex);
adamc@1113 194 return NULL;
adam@1672 195 } else {
adamc@667 196 ++n_clients;
adamc@667 197 clients = realloc(clients, sizeof(client) * n_clients);
adamc@667 198 c = malloc(sizeof(client));
adamc@667 199 c->id = n_clients-1;
adamc@682 200 pthread_mutex_init(&c->lock, NULL);
adamc@689 201 pthread_mutex_init(&c->pull_lock, NULL);
adam@1369 202 uw_buffer_init(uw_messages_max, &c->msgs, 0);
adamc@667 203 clients[n_clients-1] = c;
adamc@667 204 }
adamc@667 205
adamc@682 206 pthread_mutex_lock(&c->lock);
adamc@667 207 c->mode = USED;
adam@2104 208 c->pass = my_rand();
adamc@682 209 c->sock = -1;
adamc@682 210 c->last_contact = time(NULL);
adam@1369 211 uw_buffer_reset(&c->msgs);
adamc@682 212 c->n_channels = 0;
adamc@685 213 c->refcount = 0;
adamc@1121 214 c->data = uw_init_client_data();
adamc@682 215 pthread_mutex_unlock(&c->lock);
adamc@682 216
adamc@682 217 c->next = clients_used;
adamc@682 218 clients_used = c;
adamc@667 219
adamc@667 220 pthread_mutex_unlock(&clients_mutex);
adamc@667 221
adamc@667 222 return c;
adamc@667 223 }
adamc@667 224
adamc@685 225 static void use_client(client *c) {
adamc@685 226 pthread_mutex_lock(&c->lock);
adamc@685 227 ++c->refcount;
adamc@685 228 pthread_mutex_unlock(&c->lock);
adamc@689 229 pthread_mutex_lock(&c->pull_lock);
adamc@685 230 }
adamc@685 231
adamc@685 232 static void release_client(client *c) {
adamc@689 233 pthread_mutex_unlock(&c->pull_lock);
adamc@685 234 pthread_mutex_lock(&c->lock);
adamc@685 235 --c->refcount;
adamc@685 236 pthread_mutex_unlock(&c->lock);
adamc@685 237 }
adamc@685 238
adamc@856 239 static const char begin_msgs[] = "Content-type: text/plain\r\n\r\n";
adam@1707 240 static pthread_t pruning_thread;
adam@1707 241 static int pruning_thread_initialized = 0;
adamc@667 242
adamc@682 243 static client *find_client(unsigned id) {
adamc@667 244 client *c;
adam@1708 245 int i_am_pruner = pruning_thread_initialized && pthread_equal(pruning_thread, pthread_self());
adam@1708 246
adam@1708 247 if (!i_am_pruner) pthread_mutex_lock(&clients_mutex);
adamc@667 248
adamc@667 249 if (id >= n_clients) {
adam@1708 250 if (!i_am_pruner) pthread_mutex_unlock(&clients_mutex);
adamc@668 251 return NULL;
adamc@667 252 }
adamc@667 253
adamc@667 254 c = clients[id];
adamc@667 255
adam@1708 256 if (!i_am_pruner) pthread_mutex_unlock(&clients_mutex);
adamc@668 257 return c;
adamc@668 258 }
adamc@668 259
adamc@856 260 static char *on_success = "HTTP/1.1 200 OK\r\n";
adam@1409 261 static char *on_redirect = "HTTP/1.1 303 See Other\r\n";
adamc@856 262
adamc@856 263 void uw_set_on_success(char *s) {
adamc@856 264 on_success = s;
adamc@856 265 }
adamc@856 266
adam@1552 267 static void chastise(int (*send)(int sockfd, const void *buf, ssize_t len), int sock) {
adam@1552 268 send(sock, on_success, strlen(on_success));
adam@1552 269 send(sock, begin_msgs, sizeof(begin_msgs) - 1);
adam@1552 270 send(sock, "R", 1);
adam@1552 271 close(sock);
adam@1552 272 }
adam@1552 273
adamc@863 274 void uw_client_connect(unsigned id, int pass, int sock,
adamc@863 275 int (*send)(int sockfd, const void *buf, ssize_t len),
adamc@864 276 int (*close)(int fd),
adamc@864 277 void *logger_data, uw_logger log_error) {
adamc@682 278 client *c = find_client(id);
adamc@668 279
adamc@668 280 if (c == NULL) {
adam@1552 281 chastise(send, sock);
adamc@864 282 log_error(logger_data, "Out-of-bounds client request (%u)\n", id);
adamc@667 283 return;
adamc@667 284 }
adamc@667 285
adamc@682 286 pthread_mutex_lock(&c->lock);
adamc@671 287
adamc@682 288 if (c->mode != USED) {
adamc@682 289 pthread_mutex_unlock(&c->lock);
adam@1552 290 chastise(send, sock);
adamc@864 291 log_error(logger_data, "Client request for unused slot (%u)\n", id);
adamc@667 292 return;
adamc@667 293 }
adamc@667 294
adamc@682 295 if (pass != c->pass) {
adamc@682 296 pthread_mutex_unlock(&c->lock);
adam@1552 297 chastise(send, sock);
adamc@864 298 log_error(logger_data, "Wrong client password (%u, %d)\n", id, pass);
adamc@682 299 return;
adamc@667 300 }
adamc@667 301
adamc@682 302 if (c->sock != -1) {
adamc@863 303 c->close(c->sock);
adamc@682 304 c->sock = -1;
adamc@682 305 }
adamc@667 306
adamc@682 307 c->last_contact = time(NULL);
adamc@682 308
adam@1369 309 if (uw_buffer_used(&c->msgs) > 0) {
adamc@863 310 send(sock, on_success, strlen(on_success));
adamc@863 311 send(sock, begin_msgs, sizeof(begin_msgs) - 1);
adam@1369 312 send(sock, c->msgs.start, uw_buffer_used(&c->msgs));
adam@1369 313 uw_buffer_reset(&c->msgs);
adamc@667 314 close(sock);
adamc@667 315 }
adamc@863 316 else {
adamc@682 317 c->sock = sock;
adamc@863 318 c->send = send;
adamc@863 319 c->close = close;
adamc@863 320 }
adamc@667 321
adamc@682 322 pthread_mutex_unlock(&c->lock);
adamc@667 323 }
adamc@667 324
adamc@682 325 static void free_client(client *c) {
adamc@682 326 c->mode = UNUSED;
adamc@682 327 c->pass = -1;
adamc@667 328
adamc@682 329 c->next = clients_free;
adamc@682 330 clients_free = c;
adamc@667 331 }
adamc@667 332
adamc@682 333 static uw_Basis_channel new_channel(client *c) {
adamc@682 334 uw_Basis_channel ch = {c->id, c->n_channels++};
adamc@668 335 return ch;
adamc@668 336 }
adamc@668 337
adam@1447 338 static void client_send(client *c, uw_buffer *msg, const char *script, int script_len) {
adamc@685 339 pthread_mutex_lock(&c->lock);
adamc@671 340
adamc@682 341 if (c->sock != -1) {
adamc@863 342 c->send(c->sock, on_success, strlen(on_success));
adamc@863 343 c->send(c->sock, begin_msgs, sizeof(begin_msgs) - 1);
adam@1447 344 if (script_len > 0) {
adam@1447 345 c->send(c->sock, "E\n", 2);
adam@1447 346 c->send(c->sock, script, script_len);
adam@1447 347 c->send(c->sock, "\n", 1);
adam@1447 348 }
adam@1369 349 c->send(c->sock, msg->start, uw_buffer_used(msg));
adamc@863 350 c->close(c->sock);
adamc@682 351 c->sock = -1;
adam@1665 352 } else if (uw_buffer_append(&c->msgs, msg->start, uw_buffer_used(msg)))
adamc@1113 353 fprintf(stderr, "Client message buffer size exceeded");
adamc@671 354
adamc@685 355 pthread_mutex_unlock(&c->lock);
adamc@668 356 }
adamc@668 357
adamc@668 358
adamc@668 359 // Global entry points
adamc@668 360
adamc@1164 361 extern void uw_global_custom();
adamc@1104 362 extern void uw_init_crypto();
adamc@1104 363
adamc@668 364 void uw_global_init() {
adamc@668 365 clients = malloc(0);
adamc@1104 366
adamc@1164 367 uw_global_custom();
adamc@1104 368 uw_init_crypto();
adamc@1094 369 }
adamc@1094 370
adamc@1094 371 void uw_app_init(uw_app *app) {
adamc@1094 372 app->client_init();
adamc@668 373 }
adamc@668 374
adam@1332 375 int uw_time = 0, uw_time_max = 0, uw_min_heap = 0;
adamc@1114 376
adamc@668 377
adamc@667 378 // Single-request state
adamc@667 379
adamc@323 380 typedef struct regions {
adamc@323 381 struct regions *next;
adamc@323 382 } regions;
adamc@323 383
adamc@425 384 typedef struct {
adamc@425 385 void (*func)(void*);
adamc@425 386 void *arg;
adamc@425 387 } cleanup;
adamc@425 388
adamc@671 389 typedef struct {
adamc@682 390 unsigned client;
adam@1369 391 uw_buffer msgs;
adamc@682 392 } delta;
adamc@671 393
adamc@737 394 typedef enum {
adamc@759 395 UNSET, NORMAL, FIL, SUBFORM, SUBFORMS, ENTRY
adamc@737 396 } input_kind;
adamc@737 397
adamc@756 398 typedef struct input {
adamc@737 399 input_kind kind;
adamc@737 400 union {
adamc@737 401 char *normal;
adamc@739 402 uw_Basis_file file;
adamc@756 403 struct {
adamc@759 404 struct input *fields, *parent;
adamc@756 405 } subform;
adamc@759 406 struct {
adamc@759 407 struct input *entries, *parent;
adamc@759 408 } subforms;
adamc@759 409 struct {
adamc@759 410 struct input *fields, *next, *parent;
adamc@759 411 } entry;
adamc@737 412 } data;
adamc@737 413 } input;
adamc@737 414
adamc@771 415 typedef struct {
adamc@771 416 void *data;
adam@1327 417 uw_callback commit, rollback;
adam@1327 418 uw_callback_with_retry free;
adamc@771 419 } transactional;
adamc@771 420
adamc@1061 421 typedef struct {
adamc@1061 422 char *name;
adamc@1061 423 void *data;
adamc@1061 424 void (*free)(void*);
adamc@1061 425 } global;
adamc@1061 426
adamc@311 427 struct uw_context {
adamc@1094 428 uw_app *app;
adam@1446 429 int id;
adamc@1094 430
adamc@854 431 char *(*get_header)(void *, const char *);
adamc@854 432 void *get_header_data;
adamc@457 433
adam@1799 434 char *(*get_env)(void *, const char *);
adam@1799 435 void *get_env_data;
adam@1799 436
adam@1369 437 uw_buffer outHeaders, page, heap, script;
adam@1451 438 int allowed_to_return_indirectly, returning_indirectly;
adamc@759 439 input *inputs, *subinputs, *cur_container;
adamc@1094 440 size_t sz_inputs, n_subinputs, used_subinputs;
adamc@167 441
adam@1446 442 unsigned long long source_count;
adamc@562 443
adamc@272 444 void *db;
adam@1931 445 int transaction_started;
adamc@272 446
adamc@167 447 jmp_buf jmp_buf;
adamc@167 448
adamc@323 449 regions *regions;
adamc@323 450
adamc@425 451 cleanup *cleanup, *cleanup_front, *cleanup_back;
adamc@425 452
adamc@1094 453 const char *script_header;
adamc@643 454
adam@2056 455 int needs_push, needs_sig, could_write_db, at_most_one_query;
adamc@693 456
adamc@682 457 size_t n_deltas, used_deltas;
adamc@682 458 delta *deltas;
adamc@671 459
adamc@682 460 client *client;
adamc@682 461
adamc@771 462 transactional *transactionals;
adamc@771 463 size_t n_transactionals, used_transactionals;
adamc@771 464
adamc@1061 465 global *globals;
adamc@1061 466 size_t n_globals;
adamc@1061 467
adamc@1066 468 char *current_url;
adamc@1066 469
adamc@1114 470 int deadline;
adamc@1114 471
adamc@1121 472 void *client_data;
adamc@1121 473
grrwlf@1997 474 uw_loggers *loggers;
adam@1334 475
adam@1386 476 int isPost, hasPostBody;
adam@1347 477 uw_Basis_postBody postBody;
adam@1370 478 uw_Basis_string queryString;
adam@1347 479
adam@1556 480 unsigned nextId;
adam@1556 481
adam@1782 482 int amInitializing;
adam@1782 483
adamc@167 484 char error_message[ERROR_BUF_LEN];
adam@1877 485
adam@1877 486 int usedSig, needsResig;
adam@1949 487
adam@1949 488 char *output_buffer;
adam@1949 489 size_t output_buffer_size;
adam@2073 490
ziv@2204 491 // For caching.
ziv@2262 492 int numRecording;
ziv@2262 493 int recordingOffset;
ziv@2220 494
adam@2073 495 int remoteSock;
adamc@117 496 };
adamc@117 497
adamc@1113 498 size_t uw_headers_max = SIZE_MAX;
adamc@1113 499 size_t uw_page_max = SIZE_MAX;
adamc@1113 500 size_t uw_heap_max = SIZE_MAX;
adamc@1113 501 size_t uw_script_max = SIZE_MAX;
adamc@1113 502
grrwlf@1997 503 uw_context uw_init(int id, uw_loggers *lg) {
adamc@311 504 uw_context ctx = malloc(sizeof(struct uw_context));
adamc@136 505
adamc@1094 506 ctx->app = NULL;
adam@1446 507 ctx->id = id;
adamc@1094 508
adamc@854 509 ctx->get_header = NULL;
adamc@854 510 ctx->get_header_data = NULL;
adamc@457 511
adam@1799 512 ctx->get_env = NULL;
adam@1799 513 ctx->get_env_data = NULL;
adam@1799 514
adam@1413 515 uw_buffer_init(uw_headers_max, &ctx->outHeaders, 1);
adam@1413 516 ctx->outHeaders.start[0] = 0;
adam@1413 517 uw_buffer_init(uw_page_max, &ctx->page, 1);
adam@1413 518 ctx->page.start[0] = 0;
adam@1451 519 ctx->allowed_to_return_indirectly = ctx->returning_indirectly = 0;
adam@1369 520 uw_buffer_init(uw_heap_max, &ctx->heap, uw_min_heap);
adam@1369 521 uw_buffer_init(uw_script_max, &ctx->script, 1);
adamc@667 522 ctx->script.start[0] = 0;
adamc@136 523
adamc@1094 524 ctx->inputs = malloc(0);
adamc@759 525 ctx->cur_container = NULL;
adamc@756 526 ctx->subinputs = malloc(0);
adamc@1094 527 ctx->sz_inputs = ctx->n_subinputs = ctx->used_subinputs = 0;
adamc@144 528
adamc@272 529 ctx->db = NULL;
adam@1931 530 ctx->transaction_started = 0;
adamc@272 531
adamc@323 532 ctx->regions = NULL;
adamc@323 533
adamc@425 534 ctx->cleanup_front = ctx->cleanup_back = ctx->cleanup = malloc(0);
adamc@425 535
adamc@643 536 ctx->script_header = "";
adamc@693 537 ctx->needs_push = 0;
adamc@736 538 ctx->needs_sig = 0;
adam@1936 539 ctx->could_write_db = 1;
adam@2056 540 ctx->at_most_one_query = 0;
adamc@1104 541
adamc@565 542 ctx->source_count = 0;
adamc@562 543
adamc@682 544 ctx->n_deltas = ctx->used_deltas = 0;
adamc@671 545 ctx->deltas = malloc(0);
adamc@671 546
adamc@700 547 ctx->client = NULL;
adamc@700 548
adamc@700 549 ctx->error_message[0] = 0;
adamc@700 550
adamc@771 551 ctx->transactionals = malloc(0);
adamc@771 552 ctx->n_transactionals = ctx->used_transactionals = 0;
adamc@771 553
adamc@1061 554 ctx->globals = malloc(0);
adamc@1061 555 ctx->n_globals = 0;
adamc@1061 556
adamc@1066 557 ctx->current_url = "";
adamc@1066 558
adamc@1114 559 ctx->deadline = INT_MAX;
adamc@1114 560
adamc@1121 561 ctx->client_data = uw_init_client_data();
adamc@1121 562
grrwlf@1997 563 ctx->loggers = lg;
adam@1334 564
adam@1386 565 ctx->isPost = ctx->hasPostBody = 0;
adam@1347 566
adam@1370 567 ctx->queryString = NULL;
adam@1370 568
adam@1556 569 ctx->nextId = 0;
adam@1556 570
adam@1782 571 ctx->amInitializing = 0;
adam@1782 572
adam@1877 573 ctx->usedSig = 0;
adam@1877 574 ctx->needsResig = 0;
adam@1877 575
adam@1949 576 ctx->output_buffer = malloc(1);
adam@1949 577 ctx->output_buffer_size = 1;
adam@1949 578
ziv@2262 579 ctx->numRecording = 0;
ziv@2262 580 ctx->recordingOffset = 0;
ziv@2204 581
adam@2073 582 ctx->remoteSock = -1;
adam@2073 583
adamc@117 584 return ctx;
adamc@106 585 }
adamc@106 586
adamc@1113 587 size_t uw_inputs_max = SIZE_MAX;
adamc@1113 588
adamc@1121 589 uw_app *uw_get_app(uw_context ctx) {
adamc@1121 590 return ctx->app;
adamc@1121 591 }
adamc@1121 592
adamc@1113 593 int uw_set_app(uw_context ctx, uw_app *app) {
adamc@1094 594 ctx->app = app;
adamc@1094 595
adamc@1105 596 if (app && app->inputs_len > ctx->sz_inputs) {
adamc@1113 597 if (app->inputs_len > uw_inputs_max)
adamc@1113 598 return 1;
adamc@1113 599
adamc@1094 600 ctx->sz_inputs = app->inputs_len;
adamc@1094 601 ctx->inputs = realloc(ctx->inputs, ctx->sz_inputs * sizeof(input));
adamc@1094 602 memset(ctx->inputs, 0, ctx->sz_inputs * sizeof(input));
adamc@1094 603 }
adamc@1134 604
adamc@1134 605 return 0;
adamc@1094 606 }
adamc@1094 607
adamc@1121 608 void uw_set_client_data(uw_context ctx, void *data) {
adamc@1121 609 uw_copy_client_data(ctx->client_data, data);
adamc@1121 610 }
adamc@1121 611
adamc@311 612 void uw_set_db(uw_context ctx, void *db) {
adamc@272 613 ctx->db = db;
adamc@272 614 }
adamc@272 615
adamc@311 616 void *uw_get_db(uw_context ctx) {
adamc@272 617 return ctx->db;
adamc@272 618 }
adamc@272 619
grrwlf@1997 620
grrwlf@1997 621 uw_loggers* uw_get_loggers(struct uw_context *ctx) {
grrwlf@1997 622 return ctx->loggers;
grrwlf@1997 623 }
grrwlf@1997 624
adamc@311 625 void uw_free(uw_context ctx) {
adamc@671 626 size_t i;
adamc@671 627
adam@1369 628 uw_buffer_free(&ctx->outHeaders);
adam@1369 629 uw_buffer_free(&ctx->script);
adam@1369 630 uw_buffer_free(&ctx->page);
adam@1369 631 uw_buffer_free(&ctx->heap);
adamc@144 632 free(ctx->inputs);
adamc@756 633 free(ctx->subinputs);
adamc@425 634 free(ctx->cleanup);
adamc@771 635 free(ctx->transactionals);
adamc@1121 636 uw_free_client_data(ctx->client_data);
adamc@671 637
adamc@682 638 for (i = 0; i < ctx->n_deltas; ++i)
adam@1369 639 uw_buffer_free(&ctx->deltas[i].msgs);
marco-oweber@1330 640 free(ctx->deltas);
adamc@671 641
adamc@1061 642 for (i = 0; i < ctx->n_globals; ++i)
adamc@1089 643 if (ctx->globals[i].free)
adamc@1089 644 ctx->globals[i].free(ctx->globals[i].data);
marco-oweber@1330 645 free(ctx->globals);
adamc@1061 646
adam@1949 647 free(ctx->output_buffer);
adam@1949 648
adamc@136 649 free(ctx);
adamc@136 650 }
adamc@136 651
adamc@562 652 void uw_reset_keep_error_message(uw_context ctx) {
adam@1369 653 uw_buffer_reset(&ctx->outHeaders);
adam@1369 654 uw_buffer_reset(&ctx->script);
adamc@667 655 ctx->script.start[0] = 0;
adam@1369 656 uw_buffer_reset(&ctx->page);
adam@1451 657 ctx->allowed_to_return_indirectly = ctx->returning_indirectly = 0;
adam@1369 658 uw_buffer_reset(&ctx->heap);
adamc@323 659 ctx->regions = NULL;
adamc@425 660 ctx->cleanup_front = ctx->cleanup;
adamc@682 661 ctx->used_deltas = 0;
adamc@682 662 ctx->client = NULL;
adamc@759 663 ctx->cur_container = NULL;
adamc@771 664 ctx->used_transactionals = 0;
adamc@1117 665 ctx->script_header = "";
adam@1370 666 ctx->queryString = NULL;
adam@1556 667 ctx->nextId = 0;
adam@1782 668 ctx->amInitializing = 0;
adam@1877 669 ctx->usedSig = 0;
adam@1877 670 ctx->needsResig = 0;
adam@2073 671 ctx->remoteSock = -1;
adamc@167 672 }
adamc@167 673
adamc@562 674 void uw_reset_keep_request(uw_context ctx) {
adamc@562 675 uw_reset_keep_error_message(ctx);
adamc@562 676 ctx->error_message[0] = 0;
adamc@167 677 }
adamc@167 678
adamc@311 679 void uw_reset(uw_context ctx) {
adamc@311 680 uw_reset_keep_request(ctx);
adamc@1105 681 if (ctx->app)
adamc@1105 682 memset(ctx->inputs, 0, ctx->app->inputs_len * sizeof(input));
adamc@756 683 memset(ctx->subinputs, 0, ctx->n_subinputs * sizeof(input));
adam@1461 684 ctx->used_subinputs = ctx->hasPostBody = ctx->isPost = 0;
adam@1943 685 ctx->transaction_started = 0;
adamc@144 686 }
adamc@144 687
adamc@311 688 failure_kind uw_begin_init(uw_context ctx) {
adamc@272 689 int r = setjmp(ctx->jmp_buf);
adamc@272 690
adamc@1105 691 if (r == 0 && ctx->app)
adamc@1094 692 ctx->app->db_init(ctx);
adamc@272 693
adamc@272 694 return r;
adamc@272 695 }
adamc@272 696
adamc@1118 697 void uw_close(uw_context ctx) {
adamc@1118 698 ctx->app->db_close(ctx);
adamc@1118 699 }
adamc@1118 700
adam@1446 701 uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) {
adam@1446 702 return ctx->get_header(ctx->get_header_data, h);
adam@1446 703 }
adam@1446 704
adamc@854 705 void uw_set_headers(uw_context ctx, char *(*get_header)(void *, const char *), void *get_header_data) {
adamc@854 706 ctx->get_header = get_header;
adamc@854 707 ctx->get_header_data = get_header_data;
adamc@742 708 }
adamc@742 709
adam@1799 710 void uw_set_env(uw_context ctx, char *(*get_env)(void *, const char *), void *get_env_data) {
adam@1799 711 ctx->get_env = get_env;
adam@1799 712 ctx->get_env_data = get_env_data;
adam@1799 713 }
adam@1799 714
adamc@801 715 static void uw_set_error(uw_context ctx, const char *fmt, ...) {
adamc@801 716 va_list ap;
adamc@801 717 va_start(ap, fmt);
adamc@801 718
adamc@801 719 vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap);
adamc@801 720 }
adamc@801 721
adamc@1131 722 int uw_has_error(uw_context ctx) {
adamc@1131 723 return ctx->error_message[0] != 0;
adamc@1131 724 }
adamc@1131 725
adamc@311 726 __attribute__((noreturn)) void uw_error(uw_context ctx, failure_kind fk, const char *fmt, ...) {
adamc@425 727 cleanup *cl;
adamc@425 728
adamc@167 729 va_list ap;
adamc@167 730 va_start(ap, fmt);
adamc@167 731
adamc@167 732 vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap);
adamc@167 733
adamc@425 734 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@425 735 cl->func(cl->arg);
adamc@425 736
adamc@425 737 ctx->cleanup_front = ctx->cleanup;
adamc@425 738
adamc@190 739 longjmp(ctx->jmp_buf, fk);
adamc@167 740 }
adamc@167 741
adamc@1113 742 size_t uw_cleanup_max = SIZE_MAX;
adamc@1113 743
adamc@425 744 void uw_push_cleanup(uw_context ctx, void (*func)(void *), void *arg) {
adamc@425 745 if (ctx->cleanup_front >= ctx->cleanup_back) {
adamc@425 746 int len = ctx->cleanup_back - ctx->cleanup, newLen;
adamc@425 747 if (len == 0)
adamc@425 748 newLen = 1;
adamc@425 749 else
adamc@428 750 newLen = len * 2;
adamc@1113 751
adamc@1134 752 if (newLen > uw_cleanup_max) {
adamc@1113 753 if (len+1 <= uw_cleanup_max)
adamc@1113 754 newLen = uw_cleanup_max;
adamc@1113 755 else
adamc@1113 756 uw_error(ctx, FATAL, "Exceeded limit on number of cleanup handlers");
adamc@1134 757 }
adamc@1113 758
adamc@470 759 ctx->cleanup = realloc(ctx->cleanup, newLen * sizeof(cleanup));
adamc@425 760 ctx->cleanup_front = ctx->cleanup + len;
adamc@425 761 ctx->cleanup_back = ctx->cleanup + newLen;
adamc@425 762 }
adamc@425 763
adamc@425 764 ctx->cleanup_front->func = func;
adamc@425 765 ctx->cleanup_front->arg = arg;
adamc@425 766 ++ctx->cleanup_front;
adamc@425 767 }
adamc@425 768
adam@1444 769 char *uw_Basis_htmlifyString(uw_context, const char *);
adam@1444 770
adamc@682 771 void uw_login(uw_context ctx) {
adam@1942 772 char *id_s, *pass_s;
adam@1942 773
adam@1942 774 if ((id_s = uw_Basis_requestHeader(ctx, "UrWeb-Client"))
adam@1942 775 && (pass_s = uw_Basis_requestHeader(ctx, "UrWeb-Pass"))) {
adam@1942 776 unsigned id = atoi(id_s);
adam@1942 777 int pass = atoi(pass_s);
adam@1942 778 client *c = find_client(id);
adam@1942 779
adam@1942 780 if (c == NULL)
adam@1942 781 uw_error(ctx, FATAL, "Unknown client ID in HTTP headers (%s, %s)", uw_Basis_htmlifyString(ctx, id_s), uw_Basis_htmlifyString(ctx, pass_s));
adam@1942 782 else {
adamc@685 783 use_client(c);
adamc@682 784 ctx->client = c;
adam@1942 785
adam@1942 786 if (c->mode != USED)
adam@1942 787 uw_error(ctx, FATAL, "Stale client ID (%u) in subscription request", id);
adam@1942 788 if (c->pass != pass)
adam@1942 789 uw_error(ctx, FATAL, "Wrong client password (%u, %d) in subscription request", id, pass);
adamc@864 790 }
adam@1942 791 } else if (ctx->needs_push) {
adam@1942 792 client *c = new_client();
adam@1942 793
adam@1942 794 if (c == NULL)
adam@1942 795 uw_error(ctx, FATAL, "Limit exceeded on number of message-passing clients");
adam@1942 796
adam@1942 797 use_client(c);
adam@1942 798 uw_copy_client_data(c->data, ctx->client_data);
adam@1942 799 ctx->client = c;
adamc@682 800 }
adamc@682 801 }
adamc@682 802
adamc@671 803 failure_kind uw_begin(uw_context ctx, char *path) {
adamc@671 804 int r = setjmp(ctx->jmp_buf);
adamc@671 805
adam@1931 806 if (r == 0)
adam@1931 807 ctx->app->handle(ctx, path);
adam@1931 808
adam@1931 809 return r;
adam@1931 810 }
adam@1931 811
adam@2176 812 static void uw_try_reconnecting(uw_context ctx) {
adam@2176 813 // Hm, error starting transaction.
adam@2176 814 // Maybe the database server died but has since come back up.
adam@2176 815 // Let's try starting from scratch.
adam@2176 816 if (ctx->db) {
adam@2176 817 ctx->app->db_close(ctx);
adam@2176 818 ctx->db = NULL;
adam@2176 819 }
adam@2176 820 ctx->app->db_init(ctx);
adam@2176 821 }
adam@2176 822
adam@2177 823 void uw_try_reconnecting_and_restarting(uw_context ctx) {
adam@2177 824 uw_try_reconnecting(ctx);
adam@2178 825 uw_error(ctx, BOUNDED_RETRY, "Restarting transaction after fixing database connection");
adam@2176 826 }
adam@2176 827
adam@1931 828 void uw_ensure_transaction(uw_context ctx) {
adam@2056 829 if (!ctx->transaction_started && !ctx->at_most_one_query) {
adam@2176 830 if (!ctx->db || ctx->app->db_begin(ctx, ctx->could_write_db)) {
adam@2176 831 uw_try_reconnecting(ctx);
adam@2176 832
adam@2176 833 if (ctx->app->db_begin(ctx, ctx->could_write_db))
adam@2176 834 uw_error(ctx, FATAL, "Error running SQL BEGIN");
adam@2176 835 }
adam@2176 836
adam@1931 837 ctx->transaction_started = 1;
adam@2178 838 } else if (ctx->at_most_one_query && !ctx->db)
adam@2178 839 uw_try_reconnecting(ctx);
adamc@671 840 }
adamc@671 841
adamc@772 842 uw_Basis_client uw_Basis_self(uw_context ctx) {
adamc@682 843 if (ctx->client == NULL)
adamc@682 844 uw_error(ctx, FATAL, "Call to Basis.self() from page that has only server-side code");
adamc@682 845
adamc@682 846 return ctx->client->id;
adamc@682 847 }
adamc@682 848
adamc@425 849 void uw_pop_cleanup(uw_context ctx) {
adamc@425 850 if (ctx->cleanup_front == ctx->cleanup)
adamc@425 851 uw_error(ctx, FATAL, "Attempt to pop from empty cleanup action stack");
adamc@425 852
adamc@425 853 --ctx->cleanup_front;
adamc@425 854 ctx->cleanup_front->func(ctx->cleanup_front->arg);
adamc@425 855 }
adamc@425 856
adamc@311 857 char *uw_error_message(uw_context ctx) {
adamc@167 858 return ctx->error_message;
adamc@167 859 }
adamc@167 860
adamc@1115 861 void uw_set_error_message(uw_context ctx, const char *msg) {
adamc@1115 862 strncpy(ctx->error_message, msg, sizeof(ctx->error_message));
adamc@1115 863 ctx->error_message[sizeof(ctx->error_message)-1] = 0;
adamc@1115 864 }
adamc@1115 865
adamc@759 866 static input *INP(uw_context ctx) {
adamc@759 867 if (ctx->cur_container == NULL)
adamc@759 868 return ctx->inputs;
adamc@759 869 else if (ctx->cur_container->kind == SUBFORM)
adamc@759 870 return ctx->cur_container->data.subform.fields;
adamc@759 871 else if (ctx->cur_container->kind == ENTRY)
adamc@759 872 return ctx->cur_container->data.entry.fields;
adamc@759 873 else
adamc@760 874 uw_error(ctx, FATAL, "INP: Wrong kind (%d, %p)", ctx->cur_container->kind, ctx->cur_container);
adamc@759 875 }
adamc@759 876
adamc@760 877 static void adjust_pointer(input **ptr, input *old_start, input *new_start, size_t len) {
adamc@760 878 if (*ptr != NULL && *ptr >= old_start && *ptr < old_start + len)
adamc@760 879 *ptr += new_start - old_start;
adamc@760 880 }
adamc@760 881
adamc@760 882 static void adjust_input(input *x, input *old_start, input *new_start, size_t len) {
adamc@759 883 switch (x->kind) {
adamc@759 884 case SUBFORM:
adamc@760 885 adjust_pointer(&x->data.subform.fields, old_start, new_start, len);
adamc@760 886 adjust_pointer(&x->data.subform.parent, old_start, new_start, len);
adamc@759 887 break;
adamc@759 888 case SUBFORMS:
adamc@760 889 adjust_pointer(&x->data.subforms.entries, old_start, new_start, len);
adamc@760 890 adjust_pointer(&x->data.subforms.parent, old_start, new_start, len);
adamc@759 891 break;
adamc@759 892 case ENTRY:
adamc@760 893 adjust_pointer(&x->data.entry.fields, old_start, new_start, len);
adamc@760 894 adjust_pointer(&x->data.entry.next, old_start, new_start, len);
adamc@760 895 adjust_pointer(&x->data.entry.parent, old_start, new_start, len);
adamc@1134 896 break;
adamc@1134 897 default:
adamc@1134 898 break;
phurst@1981 899 }
adamc@759 900 }
adamc@759 901
adamc@1113 902 size_t uw_subinputs_max = SIZE_MAX;
adamc@1113 903
adamc@759 904 static input *check_input_space(uw_context ctx, size_t len) {
adamc@759 905 size_t i;
adamc@759 906 input *r;
adamc@759 907
adamc@759 908 if (ctx->used_subinputs + len >= ctx->n_subinputs) {
adamc@1113 909 if (ctx->used_subinputs + len > uw_subinputs_max)
adamc@1113 910 uw_error(ctx, FATAL, "Exceeded limit on number of subinputs");
adamc@1113 911
adamc@759 912 input *new_subinputs = realloc(ctx->subinputs, sizeof(input) * (ctx->used_subinputs + len));
adamc@759 913
adamc@760 914 if (ctx->subinputs != new_subinputs) {
adamc@760 915 for (i = 0; i < ctx->used_subinputs; ++i)
adamc@760 916 adjust_input(&new_subinputs[i], ctx->subinputs, new_subinputs, ctx->used_subinputs);
adamc@1094 917 for (i = 0; i < ctx->app->inputs_len; ++i)
adamc@760 918 adjust_input(&ctx->inputs[i], ctx->subinputs, new_subinputs, ctx->used_subinputs);
adamc@760 919
adamc@760 920 adjust_pointer(&ctx->cur_container, ctx->subinputs, new_subinputs, ctx->used_subinputs);
adamc@760 921
adamc@760 922 ctx->n_subinputs = ctx->used_subinputs + len;
adamc@760 923 ctx->subinputs = new_subinputs;
adamc@760 924 }
adamc@759 925 }
adamc@759 926
adamc@759 927 r = &ctx->subinputs[ctx->used_subinputs];
adamc@759 928
adamc@759 929 for (i = 0; i < len; ++i)
adam@1474 930 ctx->subinputs[ctx->used_subinputs++].kind = UNSET;
adamc@759 931
adamc@759 932 return r;
adamc@759 933 }
adamc@756 934
adamc@801 935 int uw_set_input(uw_context ctx, const char *name, char *value) {
adamc@1050 936 //printf("Input name %s\n", name);
adamc@1038 937
adamc@756 938 if (!strcasecmp(name, ".b")) {
adamc@1094 939 int n = ctx->app->input_num(value);
adamc@756 940 input *inps;
adamc@756 941
adamc@801 942 if (n < 0) {
adam@1444 943 uw_set_error(ctx, "Bad subform name %s", uw_Basis_htmlifyString(ctx, value));
adamc@801 944 return -1;
adamc@801 945 }
adamc@801 946
adamc@1094 947 if (n >= ctx->app->inputs_len) {
adam@1444 948 uw_set_error(ctx, "For subform name %s, index %d is out of range", uw_Basis_htmlifyString(ctx, value), n);
adamc@801 949 return -1;
adamc@801 950 }
adamc@756 951
adamc@1094 952 inps = check_input_space(ctx, ctx->app->inputs_len);
adamc@759 953
adamc@759 954 INP(ctx)[n].kind = SUBFORM;
adamc@759 955 INP(ctx)[n].data.subform.parent = ctx->cur_container;
adamc@759 956 INP(ctx)[n].data.subform.fields = inps;
adamc@759 957 ctx->cur_container = &INP(ctx)[n];
adamc@756 958 } else if (!strcasecmp(name, ".e")) {
adamc@756 959 input *tmp;
adamc@756 960
adamc@801 961 if (ctx->cur_container == NULL) {
adamc@801 962 uw_set_error(ctx, "Unmatched subform closer");
adamc@801 963 return -1;
adamc@801 964 }
adamc@756 965
adamc@759 966 tmp = ctx->cur_container;
adamc@759 967 switch (tmp->kind) {
adamc@759 968 case SUBFORM:
adamc@759 969 ctx->cur_container = tmp->data.subform.parent;
adamc@759 970 tmp->data.subform.parent = NULL;
adamc@759 971 break;
adamc@759 972 case SUBFORMS:
adamc@759 973 ctx->cur_container = tmp->data.subforms.parent;
adamc@759 974 tmp->data.subforms.parent = NULL;
adamc@759 975 break;
adamc@759 976 case ENTRY:
adamc@759 977 ctx->cur_container = tmp->data.entry.parent;
adamc@759 978 break;
adamc@759 979 default:
adamc@801 980 uw_set_error(ctx, "uw_set_input: Wrong kind");
adamc@801 981 return -1;
adamc@759 982 }
adamc@759 983 } else if (!strcasecmp(name, ".s")) {
adamc@1094 984 int n = ctx->app->input_num(value);
adamc@759 985
adamc@801 986 if (n < 0) {
adam@1444 987 uw_set_error(ctx, "Bad subforms name %s", uw_Basis_htmlifyString(ctx, value));
adamc@801 988 return -1;
adamc@801 989 }
adamc@801 990
adamc@1094 991 if (n >= ctx->app->inputs_len) {
adam@1444 992 uw_set_error(ctx, "For subforms name %s, index %d is out of range", uw_Basis_htmlifyString(ctx, value), n);
adamc@801 993 return -1;
adamc@801 994 }
adamc@759 995
adamc@759 996 INP(ctx)[n].kind = SUBFORMS;
adamc@759 997 INP(ctx)[n].data.subforms.parent = ctx->cur_container;
adamc@759 998 INP(ctx)[n].data.subforms.entries = NULL;
adamc@759 999 ctx->cur_container = &INP(ctx)[n];
adamc@759 1000 } else if (!strcasecmp(name, ".i")) {
adamc@759 1001 input *inps;
adamc@759 1002
adamc@801 1003 if (!ctx->cur_container) {
adamc@801 1004 uw_set_error(ctx, "New entry without container");
adamc@801 1005 return -1;
adamc@801 1006 }
adamc@801 1007
adamc@801 1008 if (ctx->cur_container->kind != SUBFORMS) {
adamc@801 1009 uw_set_error(ctx, "Bad kind for entry parent");
adamc@801 1010 return -1;
adamc@801 1011 }
adamc@759 1012
adamc@1094 1013 inps = check_input_space(ctx, ctx->app->inputs_len + 1);
adamc@759 1014
adamc@759 1015 inps->kind = ENTRY;
adamc@759 1016 inps->data.entry.parent = ctx->cur_container;
adamc@759 1017 inps->data.entry.next = ctx->cur_container->data.subforms.entries;
adamc@759 1018 ctx->cur_container->data.subforms.entries = inps;
adamc@759 1019
adamc@759 1020 inps->data.entry.fields = inps+1;
adamc@759 1021 ctx->cur_container = inps;
adamc@756 1022 } else {
adamc@1094 1023 int n = ctx->app->input_num(name);
adamc@756 1024
adam@1384 1025 if (n < 0)
adam@1384 1026 return 0;
adamc@801 1027
adamc@1094 1028 if (n >= ctx->app->inputs_len) {
adam@1444 1029 uw_set_error(ctx, "For input name %s, index %d is out of range", uw_Basis_htmlifyString(ctx, name), n);
adamc@801 1030 return -1;
adamc@801 1031 }
adamc@756 1032
adamc@756 1033 INP(ctx)[n].kind = NORMAL;
adamc@756 1034 INP(ctx)[n].data.normal = value;
adamc@756 1035 }
adamc@801 1036
adamc@801 1037 return 0;
adamc@144 1038 }
adamc@144 1039
adamc@311 1040 char *uw_get_input(uw_context ctx, int n) {
adamc@169 1041 if (n < 0)
adamc@311 1042 uw_error(ctx, FATAL, "Negative input index %d", n);
adamc@1094 1043 if (n >= ctx->app->inputs_len)
adamc@311 1044 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
adamc@737 1045
adamc@756 1046 switch (INP(ctx)[n].kind) {
adamc@737 1047 case UNSET:
adamc@737 1048 return NULL;
adamc@739 1049 case FIL:
adamc@739 1050 uw_error(ctx, FATAL, "Tried to read a file form input as normal");
adamc@756 1051 case SUBFORM:
adamc@756 1052 uw_error(ctx, FATAL, "Tried to read a subform form input as normal");
adamc@759 1053 case SUBFORMS:
adamc@759 1054 uw_error(ctx, FATAL, "Tried to read a subforms form input as normal");
adamc@759 1055 case ENTRY:
adamc@759 1056 uw_error(ctx, FATAL, "Tried to read an entry form input as normal");
adamc@737 1057 case NORMAL:
adamc@756 1058 return INP(ctx)[n].data.normal;
adamc@737 1059 default:
adamc@737 1060 uw_error(ctx, FATAL, "Impossible input kind");
adamc@737 1061 }
adamc@138 1062 }
adamc@138 1063
adamc@311 1064 char *uw_get_optional_input(uw_context ctx, int n) {
adamc@190 1065 if (n < 0)
adamc@311 1066 uw_error(ctx, FATAL, "Negative input index %d", n);
adamc@1094 1067 if (n >= ctx->app->inputs_len)
adamc@311 1068 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
adamc@737 1069
adamc@756 1070 switch (INP(ctx)[n].kind) {
adamc@737 1071 case UNSET:
adamc@737 1072 return "";
adamc@739 1073 case FIL:
adamc@739 1074 uw_error(ctx, FATAL, "Tried to read a file form input as normal");
adamc@756 1075 case SUBFORM:
adamc@756 1076 uw_error(ctx, FATAL, "Tried to read a subform form input as normal");
adamc@759 1077 case SUBFORMS:
adamc@759 1078 uw_error(ctx, FATAL, "Tried to read a subforms form input as normal");
adamc@759 1079 case ENTRY:
adamc@759 1080 uw_error(ctx, FATAL, "Tried to read an entry form input as normal");
adamc@737 1081 case NORMAL:
adamc@756 1082 return INP(ctx)[n].data.normal;
adamc@737 1083 default:
adamc@737 1084 uw_error(ctx, FATAL, "Impossible input kind");
adamc@737 1085 }
adamc@737 1086 }
adamc@737 1087
adamc@801 1088 int uw_set_file_input(uw_context ctx, const char *name, uw_Basis_file f) {
adamc@1094 1089 int n = ctx->app->input_num(name);
adamc@737 1090
adamc@801 1091 if (n < 0) {
adam@2054 1092 uw_set_error(ctx, "Bad file input name");
adamc@801 1093 return -1;
adamc@801 1094 }
adamc@801 1095
adamc@1094 1096 if (n >= ctx->app->inputs_len) {
adam@2054 1097 uw_set_error(ctx, "For file input name, index %d is out of range", n);
adamc@801 1098 return -1;
adamc@801 1099 }
adamc@737 1100
adamc@739 1101 ctx->inputs[n].kind = FIL;
adamc@739 1102 ctx->inputs[n].data.file = f;
adamc@801 1103
adamc@801 1104 return 0;
adamc@737 1105 }
adamc@737 1106
adamc@739 1107 void *uw_malloc(uw_context ctx, size_t len);
adamc@739 1108
adamc@739 1109 uw_Basis_file uw_get_file_input(uw_context ctx, int n) {
adamc@737 1110 if (n < 0)
adamc@737 1111 uw_error(ctx, FATAL, "Negative file input index %d", n);
adamc@1094 1112 if (n >= ctx->app->inputs_len)
adamc@737 1113 uw_error(ctx, FATAL, "Out-of-bounds file input index %d", n);
adamc@737 1114
adamc@756 1115 switch (INP(ctx)[n].kind) {
adamc@737 1116 case UNSET:
adamc@737 1117 {
adamc@739 1118 char *data = uw_malloc(ctx, 0);
adamc@740 1119 uw_Basis_file f = {NULL, "", {0, data}};
adamc@739 1120 return f;
adamc@737 1121 }
adamc@739 1122 case FIL:
adamc@756 1123 return INP(ctx)[n].data.file;
adamc@737 1124 case NORMAL:
adamc@737 1125 uw_error(ctx, FATAL, "Tried to read a normal form input as files");
adamc@756 1126 case SUBFORM:
adamc@756 1127 uw_error(ctx, FATAL, "Tried to read a subform form input as files");
adamc@759 1128 case SUBFORMS:
adamc@759 1129 uw_error(ctx, FATAL, "Tried to read a subforms form input as files");
adamc@759 1130 case ENTRY:
adamc@759 1131 uw_error(ctx, FATAL, "Tried to read an entry form input as files");
adamc@737 1132 default:
adamc@737 1133 uw_error(ctx, FATAL, "Impossible input kind");
adamc@737 1134 }
adamc@190 1135 }
adamc@190 1136
adamc@756 1137 void uw_enter_subform(uw_context ctx, int n) {
adamc@756 1138 if (n < 0)
adamc@756 1139 uw_error(ctx, FATAL, "Negative subform index %d", n);
adamc@1094 1140 if (n >= ctx->app->inputs_len)
adamc@756 1141 uw_error(ctx, FATAL, "Out-of-bounds subform index %d", n);
adamc@756 1142
adamc@756 1143 switch (INP(ctx)[n].kind) {
adamc@756 1144 case UNSET:
adamc@756 1145 uw_error(ctx, FATAL, "Missing subform");
adamc@756 1146 case FIL:
adamc@756 1147 uw_error(ctx, FATAL, "Tried to read a file form input as subform");
adamc@756 1148 case NORMAL:
adamc@756 1149 uw_error(ctx, FATAL, "Tried to read a normal form input as subform");
adamc@759 1150 case SUBFORMS:
adamc@759 1151 uw_error(ctx, FATAL, "Tried to read a subforms form input as subform");
adamc@759 1152 case ENTRY:
adamc@759 1153 uw_error(ctx, FATAL, "Tried to read an entry form input as subform");
adamc@756 1154 case SUBFORM:
adamc@759 1155 INP(ctx)[n].data.subform.parent = ctx->cur_container;
adamc@760 1156 ctx->cur_container = &INP(ctx)[n];
adamc@756 1157 return;
adamc@756 1158 default:
adamc@756 1159 uw_error(ctx, FATAL, "Impossible input kind");
adamc@756 1160 }
adamc@756 1161 }
adamc@756 1162
adamc@756 1163 void uw_leave_subform(uw_context ctx) {
adamc@756 1164 input *tmp;
adamc@756 1165
adamc@759 1166 if (ctx->cur_container == NULL)
adamc@756 1167 uw_error(ctx, FATAL, "Unmatched uw_leave_subform");
adamc@756 1168
adamc@759 1169 tmp = ctx->cur_container;
adamc@759 1170 ctx->cur_container = tmp->data.subform.parent;
adamc@759 1171 tmp->data.subform.parent = NULL;
adamc@759 1172 }
adamc@759 1173
adamc@759 1174 int uw_enter_subforms(uw_context ctx, int n) {
adamc@759 1175 input *inps;
adamc@759 1176
adamc@759 1177 if (n < 0)
adamc@759 1178 uw_error(ctx, FATAL, "Negative subforms index %d", n);
adamc@1094 1179 if (n >= ctx->app->inputs_len)
adamc@759 1180 uw_error(ctx, FATAL, "Out-of-bounds subforms index %d", n);
adamc@759 1181
adamc@759 1182 switch (INP(ctx)[n].kind) {
adamc@759 1183 case UNSET:
adamc@759 1184 uw_error(ctx, FATAL, "Missing subforms");
adamc@759 1185 case FIL:
adamc@759 1186 uw_error(ctx, FATAL, "Tried to read a file form input as subforms");
adamc@759 1187 case NORMAL:
adamc@759 1188 uw_error(ctx, FATAL, "Tried to read a normal form input %p as subforms", &INP(ctx)[n]);
adamc@759 1189 case SUBFORM:
adamc@759 1190 uw_error(ctx, FATAL, "Tried to read a subform form input as subforms");
adamc@759 1191 case ENTRY:
adamc@759 1192 uw_error(ctx, FATAL, "Tried to read an entry form input as subforms");
adamc@759 1193 case SUBFORMS:
adamc@759 1194 inps = INP(ctx)[n].data.subforms.entries;
adamc@759 1195 if (inps) {
adamc@759 1196 INP(ctx)[n].data.subforms.parent = ctx->cur_container;
adamc@759 1197 ctx->cur_container = INP(ctx)[n].data.subforms.entries;
adamc@759 1198 return 1;
adamc@759 1199 } else
adamc@759 1200 return 0;
adamc@759 1201 default:
adamc@759 1202 uw_error(ctx, FATAL, "Impossible input kind");
adamc@759 1203 }
adamc@759 1204 }
adamc@759 1205
adamc@759 1206 int uw_next_entry(uw_context ctx) {
adamc@759 1207 if (ctx->cur_container == NULL)
adamc@759 1208 uw_error(ctx, FATAL, "uw_next_entry(NULL)");
adamc@759 1209
adamc@759 1210 switch (ctx->cur_container->kind) {
adamc@759 1211 case UNSET:
adamc@759 1212 uw_error(ctx, FATAL, "Missing entry");
adamc@759 1213 case FIL:
adamc@759 1214 uw_error(ctx, FATAL, "Tried to read a file form input as entry");
adamc@759 1215 case NORMAL:
adamc@759 1216 uw_error(ctx, FATAL, "Tried to read a normal form input as entry");
adamc@759 1217 case SUBFORM:
adamc@759 1218 uw_error(ctx, FATAL, "Tried to read a subform form input as entry");
adamc@759 1219 case SUBFORMS:
adamc@759 1220 uw_error(ctx, FATAL, "Tried to read a subforms form input as entry");
adamc@759 1221 case ENTRY:
adamc@759 1222 if (ctx->cur_container->data.entry.next) {
adamc@759 1223 ctx->cur_container = ctx->cur_container->data.entry.next;
adamc@759 1224 return 1;
adamc@759 1225 } else {
adamc@759 1226 ctx->cur_container = ctx->cur_container->data.entry.parent->data.subforms.parent;
adamc@759 1227 return 0;
adamc@759 1228 }
adamc@759 1229 default:
adamc@759 1230 uw_error(ctx, FATAL, "Impossible input kind");
adamc@759 1231 }
adamc@756 1232 }
adamc@756 1233
adamc@643 1234 void uw_set_script_header(uw_context ctx, const char *s) {
adamc@643 1235 ctx->script_header = s;
adamc@643 1236 }
adamc@643 1237
adamc@1094 1238 const char *uw_get_url_prefix(uw_context ctx) {
adamc@1094 1239 return ctx->app->url_prefix;
adamc@667 1240 }
adamc@667 1241
adamc@693 1242 void uw_set_needs_push(uw_context ctx, int n) {
adamc@693 1243 ctx->needs_push = n;
adamc@693 1244 }
adamc@693 1245
adamc@736 1246 void uw_set_needs_sig(uw_context ctx, int n) {
adamc@736 1247 ctx->needs_sig = n;
adamc@736 1248 }
adamc@736 1249
adam@1936 1250 void uw_set_could_write_db(uw_context ctx, int n) {
adam@1936 1251 ctx->could_write_db = n;
adam@1936 1252 }
adam@1936 1253
adam@2056 1254 void uw_set_at_most_one_query(uw_context ctx, int n) {
adam@2056 1255 ctx->at_most_one_query = n;
adam@2056 1256 }
adam@2056 1257
adamc@667 1258
adam@1369 1259 static void uw_buffer_check_ctx(uw_context ctx, const char *kind, uw_buffer *b, size_t extra, const char *desc) {
adamc@666 1260 if (b->back - b->front < extra) {
adamc@666 1261 size_t desired = b->front - b->start + extra, next;
adamc@136 1262 char *new_heap;
adamc@136 1263
adamc@666 1264 next = b->back - b->start;
adamc@317 1265 if (next == 0)
adamc@317 1266 next = 1;
adamc@317 1267 for (; next < desired; next *= 2);
adamc@136 1268
adamc@1134 1269 if (next > b->max) {
adamc@1113 1270 if (desired <= b->max)
adamc@1113 1271 next = desired;
adamc@1113 1272 else
adamc@1113 1273 uw_error(ctx, FATAL, "Memory limit exceeded (%s)", kind);
adamc@1134 1274 }
adamc@1113 1275
adamc@666 1276 new_heap = realloc(b->start, next);
adamc@666 1277 b->front = new_heap + (b->front - b->start);
adamc@666 1278 b->back = new_heap + next;
adamc@136 1279
adamc@667 1280 if (new_heap != b->start) {
adamc@666 1281 b->start = new_heap;
adam@1389 1282 uw_error(ctx, UNLIMITED_RETRY, "Couldn't allocate new %s contiguously; increasing size to %llu", desc, (unsigned long long)next);
adamc@136 1283 }
adamc@136 1284
adamc@666 1285 b->start = new_heap;
adamc@136 1286 }
adamc@136 1287 }
adamc@136 1288
adamc@832 1289 void uw_check_heap(uw_context ctx, size_t extra) {
adam@1369 1290 uw_buffer_check_ctx(ctx, "heap", &ctx->heap, extra, "heap chunk");
adamc@666 1291 }
adamc@666 1292
adamc@832 1293 char *uw_heap_front(uw_context ctx) {
adamc@832 1294 return ctx->heap.front;
adamc@832 1295 }
adamc@832 1296
adamc@832 1297 void uw_set_heap_front(uw_context ctx, char *fr) {
adamc@832 1298 ctx->heap.front = fr;
adamc@832 1299 }
adamc@832 1300
adam@1782 1301 void uw_begin_initializing(uw_context ctx) {
adam@1782 1302 ctx->amInitializing = 1;
adam@1782 1303 }
adam@1782 1304
adam@1782 1305 void uw_end_initializing(uw_context ctx) {
adam@1782 1306 ctx->amInitializing = 0;
adam@1782 1307 }
adam@1782 1308
adam@2013 1309 static void align_heap(uw_context ctx) {
adam@2013 1310 size_t posn = ctx->heap.front - ctx->heap.start;
adam@2013 1311
adam@2017 1312 if (posn % sizeof(void *) != 0) {
adam@2017 1313 size_t bump = sizeof(void *) - posn % sizeof(void *);
adam@2013 1314 uw_check_heap(ctx, bump);
adam@2013 1315 ctx->heap.front += bump;
adam@2013 1316 }
adam@2013 1317 }
adam@2013 1318
adamc@311 1319 void *uw_malloc(uw_context ctx, size_t len) {
adam@2013 1320 // On some architectures, it's important that all word-sized memory accesses
adam@2013 1321 // be to word-aligned addresses, so we'll do a little bit of extra work here
adam@2013 1322 // in anticipation of a possible word-aligned access to the address we'll
adam@2013 1323 // return.
adam@2013 1324
adamc@136 1325 void *result;
adamc@136 1326
adam@1782 1327 if (ctx->amInitializing) {
adam@2017 1328 int error = posix_memalign(&result, sizeof(void *), len);
adam@2013 1329
adam@2013 1330 if (!error)
adam@1782 1331 return result;
adam@1782 1332 else
adam@2013 1333 uw_error(ctx, FATAL, "uw_malloc: posix_memalign() returns %d", error);
adam@1782 1334 } else {
adam@2013 1335 align_heap(ctx);
adam@2013 1336
adam@1782 1337 uw_check_heap(ctx, len);
adam@1782 1338
adam@1782 1339 result = ctx->heap.front;
adam@1782 1340 ctx->heap.front += len;
adam@1782 1341 return result;
adam@1782 1342 }
adamc@117 1343 }
adamc@117 1344
adamc@323 1345 void uw_begin_region(uw_context ctx) {
adam@2013 1346 align_heap(ctx);
adam@2013 1347
adamc@666 1348 regions *r = (regions *) ctx->heap.front;
adamc@323 1349
adamc@323 1350 uw_check_heap(ctx, sizeof(regions));
adamc@323 1351
adamc@666 1352 ctx->heap.front += sizeof(regions);
adamc@323 1353
adamc@323 1354 r->next = ctx->regions;
adamc@323 1355 ctx->regions = r;
adamc@323 1356 }
adamc@323 1357
adamc@323 1358 void uw_end_region(uw_context ctx) {
adamc@323 1359 regions *r = ctx->regions;
adamc@323 1360
adamc@323 1361 if (r == NULL)
adamc@323 1362 uw_error(ctx, FATAL, "Region stack underflow");
adamc@323 1363
adamc@666 1364 ctx->heap.front = (char *) r;
adamc@323 1365 ctx->regions = r->next;
adamc@323 1366 }
adamc@323 1367
adamc@324 1368 void uw_memstats(uw_context ctx) {
adam@1369 1369 printf("Headers: %lu/%lu\n", (unsigned long)uw_buffer_used(&ctx->outHeaders), (unsigned long)uw_buffer_avail(&ctx->outHeaders));
adam@1369 1370 printf("Script: %lu/%lu\n", (unsigned long)uw_buffer_used(&ctx->script), (unsigned long)uw_buffer_avail(&ctx->script));
adam@1369 1371 printf("Page: %lu/%lu\n", (unsigned long)uw_buffer_used(&ctx->page), (unsigned long)uw_buffer_avail(&ctx->page));
adam@1369 1372 printf("Heap: %lu/%lu\n", (unsigned long)uw_buffer_used(&ctx->heap), (unsigned long)uw_buffer_avail(&ctx->heap));
adamc@324 1373 }
adamc@324 1374
adam@1915 1375 int uw_pagelen(uw_context ctx) {
adam@1915 1376 return ctx->page.front - ctx->page.start;
adam@1915 1377 }
adam@1915 1378
adamc@311 1379 int uw_send(uw_context ctx, int sock) {
adam@1949 1380 size_t target_length = (ctx->outHeaders.front - ctx->outHeaders.start) + 2 + (ctx->page.front - ctx->page.start);
adam@1949 1381
adam@1949 1382 if (ctx->output_buffer_size < target_length) {
adam@1949 1383 do {
adam@1949 1384 ctx->output_buffer_size *= 2;
adam@1949 1385 } while (ctx->output_buffer_size < target_length);
adam@1949 1386 ctx->output_buffer = realloc(ctx->output_buffer, ctx->output_buffer_size);
adam@1949 1387 }
adam@1949 1388
adam@1949 1389 memcpy(ctx->output_buffer, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);
adam@1949 1390 memcpy(ctx->output_buffer + (ctx->outHeaders.front - ctx->outHeaders.start), "\r\n", 2);
adam@1949 1391 memcpy(ctx->output_buffer + (ctx->outHeaders.front - ctx->outHeaders.start) + 2, ctx->page.start, ctx->page.front - ctx->page.start);
adam@1949 1392
adam@1949 1393 return uw_really_send(sock, ctx->output_buffer, target_length);
adamc@462 1394 }
adamc@462 1395
adamc@856 1396 int uw_print(uw_context ctx, int fd) {
adamc@856 1397 int n = uw_really_write(fd, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);
adamc@856 1398
adamc@856 1399 if (n < 0)
adamc@856 1400 return n;
adamc@856 1401
adamc@856 1402 n = uw_really_write(fd, "\r\n", 2);
adamc@856 1403
adamc@856 1404 if (n < 0)
adamc@856 1405 return n;
adamc@856 1406
adamc@856 1407 return uw_really_write(fd, ctx->page.start, ctx->page.front - ctx->page.start);
adamc@856 1408 }
adamc@856 1409
adamc@860 1410 int uw_output(uw_context ctx, int (*output)(void *data, char *buf, size_t len), void *data) {
adamc@860 1411 int n = output(data, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);
adamc@860 1412
adamc@860 1413 if (n < 0)
adamc@860 1414 return n;
adamc@860 1415
adamc@860 1416 n = output(data, "\r\n", 2);
adamc@860 1417
adamc@860 1418 if (n < 0)
adamc@860 1419 return n;
adamc@860 1420
adamc@860 1421 return output(data, ctx->page.start, ctx->page.front - ctx->page.start);
adamc@860 1422 }
adamc@860 1423
adamc@462 1424 static void uw_check_headers(uw_context ctx, size_t extra) {
adam@1369 1425 ctx_uw_buffer_check(ctx, "headers", &ctx->outHeaders, extra);
adamc@462 1426 }
adamc@462 1427
adamc@462 1428 void uw_write_header(uw_context ctx, uw_Basis_string s) {
adamc@462 1429 int len = strlen(s);
adamc@462 1430
adamc@462 1431 uw_check_headers(ctx, len + 1);
adamc@666 1432 strcpy(ctx->outHeaders.front, s);
adamc@666 1433 ctx->outHeaders.front += len;
adamc@562 1434 }
adamc@562 1435
adam@1934 1436 int uw_has_contentLength(uw_context ctx) {
adam@1934 1437 return strstr(ctx->outHeaders.start, "Content-length: ") != NULL;
adam@1934 1438 }
adam@1934 1439
adamc@1110 1440 void uw_clear_headers(uw_context ctx) {
adam@1369 1441 uw_buffer_reset(&ctx->outHeaders);
adamc@1110 1442 }
adamc@1110 1443
adam@1932 1444 void uw_Basis_clear_page(uw_context ctx) {
adam@1932 1445 uw_buffer_reset(&ctx->page);
adam@1932 1446 }
adam@1932 1447
adamc@562 1448 static void uw_check_script(uw_context ctx, size_t extra) {
adam@1369 1449 ctx_uw_buffer_check(ctx, "script", &ctx->script, extra);
adamc@562 1450 }
adamc@562 1451
adamc@562 1452 void uw_write_script(uw_context ctx, uw_Basis_string s) {
adamc@562 1453 int len = strlen(s);
adamc@562 1454
adamc@562 1455 uw_check_script(ctx, len + 1);
adamc@666 1456 strcpy(ctx->script.front, s);
adamc@666 1457 ctx->script.front += len;
adamc@562 1458 }
adamc@562 1459
adam@1446 1460 const char *uw_get_real_script(uw_context ctx) {
adam@1671 1461 if (strstr(ctx->outHeaders.start, "Set-Cookie: ")) {
adam@1671 1462 uw_write_script(ctx, "sig=\"");
adam@1671 1463 uw_write_script(ctx, ctx->app->cookie_sig(ctx));
adam@1671 1464 uw_write_script(ctx, "\";");
adam@1671 1465 }
adam@1671 1466
adam@1446 1467 return ctx->script.start;
adam@1446 1468 }
adam@1446 1469
adamc@695 1470 uw_Basis_string uw_Basis_maybe_onload(uw_context ctx, uw_Basis_string s) {
adamc@695 1471 if (s[0] == 0)
adamc@695 1472 return "";
adamc@695 1473 else {
adamc@695 1474 char *r = uw_malloc(ctx, 11 + strlen(s));
adamc@695 1475 sprintf(r, " onload='%s'", s);
adamc@695 1476 return r;
adamc@695 1477 }
adamc@695 1478 }
adamc@695 1479
adamc@1042 1480 uw_Basis_string uw_Basis_maybe_onunload(uw_context ctx, uw_Basis_string s) {
adamc@1042 1481 if (ctx->script_header[0] == 0)
adamc@1042 1482 return "";
adamc@1042 1483 else {
vshabanoff@1763 1484 char *r = uw_malloc(ctx, 37 + strlen(s));
vshabanoff@1763 1485 sprintf(r, " onunload='unloading=true;%s;unload()'", s);
adamc@1042 1486 return r;
adamc@1042 1487 }
adamc@1042 1488 }
adamc@1042 1489
adamc@694 1490 const char *uw_Basis_get_settings(uw_context ctx, uw_unit u) {
adamc@736 1491 if (ctx->client == NULL) {
adamc@736 1492 if (ctx->needs_sig) {
adamc@1094 1493 char *sig = ctx->app->cookie_sig(ctx);
adamc@736 1494 char *r = uw_malloc(ctx, strlen(sig) + 8);
adamc@736 1495 sprintf(r, "sig=\"%s\";", sig);
adamc@736 1496 return r;
adamc@736 1497 }
adamc@736 1498 else
adamc@736 1499 return "";
adamc@736 1500 } else {
adamc@1094 1501 char *sig = ctx->needs_sig ? ctx->app->cookie_sig(ctx) : "";
adamc@1094 1502 char *r = uw_malloc(ctx, 59 + 3 * INTS_MAX + strlen(ctx->app->url_prefix)
adamc@736 1503 + (ctx->needs_sig ? strlen(sig) + 7 : 0));
adam@1552 1504 sprintf(r, "isPost=%s;client_id=%u;client_pass=%d;url_prefix=\"%s\";timeout=%d;%s%s%slistener();",
adam@1552 1505 (ctx->isPost ? "true" : "false"),
adamc@682 1506 ctx->client->id,
adamc@682 1507 ctx->client->pass,
adamc@1094 1508 ctx->app->url_prefix,
adamc@1094 1509 ctx->app->timeout,
adamc@736 1510 ctx->needs_sig ? "sig=\"" : "",
adamc@736 1511 sig,
adamc@736 1512 ctx->needs_sig ? "\";" : "");
adamc@679 1513 return r;
adamc@668 1514 }
adamc@667 1515 }
adamc@667 1516
adamc@574 1517 uw_Basis_string uw_Basis_jsifyString(uw_context ctx, uw_Basis_string s) {
adamc@574 1518 char *r, *s2;
adamc@574 1519
adam@1446 1520 uw_check_heap(ctx, strlen(s) * 4 + 3);
adamc@574 1521
adamc@666 1522 r = s2 = ctx->heap.front;
adamc@574 1523 *s2++ = '"';
adamc@574 1524
adamc@574 1525 for (; *s; s++) {
adam@1286 1526 unsigned char c = *s;
adamc@574 1527
adamc@574 1528 switch (c) {
adamc@574 1529 case '"':
adamc@574 1530 strcpy(s2, "\\\"");
adamc@574 1531 s2 += 2;
adamc@574 1532 break;
adamc@1027 1533 case '\'':
adamc@1027 1534 strcpy(s2, "\\047");
adamc@1027 1535 s2 += 4;
adamc@1027 1536 break;
adamc@574 1537 case '\\':
adamc@574 1538 strcpy(s2, "\\\\");
adamc@574 1539 s2 += 2;
adamc@574 1540 break;
adam@1457 1541 case '<':
adam@1457 1542 strcpy(s2, "\\074");
adam@1457 1543 s2 += 4;
adam@1457 1544 break;
adamc@574 1545 default:
adam@1286 1546 if (isprint((int)c) || c >= 128)
adamc@574 1547 *s2++ = c;
adamc@574 1548 else {
adam@1429 1549 sprintf(s2, "\\%03o", c);
adamc@574 1550 s2 += 4;
adamc@574 1551 }
adamc@574 1552 }
adamc@574 1553 }
adamc@574 1554
adamc@574 1555 strcpy(s2, "\"");
adamc@910 1556 ctx->heap.front = s2 + 2;
adamc@574 1557 return r;
adamc@574 1558 }
adamc@574 1559
adam@1286 1560 uw_Basis_string uw_Basis_jsifyChar(uw_context ctx, uw_Basis_char c1) {
adam@1286 1561 unsigned char c = c1;
adamc@1025 1562 char *r, *s2;
adamc@1025 1563
adam@1446 1564 uw_check_heap(ctx, 7);
adamc@1025 1565
adamc@1025 1566 r = s2 = ctx->heap.front;
adamc@1025 1567 *s2++ = '"';
adamc@1025 1568
adamc@1025 1569 switch (c) {
adamc@1025 1570 case '"':
adamc@1025 1571 strcpy(s2, "\\\"");
adamc@1025 1572 s2 += 2;
adamc@1025 1573 break;
adamc@1027 1574 case '\'':
adamc@1027 1575 strcpy(s2, "\\047");
adamc@1027 1576 s2 += 4;
adamc@1027 1577 break;
adamc@1025 1578 case '\\':
adamc@1025 1579 strcpy(s2, "\\\\");
adamc@1025 1580 s2 += 2;
adamc@1025 1581 break;
adam@1457 1582 case '<':
adam@1457 1583 strcpy(s2, "\\074");
adam@1457 1584 s2 += 4;
adam@1457 1585 break;
adamc@1025 1586 default:
adam@1286 1587 if (isprint((int)c) || c >= 128)
adamc@1025 1588 *s2++ = c;
adamc@1025 1589 else {
adam@1429 1590 sprintf(s2, "\\%03o", (unsigned char)c);
adamc@1025 1591 s2 += 4;
adamc@1025 1592 }
adamc@1025 1593 }
adamc@1025 1594
adamc@1025 1595 strcpy(s2, "\"");
adamc@1025 1596 ctx->heap.front = s2 + 2;
adamc@1025 1597 return r;
adamc@1025 1598 }
adamc@1025 1599
adamc@574 1600 uw_Basis_string uw_Basis_jsifyString_ws(uw_context ctx, uw_Basis_string s) {
adamc@574 1601 char *r, *s2;
adamc@574 1602
adam@1446 1603 uw_check_script(ctx, strlen(s) * 4 + 3);
adamc@574 1604
adamc@666 1605 r = s2 = ctx->script.front;
adamc@574 1606 *s2++ = '"';
adamc@574 1607
adamc@574 1608 for (; *s; s++) {
adam@1286 1609 unsigned char c = *s;
adamc@574 1610
adamc@574 1611 switch (c) {
adamc@577 1612 case '\'':
adamc@1027 1613 strcpy(s2, "\\");
adamc@574 1614 s2 += 2;
adamc@574 1615 break;
adamc@574 1616 case '\\':
adamc@574 1617 strcpy(s2, "\\\\");
adamc@574 1618 s2 += 2;
adamc@574 1619 break;
adam@1457 1620 case '<':
adam@1457 1621 strcpy(s2, "\\074");
adam@1457 1622 s2 += 4;
adam@1457 1623 break;
adamc@574 1624 default:
adam@1286 1625 if (isprint((int)c) || c >= 128)
adamc@574 1626 *s2++ = c;
adamc@574 1627 else {
adam@1429 1628 sprintf(s2, "\\%03o", c);
adamc@574 1629 s2 += 4;
adamc@574 1630 }
adamc@574 1631 }
adamc@574 1632 }
adamc@574 1633
adamc@574 1634 strcpy(s2, "\"");
adamc@666 1635 ctx->script.front = s2 + 1;
adamc@574 1636 return r;
adamc@574 1637 }
adamc@574 1638
adamc@682 1639 char *uw_Basis_jsifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 1640 if (ctx->client == NULL || chn.cli != ctx->client->id)
adamc@682 1641 return "null";
adamc@682 1642 else {
adamc@682 1643 int len;
adamc@682 1644 char *r;
adamc@682 1645
adamc@682 1646 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 1647 r = ctx->heap.front;
adamc@682 1648 sprintf(r, "%u%n", chn.chn, &len);
adamc@682 1649 ctx->heap.front += len+1;
adamc@682 1650 return r;
adamc@682 1651 }
adamc@682 1652 }
adamc@682 1653
adam@1446 1654 uw_Basis_source uw_Basis_new_client_source(uw_context ctx, uw_Basis_string s) {
adamc@577 1655 int len;
adamc@577 1656 size_t s_len = strlen(s);
adamc@562 1657
grrwlf@1998 1658 if(ctx->id < 0)
grrwlf@1998 1659 uw_error(ctx, FATAL, "Attempt to create client source using inappropriate context");
grrwlf@1998 1660
adam@1446 1661 uw_check_script(ctx, 15 + 2 * INTS_MAX + s_len);
adam@1446 1662 sprintf(ctx->script.front, "s%d_%llu=sc(exec(%n", ctx->id, ctx->source_count, &len);
adamc@666 1663 ctx->script.front += len;
adamc@666 1664 strcpy(ctx->script.front, s);
adamc@666 1665 ctx->script.front += s_len;
adamc@970 1666 strcpy(ctx->script.front, "));");
adamc@972 1667 ctx->script.front += 3;
adamc@562 1668
adam@1446 1669 uw_Basis_source r = {ctx->id, ctx->source_count++};
adam@1446 1670 return r;
adam@1446 1671 }
adam@1446 1672
adam@1446 1673 uw_unit uw_Basis_set_client_source(uw_context ctx, uw_Basis_source src, uw_Basis_string s) {
adamc@577 1674 int len;
adamc@577 1675 size_t s_len = strlen(s);
adamc@577 1676
adam@1446 1677 uw_check_script(ctx, 15 + 2 * INTS_MAX + s_len);
adam@1446 1678 sprintf(ctx->script.front, "sv(s%d_%llu,exec(%n", src.context, src.source, &len);
adamc@666 1679 ctx->script.front += len;
adamc@666 1680 strcpy(ctx->script.front, s);
adamc@666 1681 ctx->script.front += s_len;
adamc@970 1682 strcpy(ctx->script.front, "));");
adamc@972 1683 ctx->script.front += 3;
adamc@577 1684
adamc@577 1685 return uw_unit_v;
adamc@577 1686 }
adamc@577 1687
adamc@311 1688 static void uw_check(uw_context ctx, size_t extra) {
adam@1369 1689 ctx_uw_buffer_check(ctx, "page", &ctx->page, extra);
adamc@117 1690 }
adamc@117 1691
adamc@311 1692 static void uw_writec_unsafe(uw_context ctx, char c) {
adamc@666 1693 *(ctx->page.front)++ = c;
adam@1413 1694 *ctx->page.front = 0;
adamc@117 1695 }
adamc@117 1696
adamc@311 1697 void uw_writec(uw_context ctx, char c) {
adam@1413 1698 uw_check(ctx, 2);
adamc@311 1699 uw_writec_unsafe(ctx, c);
adamc@117 1700 }
adamc@117 1701
adam@2085 1702 void uw_Basis_writec(uw_context ctx, char c) {
adam@2085 1703 uw_writec(ctx, c);
adam@2085 1704 }
adam@2085 1705
adamc@311 1706 static void uw_write_unsafe(uw_context ctx, const char* s) {
adamc@117 1707 int len = strlen(s);
adamc@666 1708 memcpy(ctx->page.front, s, len);
adamc@666 1709 ctx->page.front += len;
adamc@117 1710 }
adamc@117 1711
adamc@311 1712 void uw_write(uw_context ctx, const char* s) {
adamc@311 1713 uw_check(ctx, strlen(s) + 1);
adamc@311 1714 uw_write_unsafe(ctx, s);
adamc@666 1715 *ctx->page.front = 0;
adamc@102 1716 }
adamc@106 1717
ziv@2204 1718 void uw_recordingStart(uw_context ctx) {
ziv@2262 1719 if (ctx->numRecording++ == 0) {
ziv@2262 1720 ctx->recordingOffset = ctx->page.front - ctx->page.start;
ziv@2262 1721 }
ziv@2204 1722 }
ziv@2204 1723
ziv@2204 1724 char *uw_recordingRead(uw_context ctx) {
ziv@2262 1725 // Only the outermost recorder can read unless the recording is empty.
ziv@2262 1726 char *recording = ctx->page.start + ctx->recordingOffset;
ziv@2262 1727 if (--ctx->numRecording > 0 && recording != ctx->page.front) {
ziv@2262 1728 return NULL;
ziv@2262 1729 }
ziv@2262 1730 return strdup(recording);
ziv@2204 1731 }
ziv@2204 1732
adamc@311 1733 char *uw_Basis_attrifyInt(uw_context ctx, uw_Basis_int n) {
adamc@136 1734 char *result;
adamc@136 1735 int len;
adamc@311 1736 uw_check_heap(ctx, INTS_MAX);
adamc@666 1737 result = ctx->heap.front;
adamc@276 1738 sprintf(result, "%lld%n", n, &len);
adamc@666 1739 ctx->heap.front += len+1;
adamc@136 1740 return result;
adamc@106 1741 }
adamc@106 1742
adamc@311 1743 char *uw_Basis_attrifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@136 1744 char *result;
adamc@136 1745 int len;
adamc@311 1746 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 1747 result = ctx->heap.front;
adam@1920 1748 sprintf(result, "%.16g%n", n, &len);
adamc@666 1749 ctx->heap.front += len+1;
adamc@136 1750 return result;
adamc@106 1751 }
adamc@106 1752
adamc@311 1753 char *uw_Basis_attrifyString(uw_context ctx, uw_Basis_string s) {
adamc@136 1754 int len = strlen(s);
adamc@136 1755 char *result, *p;
adamc@311 1756 uw_check_heap(ctx, len * 6 + 1);
adamc@136 1757
adamc@666 1758 result = p = ctx->heap.front;
adamc@136 1759
adamc@136 1760 for (; *s; s++) {
adamc@1054 1761 unsigned char c = *s;
adamc@136 1762
adamc@136 1763 if (c == '"') {
adamc@136 1764 strcpy(p, "&quot;");
adamc@136 1765 p += 6;
adamc@136 1766 } else if (c == '&') {
adamc@136 1767 strcpy(p, "&amp;");
adamc@136 1768 p += 5;
adamc@136 1769 }
adamc@1059 1770 else
adamc@136 1771 *p++ = c;
adamc@136 1772 }
adamc@136 1773
adamc@137 1774 *p++ = 0;
adamc@666 1775 ctx->heap.front = p;
adamc@136 1776 return result;
adamc@106 1777 }
adamc@106 1778
adamc@1011 1779 char *uw_Basis_attrifyChar(uw_context ctx, uw_Basis_char c) {
adamc@1011 1780 char *result, *p;
adamc@1011 1781 uw_check_heap(ctx, 7);
adamc@1011 1782
adamc@1011 1783 result = p = ctx->heap.front;
adamc@1011 1784
adamc@1011 1785 if (c == '"') {
adamc@1011 1786 strcpy(p, "&quot;");
adamc@1011 1787 p += 6;
adamc@1011 1788 } else if (c == '&') {
adamc@1011 1789 strcpy(p, "&amp;");
adamc@1011 1790 p += 5;
adamc@1011 1791 }
adamc@1059 1792 else
adamc@1011 1793 *p++ = c;
adamc@1011 1794
adamc@1011 1795 *p++ = 0;
adamc@1011 1796 ctx->heap.front = p;
adamc@1011 1797 return result;
adamc@1011 1798 }
adamc@1011 1799
adamc@721 1800 char *uw_Basis_attrifyCss_class(uw_context ctx, uw_Basis_css_class s) {
adamc@721 1801 return s;
adamc@721 1802 }
adamc@721 1803
adamc@311 1804 static void uw_Basis_attrifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
adamc@117 1805 int len;
adamc@117 1806
adamc@666 1807 sprintf(ctx->page.front, "%lld%n", n, &len);
adamc@666 1808 ctx->page.front += len;
adamc@106 1809 }
adamc@106 1810
adamc@428 1811 uw_unit uw_Basis_attrifyInt_w(uw_context ctx, uw_Basis_int n) {
adamc@311 1812 uw_check(ctx, INTS_MAX);
adamc@311 1813 uw_Basis_attrifyInt_w_unsafe(ctx, n);
adamc@428 1814
adamc@428 1815 return uw_unit_v;
adamc@106 1816 }
adamc@106 1817
adamc@428 1818 uw_unit uw_Basis_attrifyFloat_w(uw_context ctx, uw_Basis_float n) {
adamc@117 1819 int len;
adamc@117 1820
adamc@311 1821 uw_check(ctx, FLOATS_MAX);
adamc@666 1822 sprintf(ctx->page.front, "%g%n", n, &len);
adamc@666 1823 ctx->page.front += len;
adamc@428 1824
adamc@428 1825 return uw_unit_v;
adamc@117 1826 }
adamc@117 1827
adamc@428 1828 uw_unit uw_Basis_attrifyString_w(uw_context ctx, uw_Basis_string s) {
adamc@311 1829 uw_check(ctx, strlen(s) * 6);
adamc@117 1830
adamc@106 1831 for (; *s; s++) {
adamc@1054 1832 unsigned char c = *s;
adamc@106 1833
adamc@106 1834 if (c == '"')
adamc@311 1835 uw_write_unsafe(ctx, "&quot;");
adamc@136 1836 else if (c == '&')
adamc@311 1837 uw_write_unsafe(ctx, "&amp;");
adamc@1059 1838 else
adamc@311 1839 uw_writec_unsafe(ctx, c);
adamc@106 1840 }
adamc@428 1841
adamc@428 1842 return uw_unit_v;
adamc@106 1843 }
adamc@120 1844
adamc@1011 1845 uw_unit uw_Basis_attrifyChar_w(uw_context ctx, uw_Basis_char c) {
adamc@1011 1846 uw_check(ctx, 6);
adamc@1011 1847
adamc@1011 1848 if (c == '"')
adamc@1011 1849 uw_write_unsafe(ctx, "&quot;");
adamc@1011 1850 else if (c == '&')
adamc@1011 1851 uw_write_unsafe(ctx, "&amp;");
adamc@1059 1852 else
adamc@1011 1853 uw_writec_unsafe(ctx, c);
adamc@1011 1854
adamc@1011 1855 return uw_unit_v;
adamc@1011 1856 }
adamc@1011 1857
adamc@120 1858
adamc@311 1859 char *uw_Basis_urlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@137 1860 int len;
adamc@137 1861 char *r;
adamc@137 1862
adamc@311 1863 uw_check_heap(ctx, INTS_MAX);
adamc@666 1864 r = ctx->heap.front;
adamc@276 1865 sprintf(r, "%lld%n", n, &len);
adamc@666 1866 ctx->heap.front += len+1;
adamc@137 1867 return r;
adamc@120 1868 }
adamc@120 1869
adamc@682 1870 char *uw_Basis_urlifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 1871 if (ctx->client == NULL || chn.cli != ctx->client->id)
adamc@682 1872 return "";
adamc@682 1873 else {
adamc@682 1874 int len;
adamc@682 1875 char *r;
adamc@679 1876
adamc@682 1877 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 1878 r = ctx->heap.front;
adamc@682 1879 sprintf(r, "%u%n", chn.chn, &len);
adamc@682 1880 ctx->heap.front += len+1;
adamc@682 1881 return r;
adamc@682 1882 }
adamc@679 1883 }
adamc@679 1884
adamc@311 1885 char *uw_Basis_urlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@137 1886 int len;
adamc@137 1887 char *r;
adamc@137 1888
adamc@311 1889 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 1890 r = ctx->heap.front;
adamc@137 1891 sprintf(r, "%g%n", n, &len);
adamc@666 1892 ctx->heap.front += len+1;
adamc@137 1893 return r;
adamc@120 1894 }
adamc@120 1895
adamc@311 1896 char *uw_Basis_urlifyString(uw_context ctx, uw_Basis_string s) {
adamc@137 1897 char *r, *p;
adamc@137 1898
adamc@925 1899 if (s[0] == '\0')
adamc@925 1900 return "_";
adamc@925 1901
adamc@925 1902 uw_check_heap(ctx, strlen(s) * 3 + 1 + !!(s[0] == '_'));
adamc@925 1903
adamc@925 1904 r = p = ctx->heap.front;
adamc@925 1905 if (s[0] == '_')
adamc@925 1906 *p++ = '_';
adamc@925 1907
adamc@925 1908 for (; *s; s++) {
adamc@1087 1909 unsigned char c = *s;
adamc@137 1910
adamc@137 1911 if (c == ' ')
adamc@137 1912 *p++ = '+';
adamc@137 1913 else if (isalnum(c))
adamc@137 1914 *p++ = c;
adamc@137 1915 else {
adamc@1259 1916 sprintf(p, ".%02X", c);
adamc@137 1917 p += 3;
adamc@137 1918 }
adamc@137 1919 }
adamc@137 1920
adamc@137 1921 *p++ = 0;
adamc@666 1922 ctx->heap.front = p;
adamc@137 1923 return r;
adamc@120 1924 }
adamc@120 1925
adamc@311 1926 char *uw_Basis_urlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 1927 if (b == uw_Basis_False)
adamc@186 1928 return "0";
adamc@186 1929 else
adamc@186 1930 return "1";
adamc@186 1931 }
adamc@186 1932
adam@1620 1933 char *uw_Basis_urlifySource(uw_context ctx, uw_Basis_source src) {
adam@1620 1934 char *r;
adam@1620 1935 int len;
adam@1620 1936 uw_check_heap(ctx, 2 * INTS_MAX + 2);
adam@1620 1937 r = ctx->heap.front;
adam@1620 1938 sprintf(r, "%d/%llu%n", src.context, src.source, &len);
adam@1620 1939 ctx->heap.front += len+1;
adam@1620 1940 return r;
adam@1620 1941 }
adam@1620 1942
adamc@311 1943 static void uw_Basis_urlifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
adamc@120 1944 int len;
adamc@120 1945
adamc@666 1946 sprintf(ctx->page.front, "%lld%n", n, &len);
adamc@666 1947 ctx->page.front += len;
adamc@120 1948 }
adamc@120 1949
adamc@428 1950 uw_unit uw_Basis_urlifyInt_w(uw_context ctx, uw_Basis_int n) {
adamc@311 1951 uw_check(ctx, INTS_MAX);
adamc@311 1952 uw_Basis_urlifyInt_w_unsafe(ctx, n);
adamc@428 1953
adamc@428 1954 return uw_unit_v;
adamc@120 1955 }
adamc@120 1956
adamc@682 1957 uw_unit uw_Basis_urlifyChannel_w(uw_context ctx, uw_Basis_channel chn) {
adamc@682 1958 if (ctx->client != NULL && chn.cli == ctx->client->id) {
adamc@682 1959 int len;
phurst@1981 1960
adamc@682 1961 uw_check(ctx, INTS_MAX + 1);
adamc@682 1962 sprintf(ctx->page.front, "%u%n", chn.chn, &len);
adamc@682 1963 ctx->page.front += len;
adamc@682 1964 }
phurst@1981 1965
adamc@679 1966 return uw_unit_v;
adamc@679 1967 }
adamc@679 1968
adamc@428 1969 uw_unit uw_Basis_urlifyFloat_w(uw_context ctx, uw_Basis_float n) {
adamc@120 1970 int len;
adamc@120 1971
adamc@311 1972 uw_check(ctx, FLOATS_MAX);
adamc@666 1973 sprintf(ctx->page.front, "%g%n", n, &len);
adamc@666 1974 ctx->page.front += len;
adamc@428 1975
adamc@428 1976 return uw_unit_v;
adamc@120 1977 }
adamc@120 1978
adamc@488 1979 uw_Basis_string uw_Basis_urlifyTime(uw_context ctx, uw_Basis_time t) {
adamc@1376 1980 return uw_Basis_urlifyInt(ctx, (uw_Basis_int)t.seconds * 1000000 + t.microseconds);
adamc@488 1981 }
adamc@488 1982
adam@1404 1983 uw_unit uw_Basis_urlifyTime_w(uw_context ctx, uw_Basis_time t) {
adam@1404 1984 return uw_Basis_urlifyInt_w(ctx, (uw_Basis_int)t.seconds * 1000000 + t.microseconds);
adam@1404 1985 }
adam@1404 1986
adamc@428 1987 uw_unit uw_Basis_urlifyString_w(uw_context ctx, uw_Basis_string s) {
adamc@925 1988 if (s[0] == '\0') {
adamc@925 1989 uw_check(ctx, 1);
adamc@925 1990 uw_writec_unsafe(ctx, '_');
adamc@925 1991 return uw_unit_v;
adamc@925 1992 }
adamc@925 1993
adamc@925 1994 uw_check(ctx, strlen(s) * 3 + !!(s[0] == '_'));
adamc@925 1995
adamc@925 1996 if (s[0] == '_')
adamc@925 1997 uw_writec_unsafe(ctx, '_');
adamc@120 1998
adamc@120 1999 for (; *s; s++) {
adamc@1087 2000 unsigned char c = *s;
adamc@120 2001
adamc@120 2002 if (c == ' ')
adamc@311 2003 uw_writec_unsafe(ctx, '+');
adamc@120 2004 else if (isalnum(c))
adamc@311 2005 uw_writec_unsafe(ctx, c);
adamc@120 2006 else {
adamc@1259 2007 sprintf(ctx->page.front, ".%02X", c);
adamc@666 2008 ctx->page.front += 3;
adamc@120 2009 }
adamc@120 2010 }
adamc@428 2011
adamc@428 2012 return uw_unit_v;
adamc@120 2013 }
adamc@120 2014
adamc@428 2015 uw_unit uw_Basis_urlifyBool_w(uw_context ctx, uw_Basis_bool b) {
adamc@311 2016 if (b == uw_Basis_False)
adamc@311 2017 uw_writec(ctx, '0');
adamc@186 2018 else
adamc@311 2019 uw_writec(ctx, '1');
adamc@428 2020
adamc@428 2021 return uw_unit_v;
adamc@186 2022 }
adamc@186 2023
adam@1620 2024 uw_unit uw_Basis_urlifySource_w(uw_context ctx, uw_Basis_source src) {
adam@1620 2025 int len;
phurst@1981 2026
adam@1620 2027 uw_check(ctx, 2 * INTS_MAX + 2);
adam@1620 2028 sprintf(ctx->page.front, "%d/%llu%n", src.context, src.source, &len);
adam@1620 2029 ctx->page.front += len;
phurst@1981 2030
adam@1620 2031 return uw_unit_v;
adam@1620 2032 }
adam@1620 2033
adamc@120 2034
adamc@311 2035 static char *uw_unurlify_advance(char *s) {
adamc@144 2036 char *new_s = strchr(s, '/');
adamc@120 2037
adamc@120 2038 if (new_s)
adamc@120 2039 *new_s++ = 0;
adamc@120 2040 else
adamc@144 2041 new_s = strchr(s, 0);
adamc@144 2042
adamc@144 2043 return new_s;
adamc@144 2044 }
adamc@144 2045
adamc@311 2046 uw_Basis_int uw_Basis_unurlifyInt(uw_context ctx, char **s) {
adamc@311 2047 char *new_s = uw_unurlify_advance(*s);
adamc@311 2048 uw_Basis_int r;
adamc@120 2049
adamc@276 2050 r = atoll(*s);
adamc@120 2051 *s = new_s;
adamc@120 2052 return r;
adamc@120 2053 }
adamc@120 2054
adamc@311 2055 uw_Basis_float uw_Basis_unurlifyFloat(uw_context ctx, char **s) {
adamc@311 2056 char *new_s = uw_unurlify_advance(*s);
adamc@311 2057 uw_Basis_float r;
adamc@120 2058
adamc@120 2059 r = atof(*s);
adamc@120 2060 *s = new_s;
adamc@120 2061 return r;
adamc@120 2062 }
adamc@120 2063
adamc@488 2064 uw_Basis_time uw_Basis_unurlifyTime(uw_context ctx, char **s) {
adam@1365 2065 uw_Basis_int n = uw_Basis_unurlifyInt(ctx, s);
adam@1365 2066 uw_Basis_time r = {n / 1000000, n % 1000000};
adam@1365 2067 return r;
adamc@488 2068 }
adamc@488 2069
adamc@1023 2070 static uw_Basis_string uw_unurlifyString_to(int fromClient, uw_context ctx, char *r, char *s) {
adamc@927 2071 char *s1, *s2 = s;
adamc@144 2072 int n;
adamc@136 2073
adamc@1023 2074 if (!fromClient) {
adamc@1023 2075 if (*s2 == '_')
adamc@1023 2076 ++s2;
adamc@1259 2077 else if ((s2[0] == '%' || s2[0] == '.') && s2[1] == '5' && (s2[2] == 'f' || s2[2] == 'F'))
adamc@1023 2078 s2 += 3;
adamc@1023 2079 }
adamc@925 2080
adamc@927 2081 for (s1 = r; *s2; ++s1, ++s2) {
adamc@1087 2082 unsigned char c = *s2;
adamc@136 2083
adamc@136 2084 switch (c) {
adamc@136 2085 case '+':
adamc@136 2086 *s1 = ' ';
adamc@136 2087 break;
adamc@136 2088 case '%':
adamc@169 2089 if (s2[1] == 0)
adamc@311 2090 uw_error(ctx, FATAL, "Missing first character of escaped URL byte");
adamc@169 2091 if (s2[2] == 0)
adamc@311 2092 uw_error(ctx, FATAL, "Missing second character of escaped URL byte");
adamc@169 2093 if (sscanf(s2+1, "%02X", &n) != 1)
adam@1444 2094 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", uw_Basis_htmlifyString(ctx, s2));
adamc@136 2095 *s1 = n;
adamc@136 2096 s2 += 2;
adamc@136 2097 break;
adamc@1259 2098 case '.':
adamc@1259 2099 if (!fromClient) {
adamc@1259 2100 if (s2[1] == 0)
adamc@1259 2101 uw_error(ctx, FATAL, "Missing first character of escaped URL byte");
adamc@1259 2102 if (s2[2] == 0)
adamc@1259 2103 uw_error(ctx, FATAL, "Missing second character of escaped URL byte");
adamc@1259 2104 if (sscanf(s2+1, "%02X", &n) != 1)
adam@1444 2105 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", uw_Basis_htmlifyString(ctx, s2));
adamc@1259 2106 *s1 = n;
adamc@1259 2107 s2 += 2;
adamc@1259 2108 break;
adamc@1259 2109 }
adamc@136 2110 default:
adamc@136 2111 *s1 = c;
adamc@136 2112 }
adamc@136 2113 }
adamc@136 2114 *s1++ = 0;
adamc@144 2115 return s1;
adamc@144 2116 }
adamc@144 2117
adamc@311 2118 uw_Basis_bool uw_Basis_unurlifyBool(uw_context ctx, char **s) {
adamc@311 2119 char *new_s = uw_unurlify_advance(*s);
adamc@311 2120 uw_Basis_bool r;
phurst@1981 2121
adamc@186 2122 if (*s[0] == 0 || !strcmp(*s, "0") || !strcmp(*s, "off"))
adamc@311 2123 r = uw_Basis_False;
adamc@186 2124 else
adamc@311 2125 r = uw_Basis_True;
adamc@186 2126
adamc@186 2127 *s = new_s;
adamc@186 2128 return r;
adamc@186 2129 }
adamc@186 2130
adamc@311 2131 uw_Basis_string uw_Basis_unurlifyString(uw_context ctx, char **s) {
adamc@311 2132 char *new_s = uw_unurlify_advance(*s);
adamc@1134 2133 char *r;
adamc@1134 2134 int len;
adamc@144 2135
adamc@200 2136 len = strlen(*s);
adamc@311 2137 uw_check_heap(ctx, len + 1);
adamc@144 2138
adamc@666 2139 r = ctx->heap.front;
adamc@1023 2140 ctx->heap.front = uw_unurlifyString_to(0, ctx, ctx->heap.front, *s);
adamc@1023 2141 *s = new_s;
adamc@1023 2142 return r;
adamc@1023 2143 }
adamc@1023 2144
adamc@1109 2145 uw_Basis_unit uw_Basis_unurlifyUnit(uw_context ctx, char **s) {
adamc@1109 2146 *s = uw_unurlify_advance(*s);
adamc@1109 2147 return uw_unit_v;
adamc@1109 2148 }
adamc@1109 2149
adamc@1023 2150 uw_Basis_string uw_Basis_unurlifyString_fromClient(uw_context ctx, char **s) {
adamc@1023 2151 char *new_s = uw_unurlify_advance(*s);
adamc@1134 2152 char *r;
adamc@1134 2153 int len;
adamc@1023 2154
adamc@1023 2155 len = strlen(*s);
adamc@1023 2156 uw_check_heap(ctx, len + 1);
adamc@1023 2157
adamc@1023 2158 r = ctx->heap.front;
adamc@1023 2159 ctx->heap.front = uw_unurlifyString_to(1, ctx, ctx->heap.front, *s);
adamc@136 2160 *s = new_s;
adamc@136 2161 return r;
adamc@120 2162 }
adamc@135 2163
adamc@135 2164
adamc@311 2165 char *uw_Basis_htmlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@286 2166 int len;
adamc@286 2167 char *r;
adamc@286 2168
adamc@311 2169 uw_check_heap(ctx, INTS_MAX);
adamc@666 2170 r = ctx->heap.front;
adamc@286 2171 sprintf(r, "%lld%n", n, &len);
adamc@666 2172 ctx->heap.front += len+1;
adamc@286 2173 return r;
adamc@286 2174 }
adamc@286 2175
adamc@428 2176 uw_unit uw_Basis_htmlifyInt_w(uw_context ctx, uw_Basis_int n) {
adamc@286 2177 int len;
adamc@286 2178
adamc@311 2179 uw_check(ctx, INTS_MAX);
adamc@666 2180 sprintf(ctx->page.front, "%lld%n", n, &len);
adamc@666 2181 ctx->page.front += len;
phurst@1981 2182
adamc@428 2183 return uw_unit_v;
adamc@286 2184 }
adamc@286 2185
adam@1358 2186 char *uw_Basis_htmlifySpecialChar(uw_context ctx, unsigned char ch) {
adam@1358 2187 unsigned int n = ch;
adam@1358 2188 int len;
adam@1358 2189 char *r;
adam@1358 2190
adam@1358 2191 uw_check_heap(ctx, INTS_MAX+3);
adam@1358 2192 r = ctx->heap.front;
adam@1358 2193 sprintf(r, "&#%u;%n", n, &len);
adam@1358 2194 ctx->heap.front += len+1;
adam@1358 2195 return r;
adam@1358 2196 }
adam@1358 2197
adam@1358 2198 uw_unit uw_Basis_htmlifySpecialChar_w(uw_context ctx, unsigned char ch) {
adam@1358 2199 unsigned int n = ch;
adam@1358 2200 int len;
adam@1358 2201
adam@1358 2202 uw_check(ctx, INTS_MAX+3);
adam@1358 2203 sprintf(ctx->page.front, "&#%u;%n", n, &len);
adam@1358 2204 ctx->page.front += len;
adam@1358 2205 return uw_unit_v;
adam@1358 2206 }
adam@1358 2207
adamc@311 2208 char *uw_Basis_htmlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@286 2209 int len;
adamc@286 2210 char *r;
adamc@286 2211
adamc@311 2212 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 2213 r = ctx->heap.front;
adamc@286 2214 sprintf(r, "%g%n", n, &len);
adamc@666 2215 ctx->heap.front += len+1;
adamc@286 2216 return r;
adamc@286 2217 }
adamc@286 2218
adamc@428 2219 uw_unit uw_Basis_htmlifyFloat_w(uw_context ctx, uw_Basis_float n) {
adamc@286 2220 int len;
adamc@286 2221
adamc@311 2222 uw_check(ctx, FLOATS_MAX);
adamc@666 2223 sprintf(ctx->page.front, "%g%n", n, &len);
adamc@666 2224 ctx->page.front += len;
adamc@428 2225
adamc@428 2226 return uw_unit_v;
adamc@286 2227 }
adamc@286 2228
adam@1365 2229 char *uw_Basis_jsifyTime(uw_context ctx, uw_Basis_time t) {
adam@1360 2230 int len;
adam@1360 2231 char *r;
adam@1360 2232
adam@1360 2233 uw_check_heap(ctx, INTS_MAX);
adam@1360 2234 r = ctx->heap.front;
adamc@1377 2235 sprintf(r, "%lld%n", (uw_Basis_int)t.seconds * 1000000 + t.microseconds, &len);
adam@1360 2236 ctx->heap.front += len+1;
adam@1360 2237 return r;
adam@1360 2238 }
adam@1360 2239
adam@1365 2240 uw_unit uw_Basis_jsifyInt_w(uw_context ctx, uw_Basis_int n) {
adam@1360 2241 int len;
adam@1360 2242
adam@1360 2243 uw_check(ctx, INTS_MAX);
adam@1360 2244 sprintf(ctx->page.front, "%lld%n", (uw_Basis_int)n, &len);
adam@1360 2245 ctx->page.front += len;
phurst@1981 2246
adam@1360 2247 return uw_unit_v;
adam@1360 2248 }
adam@1360 2249
adam@1444 2250 char *uw_Basis_htmlifyString(uw_context ctx, const char *s) {
adamc@137 2251 char *r, *s2;
adamc@137 2252
adamc@311 2253 uw_check_heap(ctx, strlen(s) * 5 + 1);
adamc@137 2254
adamc@666 2255 for (r = s2 = ctx->heap.front; *s; s++) {
adamc@1054 2256 unsigned char c = *s;
adamc@137 2257
adamc@137 2258 switch (c) {
adamc@137 2259 case '<':
adamc@137 2260 strcpy(s2, "&lt;");
adamc@137 2261 s2 += 4;
adamc@137 2262 break;
adamc@137 2263 case '&':
adamc@137 2264 strcpy(s2, "&amp;");
adamc@137 2265 s2 += 5;
adamc@137 2266 break;
adamc@137 2267 default:
adamc@1059 2268 *s2++ = c;
adamc@137 2269 }
adamc@137 2270 }
adamc@137 2271
adamc@137 2272 *s2++ = 0;
adamc@666 2273 ctx->heap.front = s2;
adamc@137 2274 return r;
adamc@135 2275 }
adamc@135 2276
adamc@428 2277 uw_unit uw_Basis_htmlifyString_w(uw_context ctx, uw_Basis_string s) {
adamc@321 2278 uw_check(ctx, strlen(s) * 6);
adamc@135 2279
adamc@135 2280 for (; *s; s++) {
adamc@1054 2281 unsigned char c = *s;
adamc@135 2282
adamc@135 2283 switch (c) {
adamc@135 2284 case '<':
adamc@311 2285 uw_write_unsafe(ctx, "&lt;");
adamc@135 2286 break;
adamc@135 2287 case '&':
adamc@311 2288 uw_write_unsafe(ctx, "&amp;");
adamc@135 2289 break;
adamc@135 2290 default:
adamc@1059 2291 uw_writec_unsafe(ctx, c);
adamc@135 2292 }
adamc@135 2293 }
adamc@428 2294
adamc@428 2295 return uw_unit_v;
adamc@135 2296 }
adamc@180 2297
adamc@311 2298 uw_Basis_string uw_Basis_htmlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 2299 if (b == uw_Basis_False)
adamc@286 2300 return "False";
adamc@286 2301 else
adamc@286 2302 return "True";
adamc@286 2303 }
adamc@286 2304
adamc@428 2305 uw_unit uw_Basis_htmlifyBool_w(uw_context ctx, uw_Basis_bool b) {
adamc@311 2306 if (b == uw_Basis_False) {
adamc@311 2307 uw_check(ctx, 6);
adamc@666 2308 strcpy(ctx->page.front, "False");
adamc@666 2309 ctx->page.front += 5;
adamc@286 2310 } else {
adamc@311 2311 uw_check(ctx, 5);
adamc@666 2312 strcpy(ctx->page.front, "True");
adamc@666 2313 ctx->page.front += 4;
adamc@286 2314 }
adamc@428 2315
adamc@428 2316 return uw_unit_v;
adamc@286 2317 }
adamc@286 2318
adamc@436 2319 #define TIME_FMT "%x %X"
adamc@438 2320 #define TIME_FMT_PG "%Y-%m-%d %T"
adam@1632 2321 #define TIME_FMT_JS "%Y/%m/%d %T"
adamc@436 2322
adam@1629 2323 uw_Basis_string uw_Basis_timeToString(uw_context, uw_Basis_time);
adam@1629 2324
adamc@436 2325 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) {
adam@1629 2326 return uw_Basis_htmlifyString(ctx, uw_Basis_timeToString(ctx, t));
adamc@436 2327 }
adamc@436 2328
adamc@436 2329 uw_unit uw_Basis_htmlifyTime_w(uw_context ctx, uw_Basis_time t) {
adam@1629 2330 return uw_Basis_htmlifyString_w(ctx, uw_Basis_timeToString(ctx, t));
adamc@436 2331 }
adamc@436 2332
adam@1446 2333 char *uw_Basis_htmlifySource(uw_context ctx, uw_Basis_source src) {
adam@1446 2334 int len;
adam@1446 2335 char *r;
adam@1446 2336
adam@1446 2337 uw_check_heap(ctx, 2 * INTS_MAX + 2);
adam@1446 2338 r = ctx->heap.front;
adam@1446 2339 sprintf(r, "s%d_%llu%n", src.context, src.source, &len);
adam@1446 2340 ctx->heap.front += len+1;
adam@1446 2341 return r;
adam@1446 2342 }
adam@1446 2343
adam@1446 2344 uw_unit uw_Basis_htmlifySource_w(uw_context ctx, uw_Basis_source src) {
adam@1446 2345 int len;
adam@1446 2346
adam@1446 2347 uw_check(ctx, 2 * INTS_MAX + 1);
adam@1446 2348 sprintf(ctx->page.front, "s%d_%llu%n", src.context, src.source, &len);
adam@1446 2349 ctx->page.front += len;
phurst@1981 2350
adam@1446 2351 return uw_unit_v;
adam@1446 2352 }
adam@1446 2353
adamc@821 2354 uw_Basis_char uw_Basis_strsub(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
adam@1388 2355 while (n >= 0) {
adam@1388 2356 if (*s == 0)
adam@1388 2357 uw_error(ctx, FATAL, "Out-of-bounds strsub");
adam@1388 2358
adam@1388 2359 if (n == 0)
adam@1388 2360 return *s;
adam@1388 2361
adam@1388 2362 --n;
adam@1388 2363 ++s;
adam@1388 2364 }
adam@1388 2365
adam@1388 2366 uw_error(ctx, FATAL, "Negative strsub bound");
adamc@821 2367 }
adamc@821 2368
adamc@821 2369 uw_Basis_string uw_Basis_strsuffix(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
adam@1388 2370 while (n >= 0) {
adam@1388 2371 if (*s == 0 || n == 0)
adam@1388 2372 return s;
adam@1388 2373
adam@1388 2374 --n;
adam@1388 2375 ++s;
adam@1388 2376 }
adam@1388 2377
adam@1388 2378 uw_error(ctx, FATAL, "Negative strsuffix bound");
adamc@821 2379 }
adamc@821 2380
adamc@828 2381 uw_Basis_int uw_Basis_strlen(uw_context ctx, uw_Basis_string s) {
adamc@828 2382 return strlen(s);
adamc@828 2383 }
adamc@828 2384
adam@1388 2385 uw_Basis_bool uw_Basis_strlenGe(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
adam@1388 2386 while (n > 0) {
adam@1388 2387 if (*s == 0)
adam@1388 2388 return uw_Basis_False;
adam@1388 2389
adam@1388 2390 --n;
adam@1388 2391 ++s;
adam@1388 2392 }
adam@1388 2393
adam@1388 2394 return uw_Basis_True;
adam@1388 2395 }
adam@1388 2396
adamc@829 2397 uw_Basis_string uw_Basis_strchr(uw_context ctx, uw_Basis_string s, uw_Basis_char ch) {
adamc@829 2398 return strchr(s, ch);
adamc@829 2399 }
adamc@829 2400
adamc@831 2401 uw_Basis_int uw_Basis_strcspn(uw_context ctx, uw_Basis_string s, uw_Basis_string chs) {
adamc@831 2402 return strcspn(s, chs);
adamc@831 2403 }
adamc@831 2404
adamc@829 2405 uw_Basis_int *uw_Basis_strindex(uw_context ctx, uw_Basis_string s, uw_Basis_char ch) {
adamc@829 2406 uw_Basis_string r = strchr(s, ch);
adamc@829 2407 if (r == NULL)
adamc@829 2408 return NULL;
adamc@829 2409 else {
adamc@829 2410 uw_Basis_int *nr = uw_malloc(ctx, sizeof(uw_Basis_int));
adamc@829 2411 *nr = r - s;
adamc@829 2412 return nr;
adamc@829 2413 }
adamc@829 2414 }
adamc@829 2415
adam@1390 2416 uw_Basis_int *uw_Basis_strsindex(uw_context ctx, const char *haystack, const char *needle) {
adam@1390 2417 uw_Basis_string r = strstr(haystack, needle);
adam@1390 2418 if (r == NULL)
adam@1390 2419 return NULL;
adam@1390 2420 else {
adam@1390 2421 uw_Basis_int *nr = uw_malloc(ctx, sizeof(uw_Basis_int));
adam@1390 2422 *nr = r - haystack;
adam@1390 2423 return nr;
adam@1390 2424 }
adam@1390 2425 }
adam@1390 2426
adamc@311 2427 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) {
adamc@828 2428 int len = uw_Basis_strlen(ctx, s1) + uw_Basis_strlen(ctx, s2) + 1;
adamc@180 2429 char *s;
adamc@180 2430
adamc@311 2431 uw_check_heap(ctx, len);
adamc@180 2432
adamc@666 2433 s = ctx->heap.front;
adamc@180 2434
adamc@180 2435 strcpy(s, s1);
adamc@180 2436 strcat(s, s2);
adamc@666 2437 ctx->heap.front += len;
adamc@180 2438
adamc@180 2439 return s;
adamc@180 2440 }
adamc@278 2441
adamc@829 2442 uw_Basis_string uw_Basis_substring(uw_context ctx, uw_Basis_string s, uw_Basis_int start, uw_Basis_int len) {
adamc@829 2443 size_t full_len = uw_Basis_strlen(ctx, s);
adamc@829 2444
adamc@829 2445 if (start < 0)
adamc@829 2446 uw_error(ctx, FATAL, "substring: Negative start index");
adamc@829 2447 if (len < 0)
adamc@829 2448 uw_error(ctx, FATAL, "substring: Negative length");
adamc@829 2449 if (start + len > full_len)
adamc@829 2450 uw_error(ctx, FATAL, "substring: Start index plus length is too large");
adamc@829 2451
adamc@829 2452 if (start + len == full_len)
adamc@829 2453 return &s[start];
adamc@829 2454 else {
adamc@829 2455 uw_Basis_string r = uw_malloc(ctx, len+1);
adamc@959 2456 memcpy(r, s+start, len);
adamc@829 2457 r[len] = 0;
adamc@829 2458 return r;
adamc@829 2459 }
phurst@1981 2460
adamc@829 2461 }
adamc@829 2462
adamc@1023 2463 uw_Basis_string uw_Basis_str1(uw_context ctx, uw_Basis_char ch) {
adamc@1023 2464 char *r;
adamc@1023 2465
adamc@1023 2466 uw_check_heap(ctx, 2);
adamc@1023 2467 r = ctx->heap.front;
adamc@1023 2468 r[0] = ch;
adamc@1023 2469 r[1] = 0;
adamc@1023 2470
adamc@1023 2471 ctx->heap.front += 2;
adamc@1023 2472
adamc@1023 2473 return r;
adamc@1023 2474 }
adamc@1023 2475
adamc@737 2476 uw_Basis_string uw_strdup(uw_context ctx, uw_Basis_string s1) {
adamc@828 2477 int len = uw_Basis_strlen(ctx, s1) + 1;
adamc@278 2478 char *s;
adamc@278 2479
adamc@311 2480 uw_check_heap(ctx, len);
adamc@278 2481
adamc@666 2482 s = ctx->heap.front;
adamc@278 2483
adamc@278 2484 strcpy(s, s1);
adamc@666 2485 ctx->heap.front += len;
adamc@278 2486
adamc@278 2487 return s;
adamc@278 2488 }
adamc@280 2489
adam@1295 2490 uw_Basis_string uw_dup_and_clear_error_message(uw_context ctx) {
adam@1295 2491 if (ctx->error_message[0]) {
adam@1295 2492 char *s = uw_strdup(ctx, ctx->error_message);
adam@1295 2493 ctx->error_message[0] = 0;
adam@1295 2494 return s;
adam@1295 2495 } else
adam@1295 2496 return NULL;
adam@1295 2497 }
adam@1295 2498
adamc@737 2499 uw_Basis_string uw_maybe_strdup(uw_context ctx, uw_Basis_string s1) {
adamc@493 2500 if (s1)
adamc@737 2501 return uw_strdup(ctx, s1);
adamc@493 2502 else
adamc@493 2503 return NULL;
adamc@493 2504 }
adamc@493 2505
adamc@742 2506 char *uw_memdup(uw_context ctx, const char *p, size_t len) {
adamc@742 2507 char *r = uw_malloc(ctx, len);
adamc@742 2508 memcpy(r, p, len);
adamc@742 2509 return r;
adamc@742 2510 }
adamc@280 2511
adamc@879 2512 char *uw_sqlfmtInt = "%lld::int8%n";
adamc@879 2513
adamc@311 2514 char *uw_Basis_sqlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@281 2515 int len;
adamc@281 2516 char *r;
adamc@281 2517
adamc@311 2518 uw_check_heap(ctx, INTS_MAX + 6);
adamc@666 2519 r = ctx->heap.front;
adamc@879 2520 sprintf(r, uw_sqlfmtInt, n, &len);
adamc@666 2521 ctx->heap.front += len+1;
adamc@281 2522 return r;
adamc@281 2523 }
adamc@281 2524
adamc@467 2525 char *uw_Basis_sqlifyIntN(uw_context ctx, uw_Basis_int *n) {
adamc@467 2526 if (n == NULL)
adamc@467 2527 return "NULL";
adamc@467 2528 else
adamc@467 2529 return uw_Basis_sqlifyInt(ctx, *n);
adamc@467 2530 }
adamc@467 2531
adam@1920 2532 char *uw_sqlfmtFloat = "%.16g::float8%n";
adamc@879 2533
adamc@311 2534 char *uw_Basis_sqlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@281 2535 int len;
adamc@281 2536 char *r;
adamc@281 2537
adamc@311 2538 uw_check_heap(ctx, FLOATS_MAX + 8);
adamc@666 2539 r = ctx->heap.front;
adamc@879 2540 sprintf(r, uw_sqlfmtFloat, n, &len);
adamc@666 2541 ctx->heap.front += len+1;
adamc@281 2542 return r;
adamc@281 2543 }
adamc@281 2544
adamc@467 2545 char *uw_Basis_sqlifyFloatN(uw_context ctx, uw_Basis_float *n) {
adamc@467 2546 if (n == NULL)
adamc@467 2547 return "NULL";
adamc@467 2548 else
adamc@467 2549 return uw_Basis_sqlifyFloat(ctx, *n);
adamc@467 2550 }
adamc@467 2551
adam@1834 2552 int uw_Estrings = 1, uw_sql_type_annotations = 1;
adamc@879 2553 char *uw_sqlsuffixString = "::text";
adamc@1011 2554 char *uw_sqlsuffixChar = "::char";
adamc@281 2555
adamc@311 2556 uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) {
adamc@280 2557 char *r, *s2;
adamc@280 2558
adamc@879 2559 uw_check_heap(ctx, strlen(s) * 2 + 3 + uw_Estrings + strlen(uw_sqlsuffixString));
adamc@280 2560
adamc@666 2561 r = s2 = ctx->heap.front;
adamc@879 2562 if (uw_Estrings)
adamc@879 2563 *s2++ = 'E';
adamc@280 2564 *s2++ = '\'';
adamc@280 2565
adamc@280 2566 for (; *s; s++) {
adamc@280 2567 char c = *s;
adamc@280 2568
adamc@280 2569 switch (c) {
adamc@280 2570 case '\'':
adamc@885 2571 if (uw_Estrings)
adamc@885 2572 strcpy(s2, "\\'");
adamc@885 2573 else
adamc@885 2574 strcpy(s2, "''");
adamc@280 2575 s2 += 2;
adamc@280 2576 break;
adamc@280 2577 case '\\':
adamc@885 2578 if (uw_Estrings) {
adamc@885 2579 strcpy(s2, "\\\\");
adamc@885 2580 s2 += 2;
adamc@885 2581 } else
adamc@885 2582 *s2++ = '\\';
adamc@280 2583 break;
adamc@280 2584 default:
adamc@1150 2585 if (isprint((int)c))
adamc@280 2586 *s2++ = c;
adamc@879 2587 else if (uw_Estrings) {
adam@1857 2588 sprintf(s2, "\\%03o", (unsigned char)c);
adamc@280 2589 s2 += 4;
adamc@280 2590 }
adamc@879 2591 else
adam@1689 2592 *s2++ = c; // I hope this is safe to do... don't know how to support UTF-8 outside Postgres otherwise!
adamc@280 2593 }
adamc@280 2594 }
adamc@280 2595
adamc@879 2596 *s2++ = '\'';
adamc@879 2597 strcpy(s2, uw_sqlsuffixString);
adamc@879 2598 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixString);
adamc@280 2599 return r;
adamc@280 2600 }
adamc@281 2601
adamc@1011 2602 uw_Basis_string uw_Basis_sqlifyChar(uw_context ctx, uw_Basis_char c) {
adamc@1011 2603 char *r, *s2;
adamc@1011 2604
adamc@1011 2605 uw_check_heap(ctx, 5 + uw_Estrings + strlen(uw_sqlsuffixChar));
adamc@1011 2606
adamc@1011 2607 r = s2 = ctx->heap.front;
adamc@1011 2608 if (uw_Estrings)
adamc@1011 2609 *s2++ = 'E';
adamc@1011 2610 *s2++ = '\'';
adamc@1011 2611
adamc@1011 2612 switch (c) {
adamc@1011 2613 case '\'':
adamc@1011 2614 if (uw_Estrings)
adamc@1011 2615 strcpy(s2, "\\'");
adamc@1011 2616 else
adamc@1011 2617 strcpy(s2, "''");
adamc@1011 2618 s2 += 2;
adamc@1011 2619 break;
adamc@1011 2620 case '\\':
adamc@1011 2621 if (uw_Estrings) {
adamc@1011 2622 strcpy(s2, "\\\\");
adamc@1011 2623 s2 += 2;
adamc@1011 2624 } else
adamc@1011 2625 *s2++ = '\\';
adamc@1011 2626 break;
adamc@1011 2627 default:
adamc@1150 2628 if (isprint((int)c))
adamc@1011 2629 *s2++ = c;
adamc@1011 2630 else if (uw_Estrings) {
adam@1857 2631 sprintf(s2, "\\%03o", (unsigned char)c);
adamc@1011 2632 s2 += 4;
adamc@1011 2633 }
adamc@1011 2634 else
adamc@1011 2635 uw_error(ctx, FATAL, "Non-printable character %u in char to SQLify", c);
adamc@1011 2636 }
adamc@1011 2637
adamc@1011 2638 *s2++ = '\'';
adamc@1011 2639 strcpy(s2, uw_sqlsuffixChar);
adamc@1011 2640 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixChar);
adamc@1011 2641 return r;
adamc@1011 2642 }
adamc@1011 2643
adamc@879 2644 char *uw_sqlsuffixBlob = "::bytea";
adamc@879 2645
adam@1867 2646 uw_Basis_string uw_Basis_sqlifyBlob(uw_context ctx, uw_Basis_blob b) {
adamc@737 2647 char *r, *s2;
adamc@737 2648 size_t i;
adamc@737 2649
adam@1867 2650 uw_check_heap(ctx, b.size * 5 + 4 + strlen(uw_sqlsuffixBlob));
adamc@737 2651
adamc@737 2652 r = s2 = ctx->heap.front;
adamc@879 2653 if (uw_Estrings)
adamc@879 2654 *s2++ = 'E';
adam@1867 2655 else
adam@1867 2656 *s2++ = 'X';
adamc@737 2657 *s2++ = '\'';
adamc@737 2658
adamc@737 2659 for (i = 0; i < b.size; ++i) {
adam@1867 2660 unsigned char c = b.data[i];
adam@1867 2661
adam@1867 2662 if (uw_Estrings) {
adam@1867 2663 switch (c) {
adam@1867 2664 case '\'':
adamc@885 2665 strcpy(s2, "\\'");
adam@1867 2666 s2 += 2;
adam@1867 2667 break;
adam@1867 2668 case '\\':
adamc@885 2669 strcpy(s2, "\\\\\\\\");
adamc@885 2670 s2 += 4;
adam@1867 2671 break;
adam@1867 2672 default:
adam@1867 2673 if (isprint((int)c))
adam@1867 2674 *s2++ = c;
adam@1867 2675 else {
adam@1867 2676 sprintf(s2, "\\\\%03o", c);
adam@1867 2677 s2 += 5;
adam@1867 2678 }
adamc@737 2679 }
adam@1867 2680 } else {
adam@1867 2681 sprintf(s2, "%02X", c);
adam@1867 2682 s2 += 2;
adamc@737 2683 }
phurst@1981 2684 }
adamc@737 2685
adamc@879 2686 *s2++ = '\'';
adamc@879 2687 strcpy(s2, uw_sqlsuffixBlob);
adamc@879 2688 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixBlob);
adamc@737 2689 return r;
adamc@737 2690 }
adamc@737 2691
adamc@682 2692 char *uw_Basis_sqlifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 2693 int len;
adamc@682 2694 char *r;
adamc@682 2695 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
adamc@682 2696
adamc@682 2697 uw_check_heap(ctx, INTS_MAX + 7);
adamc@682 2698 r = ctx->heap.front;
adamc@879 2699 sprintf(r, uw_sqlfmtInt, combo, &len);
adamc@682 2700 ctx->heap.front += len+1;
adamc@682 2701 return r;
adamc@682 2702 }
adamc@682 2703
adamc@682 2704 char *uw_Basis_attrifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 2705 int len;
adamc@682 2706 char *r;
adamc@682 2707 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
adamc@682 2708
adamc@682 2709 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 2710 r = ctx->heap.front;
adamc@682 2711 sprintf(r, "%lld%n", combo, &len);
adamc@682 2712 ctx->heap.front += len+1;
adamc@682 2713 return r;
adamc@682 2714 }
adamc@682 2715
adamc@879 2716 char *uw_sqlfmtUint4 = "%u::int4%n";
adamc@879 2717
adamc@682 2718 char *uw_Basis_sqlifyClient(uw_context ctx, uw_Basis_client cli) {
adamc@682 2719 int len;
adamc@682 2720 char *r;
adamc@682 2721
adamc@682 2722 uw_check_heap(ctx, INTS_MAX + 7);
adamc@682 2723 r = ctx->heap.front;
adamc@879 2724 sprintf(r, uw_sqlfmtUint4, cli, &len);
adamc@682 2725 ctx->heap.front += len+1;
adamc@682 2726 return r;
adamc@682 2727 }
adamc@682 2728
adamc@682 2729 char *uw_Basis_attrifyClient(uw_context ctx, uw_Basis_client cli) {
adamc@682 2730 int len;
adamc@682 2731 char *r;
adamc@682 2732
adamc@682 2733 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 2734 r = ctx->heap.front;
adamc@682 2735 sprintf(r, "%u%n", cli, &len);
adamc@682 2736 ctx->heap.front += len+1;
adamc@682 2737 return r;
adamc@682 2738 }
adamc@682 2739
adamc@467 2740 uw_Basis_string uw_Basis_sqlifyStringN(uw_context ctx, uw_Basis_string s) {
adamc@467 2741 if (s == NULL)
adamc@467 2742 return "NULL";
adamc@467 2743 else
adamc@467 2744 return uw_Basis_sqlifyString(ctx, s);
adamc@467 2745 }
adamc@467 2746
adamc@311 2747 char *uw_Basis_sqlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 2748 if (b == uw_Basis_False)
adamc@281 2749 return "FALSE";
adamc@281 2750 else
adamc@281 2751 return "TRUE";
adamc@281 2752 }
adamc@282 2753
adamc@467 2754 char *uw_Basis_sqlifyBoolN(uw_context ctx, uw_Basis_bool *b) {
adamc@467 2755 if (b == NULL)
adamc@467 2756 return "NULL";
adamc@467 2757 else
adamc@467 2758 return uw_Basis_sqlifyBool(ctx, *b);
adamc@467 2759 }
adamc@467 2760
adamc@439 2761 char *uw_Basis_sqlifyTime(uw_context ctx, uw_Basis_time t) {
adamc@439 2762 size_t len;
adamc@682 2763 char *r, *s;
adam@1392 2764 struct tm stm = {};
adam@1392 2765 stm.tm_isdst = -1;
adamc@682 2766
adam@1365 2767 if (localtime_r(&t.seconds, &stm)) {
adamc@682 2768 s = uw_malloc(ctx, TIMES_MAX);
adam@1364 2769 len = strftime(s, TIMES_MAX, TIME_FMT_PG, &stm);
adam@1834 2770 if (uw_sql_type_annotations) {
adam@1834 2771 if (t.microseconds) {
adam@1834 2772 r = uw_malloc(ctx, len + 21);
adam@1834 2773 sprintf(r, "'%s.%06u'::timestamp", s, t.microseconds);
adam@1834 2774 } else {
adam@1834 2775 r = uw_malloc(ctx, len + 14);
adam@1834 2776 sprintf(r, "'%s'::timestamp", s);
adam@1834 2777 }
adam@1367 2778 } else {
adam@1834 2779 r = uw_malloc(ctx, len + 3);
adam@1834 2780 sprintf(r, "'%s'", s);
adam@1367 2781 }
adamc@682 2782 return r;
adamc@682 2783 } else
adamc@682 2784 return "<Invalid time>";
adamc@682 2785 }
adamc@682 2786
adamc@682 2787 char *uw_Basis_attrifyTime(uw_context ctx, uw_Basis_time t) {
adamc@682 2788 size_t len;
adamc@439 2789 char *r;
adam@1392 2790 struct tm stm = {};
adam@1392 2791 stm.tm_isdst = -1;
adamc@439 2792
adam@1365 2793 if (localtime_r(&t.seconds, &stm)) {
adamc@439 2794 uw_check_heap(ctx, TIMES_MAX);
adamc@666 2795 r = ctx->heap.front;
adamc@439 2796 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 2797 ctx->heap.front += len+1;
adamc@439 2798 return r;
adamc@439 2799 } else
adamc@439 2800 return "<Invalid time>";
adamc@439 2801 }
adamc@439 2802
adam@1364 2803 char *uw_Basis_ensqlTime(uw_context ctx, uw_Basis_time t) {
adam@1364 2804 size_t len;
adam@1364 2805 char *r;
adam@1392 2806 struct tm stm = {};
adam@1392 2807 stm.tm_isdst = -1;
adam@1364 2808
adam@1365 2809 if (localtime_r(&t.seconds, &stm)) {
adam@1364 2810 uw_check_heap(ctx, TIMES_MAX);
adam@1364 2811 r = ctx->heap.front;
adam@1365 2812 len = strftime(r, TIMES_MAX-7, TIME_FMT_PG, &stm);
adam@1365 2813 ctx->heap.front += len;
adam@1365 2814 sprintf(ctx->heap.front, ".%06u", t.microseconds);
adam@1365 2815 ctx->heap.front += 8;
adam@1364 2816 return r;
adam@1364 2817 } else
adam@1364 2818 return "<Invalid time>";
adam@1364 2819 }
adam@1364 2820
adamc@467 2821 char *uw_Basis_sqlifyTimeN(uw_context ctx, uw_Basis_time *t) {
adamc@467 2822 if (t == NULL)
adamc@467 2823 return "NULL";
adamc@467 2824 else
adamc@467 2825 return uw_Basis_sqlifyTime(ctx, *t);
adamc@467 2826 }
adamc@467 2827
adamc@311 2828 char *uw_Basis_ensqlBool(uw_Basis_bool b) {
adamc@311 2829 static uw_Basis_int true = 1;
adamc@311 2830 static uw_Basis_int false = 0;
adamc@282 2831
adamc@311 2832 if (b == uw_Basis_False)
adamc@282 2833 return (char *)&false;
adamc@282 2834 else
adamc@282 2835 return (char *)&true;
adamc@282 2836 }
adamc@284 2837
adamc@311 2838 uw_Basis_string uw_Basis_intToString(uw_context ctx, uw_Basis_int n) {
adamc@284 2839 int len;
adamc@284 2840 char *r;
adamc@284 2841
adamc@311 2842 uw_check_heap(ctx, INTS_MAX);
adamc@666 2843 r = ctx->heap.front;
adamc@284 2844 sprintf(r, "%lld%n", n, &len);
adamc@666 2845 ctx->heap.front += len+1;
adamc@284 2846 return r;
adamc@284 2847 }
adamc@285 2848
adamc@311 2849 uw_Basis_string uw_Basis_floatToString(uw_context ctx, uw_Basis_float n) {
adamc@285 2850 int len;
adamc@285 2851 char *r;
adamc@285 2852
adamc@311 2853 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 2854 r = ctx->heap.front;
adamc@285 2855 sprintf(r, "%g%n", n, &len);
adamc@666 2856 ctx->heap.front += len+1;
adamc@285 2857 return r;
adamc@285 2858 }
adamc@285 2859
adamc@821 2860 uw_Basis_string uw_Basis_charToString(uw_context ctx, uw_Basis_char ch) {
adamc@821 2861 char *r = uw_malloc(ctx, 2);
adamc@821 2862 r[0] = ch;
adamc@821 2863 r[1] = 0;
adamc@821 2864 return r;
adamc@821 2865 }
adamc@821 2866
adamc@311 2867 uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) {
adamc@311 2868 if (b == uw_Basis_False)
adamc@285 2869 return "False";
adamc@285 2870 else
adamc@285 2871 return "True";
adamc@285 2872 }
adamc@288 2873
adam@1359 2874 uw_Basis_string uw_Basis_timef(uw_context ctx, const char *fmt, uw_Basis_time t) {
adam@1352 2875 size_t len;
adam@1352 2876 char *r;
adam@1392 2877 struct tm stm = {};
adam@1392 2878 stm.tm_isdst = -1;
adam@1352 2879
adam@1365 2880 if (localtime_r(&t.seconds, &stm)) {
adam@1352 2881 uw_check_heap(ctx, TIMES_MAX);
adam@1352 2882 r = ctx->heap.front;
adam@1352 2883 len = strftime(r, TIMES_MAX, fmt, &stm);
adam@1352 2884 ctx->heap.front += len+1;
adam@1352 2885 return r;
adam@1352 2886 } else
adam@1352 2887 return "<Invalid time>";
adam@1352 2888 }
adam@1352 2889
adam@1629 2890 uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) {
adam@1629 2891 return uw_Basis_timef(ctx, ctx->app->time_format, t);
adam@1629 2892 }
adam@1629 2893
adamc@311 2894 uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) {
adamc@288 2895 char *endptr;
adamc@311 2896 uw_Basis_int n = strtoll(s, &endptr, 10);
adamc@288 2897
adamc@288 2898 if (*s != '\0' && *endptr == '\0') {
adamc@311 2899 uw_Basis_int *r = uw_malloc(ctx, sizeof(uw_Basis_int));
adamc@288 2900 *r = n;
adamc@288 2901 return r;
adamc@288 2902 } else
adamc@288 2903 return NULL;
adamc@288 2904 }
adamc@289 2905
adamc@311 2906 uw_Basis_float *uw_Basis_stringToFloat(uw_context ctx, uw_Basis_string s) {
adamc@289 2907 char *endptr;
adamc@311 2908 uw_Basis_float n = strtod(s, &endptr);
adamc@289 2909
adamc@289 2910 if (*s != '\0' && *endptr == '\0') {
adamc@311 2911 uw_Basis_float *r = uw_malloc(ctx, sizeof(uw_Basis_float));
adamc@289 2912 *r = n;
adamc@289 2913 return r;
adamc@289 2914 } else
adamc@289 2915 return NULL;
adamc@289 2916 }
adamc@289 2917
adamc@821 2918 uw_Basis_char *uw_Basis_stringToChar(uw_context ctx, uw_Basis_string s) {
adamc@821 2919 if (s[0] == 0) {
adamc@821 2920 uw_Basis_char *r = uw_malloc(ctx, 1);
adamc@821 2921 r[0] = 0;
adamc@821 2922 return r;
adamc@821 2923 } else if (s[1] != 0)
adamc@821 2924 return NULL;
adamc@821 2925 else {
adamc@821 2926 uw_Basis_char *r = uw_malloc(ctx, 1);
adamc@821 2927 r[0] = s[0];
adamc@821 2928 return r;
adamc@821 2929 }
adamc@821 2930 }
adamc@821 2931
adamc@311 2932 uw_Basis_bool *uw_Basis_stringToBool(uw_context ctx, uw_Basis_string s) {
adamc@311 2933 static uw_Basis_bool true = uw_Basis_True;
adamc@311 2934 static uw_Basis_bool false = uw_Basis_False;
adamc@289 2935
adamc@289 2936 if (!strcasecmp (s, "True"))
adamc@289 2937 return &true;
adamc@289 2938 else if (!strcasecmp (s, "False"))
adamc@289 2939 return &false;
adamc@289 2940 else
adamc@289 2941 return NULL;
adamc@289 2942 }
adamc@292 2943
adamc@436 2944 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) {
adamc@438 2945 char *dot = strchr(s, '.'), *end = strchr(s, 0);
adam@1374 2946 struct tm stm = {};
adam@1392 2947 stm.tm_isdst = -1;
adamc@436 2948
adamc@439 2949 if (dot) {
adamc@439 2950 *dot = 0;
adamc@439 2951 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adamc@439 2952 *dot = '.';
adamc@439 2953 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adam@1365 2954 r->seconds = mktime(&stm);
adam@1365 2955 r->microseconds = 0;
adamc@439 2956 return r;
adamc@439 2957 }
adamc@439 2958 else {
adamc@439 2959 *dot = '.';
adamc@439 2960 return NULL;
adamc@439 2961 }
adamc@436 2962 }
adamc@439 2963 else {
adam@2072 2964 if (strptime(s, ctx->app->time_format, &stm) == end) {
adamc@439 2965 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adam@1365 2966 r->seconds = mktime(&stm);
adam@1365 2967 r->microseconds = 0;
adamc@439 2968 return r;
adam@2072 2969 } else if (strptime(s, TIME_FMT_PG, &stm) == end) {
adam@2072 2970 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adam@2072 2971 r->seconds = mktime(&stm);
adam@2072 2972 r->microseconds = 0;
adam@2072 2973 return r;
adam@2072 2974 } else if (strptime(s, TIME_FMT, &stm) == end) {
adamc@439 2975 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adam@1365 2976 r->seconds = mktime(&stm);
adam@1365 2977 r->microseconds = 0;
adamc@439 2978 return r;
adam@1632 2979 } else if (strptime(s, TIME_FMT_JS, &stm) == end) {
adam@1632 2980 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adam@1632 2981 r->seconds = mktime(&stm);
adam@1632 2982 r->microseconds = 0;
adam@1632 2983 return r;
adamc@439 2984 }
adamc@439 2985 else
adamc@439 2986 return NULL;
adamc@439 2987 }
adamc@436 2988 }
adamc@436 2989
adam@1352 2990 uw_Basis_time *uw_Basis_stringToTimef(uw_context ctx, const char *fmt, uw_Basis_string s) {
adam@1352 2991 char *end = strchr(s, 0);
adam@1374 2992 struct tm stm = {};
adam@1392 2993 stm.tm_isdst = -1;
adam@1352 2994
adam@1352 2995 if (strptime(s, fmt, &stm) == end) {
adam@1352 2996 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adam@1365 2997 r->seconds = mktime(&stm);
adam@1365 2998 r->microseconds = 0;
adam@1352 2999 return r;
adam@1352 3000 }
adam@1352 3001 else
adam@1352 3002 return NULL;
adam@1352 3003 }
adam@1352 3004
adamc@311 3005 uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) {
adamc@292 3006 char *endptr;
adamc@311 3007 uw_Basis_int n = strtoll(s, &endptr, 10);
adamc@292 3008
adamc@292 3009 if (*s != '\0' && *endptr == '\0')
adamc@292 3010 return n;
adamc@292 3011 else
adam@1444 3012 uw_error(ctx, FATAL, "Can't parse int: %s", uw_Basis_htmlifyString(ctx, s));
adamc@292 3013 }
adamc@293 3014
adamc@682 3015 #include <errno.h>
adamc@682 3016
adamc@678 3017 uw_Basis_channel uw_Basis_stringToChannel_error(uw_context ctx, uw_Basis_string s) {
adamc@682 3018 unsigned long long n;
adamc@682 3019
adamc@682 3020 if (sscanf(s, "%llu", &n) < 1)
adam@1444 3021 uw_error(ctx, FATAL, "Can't parse channel: %s", uw_Basis_htmlifyString(ctx, s));
adamc@682 3022 else {
adamc@682 3023 uw_Basis_channel ch = {n >> 32, n & ((1ull << 32) - 1)};
adamc@682 3024 return ch;
adamc@682 3025 }
adamc@682 3026 }
adamc@682 3027
adamc@682 3028 uw_Basis_client uw_Basis_stringToClient_error(uw_context ctx, uw_Basis_string s) {
adamc@678 3029 char *endptr;
adamc@682 3030 unsigned long n = strtoul(s, &endptr, 10);
adamc@678 3031
adamc@678 3032 if (*s != '\0' && *endptr == '\0')
adamc@678 3033 return n;
adamc@678 3034 else
adam@1444 3035 uw_error(ctx, FATAL, "Can't parse client: %s", uw_Basis_htmlifyString(ctx, s));
adamc@678 3036 }
adamc@678 3037
adamc@311 3038 uw_Basis_float uw_Basis_stringToFloat_error(uw_context ctx, uw_Basis_string s) {
adamc@293 3039 char *endptr;
adamc@311 3040 uw_Basis_float n = strtod(s, &endptr);
adamc@293 3041
adamc@293 3042 if (*s != '\0' && *endptr == '\0')
adamc@293 3043 return n;
adamc@293 3044 else
adam@1444 3045 uw_error(ctx, FATAL, "Can't parse float: %s", uw_Basis_htmlifyString(ctx, s));
adamc@293 3046 }
adamc@293 3047
adamc@821 3048 uw_Basis_char uw_Basis_stringToChar_error(uw_context ctx, uw_Basis_string s) {
adamc@821 3049 if (s[0] == 0)
adamc@821 3050 return 0;
adamc@821 3051 else if (s[1] != 0)
adam@1444 3052 uw_error(ctx, FATAL, "Can't parse char: %s", uw_Basis_htmlifyString(ctx, s));
adamc@821 3053 else
adamc@821 3054 return s[0];
adamc@821 3055 }
adamc@821 3056
adamc@311 3057 uw_Basis_bool uw_Basis_stringToBool_error(uw_context ctx, uw_Basis_string s) {
adamc@296 3058 if (!strcasecmp(s, "T") || !strcasecmp (s, "True"))
adamc@311 3059 return uw_Basis_True;
adamc@296 3060 else if (!strcasecmp(s, "F") || !strcasecmp (s, "False"))
adamc@311 3061 return uw_Basis_False;
adamc@293 3062 else
adam@1444 3063 uw_error(ctx, FATAL, "Can't parse bool: %s", uw_Basis_htmlifyString(ctx, s));
adamc@293 3064 }
adamc@436 3065
adam@1352 3066 uw_Basis_time uw_Basis_unsqlTime(uw_context ctx, uw_Basis_string s) {
adam@1352 3067 char *dot = strchr(s, '.'), *end = strchr(s, 0);
adam@1352 3068 struct tm stm = {};
adam@1392 3069 stm.tm_isdst = -1;
adam@1352 3070
adam@1352 3071 if (dot) {
adam@1352 3072 *dot = 0;
adam@1352 3073 if (strptime(s, TIME_FMT_PG, &stm)) {
adam@1352 3074 *dot = '.';
adam@1410 3075 char usec[] = "000000";
adam@1410 3076 int len = strlen(dot+1);
adam@1410 3077 memcpy(usec, dot+1, len < 6 ? len : 6);
adam@1410 3078 uw_Basis_time r = { mktime(&stm), atoi(usec) };
adam@1365 3079 return r;
adam@1352 3080 }
adam@1352 3081 else {
adam@1352 3082 *dot = '.';
adam@1444 3083 uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s));
adam@1352 3084 }
adam@1352 3085 }
adam@1352 3086 else {
adam@1352 3087 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adam@1365 3088 uw_Basis_time r = { mktime(&stm) };
adam@1365 3089 return r;
adam@1352 3090 } else if (strptime(s, TIME_FMT, &stm) == end) {
adam@1365 3091 uw_Basis_time r = { mktime(&stm) };
adam@1365 3092 return r;
adam@1352 3093 } else
adam@1444 3094 uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s));
adam@1352 3095 }
adam@1352 3096 }
adam@1352 3097
adamc@436 3098 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) {
adamc@438 3099 char *dot = strchr(s, '.'), *end = strchr(s, 0);
adamc@436 3100 struct tm stm = {};
adam@1392 3101 stm.tm_isdst = -1;
adamc@436 3102
adamc@438 3103 if (dot) {
adamc@438 3104 *dot = 0;
adamc@438 3105 if (strptime(s, TIME_FMT_PG, &stm)) {
adamc@438 3106 *dot = '.';
adam@1365 3107 {
adam@1365 3108 uw_Basis_time r = { mktime(&stm) };
adam@1365 3109 return r;
adam@1365 3110 }
adamc@438 3111 }
adamc@438 3112 else {
adamc@438 3113 *dot = '.';
adam@1444 3114 uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s));
adamc@438 3115 }
adamc@438 3116 }
adamc@438 3117 else {
adam@2072 3118 if (strptime(s, ctx->app->time_format, &stm) == end) {
adam@2072 3119 uw_Basis_time r = { mktime(&stm) };
adam@2072 3120 return r;
adam@2072 3121 } else if (strptime(s, TIME_FMT_PG, &stm) == end) {
adam@1365 3122 uw_Basis_time r = { mktime(&stm) };
adam@1365 3123 return r;
adam@1365 3124 } else if (strptime(s, TIME_FMT, &stm) == end) {
adam@1365 3125 uw_Basis_time r = { mktime(&stm) };
adam@1365 3126 return r;
adam@1632 3127 } else if (strptime(s, TIME_FMT_JS, &stm) == end) {
adam@1632 3128 uw_Basis_time r = { mktime(&stm) };
adam@1632 3129 return r;
adam@1365 3130 } else
adam@1444 3131 uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s));
adamc@438 3132 }
adamc@436 3133 }
adamc@457 3134
adam@1352 3135 uw_Basis_time uw_Basis_stringToTimef_error(uw_context ctx, const char *fmt, uw_Basis_string s) {
adam@1352 3136 char *end = strchr(s, 0);
adamc@939 3137 struct tm stm = {};
adam@1392 3138 stm.tm_isdst = -1;
adamc@939 3139
adam@1365 3140 if (strptime(s, fmt, &stm) == end) {
adam@1365 3141 uw_Basis_time r = { mktime(&stm) };
adam@1365 3142 return r;
adam@1365 3143 } else
adam@1444 3144 uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s));
adamc@939 3145 }
adamc@939 3146
adamc@742 3147 uw_Basis_blob uw_Basis_stringToBlob_error(uw_context ctx, uw_Basis_string s, size_t len) {
adamc@742 3148 char *r = ctx->heap.front;
adamc@742 3149 uw_Basis_blob b = {len, r};
adamc@742 3150
adamc@742 3151 uw_check_heap(ctx, len);
adamc@742 3152
adam@1992 3153 if (s[0] == '\\' && s[1] == 'x') {
adam@1992 3154 s += 2;
adam@1992 3155
adam@1992 3156 while (*s) {
adam@1992 3157 int n;
adam@1992 3158 sscanf(s, "%02x", &n);
adam@1992 3159 *r++ = n;
adam@1992 3160 s += 2;
adam@1992 3161 }
adam@1992 3162 } else {
adam@1992 3163 while (*s) {
adam@1992 3164 if (s[0] == '\\') {
adam@1992 3165 if (s[1] == '\\') {
adam@1992 3166 *r++ = '\\';
adam@1992 3167 s += 2;
adam@1992 3168 } else if (isdigit((int)s[1]) && isdigit((int)s[2]) && isdigit((int)s[3])) {
adam@1992 3169 *r++ = (s[1] - '0') * 8 * 8 + ((s[2] - '0') * 8) + (s[3] - '0');
adam@1992 3170 s += 4;
adam@1992 3171 }
adam@1992 3172 else {
adam@1992 3173 *r++ = '\\';
adam@1992 3174 ++s;
adam@1992 3175 }
adam@1992 3176 } else {
adam@1992 3177 *r++ = s[0];
adamc@742 3178 ++s;
adamc@742 3179 }
adamc@742 3180 }
adamc@742 3181 }
adamc@742 3182
adamc@742 3183 b.size = r - ctx->heap.front;
adamc@742 3184 ctx->heap.front = r;
adam@1992 3185
adamc@742 3186 return b;
adamc@742 3187 }
adamc@742 3188
adam@1402 3189 #define THE_PAST "expires=Sat, 01-Jan-2011 00:00:00 GMT"
adamc@1052 3190
adamc@463 3191 uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) {
adamc@463 3192 int len = strlen(c);
adamc@854 3193 char *p = ctx->outHeaders.start;
adamc@474 3194
adamc@1134 3195 while ((p = strstr(p, "\nSet-Cookie: "))) {
adamc@474 3196 char *p2;
adamc@474 3197 p += 13;
adamc@474 3198 p2 = strchr(p, '=');
adamc@474 3199
adamc@474 3200 if (p2) {
adamc@474 3201 size_t sz = strcspn(p2+1, ";\r\n");
adamc@474 3202
adamc@474 3203 if (!strncasecmp(p, c, p2 - p)) {
adamc@1052 3204 if (sz == 0 && strstr(p2+2, THE_PAST))
adamc@1052 3205 return NULL;
adamc@1052 3206 else {
adamc@1052 3207 char *ret = uw_malloc(ctx, sz + 1);
adamc@1052 3208 memcpy(ret, p2+1, sz);
adamc@1052 3209 ret[sz] = 0;
adamc@1052 3210 return ret;
adamc@1052 3211 }
adamc@474 3212 }
adamc@474 3213 }
adamc@474 3214 }
adamc@463 3215
adamc@1134 3216 if ((p = uw_Basis_requestHeader(ctx, "Cookie"))) {
adamc@1051 3217 char *p2;
adamc@1051 3218
adamc@854 3219 while (1) {
adamc@1051 3220 if (!strncmp(p, c, len) && p[len] == '=') {
adamc@1134 3221 if ((p2 = strchr(p, ';'))) {
adamc@1051 3222 size_t n = p2 - (p + len);
adamc@1051 3223 char *r = uw_malloc(ctx, n);
adamc@1051 3224 memcpy(r, p + 1 + len, n-1);
adamc@1051 3225 r[n-1] = 0;
adamc@1051 3226 return r;
adamc@1051 3227 } else
adamc@1051 3228 return p + 1 + len;
adamc@1134 3229 } else if ((p = strchr(p, ';')))
adamc@854 3230 p += 2;
adamc@463 3231 else
adamc@463 3232 return NULL;
adamc@463 3233 }
adamc@463 3234 }
adamc@792 3235
adamc@792 3236 return NULL;
adamc@457 3237 }
adamc@462 3238
adam@1877 3239 static void set_cookie(uw_context ctx) {
adam@1877 3240 if (ctx->usedSig)
adam@1877 3241 ctx->needsResig = 1;
adam@1877 3242 }
adam@1877 3243
adamc@1050 3244 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 3245 uw_write_header(ctx, "Set-Cookie: ");
adamc@462 3246 uw_write_header(ctx, c);
adamc@462 3247 uw_write_header(ctx, "=");
adamc@462 3248 uw_write_header(ctx, v);
adamc@466 3249 uw_write_header(ctx, "; path=");
adamc@466 3250 uw_write_header(ctx, prefix);
adamc@1050 3251 if (expires) {
adamc@1050 3252 char formatted[30];
adam@1392 3253 struct tm tm = {};
adam@1392 3254 tm.tm_isdst = -1;
adamc@1050 3255
adam@1365 3256 gmtime_r(&expires->seconds, &tm);
adamc@1050 3257
adamc@1050 3258 strftime(formatted, sizeof formatted, "%a, %d-%b-%Y %T GMT", &tm);
adamc@1050 3259
adamc@1050 3260 uw_write_header(ctx, "; expires=");
adamc@1050 3261 uw_write_header(ctx, formatted);
adamc@1050 3262 }
adamc@1050 3263 if (secure)
adamc@1050 3264 uw_write_header(ctx, "; secure");
adamc@462 3265 uw_write_header(ctx, "\r\n");
adam@1877 3266 set_cookie(ctx);
adamc@462 3267
adamc@462 3268 return uw_unit_v;
adamc@462 3269 }
adamc@667 3270
adamc@1050 3271 uw_unit uw_Basis_clear_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c) {
adamc@1050 3272 uw_write_header(ctx, "Set-Cookie: ");
adamc@1050 3273 uw_write_header(ctx, c);
adamc@1050 3274 uw_write_header(ctx, "=; path=");
adamc@1050 3275 uw_write_header(ctx, prefix);
adamc@1052 3276 uw_write_header(ctx, "; " THE_PAST "\r\n");
adam@1877 3277 set_cookie(ctx);
adamc@1050 3278
adamc@1050 3279 return uw_unit_v;
adamc@1050 3280 }
adamc@1050 3281
adamc@1113 3282 size_t uw_deltas_max = SIZE_MAX;
adamc@1113 3283
adamc@682 3284 static delta *allocate_delta(uw_context ctx, unsigned client) {
adamc@682 3285 unsigned i;
adamc@682 3286 delta *d;
adamc@671 3287
adamc@682 3288 for (i = 0; i < ctx->used_deltas; ++i)
adamc@682 3289 if (ctx->deltas[i].client == client)
adamc@682 3290 return &ctx->deltas[i];
adamc@671 3291
adamc@682 3292 if (ctx->used_deltas >= ctx->n_deltas) {
adamc@1113 3293 if (ctx->n_deltas + 1 > uw_deltas_max)
adamc@1113 3294 uw_error(ctx, FATAL, "Exceeded limit on number of deltas");
adamc@1113 3295
adamc@682 3296 ctx->deltas = realloc(ctx->deltas, sizeof(delta) * ++ctx->n_deltas);
adam@1369 3297 uw_buffer_init(uw_messages_max, &ctx->deltas[ctx->n_deltas-1].msgs, 0);
adamc@682 3298 }
adamc@671 3299
adamc@682 3300 d = &ctx->deltas[ctx->used_deltas++];
adamc@682 3301 d->client = client;
adam@1369 3302 uw_buffer_reset(&d->msgs);
adamc@682 3303 return d;
adamc@671 3304 }
adamc@671 3305
adamc@668 3306 uw_Basis_channel uw_Basis_new_channel(uw_context ctx, uw_unit u) {
adamc@682 3307 if (ctx->client == NULL)
adamc@682 3308 uw_error(ctx, FATAL, "Attempt to create channel on request not associated with a persistent connection");
adamc@671 3309
adamc@682 3310 return new_channel(ctx->client);
adamc@668 3311 }
adamc@668 3312
adamc@668 3313 uw_unit uw_Basis_send(uw_context ctx, uw_Basis_channel chn, uw_Basis_string msg) {
adamc@682 3314 delta *d = allocate_delta(ctx, chn.cli);
adamc@682 3315 size_t len;
adamc@682 3316 int preLen;
adamc@682 3317 char pre[INTS_MAX + 2];
adamc@668 3318
adamc@682 3319 len = strlen(msg);
adamc@682 3320
adamc@682 3321 sprintf(pre, "%u\n%n", chn.chn, &preLen);
adamc@682 3322
adam@1369 3323 ctx_uw_buffer_append(ctx, "messages", &d->msgs, pre, preLen);
adam@1369 3324 ctx_uw_buffer_append(ctx, "messages", &d->msgs, msg, len);
adam@1369 3325 ctx_uw_buffer_append(ctx, "messages", &d->msgs, "\n", 1);
adamc@671 3326
adamc@671 3327 return uw_unit_v;
adamc@668 3328 }
adamc@671 3329
adam@1327 3330 int uw_rollback(uw_context ctx, int will_retry) {
adamc@1131 3331 int i;
adamc@1131 3332 cleanup *cl;
adamc@1131 3333
adamc@1131 3334 if (ctx->client)
adamc@1131 3335 release_client(ctx->client);
adamc@1131 3336
adamc@1131 3337 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@1131 3338 cl->func(cl->arg);
adamc@1131 3339
adamc@1131 3340 ctx->cleanup_front = ctx->cleanup;
adamc@1131 3341
adamc@1131 3342 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@1131 3343 if (ctx->transactionals[i].rollback != NULL)
adamc@1131 3344 ctx->transactionals[i].rollback(ctx->transactionals[i].data);
adamc@1131 3345
adamc@1131 3346 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@1131 3347 if (ctx->transactionals[i].free)
adam@1327 3348 ctx->transactionals[i].free(ctx->transactionals[i].data, will_retry);
adamc@1131 3349
adam@1941 3350 if (ctx->app && ctx->transaction_started) {
adam@1941 3351 ctx->transaction_started = 0;
adam@1941 3352 return ctx->app->db_rollback(ctx);
adam@1941 3353 } else
adam@1941 3354 return 0;
adamc@1131 3355 }
adamc@1131 3356
adam@1956 3357 const char uw_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@1956 3358 const char uw_begin_html5[] = "<!DOCTYPE html><html>";
adam@1728 3359
adam@1877 3360 extern int uw_hash_blocksize;
adam@1877 3361
adam@1877 3362 static const char sig_intro[] = "<input type=\"hidden\" name=\"Sig\" value=\"";
adam@1877 3363
adam@1877 3364 static char *find_sig(char *haystack) {
adam@1877 3365 int i;
adam@1877 3366 char *s = strstr(haystack, sig_intro);
adam@1877 3367
adam@1877 3368 if (!s || strlen(haystack) - (s - haystack) - (sizeof sig_intro - 1) < uw_hash_blocksize*2+1)
adam@1877 3369 return NULL;
phurst@1981 3370
adam@1877 3371 s += sizeof sig_intro - 1;
adam@1877 3372
adam@1877 3373 for (i = 0; i < uw_hash_blocksize*2; ++i)
adam@1877 3374 if (!isxdigit((int)s[i]))
adam@1877 3375 return NULL;
adam@1877 3376
adam@1877 3377 if (s[i] != '"')
adam@1877 3378 return NULL;
adam@1877 3379
adam@1877 3380 return s;
adam@1877 3381 }
adam@1877 3382
adam@2033 3383 static pthread_mutex_t message_send_mutex = PTHREAD_MUTEX_INITIALIZER;
adam@2033 3384
adam@1938 3385 int uw_commit(uw_context ctx) {
adamc@1104 3386 int i;
adam@1877 3387 char *sig;
adamc@1104 3388
adamc@1131 3389 if (uw_has_error(ctx)) {
adam@1327 3390 uw_rollback(ctx, 0);
adam@1938 3391 return 0;
adamc@1131 3392 }
adamc@1131 3393
adamc@1104 3394 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@771 3395 if (ctx->transactionals[i].rollback != NULL)
adamc@1131 3396 if (ctx->transactionals[i].commit) {
adamc@1083 3397 ctx->transactionals[i].commit(ctx->transactionals[i].data);
adamc@1131 3398 if (uw_has_error(ctx)) {
adam@1327 3399 uw_rollback(ctx, 0);
adam@1938 3400 return 0;
adamc@1131 3401 }
adamc@1131 3402 }
adamc@771 3403
adam@2033 3404 // Here's an important lock to provide the abstraction that all messages from one transaction are sent as an atomic unit.
adam@2033 3405 if (ctx->used_deltas > 0)
adam@2033 3406 pthread_mutex_lock(&message_send_mutex);
adam@2033 3407
adam@2000 3408 if (ctx->transaction_started) {
adam@2000 3409 int code = ctx->app->db_commit(ctx);
adam@2000 3410
adam@2000 3411 if (code) {
adam@2033 3412 if (ctx->used_deltas > 0)
adam@2033 3413 pthread_mutex_unlock(&message_send_mutex);
adam@2033 3414
adam@2000 3415 if (ctx->client)
adam@2000 3416 release_client(ctx->client);
adam@2000 3417
adam@2000 3418 if (code == -1) {
adam@2000 3419 // This case is for a serialization failure, which is not really an "error."
adam@2000 3420 // The transaction will restart, so we should rollback any transactionals
adam@2000 3421 // that triggered above.
adam@2000 3422
adam@2000 3423 for (i = ctx->used_transactionals-1; i >= 0; --i)
adam@2000 3424 if (ctx->transactionals[i].rollback != NULL)
adam@2000 3425 ctx->transactionals[i].rollback(ctx->transactionals[i].data);
adam@2000 3426
adam@2000 3427 for (i = ctx->used_transactionals-1; i >= 0; --i)
adam@2000 3428 if (ctx->transactionals[i].free)
adam@2000 3429 ctx->transactionals[i].free(ctx->transactionals[i].data, 1);
adam@2000 3430
grrwlf@2030 3431 return 1;
adam@2000 3432 }
adam@2000 3433
adam@2000 3434 for (i = ctx->used_transactionals-1; i >= 0; --i)
adam@2000 3435 if (ctx->transactionals[i].free)
adam@2000 3436 ctx->transactionals[i].free(ctx->transactionals[i].data, 0);
adam@2000 3437
adam@2000 3438 uw_set_error_message(ctx, "Error running SQL COMMIT");
adam@2000 3439 return 0;
adam@2000 3440 }
adam@2000 3441 }
adam@2000 3442
adamc@1104 3443 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@771 3444 if (ctx->transactionals[i].rollback == NULL)
adamc@1131 3445 if (ctx->transactionals[i].commit) {
adamc@1083 3446 ctx->transactionals[i].commit(ctx->transactionals[i].data);
adamc@1131 3447 if (uw_has_error(ctx)) {
adam@2033 3448 if (ctx->used_deltas > 0)
adam@2033 3449 pthread_mutex_unlock(&message_send_mutex);
adam@2033 3450
adam@2033 3451 if (ctx->client)
adam@2033 3452 release_client(ctx->client);
adam@2033 3453
adam@2033 3454 for (i = ctx->used_transactionals-1; i >= 0; --i)
adam@2033 3455 if (ctx->transactionals[i].rollback != NULL)
adam@2033 3456 ctx->transactionals[i].rollback(ctx->transactionals[i].data);
adam@2033 3457
adam@2033 3458 for (i = ctx->used_transactionals-1; i >= 0; --i)
adam@2033 3459 if (ctx->transactionals[i].free)
adam@2033 3460 ctx->transactionals[i].free(ctx->transactionals[i].data, 0);
adam@2000 3461
adam@1938 3462 return 0;
adamc@1131 3463 }
adamc@1131 3464 }
adamc@1131 3465
adamc@682 3466 for (i = 0; i < ctx->used_deltas; ++i) {
adamc@682 3467 delta *d = &ctx->deltas[i];
adamc@682 3468 client *c = find_client(d->client);
adamc@682 3469
adam@1551 3470 assert (c != NULL);
adam@1551 3471 assert(c->mode == USED);
adamc@682 3472
adam@1447 3473 client_send(c, &d->msgs, ctx->script.start, uw_buffer_used(&ctx->script));
adamc@682 3474 }
adamc@682 3475
adam@2033 3476 if (ctx->used_deltas > 0)
adam@2033 3477 pthread_mutex_unlock(&message_send_mutex);
adam@2033 3478
adamc@682 3479 if (ctx->client)
adamc@685 3480 release_client(ctx->client);
adamc@771 3481
adamc@1104 3482 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@1083 3483 if (ctx->transactionals[i].free)
adam@1327 3484 ctx->transactionals[i].free(ctx->transactionals[i].data, 0);
adamc@916 3485
vshabanoff@1726 3486 uw_check(ctx, 1);
vshabanoff@1726 3487 *ctx->page.front = 0;
adam@1413 3488
adam@1956 3489 if (!ctx->returning_indirectly
adam@1956 3490 && (ctx->app->is_html5
adam@1956 3491 ? !strncmp(ctx->page.start, uw_begin_html5, sizeof uw_begin_html5 - 1)
adam@1956 3492 : !strncmp(ctx->page.start, uw_begin_xhtml, sizeof uw_begin_xhtml - 1))) {
adam@1728 3493 char *s;
adam@1728 3494
adam@1728 3495 // Splice script data into appropriate part of page, also adding <head> if needed.
adam@1956 3496 s = ctx->page.start + (ctx->app->is_html5 ? sizeof uw_begin_html5 - 1 : sizeof uw_begin_xhtml - 1);
adam@1728 3497 s = strchr(s, '<');
adam@1728 3498 if (s == NULL) {
adam@1728 3499 // Weird. Document has no tags!
adam@1728 3500
adam@1728 3501 uw_write(ctx, "<head></head><body></body>");
adam@1728 3502 uw_check(ctx, 1);
adam@1728 3503 *ctx->page.front = 0;
adam@1728 3504 } else if (!strncmp(s, "<head>", 6)) {
adam@1728 3505 // <head> is present. Let's add the <script> tags immediately after it.
adam@1728 3506
adam@1729 3507 // Any freeform JavaScript to include?
adam@1729 3508 if (uw_buffer_used(&ctx->script) > 0) {
adam@1729 3509 size_t lenH = strlen(ctx->script_header), len = uw_buffer_used(&ctx->script);
adam@1729 3510 size_t lenP = lenH + 40 + len;
adam@1729 3511 char *start = s + 6, *oldPage = ctx->page.start;
adam@1729 3512
adam@1729 3513 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenP);
adam@1729 3514 start += ctx->page.start - oldPage;
adam@1729 3515 memmove(start + lenP, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1);
adam@1729 3516 ctx->page.front += lenP;
adam@1729 3517 memcpy(start, ctx->script_header, lenH);
adam@1729 3518 memcpy(start + lenH, "<script type=\"text/javascript\">", 31);
adam@1729 3519 memcpy(start + lenH + 31, ctx->script.start, len);
adam@1729 3520 memcpy(start + lenH + 31 + len, "</script>", 9);
adam@1729 3521 } else {
adam@1729 3522 size_t lenH = strlen(ctx->script_header);
adam@1729 3523 char *start = s + 6, *oldPage = ctx->page.start;
adam@1729 3524
adam@1729 3525 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenH);
adam@1729 3526 start += ctx->page.start - oldPage;
adam@1729 3527 memmove(start + lenH, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1);
adam@1729 3528 ctx->page.front += lenH;
adam@1729 3529 memcpy(start, ctx->script_header, lenH);
adam@1729 3530 }
adam@1728 3531 } else {
adam@1728 3532 // No <head>. At this point, add it, with <script> tags inside.
adam@1728 3533
adam@1729 3534 if (uw_buffer_used(&ctx->script) > 0) {
adam@1729 3535 size_t lenH = strlen(ctx->script_header), len = uw_buffer_used(&ctx->script);
adam@1729 3536 size_t lenP = lenH + 53 + len;
adam@1729 3537 char *start = s, *oldPage = ctx->page.start;
adam@1729 3538
adam@1729 3539 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenP);
adam@1729 3540 start += ctx->page.start - oldPage;
adam@1729 3541 memmove(start + lenP, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1);
adam@1729 3542 ctx->page.front += lenP;
adam@1729 3543 memcpy(start, "<head>", 6);
adam@1729 3544 memcpy(start + 6, ctx->script_header, lenH);
adam@1729 3545 memcpy(start + 6 + lenH, "<script type=\"text/javascript\">", 31);
adam@1729 3546 memcpy(start + 6 + lenH + 31, ctx->script.start, len);
adam@1729 3547 memcpy(start + 6 + lenH + 31 + len, "</script></head>", 16);
adam@1729 3548 } else {
adam@1729 3549 size_t lenH = strlen(ctx->script_header);
adam@1729 3550 size_t lenP = lenH + 13;
adam@1729 3551 char *start = s, *oldPage = ctx->page.start;
adam@1729 3552
adam@1729 3553 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenP);
adam@1729 3554 start += ctx->page.start - oldPage;
adam@1729 3555 memmove(start + lenP, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1);
adam@1729 3556 ctx->page.front += lenP;
adam@1729 3557 memcpy(start, "<head>", 6);
adam@1729 3558 memcpy(start + 6, ctx->script_header, lenH);
adam@1729 3559 memcpy(start + 6 + lenH, "</head>", 7);
adam@1729 3560 }
adamc@916 3561 }
adamc@916 3562 }
adam@1877 3563
adam@1877 3564 if (ctx->needsResig) {
adam@1877 3565 sig = find_sig(ctx->page.start);
adam@1877 3566 if (sig) {
adam@1877 3567 char *realsig = ctx->app->cookie_sig(ctx);
adam@1877 3568
adam@1877 3569 do {
adam@1877 3570 memcpy(sig, realsig, 2*uw_hash_blocksize);
adam@1877 3571 sig = find_sig(sig);
adam@1877 3572 } while (sig);
adam@1877 3573 }
adam@1877 3574 }
adam@1938 3575
adam@1938 3576 return 0;
adamc@671 3577 }
adamc@671 3578
adamc@683 3579
adamc@1113 3580 size_t uw_transactionals_max = SIZE_MAX;
adamc@1113 3581
adam@2001 3582 int uw_register_transactional(uw_context ctx, void *data, uw_callback commit, uw_callback rollback,
adam@1327 3583 uw_callback_with_retry free) {
adamc@771 3584 if (ctx->used_transactionals >= ctx->n_transactionals) {
adamc@1113 3585 if (ctx->used_transactionals+1 > uw_transactionals_max)
adam@2001 3586 // Exceeded limit on number of transactionals.
adam@2001 3587 return -1;
adamc@1084 3588 ctx->transactionals = realloc(ctx->transactionals, sizeof(transactional) * (ctx->used_transactionals+1));
adamc@771 3589 ++ctx->n_transactionals;
adamc@771 3590 }
adamc@771 3591
adamc@771 3592 ctx->transactionals[ctx->used_transactionals].data = data;
adamc@771 3593 ctx->transactionals[ctx->used_transactionals].commit = commit;
adamc@771 3594 ctx->transactionals[ctx->used_transactionals].rollback = rollback;
adamc@771 3595 ctx->transactionals[ctx->used_transactionals++].free = free;
adam@2001 3596
adam@2001 3597 return 0;
adamc@771 3598 }
adamc@771 3599
adamc@683 3600
adamc@683 3601 // "Garbage collection"
adamc@683 3602
adamc@1121 3603 void uw_do_expunge(uw_context ctx, uw_Basis_client cli, void *data);
adamc@1121 3604 void uw_post_expunge(uw_context ctx, void *data);
adamc@1121 3605
adamc@1121 3606 static failure_kind uw_expunge(uw_context ctx, uw_Basis_client cli, void *data) {
adamc@683 3607 int r = setjmp(ctx->jmp_buf);
adamc@683 3608
adamc@1121 3609 if (r == 0)
adamc@1121 3610 uw_do_expunge(ctx, cli, data);
adamc@1121 3611 else
adamc@1121 3612 ctx->app->db_rollback(ctx);
adamc@1121 3613
adamc@1121 3614 uw_post_expunge(ctx, data);
adamc@683 3615
adamc@683 3616 return r;
adamc@683 3617 }
adamc@683 3618
adamc@683 3619 void uw_prune_clients(uw_context ctx) {
adamc@683 3620 client *c, *next, *prev = NULL;
adamc@683 3621 time_t cutoff;
adamc@683 3622
adamc@1094 3623 cutoff = time(NULL) - ctx->app->timeout;
adamc@683 3624
adamc@683 3625 pthread_mutex_lock(&clients_mutex);
adam@1707 3626 pruning_thread = pthread_self();
adam@1707 3627 pruning_thread_initialized = 1;
adamc@683 3628
adamc@683 3629 for (c = clients_used; c; c = next) {
adamc@683 3630 next = c->next;
adamc@683 3631 pthread_mutex_lock(&c->lock);
adamc@685 3632 if (c->last_contact < cutoff && c->refcount == 0) {
adamc@683 3633 failure_kind fk = UNLIMITED_RETRY;
adamc@683 3634 if (prev)
adamc@683 3635 prev->next = next;
adamc@683 3636 else
adamc@683 3637 clients_used = next;
adamc@683 3638 while (fk == UNLIMITED_RETRY) {
adam@1941 3639 uw_reset(ctx);
adamc@1121 3640 fk = uw_expunge(ctx, c->id, c->data);
adamc@1121 3641 if (fk == UNLIMITED_RETRY)
adamc@684 3642 printf("Unlimited retry during expunge: %s\n", uw_error_message(ctx));
adamc@683 3643 }
adamc@684 3644 if (fk == SUCCESS)
adamc@684 3645 free_client(c);
adamc@1121 3646 else
adamc@856 3647 fprintf(stderr, "Expunge blocked by error: %s\n", uw_error_message(ctx));
adamc@683 3648 }
adamc@683 3649 else
adamc@683 3650 prev = c;
adamc@683 3651 pthread_mutex_unlock(&c->lock);
adamc@683 3652 }
adamc@683 3653
adamc@683 3654 pthread_mutex_unlock(&clients_mutex);
adamc@683 3655 }
adamc@687 3656
adamc@687 3657 failure_kind uw_initialize(uw_context ctx) {
adamc@687 3658 int r = setjmp(ctx->jmp_buf);
adamc@687 3659
adamc@687 3660 if (r == 0) {
adam@1936 3661 uw_ensure_transaction(ctx);
adamc@1094 3662 ctx->app->initializer(ctx);
adamc@1094 3663 if (ctx->app->db_commit(ctx))
adamc@687 3664 uw_error(ctx, FATAL, "Error running SQL COMMIT");
adamc@687 3665 }
adamc@687 3666
adamc@687 3667 return r;
adamc@687 3668 }
adamc@717 3669
adamc@1065 3670 static int url_bad(uw_Basis_string s) {
adamc@1065 3671 for (; *s; ++s)
adamc@1153 3672 if (!isgraph((int)*s))
adamc@1065 3673 return 1;
adamc@1065 3674
adamc@1065 3675 return 0;
adamc@1065 3676 }
adamc@1065 3677
adamc@717 3678 uw_Basis_string uw_Basis_bless(uw_context ctx, uw_Basis_string s) {
adamc@1065 3679 if (url_bad(s))
adamc@1065 3680 uw_error(ctx, FATAL, "Invalid URL %s", uw_Basis_htmlifyString(ctx, s));
adamc@1094 3681 if (ctx->app->check_url(s))
adamc@770 3682 return s;
adamc@770 3683 else
adamc@770 3684 uw_error(ctx, FATAL, "Disallowed URL %s", uw_Basis_htmlifyString(ctx, s));
adamc@717 3685 }
adamc@734 3686
adamc@770 3687 uw_Basis_string uw_Basis_checkUrl(uw_context ctx, uw_Basis_string s) {
adamc@1065 3688 if (url_bad(s))
adamc@1065 3689 return NULL;
adamc@1094 3690 if (ctx->app->check_url(s))
adamc@770 3691 return s;
adamc@770 3692 else
adamc@770 3693 return NULL;
adamc@770 3694 }
adamc@770 3695
adam@1465 3696 static int mime_format(const char *s) {
adamc@770 3697 for (; *s; ++s)
grrwlf@2024 3698 if (!isalnum((int)*s) && *s != '/' && *s != '-' && *s != '.' && *s != '+')
adamc@770 3699 return 0;
adamc@770 3700
adamc@770 3701 return 1;
adamc@770 3702 }
adamc@770 3703
adamc@741 3704 uw_Basis_string uw_Basis_blessMime(uw_context ctx, uw_Basis_string s) {
adamc@770 3705 if (!mime_format(s))
adamc@770 3706 uw_error(ctx, FATAL, "MIME type \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s));
adamc@770 3707
adamc@1094 3708 if (ctx->app->check_mime(s))
adamc@770 3709 return s;
adamc@770 3710 else
adamc@770 3711 uw_error(ctx, FATAL, "Disallowed MIME type %s", uw_Basis_htmlifyString(ctx, s));
adamc@770 3712 }
adamc@770 3713
adamc@770 3714 uw_Basis_string uw_Basis_checkMime(uw_context ctx, uw_Basis_string s) {
adamc@770 3715 if (!mime_format(s))
adamc@770 3716 return NULL;
adamc@770 3717
adamc@1094 3718 if (ctx->app->check_mime(s))
adamc@770 3719 return s;
adamc@770 3720 else
adamc@770 3721 return NULL;
adamc@741 3722 }
adamc@741 3723
adam@1465 3724 uw_Basis_string uw_Basis_blessRequestHeader(uw_context ctx, uw_Basis_string s) {
adam@1465 3725 if (!mime_format(s))
adam@1465 3726 uw_error(ctx, FATAL, "Request header \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s));
adam@1465 3727
adam@1465 3728 if (ctx->app->check_requestHeader(s))
adam@1465 3729 return s;
adam@1465 3730 else
adam@1465 3731 uw_error(ctx, FATAL, "Disallowed request header %s", uw_Basis_htmlifyString(ctx, s));
adam@1465 3732 }
adam@1465 3733
adam@1465 3734 uw_Basis_string uw_Basis_checkRequestHeader(uw_context ctx, uw_Basis_string s) {
adam@1465 3735 if (!mime_format(s))
adam@1465 3736 return NULL;
adam@1465 3737
adam@1465 3738 if (ctx->app->check_requestHeader(s))
adam@1465 3739 return s;
adam@1465 3740 else
adam@1465 3741 return NULL;
adam@1465 3742 }
adam@1465 3743
adam@1465 3744 uw_Basis_string uw_Basis_blessResponseHeader(uw_context ctx, uw_Basis_string s) {
adam@1465 3745 if (!mime_format(s))
adam@1465 3746 uw_error(ctx, FATAL, "Response header \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s));
adam@1465 3747
adam@1465 3748 if (ctx->app->check_responseHeader(s))
adam@1465 3749 return s;
adam@1465 3750 else
adam@1465 3751 uw_error(ctx, FATAL, "Disallowed response header %s", uw_Basis_htmlifyString(ctx, s));
adam@1465 3752 }
adam@1465 3753
adam@1799 3754 static int envVar_format(const char *s) {
adam@1799 3755 for (; *s; ++s)
adam@1799 3756 if (!isalnum((int)*s) && *s != '_' && *s != '.')
adam@1799 3757 return 0;
adam@1799 3758
adam@1799 3759 return 1;
adam@1799 3760 }
adam@1799 3761
adam@1465 3762 uw_Basis_string uw_Basis_checkResponseHeader(uw_context ctx, uw_Basis_string s) {
adam@1799 3763 if (!envVar_format(s))
adam@1465 3764 return NULL;
adam@1465 3765
adam@1465 3766 if (ctx->app->check_responseHeader(s))
adam@1465 3767 return s;
adam@1465 3768 else
adam@1465 3769 return NULL;
adam@1465 3770 }
adam@1465 3771
adam@1799 3772 uw_Basis_string uw_Basis_blessEnvVar(uw_context ctx, uw_Basis_string s) {
adam@1799 3773 if (!envVar_format(s))
adam@1799 3774 uw_error(ctx, FATAL, "Environment variable \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s));
adam@1799 3775
adam@1799 3776 if (ctx->app->check_envVar(s))
adam@1799 3777 return s;
adam@1799 3778 else
adam@1799 3779 uw_error(ctx, FATAL, "Disallowed environment variable %s", uw_Basis_htmlifyString(ctx, s));
adam@1799 3780 }
adam@1799 3781
adam@1799 3782 uw_Basis_string uw_Basis_checkEnvVar(uw_context ctx, uw_Basis_string s) {
adam@1799 3783 if (!mime_format(s))
adam@1799 3784 return NULL;
adam@1799 3785
adam@1799 3786 if (ctx->app->check_envVar(s))
adam@1799 3787 return s;
adam@1799 3788 else
adam@1799 3789 return NULL;
adam@1799 3790 }
adam@1799 3791
adam@1465 3792 uw_Basis_string uw_Basis_getHeader(uw_context ctx, uw_Basis_string name) {
adam@1465 3793 return uw_Basis_requestHeader(ctx, name);
adam@1465 3794 }
adam@1465 3795
adam@1465 3796 static int mime_value_format(const char *s) {
adam@1465 3797 for (; *s; ++s)
adam@1465 3798 if (*s == '\r' || *s == '\n')
adam@1465 3799 return 0;
adam@1465 3800
adam@1465 3801 return 1;
adam@1465 3802 }
adam@1465 3803
adam@1465 3804 uw_unit uw_Basis_setHeader(uw_context ctx, uw_Basis_string name, uw_Basis_string value) {
adam@1465 3805 if (!mime_value_format(value))
adam@1465 3806 uw_error(ctx, FATAL, "Invalid value for HTTP response header");
adam@1465 3807
adam@1465 3808 uw_write_header(ctx, name);
adam@1465 3809 uw_write_header(ctx, ": ");
adam@1465 3810 uw_write_header(ctx, value);
adam@1465 3811 uw_write_header(ctx, "\r\n");
adam@1465 3812
adam@1465 3813 return uw_unit_v;
adam@1465 3814 }
adam@1465 3815
adam@1799 3816 uw_Basis_string uw_Basis_getenv(uw_context ctx, uw_Basis_string name) {
adam@1993 3817 if (ctx->get_env)
adam@1993 3818 return ctx->get_env(ctx->get_env_data, name);
adam@1993 3819 else
adam@1994 3820 return getenv(name);
adam@1799 3821 }
adam@1799 3822
adamc@734 3823 uw_Basis_string uw_unnull(uw_Basis_string s) {
adamc@734 3824 return s ? s : "";
adamc@734 3825 }
adamc@734 3826
adamc@734 3827 uw_Basis_string uw_Basis_makeSigString(uw_context ctx, uw_Basis_string sig) {
adamc@734 3828 uw_Basis_string r = uw_malloc(ctx, 2 * uw_hash_blocksize + 1);
adamc@734 3829 int i;
phurst@1981 3830
adamc@734 3831 for (i = 0; i < uw_hash_blocksize; ++i)
adamc@734 3832 sprintf(&r[2*i], "%.02X", ((unsigned char *)sig)[i]);
adamc@734 3833
adamc@734 3834 return r;
adamc@734 3835 }
adamc@734 3836
adam@1512 3837 /* This bit of crafty code is intended to prevent GCC from performing
adam@1512 3838 * optimizations that would enable timing attacks. See:
adam@1512 3839 * http://www.impredicative.com/pipermail/ur/2011-July/000659.html
adam@1512 3840 */
adam@1512 3841 int uw_streq(uw_Basis_string s1, uw_Basis_string s2) {
adam@1512 3842 int i, x = 0, len1 = strlen(s1);
adam@1512 3843
adam@1512 3844 if (len1 != strlen(s2)) return 0;
adam@1512 3845
adam@1512 3846 for (i = 0; i < len1; ++i) {
adam@1512 3847 __asm__ __volatile__ ("");
adam@1512 3848 x |= s1[i] ^ s2[i];
adam@1512 3849 }
adam@1512 3850
adam@1512 3851 return x == 0;
adam@1512 3852 }
adam@1512 3853
adamc@734 3854 uw_Basis_string uw_Basis_sigString(uw_context ctx, uw_unit u) {
adam@1877 3855 ctx->usedSig = 1;
adamc@1094 3856 return ctx->app->cookie_sig(ctx);
adamc@734 3857 }
adamc@737 3858
adamc@737 3859 uw_Basis_string uw_Basis_fileName(uw_context ctx, uw_Basis_file f) {
adamc@737 3860 return f.name;
adamc@737 3861 }
adamc@737 3862
adamc@740 3863 uw_Basis_string uw_Basis_fileMimeType(uw_context ctx, uw_Basis_file f) {
adamc@740 3864 return f.type;
adamc@740 3865 }
adamc@740 3866
adamc@745 3867 uw_Basis_int uw_Basis_blobSize(uw_context ctx, uw_Basis_blob b) {
adamc@745 3868 return b.size;
adamc@745 3869 }
adamc@745 3870
adamc@1119 3871 uw_Basis_blob uw_Basis_textBlob(uw_context ctx, uw_Basis_string s) {
adamc@1119 3872 uw_Basis_blob b = {strlen(s), s};
adamc@1119 3873
adamc@1119 3874 return b;
adamc@1119 3875 }
adamc@1119 3876
adamc@737 3877 uw_Basis_blob uw_Basis_fileData(uw_context ctx, uw_Basis_file f) {
adamc@737 3878 return f.data;
adamc@737 3879 }
adamc@741 3880
adam@1347 3881 uw_Basis_string uw_Basis_postType(uw_context ctx, uw_Basis_postBody pb) {
adam@1347 3882 return pb.type;
adam@1347 3883 }
adam@1347 3884
adam@1347 3885 uw_Basis_string uw_Basis_postData(uw_context ctx, uw_Basis_postBody pb) {
adam@1347 3886 return pb.data;
adam@1347 3887 }
adam@1347 3888
adam@1471 3889 static char *old_headers(uw_context ctx) {
adam@1471 3890 if (uw_buffer_used(&ctx->outHeaders) == 0)
adam@1471 3891 return NULL;
adam@1471 3892 else {
adam@1471 3893 char *s = strchr(ctx->outHeaders.start, '\n');
adam@1471 3894
adam@1471 3895 if (s == NULL || strncasecmp(s+1, "Content-type: ", 14))
adam@1471 3896 return NULL;
adam@1471 3897 else {
adam@1471 3898 s = strchr(s+15, '\n');
adam@1471 3899 if (s == NULL)
adam@1471 3900 return NULL;
adam@1471 3901 else
adam@1471 3902 return uw_strdup(ctx, s+1);
adam@1471 3903 }
adam@1471 3904 }
adam@1471 3905 }
adam@1471 3906
adamc@741 3907 __attribute__((noreturn)) void uw_return_blob(uw_context ctx, uw_Basis_blob b, uw_Basis_string mimeType) {
adamc@741 3908 cleanup *cl;
adamc@741 3909 int len;
adam@1471 3910 char *oldh;
adamc@741 3911
adam@1451 3912 if (!ctx->allowed_to_return_indirectly)
adam@1451 3913 uw_error(ctx, FATAL, "Tried to return a blob from an RPC");
adam@1451 3914
adamc@1065 3915 ctx->returning_indirectly = 1;
adam@1471 3916 oldh = old_headers(ctx);
adam@1369 3917 uw_buffer_reset(&ctx->outHeaders);
adam@1369 3918 uw_buffer_reset(&ctx->page);
adamc@741 3919
adamc@856 3920 uw_write_header(ctx, on_success);
adamc@856 3921 uw_write_header(ctx, "Content-Type: ");
adamc@741 3922 uw_write_header(ctx, mimeType);
adam@1934 3923 uw_write_header(ctx, "\r\nContent-length: ");
adam@1369 3924 ctx_uw_buffer_check(ctx, "headers", &ctx->outHeaders, INTS_MAX);
adamc@1134 3925 sprintf(ctx->outHeaders.front, "%lu%n", (unsigned long)b.size, &len);
adamc@741 3926 ctx->outHeaders.front += len;
adamc@1066 3927 uw_write_header(ctx, "\r\n");
adam@1471 3928 if (oldh) uw_write_header(ctx, oldh);
adamc@741 3929
adam@1369 3930 ctx_uw_buffer_append(ctx, "page", &ctx->page, b.data, b.size);
adamc@741 3931
adamc@741 3932 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@741 3933 cl->func(cl->arg);
adamc@741 3934
adamc@741 3935 ctx->cleanup_front = ctx->cleanup;
adamc@741 3936
adamc@1065 3937 longjmp(ctx->jmp_buf, RETURN_INDIRECTLY);
adamc@1065 3938 }
adamc@1065 3939
adam@2046 3940 void uw_replace_page(uw_context ctx, const char *data, size_t size) {
adam@2046 3941 uw_buffer_reset(&ctx->page);
adam@2046 3942 ctx_uw_buffer_append(ctx, "page", &ctx->page, data, size);
adam@2046 3943 }
adam@2046 3944
adam@1932 3945 __attribute__((noreturn)) void uw_return_blob_from_page(uw_context ctx, uw_Basis_string mimeType) {
adam@1932 3946 cleanup *cl;
adam@1932 3947 int len;
adam@1932 3948 char *oldh;
adam@1932 3949
adam@1932 3950 if (!ctx->allowed_to_return_indirectly)
adam@1932 3951 uw_error(ctx, FATAL, "Tried to return a blob from an RPC");
adam@1932 3952
adam@1932 3953 ctx->returning_indirectly = 1;
adam@1932 3954 oldh = old_headers(ctx);
adam@1932 3955 uw_buffer_reset(&ctx->outHeaders);
adam@1932 3956
adam@1932 3957 uw_write_header(ctx, on_success);
adam@1932 3958 uw_write_header(ctx, "Content-Type: ");
adam@1932 3959 uw_write_header(ctx, mimeType);
adam@1934 3960 uw_write_header(ctx, "\r\nContent-length: ");
adam@1932 3961 ctx_uw_buffer_check(ctx, "headers", &ctx->outHeaders, INTS_MAX);
adam@1932 3962 sprintf(ctx->outHeaders.front, "%lu%n", (unsigned long)uw_buffer_used(&ctx->page), &len);
adam@1932 3963 ctx->outHeaders.front += len;
adam@1932 3964 uw_write_header(ctx, "\r\n");
adam@1932 3965 if (oldh) uw_write_header(ctx, oldh);
adam@1932 3966
adam@1932 3967 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adam@1932 3968 cl->func(cl->arg);
adam@1932 3969
adam@1932 3970 ctx->cleanup_front = ctx->cleanup;
adam@1932 3971
adam@1932 3972 longjmp(ctx->jmp_buf, RETURN_INDIRECTLY);
adam@1932 3973 }
adam@1932 3974
adamc@1065 3975 __attribute__((noreturn)) void uw_redirect(uw_context ctx, uw_Basis_string url) {
adamc@1065 3976 cleanup *cl;
adamc@1066 3977 char *s;
adam@1471 3978 char *oldh;
adamc@1065 3979
adam@1451 3980 if (!ctx->allowed_to_return_indirectly)
adam@1451 3981 uw_error(ctx, FATAL, "Tried to redirect from an RPC");
adam@1451 3982
adamc@1065 3983 ctx->returning_indirectly = 1;
adam@1471 3984 oldh = old_headers(ctx);
adam@1369 3985 uw_buffer_reset(&ctx->page);
adam@1369 3986 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->outHeaders)+1);
adam@1369 3987 memcpy(ctx->page.start, ctx->outHeaders.start, uw_buffer_used(&ctx->outHeaders));
adam@1369 3988 ctx->page.start[uw_buffer_used(&ctx->outHeaders)] = 0;
adam@1369 3989 uw_buffer_reset(&ctx->outHeaders);
adamc@1065 3990
adam@1409 3991 if (on_success[0])
adam@1409 3992 uw_write_header(ctx, on_redirect);
adam@1409 3993 else
adam@1409 3994 uw_write_header(ctx, "Status: 303 See Other\r\n");
adam@1409 3995 s = on_success[0] ? strchr(ctx->page.start, '\n') : ctx->page.start;
adamc@1066 3996 if (s) {
adamc@1066 3997 char *s2;
adam@1409 3998 if (s[0] == '\n') ++s;
adam@1409 3999 for (; (s2 = strchr(s, '\n')); s = s2+1) {
adamc@1066 4000 *s2 = 0;
adamc@1066 4001 if (!strncmp(s, "Set-Cookie: ", 12)) {
adamc@1066 4002 uw_write_header(ctx, s);
adamc@1066 4003 uw_write_header(ctx, "\n");
adamc@1066 4004 }
adamc@1066 4005 }
adamc@1066 4006 }
adamc@1066 4007
adamc@1065 4008 uw_write_header(ctx, "Location: ");
adamc@1065 4009 uw_write_header(ctx, url);
adamc@1065 4010 uw_write_header(ctx, "\r\n\r\n");
adam@1471 4011 if (oldh) uw_write_header(ctx, oldh);
adamc@1065 4012
adamc@1065 4013 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@1065 4014 cl->func(cl->arg);
adamc@1065 4015
adamc@1065 4016 ctx->cleanup_front = ctx->cleanup;
adamc@1065 4017
adamc@1065 4018 longjmp(ctx->jmp_buf, RETURN_INDIRECTLY);
adamc@741 4019 }
adamc@874 4020
adamc@874 4021 uw_Basis_string uw_Basis_unAs(uw_context ctx, uw_Basis_string s) {
adam@1467 4022 uw_Basis_string ret = uw_malloc(ctx, strlen(s) + 1), r = ret;
adamc@874 4023
adamc@874 4024 for (; *s; ++s) {
adamc@874 4025 if (s[0] == '\'') {
adamc@874 4026 *r++ = '\'';
adamc@874 4027 for (++s; *s; ++s) {
adamc@874 4028 if (s[0] == '\'') {
adamc@874 4029 *r++ = '\'';
adamc@874 4030 break;
adamc@874 4031 } else if (s[0] == '\\') {
adam@1467 4032 *r++ = '\\';
adam@1467 4033 *r++ = s[1];
adam@1467 4034 ++s;
adamc@874 4035 } else
adamc@874 4036 *r++ = s[0];
adamc@874 4037 }
adamc@874 4038 if (*s == 0) break;
adam@1467 4039 } else if (s[0] == 'T' && s[1] == '_' && s[2] == 'T' && s[3] == '.')
adam@1467 4040 s += 3;
adamc@874 4041 else
adamc@874 4042 *r++ = s[0];
adamc@874 4043 }
adamc@874 4044
adam@1467 4045 *r = 0;
adam@1467 4046 return ret;
adamc@874 4047 }
adamc@874 4048
adamc@922 4049 uw_Basis_string uw_Basis_mstrcat(uw_context ctx, ...) {
adamc@922 4050 va_list ap;
adamc@922 4051 size_t len = 1;
adamc@922 4052 char *s, *r, *s2;
phurst@1981 4053
adamc@922 4054 va_start(ap, ctx);
adamc@922 4055 for (s = va_arg(ap, char*); s; s = va_arg(ap, char*))
adamc@922 4056 len += strlen(s);
adamc@922 4057 va_end(ap);
adamc@922 4058
adamc@922 4059 r = uw_malloc(ctx, len);
adamc@922 4060 va_start(ap, ctx);
adamc@922 4061 for (s2 = r, s = va_arg(ap, char*); s; s = va_arg(ap, char*))
adamc@922 4062 while (*s)
adamc@922 4063 *s2++ = *s++;
adamc@922 4064 va_end(ap);
adamc@922 4065 *s2 = 0;
adamc@922 4066
adamc@922 4067 return r;
adamc@922 4068 }
adamc@1006 4069
adam@1365 4070 const uw_Basis_time uw_Basis_minTime = {};
adamc@1050 4071
adamc@1006 4072 uw_Basis_time uw_Basis_now(uw_context ctx) {
adam@1365 4073 uw_Basis_time r = { time(NULL) };
adam@1365 4074 return r;
adamc@1006 4075 }
adamc@1061 4076
adam@1369 4077 uw_Basis_time uw_Basis_addSeconds(uw_context ctx, uw_Basis_time tm, uw_Basis_int n) {
adam@1369 4078 tm.seconds += n;
adam@1365 4079 return tm;
adamc@1138 4080 }
adamc@1138 4081
adam@1514 4082 uw_Basis_int uw_Basis_diffInSeconds(uw_context ctx, uw_Basis_time tm1, uw_Basis_time tm2) {
adam@1514 4083 return difftime(tm2.seconds, tm1.seconds);
adam@1514 4084 }
adam@1514 4085
adam@1685 4086 uw_Basis_int uw_Basis_toMilliseconds(uw_context ctx, uw_Basis_time tm) {
adam@1685 4087 return tm.seconds * 1000 + tm.microseconds / 1000;
adam@1685 4088 }
adam@1685 4089
adam@2187 4090 uw_Basis_time uw_Basis_fromMilliseconds(uw_context ctx, uw_Basis_int n) {
adam@2187 4091 uw_Basis_time tm = {n / 1000, n % 1000 * 1000};
adam@2187 4092 return tm;
adam@2187 4093 }
adam@2187 4094
adam@1685 4095 uw_Basis_int uw_Basis_diffInMilliseconds(uw_context ctx, uw_Basis_time tm1, uw_Basis_time tm2) {
adam@1685 4096 return uw_Basis_toMilliseconds(ctx, tm2) - uw_Basis_toMilliseconds(ctx, tm1);
adam@1685 4097 }
adam@1685 4098
adam@1514 4099 uw_Basis_int uw_Basis_toSeconds(uw_context ctx, uw_Basis_time tm) {
adam@1514 4100 return tm.seconds;
adam@1514 4101 }
adam@1514 4102
phurst@1971 4103 uw_Basis_time uw_Basis_fromDatetime(uw_context ctx, uw_Basis_int year, uw_Basis_int month, uw_Basis_int day, uw_Basis_int hour, uw_Basis_int minute, uw_Basis_int second) {
phurst@1971 4104 struct tm tm = { .tm_year = year - 1900, .tm_mon = month, .tm_mday = day,
adam@2015 4105 .tm_hour = hour, .tm_min = minute, .tm_sec = second,
adam@2015 4106 .tm_isdst = -1 };
phurst@1984 4107 uw_Basis_time r = { timelocal(&tm) };
phurst@1971 4108 return r;
phurst@1971 4109 }
phurst@1971 4110
phurst@1971 4111 uw_Basis_int uw_Basis_datetimeYear(uw_context ctx, uw_Basis_time time) {
phurst@1971 4112 struct tm tm;
phurst@1982 4113 localtime_r(&time.seconds, &tm);
phurst@1971 4114 return tm.tm_year + 1900;
phurst@1971 4115 }
phurst@1971 4116
phurst@1971 4117 uw_Basis_int uw_Basis_datetimeMonth(uw_context ctx, uw_Basis_time time) {
phurst@1971 4118 struct tm tm;
phurst@1982 4119 localtime_r(&time.seconds, &tm);
phurst@1971 4120 return tm.tm_mon;
phurst@1971 4121 }
phurst@1971 4122
phurst@1971 4123 uw_Basis_int uw_Basis_datetimeDay(uw_context ctx, uw_Basis_time time) {
phurst@1971 4124 struct tm tm;
phurst@1982 4125 localtime_r(&time.seconds, &tm);
phurst@1971 4126 return tm.tm_mday;
phurst@1971 4127 }
phurst@1971 4128
phurst@1971 4129 uw_Basis_int uw_Basis_datetimeHour(uw_context ctx, uw_Basis_time time) {
phurst@1971 4130 struct tm tm;
phurst@1982 4131 localtime_r(&time.seconds, &tm);
phurst@1971 4132 return tm.tm_hour;
phurst@1971 4133 }
phurst@1971 4134
phurst@1971 4135 uw_Basis_int uw_Basis_datetimeMinute(uw_context ctx, uw_Basis_time time) {
phurst@1971 4136 struct tm tm;
phurst@1982 4137 localtime_r(&time.seconds, &tm);
phurst@1971 4138 return tm.tm_min;
phurst@1971 4139 }
phurst@1971 4140
phurst@1971 4141 uw_Basis_int uw_Basis_datetimeSecond(uw_context ctx, uw_Basis_time time) {
phurst@1971 4142 struct tm tm;
phurst@1982 4143 localtime_r(&time.seconds, &tm);
phurst@1971 4144 return tm.tm_sec;
phurst@1971 4145 }
phurst@1971 4146
phurst@1973 4147 uw_Basis_int uw_Basis_datetimeDayOfWeek(uw_context ctx, uw_Basis_time time) {
phurst@1973 4148 struct tm tm;
phurst@1982 4149 localtime_r(&time.seconds, &tm);
phurst@1973 4150 return tm.tm_wday;
phurst@1973 4151 }
phurst@1971 4152
phurst@1971 4153
adamc@1061 4154 void *uw_get_global(uw_context ctx, char *name) {
adamc@1061 4155 int i;
adamc@1061 4156
adamc@1061 4157 for (i = 0; i < ctx->n_globals; ++i)
adamc@1061 4158 if (!strcmp(name, ctx->globals[i].name))
adamc@1061 4159 return ctx->globals[i].data;
adamc@1061 4160
adamc@1061 4161 return NULL;
adamc@1061 4162 }
adamc@1061 4163
adamc@1113 4164 size_t uw_globals_max = SIZE_MAX;
adamc@1113 4165
adamc@1061 4166 void uw_set_global(uw_context ctx, char *name, void *data, void (*free)(void*)) {
adamc@1061 4167 int i;
adamc@1061 4168
adamc@1061 4169 for (i = 0; i < ctx->n_globals; ++i)
adamc@1061 4170 if (!strcmp(name, ctx->globals[i].name)) {
adamc@1089 4171 if (ctx->globals[i].free)
adamc@1061 4172 ctx->globals[i].free(ctx->globals[i].data);
adamc@1061 4173 ctx->globals[i].data = data;
adamc@1061 4174 ctx->globals[i].free = free;
adamc@1061 4175 return;
adamc@1061 4176 }
adamc@1089 4177
adamc@1113 4178 if (ctx->n_globals+1 > uw_globals_max)
adamc@1113 4179 uw_error(ctx, FATAL, "Exceeded limit on number of globals");
adamc@1113 4180
adamc@1061 4181 ++ctx->n_globals;
adamc@1061 4182 ctx->globals = realloc(ctx->globals, ctx->n_globals * sizeof(global));
adamc@1061 4183 ctx->globals[ctx->n_globals-1].name = name;
adamc@1061 4184 ctx->globals[ctx->n_globals-1].data = data;
adamc@1061 4185 ctx->globals[ctx->n_globals-1].free = free;
adamc@1061 4186 }
adamc@1061 4187
adamc@1061 4188 uw_Basis_bool uw_Basis_isalnum(uw_context ctx, uw_Basis_char c) {
adamc@1153 4189 return !!isalnum((int)c);
adamc@1061 4190 }
adamc@1061 4191
adamc@1061 4192 uw_Basis_bool uw_Basis_isalpha(uw_context ctx, uw_Basis_char c) {
adamc@1153 4193 return !!isalpha((int)c);
adamc@1061 4194 }
adamc@1061 4195
adamc@1061 4196 uw_Basis_bool uw_Basis_isblank(uw_context ctx, uw_Basis_char c) {
adamc@1153 4197 return !!isblank((int)c);
adamc@1061 4198 }
adamc@1061 4199
adamc@1061 4200 uw_Basis_bool uw_Basis_iscntrl(uw_context ctx, uw_Basis_char c) {
adamc@1153 4201 return !!iscntrl((int)c);
adamc@1061 4202 }
adamc@1061 4203
adamc@1061 4204 uw_Basis_bool uw_Basis_isdigit(uw_context ctx, uw_Basis_char c) {
adamc@1153 4205 return !!isdigit((int)c);
adamc@1061 4206 }
adamc@1061 4207
adamc@1061 4208 uw_Basis_bool uw_Basis_isgraph(uw_context ctx, uw_Basis_char c) {
adamc@1153 4209 return !!isgraph((int)c);
adamc@1061 4210 }
adamc@1061 4211
adamc@1061 4212 uw_Basis_bool uw_Basis_islower(uw_context ctx, uw_Basis_char c) {
adamc@1153 4213 return !!islower((int)c);
adamc@1061 4214 }
adamc@1061 4215
adamc@1061 4216 uw_Basis_bool uw_Basis_isprint(uw_context ctx, uw_Basis_char c) {
adamc@1153 4217 return !!isprint((int)c);
adamc@1061 4218 }
adamc@1061 4219
adamc@1061 4220 uw_Basis_bool uw_Basis_ispunct(uw_context ctx, uw_Basis_char c) {
adamc@1153 4221 return !!ispunct((int)c);
adamc@1061 4222 }
adamc@1061 4223
adamc@1061 4224 uw_Basis_bool uw_Basis_isspace(uw_context ctx, uw_Basis_char c) {
adamc@1153 4225 return !!isspace((int)c);
adamc@1061 4226 }
adamc@1063 4227
adamc@1061 4228 uw_Basis_bool uw_Basis_isupper(uw_context ctx, uw_Basis_char c) {
adamc@1153 4229 return !!isupper((int)c);
adamc@1061 4230 }
adamc@1061 4231
adamc@1061 4232 uw_Basis_bool uw_Basis_isxdigit(uw_context ctx, uw_Basis_char c) {
adamc@1153 4233 return !!isxdigit((int)c);
adamc@1061 4234 }
adamc@1061 4235
adamc@1061 4236 uw_Basis_char uw_Basis_tolower(uw_context ctx, uw_Basis_char c) {
adamc@1153 4237 return tolower((int)c);
adamc@1061 4238 }
adamc@1061 4239
adamc@1061 4240 uw_Basis_char uw_Basis_toupper(uw_context ctx, uw_Basis_char c) {
adamc@1153 4241 return toupper((int)c);
adamc@1061 4242 }
adamc@1066 4243
adamc@1128 4244 uw_Basis_int uw_Basis_ord(uw_context ctx, uw_Basis_char c) {
adamc@1128 4245 return (unsigned char)c;
adamc@1128 4246 }
adamc@1128 4247
adamc@1128 4248 uw_Basis_char uw_Basis_chr(uw_context ctx, uw_Basis_int n) {
adamc@1128 4249 return n;
adamc@1128 4250 }
adamc@1128 4251
adamc@1066 4252 uw_Basis_string uw_Basis_currentUrl(uw_context ctx) {
adamc@1066 4253 return ctx->current_url;
adamc@1066 4254 }
adamc@1066 4255
adamc@1066 4256 void uw_set_currentUrl(uw_context ctx, char *s) {
adamc@1066 4257 ctx->current_url = s;
adamc@1066 4258 }
adamc@1114 4259
adamc@1114 4260 void uw_set_deadline(uw_context ctx, int n) {
adamc@1114 4261 ctx->deadline = n;
adamc@1114 4262 }
adamc@1114 4263
adamc@1114 4264 void uw_check_deadline(uw_context ctx) {
adamc@1114 4265 if (uw_time > ctx->deadline)
adamc@1114 4266 uw_error(ctx, FATAL, "Maximum running time exceeded");
adamc@1114 4267 }
adamc@1115 4268
adamc@1115 4269 size_t uw_database_max = SIZE_MAX;
adamc@1120 4270
adam@1389 4271 uw_Basis_int uw_Basis_naughtyDebug(uw_context ctx, uw_Basis_string s) {
adam@2143 4272 if (ctx->loggers->log_debug)
adam@2143 4273 ctx->loggers->log_debug(ctx->loggers->logger_data, "%s\n", s);
adam@2143 4274 else
adam@2143 4275 fprintf(stderr, "%s\n", s);
adam@1389 4276 return 0;
adam@1389 4277 }
adam@1389 4278
adamc@1120 4279 uw_Basis_unit uw_Basis_debug(uw_context ctx, uw_Basis_string s) {
grrwlf@1997 4280 if (ctx->loggers->log_debug)
grrwlf@1997 4281 ctx->loggers->log_debug(ctx->loggers->logger_data, "%s\n", s);
adam@1334 4282 else
adam@1334 4283 fprintf(stderr, "%s\n", s);
adamc@1120 4284 return uw_unit_v;
adamc@1120 4285 }
adamc@1250 4286
adamc@1250 4287 uw_Basis_int uw_Basis_rand(uw_context ctx) {
adam@2104 4288 int r = my_rand();
adam@2104 4289
adam@2104 4290 if (r >= 0)
adam@2104 4291 return r;
adam@1486 4292 else
adam@1486 4293 uw_error(ctx, FATAL, "Random number generation failed");
adamc@1250 4294 }
adam@1347 4295
adam@1347 4296 void uw_noPostBody(uw_context ctx) {
adam@1347 4297 ctx->hasPostBody = 0;
adam@1347 4298 }
adam@1347 4299
adam@1347 4300 void uw_postBody(uw_context ctx, uw_Basis_postBody pb) {
adam@1347 4301 ctx->hasPostBody = 1;
adam@1347 4302 ctx->postBody = pb;
adam@1347 4303 }
adam@1347 4304
adam@1347 4305 int uw_hasPostBody(uw_context ctx) {
adam@1347 4306 return ctx->hasPostBody;
adam@1347 4307 }
adam@1347 4308
adam@1386 4309 void uw_isPost(uw_context ctx) {
adam@1386 4310 ctx->isPost = 1;
adam@1386 4311 }
adam@1386 4312
adam@1386 4313 uw_Basis_bool uw_Basis_currentUrlHasPost(uw_context ctx) {
adam@1386 4314 return ctx->isPost;
adam@1386 4315 }
adam@1386 4316
adam@1485 4317 uw_Basis_bool uw_Basis_currentUrlHasQueryString(uw_context ctx) {
adam@1485 4318 return ctx->queryString != NULL && ctx->queryString[0] != 0;
adam@1485 4319 }
adam@1485 4320
adam@1370 4321 void uw_setQueryString(uw_context ctx, uw_Basis_string s) {
adam@1370 4322 ctx->queryString = s;
adam@1370 4323 }
adam@1370 4324
adam@1370 4325 uw_Basis_string uw_queryString(uw_context ctx) {
adam@1370 4326 return ctx->queryString;
adam@1370 4327 }
adam@1370 4328
adam@1347 4329 uw_Basis_postBody uw_getPostBody(uw_context ctx) {
adam@1347 4330 if (ctx->hasPostBody)
adam@1347 4331 return ctx->postBody;
adam@1347 4332 else
adam@1347 4333 uw_error(ctx, FATAL, "Asked for POST body when none exists");
adam@1347 4334 }
adam@1349 4335
adam@1349 4336 failure_kind uw_runCallback(uw_context ctx, void (*callback)(uw_context)) {
adam@1349 4337 int r = setjmp(ctx->jmp_buf);
adam@1349 4338
adam@1349 4339 if (r == 0) {
adam@1936 4340 uw_ensure_transaction(ctx);
adam@1418 4341
adam@1349 4342 callback(ctx);
adam@1349 4343 }
adam@1349 4344
adam@1349 4345 return r;
adam@1349 4346 }
adam@1360 4347
adam@1360 4348 uw_Basis_string uw_Basis_crypt(uw_context ctx, uw_Basis_string key, uw_Basis_string salt) {
adam@1397 4349 char buf[14];
adam@1397 4350 return uw_strdup(ctx, DES_fcrypt(key, salt, buf));
adam@1360 4351 }
adam@1365 4352
adam@1365 4353 uw_Basis_bool uw_Basis_eq_time(uw_context ctx, uw_Basis_time t1, uw_Basis_time t2) {
adam@1365 4354 return !!(t1.seconds == t2.seconds && t1.microseconds == t2.microseconds);
adam@1365 4355 }
adam@1365 4356
adam@1365 4357 uw_Basis_bool uw_Basis_lt_time(uw_context ctx, uw_Basis_time t1, uw_Basis_time t2) {
adam@2034 4358 return !!(t1.seconds < t2.seconds || (t1.seconds == t2.seconds && t1.microseconds < t2.microseconds));
adam@1365 4359 }
adam@1365 4360
adam@1365 4361 uw_Basis_bool uw_Basis_le_time(uw_context ctx, uw_Basis_time t1, uw_Basis_time t2) {
adam@1365 4362 return !!(uw_Basis_eq_time(ctx, t1, t2) || uw_Basis_lt_time(ctx, t1, t2));
adam@1365 4363 }
adam@1365 4364
adam@1372 4365 uw_Basis_time *uw_Basis_readUtc(uw_context ctx, uw_Basis_string s) {
adam@1374 4366 struct tm stm = {};
adam@1375 4367 char *end = strchr(s, 0);
adam@1449 4368 stm.tm_isdst = -1;
adam@1375 4369
adam@1632 4370 if (strptime(s, TIME_FMT_PG, &stm) == end || strptime(s, TIME_FMT, &stm) == end || strptime(s, TIME_FMT_JS, &stm) == end) {
adam@1373 4371 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adam@1375 4372
kkallio@1448 4373 r->seconds = timegm(&stm);
adam@1373 4374 r->microseconds = 0;
adam@1373 4375
adam@1373 4376 return r;
adam@1374 4377 }
adam@1374 4378 else
adam@1373 4379 return NULL;
adam@1372 4380 }
adam@1407 4381
adam@1407 4382 failure_kind uw_begin_onError(uw_context ctx, char *msg) {
adam@1407 4383 int r = setjmp(ctx->jmp_buf);
adam@1407 4384
adam@1407 4385 if (ctx->app->on_error) {
adam@1407 4386 if (r == 0) {
adam@1936 4387 uw_ensure_transaction(ctx);
adam@1407 4388
adam@1409 4389 uw_buffer_reset(&ctx->outHeaders);
adam@1409 4390 if (on_success[0])
adam@1409 4391 uw_write_header(ctx, "HTTP/1.1 ");
adam@1409 4392 else
adam@1409 4393 uw_write_header(ctx, "Status: ");
adam@1409 4394 uw_write_header(ctx, "500 Internal Server Error\r\n");
adam@1919 4395 uw_write_header(ctx, "Content-type: text/html\r\n");
adam@1956 4396 uw_write(ctx, ctx->app->is_html5 ? uw_begin_html5 : uw_begin_xhtml);
adam@1407 4397 ctx->app->on_error(ctx, msg);
adam@1407 4398 uw_write(ctx, "</html>");
adam@1407 4399 }
adam@1407 4400
adam@1407 4401 return r;
adam@1407 4402 } else
adam@1407 4403 uw_error(ctx, FATAL, "Tried to run nonexistent onError handler");
adam@1407 4404 }
adam@1451 4405
adam@1451 4406 void uw_mayReturnIndirectly(uw_context ctx) {
adam@1451 4407 ctx->allowed_to_return_indirectly = 1;
adam@1451 4408 }
adam@1483 4409
adam@1556 4410 uw_Basis_string uw_Basis_fresh(uw_context ctx) {
adam@1703 4411 int len;
adam@1703 4412 char *r;
adam@1703 4413
adam@1703 4414 uw_check_heap(ctx, 2+INTS_MAX);
adam@1703 4415 r = ctx->heap.front;
adam@1703 4416 sprintf(r, "uw%u%n", ctx->nextId++, &len);
adam@1703 4417 ctx->heap.front += len+1;
adam@1703 4418 return r;
adam@1556 4419 }
adam@1571 4420
adam@1571 4421 uw_Basis_float uw_Basis_floatFromInt(uw_context ctx, uw_Basis_int n) {
adam@1571 4422 return n;
adam@1571 4423 }
adam@1571 4424
adam@1571 4425 uw_Basis_int uw_Basis_ceil(uw_context ctx, uw_Basis_float n) {
adam@1571 4426 return ceil(n);
adam@1571 4427 }
adam@1571 4428
adam@1571 4429 uw_Basis_int uw_Basis_trunc(uw_context ctx, uw_Basis_float n) {
adam@1571 4430 return trunc(n);
adam@1571 4431 }
adam@1571 4432
adam@1571 4433 uw_Basis_int uw_Basis_round(uw_context ctx, uw_Basis_float n) {
adam@1571 4434 return round(n);
adam@1571 4435 }
adam@1755 4436
adam@1755 4437 uw_Basis_string uw_Basis_atom(uw_context ctx, uw_Basis_string s) {
adam@1755 4438 char *p;
adam@1755 4439
adam@1755 4440 for (p = s; *p; ++p) {
adam@1755 4441 char c = *p;
adam@1801 4442 if (!isalnum((int)c) && c != '+' && c != '-' && c != '.' && c != '%' && c != '#')
adam@1755 4443 uw_error(ctx, FATAL, "Disallowed character in CSS atom");
adam@1755 4444 }
adam@1755 4445
adam@1755 4446 return s;
adam@1755 4447 }
adam@1755 4448
adam@1755 4449 uw_Basis_string uw_Basis_css_url(uw_context ctx, uw_Basis_string s) {
adam@1755 4450 char *p;
adam@1755 4451
adam@1755 4452 for (p = s; *p; ++p) {
adam@1755 4453 char c = *p;
adam@1801 4454 if (!isalnum((int)c) && c != ':' && c != '/' && c != '.' && c != '_' && c != '+'
adam@1755 4455 && c != '-' && c != '%' && c != '?' && c != '&' && c != '=' && c != '#')
adam@1755 4456 uw_error(ctx, FATAL, "Disallowed character in CSS URL");
adam@1755 4457 }
adam@1755 4458
adam@1755 4459 return s;
adam@1755 4460 }
adam@1755 4461
adam@1755 4462 uw_Basis_string uw_Basis_property(uw_context ctx, uw_Basis_string s) {
adam@1755 4463 char *p;
adam@1755 4464
adam@1755 4465 if (!*s)
adam@1755 4466 uw_error(ctx, FATAL, "Empty CSS property");
adam@1755 4467
adam@1801 4468 if (!islower((int)s[0]) && s[0] != '_')
adam@1755 4469 uw_error(ctx, FATAL, "Bad initial character in CSS property");
adam@1755 4470
adam@1755 4471 for (p = s; *p; ++p) {
adam@1755 4472 char c = *p;
adam@1801 4473 if (!islower((int)c) && !isdigit((int)c) && c != '_' && c != '-')
adam@1755 4474 uw_error(ctx, FATAL, "Disallowed character in CSS property");
adam@1755 4475 }
adam@1755 4476
adam@1755 4477 return s;
adam@1755 4478 }
adam@1787 4479
adam@1787 4480 uw_Basis_string uw_Basis_fieldName(uw_context ctx, uw_Basis_postField f) {
adam@1787 4481 return f.name;
adam@1787 4482 }
adam@1787 4483
adam@1787 4484 uw_Basis_string uw_Basis_fieldValue(uw_context ctx, uw_Basis_postField f) {
adam@1787 4485 return f.value;
adam@1787 4486 }
adam@1787 4487
adam@1787 4488 uw_Basis_string uw_Basis_remainingFields(uw_context ctx, uw_Basis_postField f) {
adam@1787 4489 return f.remaining;
adam@1787 4490 }
adam@1787 4491
adam@1787 4492 uw_Basis_postField *uw_Basis_firstFormField(uw_context ctx, uw_Basis_string s) {
adam@1788 4493 char *unurl;
adam@1787 4494 uw_Basis_postField *f;
adam@1787 4495
adam@1788 4496 if (!ctx->hasPostBody)
adam@1788 4497 uw_error(ctx, FATAL, "firstFormField called when there is no POST body");
adam@1788 4498
adam@1788 4499 if (s < ctx->postBody.data || s >= ctx->postBody.data + ctx->postBody.len)
adam@1787 4500 return NULL;
adam@1787 4501
adam@1787 4502 f = uw_malloc(ctx, sizeof(uw_Basis_postField));
adam@1788 4503 unurl = s;
adam@1787 4504 f->name = uw_Basis_unurlifyString(ctx, &unurl);
adam@1788 4505 s = strchr(s, 0);
adam@1788 4506 if (!s)
adam@1788 4507 uw_error(ctx, FATAL, "firstFormField: Missing null terminator");
adam@1788 4508 ++s;
adam@1788 4509 unurl = s;
adam@1788 4510 f->value = uw_Basis_unurlifyString(ctx, &unurl);
adam@1788 4511 s = strchr(s, 0);
adam@1788 4512 if (!s)
adam@1788 4513 uw_error(ctx, FATAL, "firstFormField: Missing null terminator");
adam@1788 4514 f->remaining = s+1;
adam@1787 4515
adam@1787 4516 return f;
adam@1787 4517 }
adam@2008 4518
adam@2008 4519 uw_Basis_string uw_Basis_blessData(uw_context ctx, uw_Basis_string s) {
adam@2008 4520 char *p = s;
adam@2008 4521
adam@2008 4522 for (; *p; ++p)
adam@2008 4523 if (!isalnum(*p) && *p != '-' && *p != '_')
adam@2008 4524 uw_error(ctx, FATAL, "Illegal HTML5 data-* attribute: %s", s);
adam@2008 4525
adam@2008 4526 return s;
adam@2008 4527 }
adam@2073 4528
adam@2073 4529 int uw_remoteSock(uw_context ctx) {
adam@2073 4530 return ctx->remoteSock;
adam@2073 4531 }
adam@2073 4532
adam@2073 4533 void uw_set_remoteSock(uw_context ctx, int sock) {
adam@2073 4534 ctx->remoteSock = sock;
adam@2073 4535 }
ziv@2234 4536
ziv@2234 4537
ziv@2234 4538 // Sqlcache
ziv@2234 4539
ziv@2279 4540 void uw_Sqlcache_freeValue(uw_Sqlcache_Value *value) {
ziv@2234 4541 if (value) {
ziv@2234 4542 free(value->result);
ziv@2234 4543 free(value->output);
ziv@2234 4544 free(value);
ziv@2234 4545 }
ziv@2234 4546 }
ziv@2234 4547
ziv@2279 4548 void uw_Sqlcache_freeEntry(uw_Sqlcache_Entry* entry) {
ziv@2279 4549 if (entry) {
ziv@2279 4550 free(entry->key);
ziv@2279 4551 uw_Sqlcache_freeValue(entry->value);
ziv@2279 4552 free(entry);
ziv@2279 4553 }
ziv@2279 4554 }
ziv@2279 4555
ziv@2279 4556 // TODO: pick a number.
ziv@2279 4557 unsigned int uw_Sqlcache_maxSize = 1234567890;
ziv@2279 4558
ziv@2279 4559 void uw_Sqlcache_delete(uw_Sqlcache_Cache *cache, uw_Sqlcache_Entry *entry) {
ziv@2279 4560 HASH_DEL(cache->table, entry);
ziv@2279 4561 uw_Sqlcache_freeEntry(entry);
ziv@2279 4562 }
ziv@2279 4563
ziv@2279 4564 uw_Sqlcache_Entry *uw_Sqlcache_find(uw_Sqlcache_Cache *cache, char *key, size_t len, int bump) {
ziv@2279 4565 uw_Sqlcache_Entry *entry = NULL;
ziv@2279 4566 HASH_FIND(hh, cache->table, key, len, entry);
ziv@2279 4567 if (entry && bump) {
ziv@2279 4568 // Bump for LRU purposes.
ziv@2279 4569 HASH_DEL(cache->table, entry);
ziv@2279 4570 // Important that we use [entry->key], because [key] might be ephemeral.
ziv@2279 4571 HASH_ADD_KEYPTR(hh, cache->table, entry->key, len, entry);
ziv@2279 4572 }
ziv@2279 4573 return entry;
ziv@2279 4574 }
ziv@2279 4575
ziv@2279 4576 void uw_Sqlcache_add(uw_Sqlcache_Cache *cache, uw_Sqlcache_Entry *entry, size_t len) {
ziv@2279 4577 HASH_ADD_KEYPTR(hh, cache->table, entry->key, len, entry);
ziv@2279 4578 if (HASH_COUNT(cache->table) > uw_Sqlcache_maxSize) {
ziv@2279 4579 // Deletes the first element of the cache.
ziv@2279 4580 uw_Sqlcache_delete(cache, cache->table);
ziv@2279 4581 }
ziv@2279 4582 }
ziv@2279 4583
ziv@2279 4584 unsigned long uw_Sqlcache_getTimeNow(uw_Sqlcache_Cache *cache) {
ziv@2279 4585 return ++cache->timeNow;
ziv@2279 4586 }
ziv@2279 4587
ziv@2279 4588 unsigned long uw_Sqlcache_timeMax(unsigned long x, unsigned long y) {
ziv@2279 4589 return x > y ? x : y;
ziv@2279 4590 }
ziv@2279 4591
ziv@2279 4592 char uw_Sqlcache_keySep = '_';
ziv@2279 4593
ziv@2279 4594 char *uw_Sqlcache_allocKeyBuffer(char **keys, int numKeys) {
ziv@2279 4595 size_t len = 0;
ziv@2279 4596 while (numKeys-- > 0) {
ziv@2279 4597 char* k = keys[numKeys];
ziv@2279 4598 if (!k) {
ziv@2279 4599 // Can only happen when flushihg, in which case we don't need anything past the null key.
ziv@2279 4600 break;
ziv@2234 4601 }
ziv@2279 4602 // Leave room for separator.
ziv@2279 4603 len += 1 + strlen(k);
ziv@2234 4604 }
ziv@2279 4605 char *buf = malloc(len+1);
ziv@2279 4606 // If nothing is copied into the buffer, it should look like it has length 0.
ziv@2279 4607 buf[0] = 0;
ziv@2279 4608 return buf;
ziv@2279 4609 }
ziv@2279 4610
ziv@2279 4611 char *uw_Sqlcache_keyCopy(char *buf, char *key) {
ziv@2279 4612 *buf++ = uw_Sqlcache_keySep;
ziv@2279 4613 return stpcpy(buf, key);
ziv@2279 4614 }
ziv@2279 4615
ziv@2279 4616 // The NUL-terminated prefix of [key] below always looks something like "_k1_k2_k3..._kn".
ziv@2279 4617 // TODO: strlen(key) = buf - key?
ziv@2279 4618
ziv@2279 4619 uw_Sqlcache_Value *uw_Sqlcache_check(uw_Sqlcache_Cache *cache, char **keys, int numKeys) {
ziv@2279 4620 char *key = uw_Sqlcache_allocKeyBuffer(keys, numKeys);
ziv@2279 4621 char *buf = key;
ziv@2279 4622 time_t timeInvalid = cache->timeInvalid;
ziv@2279 4623 uw_Sqlcache_Entry *entry;
ziv@2279 4624 while (numKeys-- > 0) {
ziv@2279 4625 buf = uw_Sqlcache_keyCopy(buf, keys[numKeys]);
ziv@2279 4626 size_t len = buf - key;
ziv@2279 4627 entry = uw_Sqlcache_find(cache, key, len, 1);
ziv@2279 4628 if (!entry) {
ziv@2279 4629 free(key);
ziv@2279 4630 return NULL;
ziv@2279 4631 }
ziv@2279 4632 timeInvalid = uw_Sqlcache_timeMax(timeInvalid, entry->timeInvalid);
ziv@2234 4633 }
ziv@2279 4634 free(key);
ziv@2279 4635 uw_Sqlcache_Value *value = entry->value;
ziv@2279 4636 return value && value->timeValid > timeInvalid ? value : NULL;
ziv@2279 4637 }
ziv@2279 4638
ziv@2279 4639 void uw_Sqlcache_store(uw_Sqlcache_Cache *cache, char **keys, int numKeys, uw_Sqlcache_Value *value) {
ziv@2279 4640 char *key = uw_Sqlcache_allocKeyBuffer(keys, numKeys);
ziv@2279 4641 char *buf = key;
ziv@2279 4642 time_t timeNow = uw_Sqlcache_getTimeNow(cache);
ziv@2279 4643 uw_Sqlcache_Entry *entry;
ziv@2279 4644 while (numKeys-- > 0) {
ziv@2279 4645 buf = uw_Sqlcache_keyCopy(buf, keys[numKeys]);
ziv@2279 4646 size_t len = buf - key;
ziv@2279 4647 entry = uw_Sqlcache_find(cache, key, len, 1);
ziv@2279 4648 if (!entry) {
ziv@2279 4649 entry = malloc(sizeof(uw_Sqlcache_Entry));
ziv@2279 4650 entry->key = strdup(key);
ziv@2279 4651 entry->value = NULL;
ziv@2279 4652 entry->timeInvalid = 0; // ASK: is this okay?
ziv@2279 4653 uw_Sqlcache_add(cache, entry, len);
ziv@2234 4654 }
ziv@2234 4655 }
ziv@2279 4656 free(key);
ziv@2279 4657 uw_Sqlcache_freeValue(entry->value);
ziv@2279 4658 entry->value = value;
ziv@2279 4659 entry->value->timeValid = timeNow;
ziv@2279 4660 }
ziv@2279 4661
ziv@2279 4662 void uw_Sqlcache_flush(uw_Sqlcache_Cache *cache, char **keys, int numKeys) {
ziv@2279 4663 char *key = uw_Sqlcache_allocKeyBuffer(keys, numKeys);
ziv@2279 4664 char *buf = key;
ziv@2279 4665 time_t timeNow = uw_Sqlcache_getTimeNow(cache);
ziv@2279 4666 uw_Sqlcache_Entry *entry;
ziv@2279 4667 while (numKeys-- > 0) {
ziv@2279 4668 char *k = keys[numKeys];
ziv@2279 4669 if (!k) {
ziv@2279 4670 if (entry) {
ziv@2279 4671 entry->timeInvalid = timeNow;
ziv@2234 4672 } else {
ziv@2279 4673 // Haven't found an entry yet, so the first key was null.
ziv@2279 4674 cache->timeInvalid = timeNow;
ziv@2234 4675 }
ziv@2279 4676 free(key);
ziv@2279 4677 return;
ziv@2234 4678 }
ziv@2279 4679 buf = uw_Sqlcache_keyCopy(buf, k);
ziv@2279 4680 size_t len = buf - key;
ziv@2279 4681 entry = uw_Sqlcache_find(cache, key, len, 0);
ziv@2279 4682 if (!entry) {
ziv@2279 4683 free(key);
ziv@2279 4684 return;
ziv@2279 4685 }
ziv@2234 4686 }
ziv@2279 4687 free(key);
ziv@2279 4688 // All the keys were non-null and the relevant entry is present, so we delete it.
ziv@2279 4689 uw_Sqlcache_delete(cache, entry);
ziv@2279 4690 }