annotate src/c/urweb.c @ 2258:b1ba35ce2613

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