annotate src/c/urweb.c @ 2229:54884b28b6c6

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