annotate src/c/urweb.c @ 2282:19b233bb3176

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