annotate src/c/urweb.c @ 2104:7755f00a4fc3

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