annotate src/c/urweb.c @ 2235:0aae15c2a05a

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