annotate src/c/urweb.c @ 1933:8e6e3d9cea22

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