annotate src/c/urweb.c @ 1934:420d129c4174

HTTP: avoid duplicate Content-length
author Adam Chlipala <adam@chlipala.net>
date Thu, 12 Dec 2013 10:24:38 -0500
parents 8e6e3d9cea22
children 6745eafff617
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
adam@1934 1350 int uw_has_contentLength(uw_context ctx) {
adam@1934 1351 return strstr(ctx->outHeaders.start, "Content-length: ") != NULL;
adam@1934 1352 }
adam@1934 1353
adamc@1110 1354 void uw_clear_headers(uw_context ctx) {
adam@1369 1355 uw_buffer_reset(&ctx->outHeaders);
adamc@1110 1356 }
adamc@1110 1357
adam@1932 1358 void uw_Basis_clear_page(uw_context ctx) {
adam@1932 1359 uw_buffer_reset(&ctx->page);
adam@1932 1360 }
adam@1932 1361
adamc@562 1362 static void uw_check_script(uw_context ctx, size_t extra) {
adam@1369 1363 ctx_uw_buffer_check(ctx, "script", &ctx->script, extra);
adamc@562 1364 }
adamc@562 1365
adamc@562 1366 void uw_write_script(uw_context ctx, uw_Basis_string s) {
adamc@562 1367 int len = strlen(s);
adamc@562 1368
adamc@562 1369 uw_check_script(ctx, len + 1);
adamc@666 1370 strcpy(ctx->script.front, s);
adamc@666 1371 ctx->script.front += len;
adamc@562 1372 }
adamc@562 1373
adam@1446 1374 const char *uw_get_real_script(uw_context ctx) {
adam@1671 1375 if (strstr(ctx->outHeaders.start, "Set-Cookie: ")) {
adam@1671 1376 uw_write_script(ctx, "sig=\"");
adam@1671 1377 uw_write_script(ctx, ctx->app->cookie_sig(ctx));
adam@1671 1378 uw_write_script(ctx, "\";");
adam@1671 1379 }
adam@1671 1380
adam@1446 1381 return ctx->script.start;
adam@1446 1382 }
adam@1446 1383
adamc@695 1384 uw_Basis_string uw_Basis_maybe_onload(uw_context ctx, uw_Basis_string s) {
adamc@695 1385 if (s[0] == 0)
adamc@695 1386 return "";
adamc@695 1387 else {
adamc@695 1388 char *r = uw_malloc(ctx, 11 + strlen(s));
adamc@695 1389 sprintf(r, " onload='%s'", s);
adamc@695 1390 return r;
adamc@695 1391 }
adamc@695 1392 }
adamc@695 1393
adamc@1042 1394 uw_Basis_string uw_Basis_maybe_onunload(uw_context ctx, uw_Basis_string s) {
adamc@1042 1395 if (ctx->script_header[0] == 0)
adamc@1042 1396 return "";
adamc@1042 1397 else {
vshabanoff@1763 1398 char *r = uw_malloc(ctx, 37 + strlen(s));
vshabanoff@1763 1399 sprintf(r, " onunload='unloading=true;%s;unload()'", s);
adamc@1042 1400 return r;
adamc@1042 1401 }
adamc@1042 1402 }
adamc@1042 1403
adamc@694 1404 const char *uw_Basis_get_settings(uw_context ctx, uw_unit u) {
adamc@736 1405 if (ctx->client == NULL) {
adamc@736 1406 if (ctx->needs_sig) {
adamc@1094 1407 char *sig = ctx->app->cookie_sig(ctx);
adamc@736 1408 char *r = uw_malloc(ctx, strlen(sig) + 8);
adamc@736 1409 sprintf(r, "sig=\"%s\";", sig);
adamc@736 1410 return r;
adamc@736 1411 }
adamc@736 1412 else
adamc@736 1413 return "";
adamc@736 1414 } else {
adamc@1094 1415 char *sig = ctx->needs_sig ? ctx->app->cookie_sig(ctx) : "";
adamc@1094 1416 char *r = uw_malloc(ctx, 59 + 3 * INTS_MAX + strlen(ctx->app->url_prefix)
adamc@736 1417 + (ctx->needs_sig ? strlen(sig) + 7 : 0));
adam@1552 1418 sprintf(r, "isPost=%s;client_id=%u;client_pass=%d;url_prefix=\"%s\";timeout=%d;%s%s%slistener();",
adam@1552 1419 (ctx->isPost ? "true" : "false"),
adamc@682 1420 ctx->client->id,
adamc@682 1421 ctx->client->pass,
adamc@1094 1422 ctx->app->url_prefix,
adamc@1094 1423 ctx->app->timeout,
adamc@736 1424 ctx->needs_sig ? "sig=\"" : "",
adamc@736 1425 sig,
adamc@736 1426 ctx->needs_sig ? "\";" : "");
adamc@679 1427 return r;
adamc@668 1428 }
adamc@667 1429 }
adamc@667 1430
adamc@574 1431 uw_Basis_string uw_Basis_jsifyString(uw_context ctx, uw_Basis_string s) {
adamc@574 1432 char *r, *s2;
adamc@574 1433
adam@1446 1434 uw_check_heap(ctx, strlen(s) * 4 + 3);
adamc@574 1435
adamc@666 1436 r = s2 = ctx->heap.front;
adamc@574 1437 *s2++ = '"';
adamc@574 1438
adamc@574 1439 for (; *s; s++) {
adam@1286 1440 unsigned char c = *s;
adamc@574 1441
adamc@574 1442 switch (c) {
adamc@574 1443 case '"':
adamc@574 1444 strcpy(s2, "\\\"");
adamc@574 1445 s2 += 2;
adamc@574 1446 break;
adamc@1027 1447 case '\'':
adamc@1027 1448 strcpy(s2, "\\047");
adamc@1027 1449 s2 += 4;
adamc@1027 1450 break;
adamc@574 1451 case '\\':
adamc@574 1452 strcpy(s2, "\\\\");
adamc@574 1453 s2 += 2;
adamc@574 1454 break;
adam@1457 1455 case '<':
adam@1457 1456 strcpy(s2, "\\074");
adam@1457 1457 s2 += 4;
adam@1457 1458 break;
adamc@574 1459 default:
adam@1286 1460 if (isprint((int)c) || c >= 128)
adamc@574 1461 *s2++ = c;
adamc@574 1462 else {
adam@1429 1463 sprintf(s2, "\\%03o", c);
adamc@574 1464 s2 += 4;
adamc@574 1465 }
adamc@574 1466 }
adamc@574 1467 }
adamc@574 1468
adamc@574 1469 strcpy(s2, "\"");
adamc@910 1470 ctx->heap.front = s2 + 2;
adamc@574 1471 return r;
adamc@574 1472 }
adamc@574 1473
adam@1286 1474 uw_Basis_string uw_Basis_jsifyChar(uw_context ctx, uw_Basis_char c1) {
adam@1286 1475 unsigned char c = c1;
adamc@1025 1476 char *r, *s2;
adamc@1025 1477
adam@1446 1478 uw_check_heap(ctx, 7);
adamc@1025 1479
adamc@1025 1480 r = s2 = ctx->heap.front;
adamc@1025 1481 *s2++ = '"';
adamc@1025 1482
adamc@1025 1483 switch (c) {
adamc@1025 1484 case '"':
adamc@1025 1485 strcpy(s2, "\\\"");
adamc@1025 1486 s2 += 2;
adamc@1025 1487 break;
adamc@1027 1488 case '\'':
adamc@1027 1489 strcpy(s2, "\\047");
adamc@1027 1490 s2 += 4;
adamc@1027 1491 break;
adamc@1025 1492 case '\\':
adamc@1025 1493 strcpy(s2, "\\\\");
adamc@1025 1494 s2 += 2;
adamc@1025 1495 break;
adam@1457 1496 case '<':
adam@1457 1497 strcpy(s2, "\\074");
adam@1457 1498 s2 += 4;
adam@1457 1499 break;
adamc@1025 1500 default:
adam@1286 1501 if (isprint((int)c) || c >= 128)
adamc@1025 1502 *s2++ = c;
adamc@1025 1503 else {
adam@1429 1504 sprintf(s2, "\\%03o", (unsigned char)c);
adamc@1025 1505 s2 += 4;
adamc@1025 1506 }
adamc@1025 1507 }
adamc@1025 1508
adamc@1025 1509 strcpy(s2, "\"");
adamc@1025 1510 ctx->heap.front = s2 + 2;
adamc@1025 1511 return r;
adamc@1025 1512 }
adamc@1025 1513
adamc@574 1514 uw_Basis_string uw_Basis_jsifyString_ws(uw_context ctx, uw_Basis_string s) {
adamc@574 1515 char *r, *s2;
adamc@574 1516
adam@1446 1517 uw_check_script(ctx, strlen(s) * 4 + 3);
adamc@574 1518
adamc@666 1519 r = s2 = ctx->script.front;
adamc@574 1520 *s2++ = '"';
adamc@574 1521
adamc@574 1522 for (; *s; s++) {
adam@1286 1523 unsigned char c = *s;
adamc@574 1524
adamc@574 1525 switch (c) {
adamc@577 1526 case '\'':
adamc@1027 1527 strcpy(s2, "\\");
adamc@574 1528 s2 += 2;
adamc@574 1529 break;
adamc@574 1530 case '\\':
adamc@574 1531 strcpy(s2, "\\\\");
adamc@574 1532 s2 += 2;
adamc@574 1533 break;
adam@1457 1534 case '<':
adam@1457 1535 strcpy(s2, "\\074");
adam@1457 1536 s2 += 4;
adam@1457 1537 break;
adamc@574 1538 default:
adam@1286 1539 if (isprint((int)c) || c >= 128)
adamc@574 1540 *s2++ = c;
adamc@574 1541 else {
adam@1429 1542 sprintf(s2, "\\%03o", c);
adamc@574 1543 s2 += 4;
adamc@574 1544 }
adamc@574 1545 }
adamc@574 1546 }
adamc@574 1547
adamc@574 1548 strcpy(s2, "\"");
adamc@666 1549 ctx->script.front = s2 + 1;
adamc@574 1550 return r;
adamc@574 1551 }
adamc@574 1552
adamc@682 1553 char *uw_Basis_jsifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 1554 if (ctx->client == NULL || chn.cli != ctx->client->id)
adamc@682 1555 return "null";
adamc@682 1556 else {
adamc@682 1557 int len;
adamc@682 1558 char *r;
adamc@682 1559
adamc@682 1560 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 1561 r = ctx->heap.front;
adamc@682 1562 sprintf(r, "%u%n", chn.chn, &len);
adamc@682 1563 ctx->heap.front += len+1;
adamc@682 1564 return r;
adamc@682 1565 }
adamc@682 1566 }
adamc@682 1567
adam@1446 1568 uw_Basis_source uw_Basis_new_client_source(uw_context ctx, uw_Basis_string s) {
adamc@577 1569 int len;
adamc@577 1570 size_t s_len = strlen(s);
adamc@562 1571
adam@1446 1572 uw_check_script(ctx, 15 + 2 * INTS_MAX + s_len);
adam@1446 1573 sprintf(ctx->script.front, "s%d_%llu=sc(exec(%n", ctx->id, ctx->source_count, &len);
adamc@666 1574 ctx->script.front += len;
adamc@666 1575 strcpy(ctx->script.front, s);
adamc@666 1576 ctx->script.front += s_len;
adamc@970 1577 strcpy(ctx->script.front, "));");
adamc@972 1578 ctx->script.front += 3;
adamc@562 1579
adam@1446 1580 uw_Basis_source r = {ctx->id, ctx->source_count++};
adam@1446 1581 return r;
adam@1446 1582 }
adam@1446 1583
adam@1446 1584 uw_unit uw_Basis_set_client_source(uw_context ctx, uw_Basis_source src, uw_Basis_string s) {
adamc@577 1585 int len;
adamc@577 1586 size_t s_len = strlen(s);
adamc@577 1587
adam@1446 1588 uw_check_script(ctx, 15 + 2 * INTS_MAX + s_len);
adam@1446 1589 sprintf(ctx->script.front, "sv(s%d_%llu,exec(%n", src.context, src.source, &len);
adamc@666 1590 ctx->script.front += len;
adamc@666 1591 strcpy(ctx->script.front, s);
adamc@666 1592 ctx->script.front += s_len;
adamc@970 1593 strcpy(ctx->script.front, "));");
adamc@972 1594 ctx->script.front += 3;
adamc@577 1595
adamc@577 1596 return uw_unit_v;
adamc@577 1597 }
adamc@577 1598
adamc@311 1599 static void uw_check(uw_context ctx, size_t extra) {
adam@1369 1600 ctx_uw_buffer_check(ctx, "page", &ctx->page, extra);
adamc@117 1601 }
adamc@117 1602
adamc@311 1603 static void uw_writec_unsafe(uw_context ctx, char c) {
adamc@666 1604 *(ctx->page.front)++ = c;
adam@1413 1605 *ctx->page.front = 0;
adamc@117 1606 }
adamc@117 1607
adamc@311 1608 void uw_writec(uw_context ctx, char c) {
adam@1413 1609 uw_check(ctx, 2);
adamc@311 1610 uw_writec_unsafe(ctx, c);
adamc@117 1611 }
adamc@117 1612
adamc@311 1613 static void uw_write_unsafe(uw_context ctx, const char* s) {
adamc@117 1614 int len = strlen(s);
adamc@666 1615 memcpy(ctx->page.front, s, len);
adamc@666 1616 ctx->page.front += len;
adamc@117 1617 }
adamc@117 1618
adamc@311 1619 void uw_write(uw_context ctx, const char* s) {
adamc@311 1620 uw_check(ctx, strlen(s) + 1);
adamc@311 1621 uw_write_unsafe(ctx, s);
adamc@666 1622 *ctx->page.front = 0;
adamc@102 1623 }
adamc@106 1624
adamc@311 1625 char *uw_Basis_attrifyInt(uw_context ctx, uw_Basis_int n) {
adamc@136 1626 char *result;
adamc@136 1627 int len;
adamc@311 1628 uw_check_heap(ctx, INTS_MAX);
adamc@666 1629 result = ctx->heap.front;
adamc@276 1630 sprintf(result, "%lld%n", n, &len);
adamc@666 1631 ctx->heap.front += len+1;
adamc@136 1632 return result;
adamc@106 1633 }
adamc@106 1634
adamc@311 1635 char *uw_Basis_attrifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@136 1636 char *result;
adamc@136 1637 int len;
adamc@311 1638 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 1639 result = ctx->heap.front;
adam@1920 1640 sprintf(result, "%.16g%n", n, &len);
adamc@666 1641 ctx->heap.front += len+1;
adamc@136 1642 return result;
adamc@106 1643 }
adamc@106 1644
adamc@311 1645 char *uw_Basis_attrifyString(uw_context ctx, uw_Basis_string s) {
adamc@136 1646 int len = strlen(s);
adamc@136 1647 char *result, *p;
adamc@311 1648 uw_check_heap(ctx, len * 6 + 1);
adamc@136 1649
adamc@666 1650 result = p = ctx->heap.front;
adamc@136 1651
adamc@136 1652 for (; *s; s++) {
adamc@1054 1653 unsigned char c = *s;
adamc@136 1654
adamc@136 1655 if (c == '"') {
adamc@136 1656 strcpy(p, "&quot;");
adamc@136 1657 p += 6;
adamc@136 1658 } else if (c == '&') {
adamc@136 1659 strcpy(p, "&amp;");
adamc@136 1660 p += 5;
adamc@136 1661 }
adamc@1059 1662 else
adamc@136 1663 *p++ = c;
adamc@136 1664 }
adamc@136 1665
adamc@137 1666 *p++ = 0;
adamc@666 1667 ctx->heap.front = p;
adamc@136 1668 return result;
adamc@106 1669 }
adamc@106 1670
adamc@1011 1671 char *uw_Basis_attrifyChar(uw_context ctx, uw_Basis_char c) {
adamc@1011 1672 char *result, *p;
adamc@1011 1673 uw_check_heap(ctx, 7);
adamc@1011 1674
adamc@1011 1675 result = p = ctx->heap.front;
adamc@1011 1676
adamc@1011 1677 if (c == '"') {
adamc@1011 1678 strcpy(p, "&quot;");
adamc@1011 1679 p += 6;
adamc@1011 1680 } else if (c == '&') {
adamc@1011 1681 strcpy(p, "&amp;");
adamc@1011 1682 p += 5;
adamc@1011 1683 }
adamc@1059 1684 else
adamc@1011 1685 *p++ = c;
adamc@1011 1686
adamc@1011 1687 *p++ = 0;
adamc@1011 1688 ctx->heap.front = p;
adamc@1011 1689 return result;
adamc@1011 1690 }
adamc@1011 1691
adamc@721 1692 char *uw_Basis_attrifyCss_class(uw_context ctx, uw_Basis_css_class s) {
adamc@721 1693 return s;
adamc@721 1694 }
adamc@721 1695
adamc@311 1696 static void uw_Basis_attrifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
adamc@117 1697 int len;
adamc@117 1698
adamc@666 1699 sprintf(ctx->page.front, "%lld%n", n, &len);
adamc@666 1700 ctx->page.front += len;
adamc@106 1701 }
adamc@106 1702
adamc@428 1703 uw_unit uw_Basis_attrifyInt_w(uw_context ctx, uw_Basis_int n) {
adamc@311 1704 uw_check(ctx, INTS_MAX);
adamc@311 1705 uw_Basis_attrifyInt_w_unsafe(ctx, n);
adamc@428 1706
adamc@428 1707 return uw_unit_v;
adamc@106 1708 }
adamc@106 1709
adamc@428 1710 uw_unit uw_Basis_attrifyFloat_w(uw_context ctx, uw_Basis_float n) {
adamc@117 1711 int len;
adamc@117 1712
adamc@311 1713 uw_check(ctx, FLOATS_MAX);
adamc@666 1714 sprintf(ctx->page.front, "%g%n", n, &len);
adamc@666 1715 ctx->page.front += len;
adamc@428 1716
adamc@428 1717 return uw_unit_v;
adamc@117 1718 }
adamc@117 1719
adamc@428 1720 uw_unit uw_Basis_attrifyString_w(uw_context ctx, uw_Basis_string s) {
adamc@311 1721 uw_check(ctx, strlen(s) * 6);
adamc@117 1722
adamc@106 1723 for (; *s; s++) {
adamc@1054 1724 unsigned char c = *s;
adamc@106 1725
adamc@106 1726 if (c == '"')
adamc@311 1727 uw_write_unsafe(ctx, "&quot;");
adamc@136 1728 else if (c == '&')
adamc@311 1729 uw_write_unsafe(ctx, "&amp;");
adamc@1059 1730 else
adamc@311 1731 uw_writec_unsafe(ctx, c);
adamc@106 1732 }
adamc@428 1733
adamc@428 1734 return uw_unit_v;
adamc@106 1735 }
adamc@120 1736
adamc@1011 1737 uw_unit uw_Basis_attrifyChar_w(uw_context ctx, uw_Basis_char c) {
adamc@1011 1738 uw_check(ctx, 6);
adamc@1011 1739
adamc@1011 1740 if (c == '"')
adamc@1011 1741 uw_write_unsafe(ctx, "&quot;");
adamc@1011 1742 else if (c == '&')
adamc@1011 1743 uw_write_unsafe(ctx, "&amp;");
adamc@1059 1744 else
adamc@1011 1745 uw_writec_unsafe(ctx, c);
adamc@1011 1746
adamc@1011 1747 return uw_unit_v;
adamc@1011 1748 }
adamc@1011 1749
adamc@120 1750
adamc@311 1751 char *uw_Basis_urlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@137 1752 int len;
adamc@137 1753 char *r;
adamc@137 1754
adamc@311 1755 uw_check_heap(ctx, INTS_MAX);
adamc@666 1756 r = ctx->heap.front;
adamc@276 1757 sprintf(r, "%lld%n", n, &len);
adamc@666 1758 ctx->heap.front += len+1;
adamc@137 1759 return r;
adamc@120 1760 }
adamc@120 1761
adamc@682 1762 char *uw_Basis_urlifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 1763 if (ctx->client == NULL || chn.cli != ctx->client->id)
adamc@682 1764 return "";
adamc@682 1765 else {
adamc@682 1766 int len;
adamc@682 1767 char *r;
adamc@679 1768
adamc@682 1769 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 1770 r = ctx->heap.front;
adamc@682 1771 sprintf(r, "%u%n", chn.chn, &len);
adamc@682 1772 ctx->heap.front += len+1;
adamc@682 1773 return r;
adamc@682 1774 }
adamc@679 1775 }
adamc@679 1776
adamc@311 1777 char *uw_Basis_urlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@137 1778 int len;
adamc@137 1779 char *r;
adamc@137 1780
adamc@311 1781 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 1782 r = ctx->heap.front;
adamc@137 1783 sprintf(r, "%g%n", n, &len);
adamc@666 1784 ctx->heap.front += len+1;
adamc@137 1785 return r;
adamc@120 1786 }
adamc@120 1787
adamc@311 1788 char *uw_Basis_urlifyString(uw_context ctx, uw_Basis_string s) {
adamc@137 1789 char *r, *p;
adamc@137 1790
adamc@925 1791 if (s[0] == '\0')
adamc@925 1792 return "_";
adamc@925 1793
adamc@925 1794 uw_check_heap(ctx, strlen(s) * 3 + 1 + !!(s[0] == '_'));
adamc@925 1795
adamc@925 1796 r = p = ctx->heap.front;
adamc@925 1797 if (s[0] == '_')
adamc@925 1798 *p++ = '_';
adamc@925 1799
adamc@925 1800 for (; *s; s++) {
adamc@1087 1801 unsigned char c = *s;
adamc@137 1802
adamc@137 1803 if (c == ' ')
adamc@137 1804 *p++ = '+';
adamc@137 1805 else if (isalnum(c))
adamc@137 1806 *p++ = c;
adamc@137 1807 else {
adamc@1259 1808 sprintf(p, ".%02X", c);
adamc@137 1809 p += 3;
adamc@137 1810 }
adamc@137 1811 }
adamc@137 1812
adamc@137 1813 *p++ = 0;
adamc@666 1814 ctx->heap.front = p;
adamc@137 1815 return r;
adamc@120 1816 }
adamc@120 1817
adamc@311 1818 char *uw_Basis_urlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 1819 if (b == uw_Basis_False)
adamc@186 1820 return "0";
adamc@186 1821 else
adamc@186 1822 return "1";
adamc@186 1823 }
adamc@186 1824
adam@1620 1825 char *uw_Basis_urlifySource(uw_context ctx, uw_Basis_source src) {
adam@1620 1826 char *r;
adam@1620 1827 int len;
adam@1620 1828 uw_check_heap(ctx, 2 * INTS_MAX + 2);
adam@1620 1829 r = ctx->heap.front;
adam@1620 1830 sprintf(r, "%d/%llu%n", src.context, src.source, &len);
adam@1620 1831 ctx->heap.front += len+1;
adam@1620 1832 return r;
adam@1620 1833 }
adam@1620 1834
adamc@311 1835 static void uw_Basis_urlifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
adamc@120 1836 int len;
adamc@120 1837
adamc@666 1838 sprintf(ctx->page.front, "%lld%n", n, &len);
adamc@666 1839 ctx->page.front += len;
adamc@120 1840 }
adamc@120 1841
adamc@428 1842 uw_unit uw_Basis_urlifyInt_w(uw_context ctx, uw_Basis_int n) {
adamc@311 1843 uw_check(ctx, INTS_MAX);
adamc@311 1844 uw_Basis_urlifyInt_w_unsafe(ctx, n);
adamc@428 1845
adamc@428 1846 return uw_unit_v;
adamc@120 1847 }
adamc@120 1848
adamc@682 1849 uw_unit uw_Basis_urlifyChannel_w(uw_context ctx, uw_Basis_channel chn) {
adamc@682 1850 if (ctx->client != NULL && chn.cli == ctx->client->id) {
adamc@682 1851 int len;
adamc@682 1852
adamc@682 1853 uw_check(ctx, INTS_MAX + 1);
adamc@682 1854 sprintf(ctx->page.front, "%u%n", chn.chn, &len);
adamc@682 1855 ctx->page.front += len;
adamc@682 1856 }
adamc@682 1857
adamc@679 1858 return uw_unit_v;
adamc@679 1859 }
adamc@679 1860
adamc@428 1861 uw_unit uw_Basis_urlifyFloat_w(uw_context ctx, uw_Basis_float n) {
adamc@120 1862 int len;
adamc@120 1863
adamc@311 1864 uw_check(ctx, FLOATS_MAX);
adamc@666 1865 sprintf(ctx->page.front, "%g%n", n, &len);
adamc@666 1866 ctx->page.front += len;
adamc@428 1867
adamc@428 1868 return uw_unit_v;
adamc@120 1869 }
adamc@120 1870
adamc@488 1871 uw_Basis_string uw_Basis_urlifyTime(uw_context ctx, uw_Basis_time t) {
adamc@1376 1872 return uw_Basis_urlifyInt(ctx, (uw_Basis_int)t.seconds * 1000000 + t.microseconds);
adamc@488 1873 }
adamc@488 1874
adam@1404 1875 uw_unit uw_Basis_urlifyTime_w(uw_context ctx, uw_Basis_time t) {
adam@1404 1876 return uw_Basis_urlifyInt_w(ctx, (uw_Basis_int)t.seconds * 1000000 + t.microseconds);
adam@1404 1877 }
adam@1404 1878
adamc@428 1879 uw_unit uw_Basis_urlifyString_w(uw_context ctx, uw_Basis_string s) {
adamc@925 1880 if (s[0] == '\0') {
adamc@925 1881 uw_check(ctx, 1);
adamc@925 1882 uw_writec_unsafe(ctx, '_');
adamc@925 1883 return uw_unit_v;
adamc@925 1884 }
adamc@925 1885
adamc@925 1886 uw_check(ctx, strlen(s) * 3 + !!(s[0] == '_'));
adamc@925 1887
adamc@925 1888 if (s[0] == '_')
adamc@925 1889 uw_writec_unsafe(ctx, '_');
adamc@120 1890
adamc@120 1891 for (; *s; s++) {
adamc@1087 1892 unsigned char c = *s;
adamc@120 1893
adamc@120 1894 if (c == ' ')
adamc@311 1895 uw_writec_unsafe(ctx, '+');
adamc@120 1896 else if (isalnum(c))
adamc@311 1897 uw_writec_unsafe(ctx, c);
adamc@120 1898 else {
adamc@1259 1899 sprintf(ctx->page.front, ".%02X", c);
adamc@666 1900 ctx->page.front += 3;
adamc@120 1901 }
adamc@120 1902 }
adamc@428 1903
adamc@428 1904 return uw_unit_v;
adamc@120 1905 }
adamc@120 1906
adamc@428 1907 uw_unit uw_Basis_urlifyBool_w(uw_context ctx, uw_Basis_bool b) {
adamc@311 1908 if (b == uw_Basis_False)
adamc@311 1909 uw_writec(ctx, '0');
adamc@186 1910 else
adamc@311 1911 uw_writec(ctx, '1');
adamc@428 1912
adamc@428 1913 return uw_unit_v;
adamc@186 1914 }
adamc@186 1915
adam@1620 1916 uw_unit uw_Basis_urlifySource_w(uw_context ctx, uw_Basis_source src) {
adam@1620 1917 int len;
adam@1620 1918
adam@1620 1919 uw_check(ctx, 2 * INTS_MAX + 2);
adam@1620 1920 sprintf(ctx->page.front, "%d/%llu%n", src.context, src.source, &len);
adam@1620 1921 ctx->page.front += len;
adam@1620 1922
adam@1620 1923 return uw_unit_v;
adam@1620 1924 }
adam@1620 1925
adamc@120 1926
adamc@311 1927 static char *uw_unurlify_advance(char *s) {
adamc@144 1928 char *new_s = strchr(s, '/');
adamc@120 1929
adamc@120 1930 if (new_s)
adamc@120 1931 *new_s++ = 0;
adamc@120 1932 else
adamc@144 1933 new_s = strchr(s, 0);
adamc@144 1934
adamc@144 1935 return new_s;
adamc@144 1936 }
adamc@144 1937
adamc@311 1938 uw_Basis_int uw_Basis_unurlifyInt(uw_context ctx, char **s) {
adamc@311 1939 char *new_s = uw_unurlify_advance(*s);
adamc@311 1940 uw_Basis_int r;
adamc@120 1941
adamc@276 1942 r = atoll(*s);
adamc@120 1943 *s = new_s;
adamc@120 1944 return r;
adamc@120 1945 }
adamc@120 1946
adamc@311 1947 uw_Basis_float uw_Basis_unurlifyFloat(uw_context ctx, char **s) {
adamc@311 1948 char *new_s = uw_unurlify_advance(*s);
adamc@311 1949 uw_Basis_float r;
adamc@120 1950
adamc@120 1951 r = atof(*s);
adamc@120 1952 *s = new_s;
adamc@120 1953 return r;
adamc@120 1954 }
adamc@120 1955
adamc@488 1956 uw_Basis_time uw_Basis_unurlifyTime(uw_context ctx, char **s) {
adam@1365 1957 uw_Basis_int n = uw_Basis_unurlifyInt(ctx, s);
adam@1365 1958 uw_Basis_time r = {n / 1000000, n % 1000000};
adam@1365 1959 return r;
adamc@488 1960 }
adamc@488 1961
adamc@1023 1962 static uw_Basis_string uw_unurlifyString_to(int fromClient, uw_context ctx, char *r, char *s) {
adamc@927 1963 char *s1, *s2 = s;
adamc@144 1964 int n;
adamc@136 1965
adamc@1023 1966 if (!fromClient) {
adamc@1023 1967 if (*s2 == '_')
adamc@1023 1968 ++s2;
adamc@1259 1969 else if ((s2[0] == '%' || s2[0] == '.') && s2[1] == '5' && (s2[2] == 'f' || s2[2] == 'F'))
adamc@1023 1970 s2 += 3;
adamc@1023 1971 }
adamc@925 1972
adamc@927 1973 for (s1 = r; *s2; ++s1, ++s2) {
adamc@1087 1974 unsigned char c = *s2;
adamc@136 1975
adamc@136 1976 switch (c) {
adamc@136 1977 case '+':
adamc@136 1978 *s1 = ' ';
adamc@136 1979 break;
adamc@136 1980 case '%':
adamc@169 1981 if (s2[1] == 0)
adamc@311 1982 uw_error(ctx, FATAL, "Missing first character of escaped URL byte");
adamc@169 1983 if (s2[2] == 0)
adamc@311 1984 uw_error(ctx, FATAL, "Missing second character of escaped URL byte");
adamc@169 1985 if (sscanf(s2+1, "%02X", &n) != 1)
adam@1444 1986 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", uw_Basis_htmlifyString(ctx, s2));
adamc@136 1987 *s1 = n;
adamc@136 1988 s2 += 2;
adamc@136 1989 break;
adamc@1259 1990 case '.':
adamc@1259 1991 if (!fromClient) {
adamc@1259 1992 if (s2[1] == 0)
adamc@1259 1993 uw_error(ctx, FATAL, "Missing first character of escaped URL byte");
adamc@1259 1994 if (s2[2] == 0)
adamc@1259 1995 uw_error(ctx, FATAL, "Missing second character of escaped URL byte");
adamc@1259 1996 if (sscanf(s2+1, "%02X", &n) != 1)
adam@1444 1997 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", uw_Basis_htmlifyString(ctx, s2));
adamc@1259 1998 *s1 = n;
adamc@1259 1999 s2 += 2;
adamc@1259 2000 break;
adamc@1259 2001 }
adamc@136 2002 default:
adamc@136 2003 *s1 = c;
adamc@136 2004 }
adamc@136 2005 }
adamc@136 2006 *s1++ = 0;
adamc@144 2007 return s1;
adamc@144 2008 }
adamc@144 2009
adamc@311 2010 uw_Basis_bool uw_Basis_unurlifyBool(uw_context ctx, char **s) {
adamc@311 2011 char *new_s = uw_unurlify_advance(*s);
adamc@311 2012 uw_Basis_bool r;
adamc@186 2013
adamc@186 2014 if (*s[0] == 0 || !strcmp(*s, "0") || !strcmp(*s, "off"))
adamc@311 2015 r = uw_Basis_False;
adamc@186 2016 else
adamc@311 2017 r = uw_Basis_True;
adamc@186 2018
adamc@186 2019 *s = new_s;
adamc@186 2020 return r;
adamc@186 2021 }
adamc@186 2022
adamc@311 2023 uw_Basis_string uw_Basis_unurlifyString(uw_context ctx, char **s) {
adamc@311 2024 char *new_s = uw_unurlify_advance(*s);
adamc@1134 2025 char *r;
adamc@1134 2026 int len;
adamc@144 2027
adamc@200 2028 len = strlen(*s);
adamc@311 2029 uw_check_heap(ctx, len + 1);
adamc@144 2030
adamc@666 2031 r = ctx->heap.front;
adamc@1023 2032 ctx->heap.front = uw_unurlifyString_to(0, ctx, ctx->heap.front, *s);
adamc@1023 2033 *s = new_s;
adamc@1023 2034 return r;
adamc@1023 2035 }
adamc@1023 2036
adamc@1109 2037 uw_Basis_unit uw_Basis_unurlifyUnit(uw_context ctx, char **s) {
adamc@1109 2038 *s = uw_unurlify_advance(*s);
adamc@1109 2039 return uw_unit_v;
adamc@1109 2040 }
adamc@1109 2041
adamc@1023 2042 uw_Basis_string uw_Basis_unurlifyString_fromClient(uw_context ctx, char **s) {
adamc@1023 2043 char *new_s = uw_unurlify_advance(*s);
adamc@1134 2044 char *r;
adamc@1134 2045 int len;
adamc@1023 2046
adamc@1023 2047 len = strlen(*s);
adamc@1023 2048 uw_check_heap(ctx, len + 1);
adamc@1023 2049
adamc@1023 2050 r = ctx->heap.front;
adamc@1023 2051 ctx->heap.front = uw_unurlifyString_to(1, ctx, ctx->heap.front, *s);
adamc@136 2052 *s = new_s;
adamc@136 2053 return r;
adamc@120 2054 }
adamc@135 2055
adamc@135 2056
adamc@311 2057 char *uw_Basis_htmlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@286 2058 int len;
adamc@286 2059 char *r;
adamc@286 2060
adamc@311 2061 uw_check_heap(ctx, INTS_MAX);
adamc@666 2062 r = ctx->heap.front;
adamc@286 2063 sprintf(r, "%lld%n", n, &len);
adamc@666 2064 ctx->heap.front += len+1;
adamc@286 2065 return r;
adamc@286 2066 }
adamc@286 2067
adamc@428 2068 uw_unit uw_Basis_htmlifyInt_w(uw_context ctx, uw_Basis_int n) {
adamc@286 2069 int len;
adamc@286 2070
adamc@311 2071 uw_check(ctx, INTS_MAX);
adamc@666 2072 sprintf(ctx->page.front, "%lld%n", n, &len);
adamc@666 2073 ctx->page.front += len;
adamc@428 2074
adamc@428 2075 return uw_unit_v;
adamc@286 2076 }
adamc@286 2077
adam@1358 2078 char *uw_Basis_htmlifySpecialChar(uw_context ctx, unsigned char ch) {
adam@1358 2079 unsigned int n = ch;
adam@1358 2080 int len;
adam@1358 2081 char *r;
adam@1358 2082
adam@1358 2083 uw_check_heap(ctx, INTS_MAX+3);
adam@1358 2084 r = ctx->heap.front;
adam@1358 2085 sprintf(r, "&#%u;%n", n, &len);
adam@1358 2086 ctx->heap.front += len+1;
adam@1358 2087 return r;
adam@1358 2088 }
adam@1358 2089
adam@1358 2090 uw_unit uw_Basis_htmlifySpecialChar_w(uw_context ctx, unsigned char ch) {
adam@1358 2091 unsigned int n = ch;
adam@1358 2092 int len;
adam@1358 2093
adam@1358 2094 uw_check(ctx, INTS_MAX+3);
adam@1358 2095 sprintf(ctx->page.front, "&#%u;%n", n, &len);
adam@1358 2096 ctx->page.front += len;
adam@1358 2097 return uw_unit_v;
adam@1358 2098 }
adam@1358 2099
adamc@311 2100 char *uw_Basis_htmlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@286 2101 int len;
adamc@286 2102 char *r;
adamc@286 2103
adamc@311 2104 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 2105 r = ctx->heap.front;
adamc@286 2106 sprintf(r, "%g%n", n, &len);
adamc@666 2107 ctx->heap.front += len+1;
adamc@286 2108 return r;
adamc@286 2109 }
adamc@286 2110
adamc@428 2111 uw_unit uw_Basis_htmlifyFloat_w(uw_context ctx, uw_Basis_float n) {
adamc@286 2112 int len;
adamc@286 2113
adamc@311 2114 uw_check(ctx, FLOATS_MAX);
adamc@666 2115 sprintf(ctx->page.front, "%g%n", n, &len);
adamc@666 2116 ctx->page.front += len;
adamc@428 2117
adamc@428 2118 return uw_unit_v;
adamc@286 2119 }
adamc@286 2120
adam@1365 2121 char *uw_Basis_jsifyTime(uw_context ctx, uw_Basis_time t) {
adam@1360 2122 int len;
adam@1360 2123 char *r;
adam@1360 2124
adam@1360 2125 uw_check_heap(ctx, INTS_MAX);
adam@1360 2126 r = ctx->heap.front;
adamc@1377 2127 sprintf(r, "%lld%n", (uw_Basis_int)t.seconds * 1000000 + t.microseconds, &len);
adam@1360 2128 ctx->heap.front += len+1;
adam@1360 2129 return r;
adam@1360 2130 }
adam@1360 2131
adam@1365 2132 uw_unit uw_Basis_jsifyInt_w(uw_context ctx, uw_Basis_int n) {
adam@1360 2133 int len;
adam@1360 2134
adam@1360 2135 uw_check(ctx, INTS_MAX);
adam@1360 2136 sprintf(ctx->page.front, "%lld%n", (uw_Basis_int)n, &len);
adam@1360 2137 ctx->page.front += len;
adam@1360 2138
adam@1360 2139 return uw_unit_v;
adam@1360 2140 }
adam@1360 2141
adam@1444 2142 char *uw_Basis_htmlifyString(uw_context ctx, const char *s) {
adamc@137 2143 char *r, *s2;
adamc@137 2144
adamc@311 2145 uw_check_heap(ctx, strlen(s) * 5 + 1);
adamc@137 2146
adamc@666 2147 for (r = s2 = ctx->heap.front; *s; s++) {
adamc@1054 2148 unsigned char c = *s;
adamc@137 2149
adamc@137 2150 switch (c) {
adamc@137 2151 case '<':
adamc@137 2152 strcpy(s2, "&lt;");
adamc@137 2153 s2 += 4;
adamc@137 2154 break;
adamc@137 2155 case '&':
adamc@137 2156 strcpy(s2, "&amp;");
adamc@137 2157 s2 += 5;
adamc@137 2158 break;
adamc@137 2159 default:
adamc@1059 2160 *s2++ = c;
adamc@137 2161 }
adamc@137 2162 }
adamc@137 2163
adamc@137 2164 *s2++ = 0;
adamc@666 2165 ctx->heap.front = s2;
adamc@137 2166 return r;
adamc@135 2167 }
adamc@135 2168
adamc@428 2169 uw_unit uw_Basis_htmlifyString_w(uw_context ctx, uw_Basis_string s) {
adamc@321 2170 uw_check(ctx, strlen(s) * 6);
adamc@135 2171
adamc@135 2172 for (; *s; s++) {
adamc@1054 2173 unsigned char c = *s;
adamc@135 2174
adamc@135 2175 switch (c) {
adamc@135 2176 case '<':
adamc@311 2177 uw_write_unsafe(ctx, "&lt;");
adamc@135 2178 break;
adamc@135 2179 case '&':
adamc@311 2180 uw_write_unsafe(ctx, "&amp;");
adamc@135 2181 break;
adamc@135 2182 default:
adamc@1059 2183 uw_writec_unsafe(ctx, c);
adamc@135 2184 }
adamc@135 2185 }
adamc@428 2186
adamc@428 2187 return uw_unit_v;
adamc@135 2188 }
adamc@180 2189
adamc@311 2190 uw_Basis_string uw_Basis_htmlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 2191 if (b == uw_Basis_False)
adamc@286 2192 return "False";
adamc@286 2193 else
adamc@286 2194 return "True";
adamc@286 2195 }
adamc@286 2196
adamc@428 2197 uw_unit uw_Basis_htmlifyBool_w(uw_context ctx, uw_Basis_bool b) {
adamc@311 2198 if (b == uw_Basis_False) {
adamc@311 2199 uw_check(ctx, 6);
adamc@666 2200 strcpy(ctx->page.front, "False");
adamc@666 2201 ctx->page.front += 5;
adamc@286 2202 } else {
adamc@311 2203 uw_check(ctx, 5);
adamc@666 2204 strcpy(ctx->page.front, "True");
adamc@666 2205 ctx->page.front += 4;
adamc@286 2206 }
adamc@428 2207
adamc@428 2208 return uw_unit_v;
adamc@286 2209 }
adamc@286 2210
adamc@436 2211 #define TIME_FMT "%x %X"
adamc@438 2212 #define TIME_FMT_PG "%Y-%m-%d %T"
adam@1632 2213 #define TIME_FMT_JS "%Y/%m/%d %T"
adamc@436 2214
adam@1629 2215 uw_Basis_string uw_Basis_timeToString(uw_context, uw_Basis_time);
adam@1629 2216
adamc@436 2217 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) {
adam@1629 2218 return uw_Basis_htmlifyString(ctx, uw_Basis_timeToString(ctx, t));
adamc@436 2219 }
adamc@436 2220
adamc@436 2221 uw_unit uw_Basis_htmlifyTime_w(uw_context ctx, uw_Basis_time t) {
adam@1629 2222 return uw_Basis_htmlifyString_w(ctx, uw_Basis_timeToString(ctx, t));
adamc@436 2223 }
adamc@436 2224
adam@1446 2225 char *uw_Basis_htmlifySource(uw_context ctx, uw_Basis_source src) {
adam@1446 2226 int len;
adam@1446 2227 char *r;
adam@1446 2228
adam@1446 2229 uw_check_heap(ctx, 2 * INTS_MAX + 2);
adam@1446 2230 r = ctx->heap.front;
adam@1446 2231 sprintf(r, "s%d_%llu%n", src.context, src.source, &len);
adam@1446 2232 ctx->heap.front += len+1;
adam@1446 2233 return r;
adam@1446 2234 }
adam@1446 2235
adam@1446 2236 uw_unit uw_Basis_htmlifySource_w(uw_context ctx, uw_Basis_source src) {
adam@1446 2237 int len;
adam@1446 2238
adam@1446 2239 uw_check(ctx, 2 * INTS_MAX + 1);
adam@1446 2240 sprintf(ctx->page.front, "s%d_%llu%n", src.context, src.source, &len);
adam@1446 2241 ctx->page.front += len;
adam@1446 2242
adam@1446 2243 return uw_unit_v;
adam@1446 2244 }
adam@1446 2245
adamc@821 2246 uw_Basis_char uw_Basis_strsub(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
adam@1388 2247 while (n >= 0) {
adam@1388 2248 if (*s == 0)
adam@1388 2249 uw_error(ctx, FATAL, "Out-of-bounds strsub");
adam@1388 2250
adam@1388 2251 if (n == 0)
adam@1388 2252 return *s;
adam@1388 2253
adam@1388 2254 --n;
adam@1388 2255 ++s;
adam@1388 2256 }
adam@1388 2257
adam@1388 2258 uw_error(ctx, FATAL, "Negative strsub bound");
adamc@821 2259 }
adamc@821 2260
adamc@821 2261 uw_Basis_string uw_Basis_strsuffix(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
adam@1388 2262 while (n >= 0) {
adam@1388 2263 if (*s == 0 || n == 0)
adam@1388 2264 return s;
adam@1388 2265
adam@1388 2266 --n;
adam@1388 2267 ++s;
adam@1388 2268 }
adam@1388 2269
adam@1388 2270 uw_error(ctx, FATAL, "Negative strsuffix bound");
adamc@821 2271 }
adamc@821 2272
adamc@828 2273 uw_Basis_int uw_Basis_strlen(uw_context ctx, uw_Basis_string s) {
adamc@828 2274 return strlen(s);
adamc@828 2275 }
adamc@828 2276
adam@1388 2277 uw_Basis_bool uw_Basis_strlenGe(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
adam@1388 2278 while (n > 0) {
adam@1388 2279 if (*s == 0)
adam@1388 2280 return uw_Basis_False;
adam@1388 2281
adam@1388 2282 --n;
adam@1388 2283 ++s;
adam@1388 2284 }
adam@1388 2285
adam@1388 2286 return uw_Basis_True;
adam@1388 2287 }
adam@1388 2288
adamc@829 2289 uw_Basis_string uw_Basis_strchr(uw_context ctx, uw_Basis_string s, uw_Basis_char ch) {
adamc@829 2290 return strchr(s, ch);
adamc@829 2291 }
adamc@829 2292
adamc@831 2293 uw_Basis_int uw_Basis_strcspn(uw_context ctx, uw_Basis_string s, uw_Basis_string chs) {
adamc@831 2294 return strcspn(s, chs);
adamc@831 2295 }
adamc@831 2296
adamc@829 2297 uw_Basis_int *uw_Basis_strindex(uw_context ctx, uw_Basis_string s, uw_Basis_char ch) {
adamc@829 2298 uw_Basis_string r = strchr(s, ch);
adamc@829 2299 if (r == NULL)
adamc@829 2300 return NULL;
adamc@829 2301 else {
adamc@829 2302 uw_Basis_int *nr = uw_malloc(ctx, sizeof(uw_Basis_int));
adamc@829 2303 *nr = r - s;
adamc@829 2304 return nr;
adamc@829 2305 }
adamc@829 2306 }
adamc@829 2307
adam@1390 2308 uw_Basis_int *uw_Basis_strsindex(uw_context ctx, const char *haystack, const char *needle) {
adam@1390 2309 uw_Basis_string r = strstr(haystack, needle);
adam@1390 2310 if (r == NULL)
adam@1390 2311 return NULL;
adam@1390 2312 else {
adam@1390 2313 uw_Basis_int *nr = uw_malloc(ctx, sizeof(uw_Basis_int));
adam@1390 2314 *nr = r - haystack;
adam@1390 2315 return nr;
adam@1390 2316 }
adam@1390 2317 }
adam@1390 2318
adamc@311 2319 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) {
adamc@828 2320 int len = uw_Basis_strlen(ctx, s1) + uw_Basis_strlen(ctx, s2) + 1;
adamc@180 2321 char *s;
adamc@180 2322
adamc@311 2323 uw_check_heap(ctx, len);
adamc@180 2324
adamc@666 2325 s = ctx->heap.front;
adamc@180 2326
adamc@180 2327 strcpy(s, s1);
adamc@180 2328 strcat(s, s2);
adamc@666 2329 ctx->heap.front += len;
adamc@180 2330
adamc@180 2331 return s;
adamc@180 2332 }
adamc@278 2333
adamc@829 2334 uw_Basis_string uw_Basis_substring(uw_context ctx, uw_Basis_string s, uw_Basis_int start, uw_Basis_int len) {
adamc@829 2335 size_t full_len = uw_Basis_strlen(ctx, s);
adamc@829 2336
adamc@829 2337 if (start < 0)
adamc@829 2338 uw_error(ctx, FATAL, "substring: Negative start index");
adamc@829 2339 if (len < 0)
adamc@829 2340 uw_error(ctx, FATAL, "substring: Negative length");
adamc@829 2341 if (start + len > full_len)
adamc@829 2342 uw_error(ctx, FATAL, "substring: Start index plus length is too large");
adamc@829 2343
adamc@829 2344 if (start + len == full_len)
adamc@829 2345 return &s[start];
adamc@829 2346 else {
adamc@829 2347 uw_Basis_string r = uw_malloc(ctx, len+1);
adamc@959 2348 memcpy(r, s+start, len);
adamc@829 2349 r[len] = 0;
adamc@829 2350 return r;
adamc@829 2351 }
adamc@829 2352
adamc@829 2353 }
adamc@829 2354
adamc@1023 2355 uw_Basis_string uw_Basis_str1(uw_context ctx, uw_Basis_char ch) {
adamc@1023 2356 char *r;
adamc@1023 2357
adamc@1023 2358 uw_check_heap(ctx, 2);
adamc@1023 2359 r = ctx->heap.front;
adamc@1023 2360 r[0] = ch;
adamc@1023 2361 r[1] = 0;
adamc@1023 2362
adamc@1023 2363 ctx->heap.front += 2;
adamc@1023 2364
adamc@1023 2365 return r;
adamc@1023 2366 }
adamc@1023 2367
adamc@737 2368 uw_Basis_string uw_strdup(uw_context ctx, uw_Basis_string s1) {
adamc@828 2369 int len = uw_Basis_strlen(ctx, s1) + 1;
adamc@278 2370 char *s;
adamc@278 2371
adamc@311 2372 uw_check_heap(ctx, len);
adamc@278 2373
adamc@666 2374 s = ctx->heap.front;
adamc@278 2375
adamc@278 2376 strcpy(s, s1);
adamc@666 2377 ctx->heap.front += len;
adamc@278 2378
adamc@278 2379 return s;
adamc@278 2380 }
adamc@280 2381
adam@1295 2382 uw_Basis_string uw_dup_and_clear_error_message(uw_context ctx) {
adam@1295 2383 if (ctx->error_message[0]) {
adam@1295 2384 char *s = uw_strdup(ctx, ctx->error_message);
adam@1295 2385 ctx->error_message[0] = 0;
adam@1295 2386 return s;
adam@1295 2387 } else
adam@1295 2388 return NULL;
adam@1295 2389 }
adam@1295 2390
adamc@737 2391 uw_Basis_string uw_maybe_strdup(uw_context ctx, uw_Basis_string s1) {
adamc@493 2392 if (s1)
adamc@737 2393 return uw_strdup(ctx, s1);
adamc@493 2394 else
adamc@493 2395 return NULL;
adamc@493 2396 }
adamc@493 2397
adamc@742 2398 char *uw_memdup(uw_context ctx, const char *p, size_t len) {
adamc@742 2399 char *r = uw_malloc(ctx, len);
adamc@742 2400 memcpy(r, p, len);
adamc@742 2401 return r;
adamc@742 2402 }
adamc@280 2403
adamc@879 2404 char *uw_sqlfmtInt = "%lld::int8%n";
adamc@879 2405
adamc@311 2406 char *uw_Basis_sqlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@281 2407 int len;
adamc@281 2408 char *r;
adamc@281 2409
adamc@311 2410 uw_check_heap(ctx, INTS_MAX + 6);
adamc@666 2411 r = ctx->heap.front;
adamc@879 2412 sprintf(r, uw_sqlfmtInt, n, &len);
adamc@666 2413 ctx->heap.front += len+1;
adamc@281 2414 return r;
adamc@281 2415 }
adamc@281 2416
adamc@467 2417 char *uw_Basis_sqlifyIntN(uw_context ctx, uw_Basis_int *n) {
adamc@467 2418 if (n == NULL)
adamc@467 2419 return "NULL";
adamc@467 2420 else
adamc@467 2421 return uw_Basis_sqlifyInt(ctx, *n);
adamc@467 2422 }
adamc@467 2423
adam@1920 2424 char *uw_sqlfmtFloat = "%.16g::float8%n";
adamc@879 2425
adamc@311 2426 char *uw_Basis_sqlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@281 2427 int len;
adamc@281 2428 char *r;
adamc@281 2429
adamc@311 2430 uw_check_heap(ctx, FLOATS_MAX + 8);
adamc@666 2431 r = ctx->heap.front;
adamc@879 2432 sprintf(r, uw_sqlfmtFloat, n, &len);
adamc@666 2433 ctx->heap.front += len+1;
adamc@281 2434 return r;
adamc@281 2435 }
adamc@281 2436
adamc@467 2437 char *uw_Basis_sqlifyFloatN(uw_context ctx, uw_Basis_float *n) {
adamc@467 2438 if (n == NULL)
adamc@467 2439 return "NULL";
adamc@467 2440 else
adamc@467 2441 return uw_Basis_sqlifyFloat(ctx, *n);
adamc@467 2442 }
adamc@467 2443
adam@1834 2444 int uw_Estrings = 1, uw_sql_type_annotations = 1;
adamc@879 2445 char *uw_sqlsuffixString = "::text";
adamc@1011 2446 char *uw_sqlsuffixChar = "::char";
adamc@281 2447
adamc@311 2448 uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) {
adamc@280 2449 char *r, *s2;
adamc@280 2450
adamc@879 2451 uw_check_heap(ctx, strlen(s) * 2 + 3 + uw_Estrings + strlen(uw_sqlsuffixString));
adamc@280 2452
adamc@666 2453 r = s2 = ctx->heap.front;
adamc@879 2454 if (uw_Estrings)
adamc@879 2455 *s2++ = 'E';
adamc@280 2456 *s2++ = '\'';
adamc@280 2457
adamc@280 2458 for (; *s; s++) {
adamc@280 2459 char c = *s;
adamc@280 2460
adamc@280 2461 switch (c) {
adamc@280 2462 case '\'':
adamc@885 2463 if (uw_Estrings)
adamc@885 2464 strcpy(s2, "\\'");
adamc@885 2465 else
adamc@885 2466 strcpy(s2, "''");
adamc@280 2467 s2 += 2;
adamc@280 2468 break;
adamc@280 2469 case '\\':
adamc@885 2470 if (uw_Estrings) {
adamc@885 2471 strcpy(s2, "\\\\");
adamc@885 2472 s2 += 2;
adamc@885 2473 } else
adamc@885 2474 *s2++ = '\\';
adamc@280 2475 break;
adamc@280 2476 default:
adamc@1150 2477 if (isprint((int)c))
adamc@280 2478 *s2++ = c;
adamc@879 2479 else if (uw_Estrings) {
adam@1857 2480 sprintf(s2, "\\%03o", (unsigned char)c);
adamc@280 2481 s2 += 4;
adamc@280 2482 }
adamc@879 2483 else
adam@1689 2484 *s2++ = c; // I hope this is safe to do... don't know how to support UTF-8 outside Postgres otherwise!
adamc@280 2485 }
adamc@280 2486 }
adamc@280 2487
adamc@879 2488 *s2++ = '\'';
adamc@879 2489 strcpy(s2, uw_sqlsuffixString);
adamc@879 2490 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixString);
adamc@280 2491 return r;
adamc@280 2492 }
adamc@281 2493
adamc@1011 2494 uw_Basis_string uw_Basis_sqlifyChar(uw_context ctx, uw_Basis_char c) {
adamc@1011 2495 char *r, *s2;
adamc@1011 2496
adamc@1011 2497 uw_check_heap(ctx, 5 + uw_Estrings + strlen(uw_sqlsuffixChar));
adamc@1011 2498
adamc@1011 2499 r = s2 = ctx->heap.front;
adamc@1011 2500 if (uw_Estrings)
adamc@1011 2501 *s2++ = 'E';
adamc@1011 2502 *s2++ = '\'';
adamc@1011 2503
adamc@1011 2504 switch (c) {
adamc@1011 2505 case '\'':
adamc@1011 2506 if (uw_Estrings)
adamc@1011 2507 strcpy(s2, "\\'");
adamc@1011 2508 else
adamc@1011 2509 strcpy(s2, "''");
adamc@1011 2510 s2 += 2;
adamc@1011 2511 break;
adamc@1011 2512 case '\\':
adamc@1011 2513 if (uw_Estrings) {
adamc@1011 2514 strcpy(s2, "\\\\");
adamc@1011 2515 s2 += 2;
adamc@1011 2516 } else
adamc@1011 2517 *s2++ = '\\';
adamc@1011 2518 break;
adamc@1011 2519 default:
adamc@1150 2520 if (isprint((int)c))
adamc@1011 2521 *s2++ = c;
adamc@1011 2522 else if (uw_Estrings) {
adam@1857 2523 sprintf(s2, "\\%03o", (unsigned char)c);
adamc@1011 2524 s2 += 4;
adamc@1011 2525 }
adamc@1011 2526 else
adamc@1011 2527 uw_error(ctx, FATAL, "Non-printable character %u in char to SQLify", c);
adamc@1011 2528 }
adamc@1011 2529
adamc@1011 2530 *s2++ = '\'';
adamc@1011 2531 strcpy(s2, uw_sqlsuffixChar);
adamc@1011 2532 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixChar);
adamc@1011 2533 return r;
adamc@1011 2534 }
adamc@1011 2535
adamc@879 2536 char *uw_sqlsuffixBlob = "::bytea";
adamc@879 2537
adam@1867 2538 uw_Basis_string uw_Basis_sqlifyBlob(uw_context ctx, uw_Basis_blob b) {
adamc@737 2539 char *r, *s2;
adamc@737 2540 size_t i;
adamc@737 2541
adam@1867 2542 uw_check_heap(ctx, b.size * 5 + 4 + strlen(uw_sqlsuffixBlob));
adamc@737 2543
adamc@737 2544 r = s2 = ctx->heap.front;
adamc@879 2545 if (uw_Estrings)
adamc@879 2546 *s2++ = 'E';
adam@1867 2547 else
adam@1867 2548 *s2++ = 'X';
adamc@737 2549 *s2++ = '\'';
adamc@737 2550
adamc@737 2551 for (i = 0; i < b.size; ++i) {
adam@1867 2552 unsigned char c = b.data[i];
adam@1867 2553
adam@1867 2554 if (uw_Estrings) {
adam@1867 2555 switch (c) {
adam@1867 2556 case '\'':
adamc@885 2557 strcpy(s2, "\\'");
adam@1867 2558 s2 += 2;
adam@1867 2559 break;
adam@1867 2560 case '\\':
adamc@885 2561 strcpy(s2, "\\\\\\\\");
adamc@885 2562 s2 += 4;
adam@1867 2563 break;
adam@1867 2564 default:
adam@1867 2565 if (isprint((int)c))
adam@1867 2566 *s2++ = c;
adam@1867 2567 else {
adam@1867 2568 sprintf(s2, "\\\\%03o", c);
adam@1867 2569 s2 += 5;
adam@1867 2570 }
adamc@737 2571 }
adam@1867 2572 } else {
adam@1867 2573 sprintf(s2, "%02X", c);
adam@1867 2574 s2 += 2;
adamc@737 2575 }
adam@1867 2576 }
adamc@737 2577
adamc@879 2578 *s2++ = '\'';
adamc@879 2579 strcpy(s2, uw_sqlsuffixBlob);
adamc@879 2580 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixBlob);
adamc@737 2581 return r;
adamc@737 2582 }
adamc@737 2583
adamc@682 2584 char *uw_Basis_sqlifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 2585 int len;
adamc@682 2586 char *r;
adamc@682 2587 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
adamc@682 2588
adamc@682 2589 uw_check_heap(ctx, INTS_MAX + 7);
adamc@682 2590 r = ctx->heap.front;
adamc@879 2591 sprintf(r, uw_sqlfmtInt, combo, &len);
adamc@682 2592 ctx->heap.front += len+1;
adamc@682 2593 return r;
adamc@682 2594 }
adamc@682 2595
adamc@682 2596 char *uw_Basis_attrifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 2597 int len;
adamc@682 2598 char *r;
adamc@682 2599 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
adamc@682 2600
adamc@682 2601 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 2602 r = ctx->heap.front;
adamc@682 2603 sprintf(r, "%lld%n", combo, &len);
adamc@682 2604 ctx->heap.front += len+1;
adamc@682 2605 return r;
adamc@682 2606 }
adamc@682 2607
adamc@879 2608 char *uw_sqlfmtUint4 = "%u::int4%n";
adamc@879 2609
adamc@682 2610 char *uw_Basis_sqlifyClient(uw_context ctx, uw_Basis_client cli) {
adamc@682 2611 int len;
adamc@682 2612 char *r;
adamc@682 2613
adamc@682 2614 uw_check_heap(ctx, INTS_MAX + 7);
adamc@682 2615 r = ctx->heap.front;
adamc@879 2616 sprintf(r, uw_sqlfmtUint4, cli, &len);
adamc@682 2617 ctx->heap.front += len+1;
adamc@682 2618 return r;
adamc@682 2619 }
adamc@682 2620
adamc@682 2621 char *uw_Basis_attrifyClient(uw_context ctx, uw_Basis_client cli) {
adamc@682 2622 int len;
adamc@682 2623 char *r;
adamc@682 2624
adamc@682 2625 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 2626 r = ctx->heap.front;
adamc@682 2627 sprintf(r, "%u%n", cli, &len);
adamc@682 2628 ctx->heap.front += len+1;
adamc@682 2629 return r;
adamc@682 2630 }
adamc@682 2631
adamc@467 2632 uw_Basis_string uw_Basis_sqlifyStringN(uw_context ctx, uw_Basis_string s) {
adamc@467 2633 if (s == NULL)
adamc@467 2634 return "NULL";
adamc@467 2635 else
adamc@467 2636 return uw_Basis_sqlifyString(ctx, s);
adamc@467 2637 }
adamc@467 2638
adamc@311 2639 char *uw_Basis_sqlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 2640 if (b == uw_Basis_False)
adamc@281 2641 return "FALSE";
adamc@281 2642 else
adamc@281 2643 return "TRUE";
adamc@281 2644 }
adamc@282 2645
adamc@467 2646 char *uw_Basis_sqlifyBoolN(uw_context ctx, uw_Basis_bool *b) {
adamc@467 2647 if (b == NULL)
adamc@467 2648 return "NULL";
adamc@467 2649 else
adamc@467 2650 return uw_Basis_sqlifyBool(ctx, *b);
adamc@467 2651 }
adamc@467 2652
adamc@439 2653 char *uw_Basis_sqlifyTime(uw_context ctx, uw_Basis_time t) {
adamc@439 2654 size_t len;
adamc@682 2655 char *r, *s;
adam@1392 2656 struct tm stm = {};
adam@1392 2657 stm.tm_isdst = -1;
adamc@682 2658
adam@1365 2659 if (localtime_r(&t.seconds, &stm)) {
adamc@682 2660 s = uw_malloc(ctx, TIMES_MAX);
adam@1364 2661 len = strftime(s, TIMES_MAX, TIME_FMT_PG, &stm);
adam@1834 2662 if (uw_sql_type_annotations) {
adam@1834 2663 if (t.microseconds) {
adam@1834 2664 r = uw_malloc(ctx, len + 21);
adam@1834 2665 sprintf(r, "'%s.%06u'::timestamp", s, t.microseconds);
adam@1834 2666 } else {
adam@1834 2667 r = uw_malloc(ctx, len + 14);
adam@1834 2668 sprintf(r, "'%s'::timestamp", s);
adam@1834 2669 }
adam@1367 2670 } else {
adam@1834 2671 r = uw_malloc(ctx, len + 3);
adam@1834 2672 sprintf(r, "'%s'", s);
adam@1367 2673 }
adamc@682 2674 return r;
adamc@682 2675 } else
adamc@682 2676 return "<Invalid time>";
adamc@682 2677 }
adamc@682 2678
adamc@682 2679 char *uw_Basis_attrifyTime(uw_context ctx, uw_Basis_time t) {
adamc@682 2680 size_t len;
adamc@439 2681 char *r;
adam@1392 2682 struct tm stm = {};
adam@1392 2683 stm.tm_isdst = -1;
adamc@439 2684
adam@1365 2685 if (localtime_r(&t.seconds, &stm)) {
adamc@439 2686 uw_check_heap(ctx, TIMES_MAX);
adamc@666 2687 r = ctx->heap.front;
adamc@439 2688 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 2689 ctx->heap.front += len+1;
adamc@439 2690 return r;
adamc@439 2691 } else
adamc@439 2692 return "<Invalid time>";
adamc@439 2693 }
adamc@439 2694
adam@1364 2695 char *uw_Basis_ensqlTime(uw_context ctx, uw_Basis_time t) {
adam@1364 2696 size_t len;
adam@1364 2697 char *r;
adam@1392 2698 struct tm stm = {};
adam@1392 2699 stm.tm_isdst = -1;
adam@1364 2700
adam@1365 2701 if (localtime_r(&t.seconds, &stm)) {
adam@1364 2702 uw_check_heap(ctx, TIMES_MAX);
adam@1364 2703 r = ctx->heap.front;
adam@1365 2704 len = strftime(r, TIMES_MAX-7, TIME_FMT_PG, &stm);
adam@1365 2705 ctx->heap.front += len;
adam@1365 2706 sprintf(ctx->heap.front, ".%06u", t.microseconds);
adam@1365 2707 ctx->heap.front += 8;
adam@1364 2708 return r;
adam@1364 2709 } else
adam@1364 2710 return "<Invalid time>";
adam@1364 2711 }
adam@1364 2712
adamc@467 2713 char *uw_Basis_sqlifyTimeN(uw_context ctx, uw_Basis_time *t) {
adamc@467 2714 if (t == NULL)
adamc@467 2715 return "NULL";
adamc@467 2716 else
adamc@467 2717 return uw_Basis_sqlifyTime(ctx, *t);
adamc@467 2718 }
adamc@467 2719
adamc@311 2720 char *uw_Basis_ensqlBool(uw_Basis_bool b) {
adamc@311 2721 static uw_Basis_int true = 1;
adamc@311 2722 static uw_Basis_int false = 0;
adamc@282 2723
adamc@311 2724 if (b == uw_Basis_False)
adamc@282 2725 return (char *)&false;
adamc@282 2726 else
adamc@282 2727 return (char *)&true;
adamc@282 2728 }
adamc@284 2729
adamc@311 2730 uw_Basis_string uw_Basis_intToString(uw_context ctx, uw_Basis_int n) {
adamc@284 2731 int len;
adamc@284 2732 char *r;
adamc@284 2733
adamc@311 2734 uw_check_heap(ctx, INTS_MAX);
adamc@666 2735 r = ctx->heap.front;
adamc@284 2736 sprintf(r, "%lld%n", n, &len);
adamc@666 2737 ctx->heap.front += len+1;
adamc@284 2738 return r;
adamc@284 2739 }
adamc@285 2740
adamc@311 2741 uw_Basis_string uw_Basis_floatToString(uw_context ctx, uw_Basis_float n) {
adamc@285 2742 int len;
adamc@285 2743 char *r;
adamc@285 2744
adamc@311 2745 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 2746 r = ctx->heap.front;
adamc@285 2747 sprintf(r, "%g%n", n, &len);
adamc@666 2748 ctx->heap.front += len+1;
adamc@285 2749 return r;
adamc@285 2750 }
adamc@285 2751
adamc@821 2752 uw_Basis_string uw_Basis_charToString(uw_context ctx, uw_Basis_char ch) {
adamc@821 2753 char *r = uw_malloc(ctx, 2);
adamc@821 2754 r[0] = ch;
adamc@821 2755 r[1] = 0;
adamc@821 2756 return r;
adamc@821 2757 }
adamc@821 2758
adamc@311 2759 uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) {
adamc@311 2760 if (b == uw_Basis_False)
adamc@285 2761 return "False";
adamc@285 2762 else
adamc@285 2763 return "True";
adamc@285 2764 }
adamc@288 2765
adam@1359 2766 uw_Basis_string uw_Basis_timef(uw_context ctx, const char *fmt, uw_Basis_time t) {
adam@1352 2767 size_t len;
adam@1352 2768 char *r;
adam@1392 2769 struct tm stm = {};
adam@1392 2770 stm.tm_isdst = -1;
adam@1352 2771
adam@1365 2772 if (localtime_r(&t.seconds, &stm)) {
adam@1352 2773 uw_check_heap(ctx, TIMES_MAX);
adam@1352 2774 r = ctx->heap.front;
adam@1352 2775 len = strftime(r, TIMES_MAX, fmt, &stm);
adam@1352 2776 ctx->heap.front += len+1;
adam@1352 2777 return r;
adam@1352 2778 } else
adam@1352 2779 return "<Invalid time>";
adam@1352 2780 }
adam@1352 2781
adam@1629 2782 uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) {
adam@1629 2783 return uw_Basis_timef(ctx, ctx->app->time_format, t);
adam@1629 2784 }
adam@1629 2785
adamc@311 2786 uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) {
adamc@288 2787 char *endptr;
adamc@311 2788 uw_Basis_int n = strtoll(s, &endptr, 10);
adamc@288 2789
adamc@288 2790 if (*s != '\0' && *endptr == '\0') {
adamc@311 2791 uw_Basis_int *r = uw_malloc(ctx, sizeof(uw_Basis_int));
adamc@288 2792 *r = n;
adamc@288 2793 return r;
adamc@288 2794 } else
adamc@288 2795 return NULL;
adamc@288 2796 }
adamc@289 2797
adamc@311 2798 uw_Basis_float *uw_Basis_stringToFloat(uw_context ctx, uw_Basis_string s) {
adamc@289 2799 char *endptr;
adamc@311 2800 uw_Basis_float n = strtod(s, &endptr);
adamc@289 2801
adamc@289 2802 if (*s != '\0' && *endptr == '\0') {
adamc@311 2803 uw_Basis_float *r = uw_malloc(ctx, sizeof(uw_Basis_float));
adamc@289 2804 *r = n;
adamc@289 2805 return r;
adamc@289 2806 } else
adamc@289 2807 return NULL;
adamc@289 2808 }
adamc@289 2809
adamc@821 2810 uw_Basis_char *uw_Basis_stringToChar(uw_context ctx, uw_Basis_string s) {
adamc@821 2811 if (s[0] == 0) {
adamc@821 2812 uw_Basis_char *r = uw_malloc(ctx, 1);
adamc@821 2813 r[0] = 0;
adamc@821 2814 return r;
adamc@821 2815 } else if (s[1] != 0)
adamc@821 2816 return NULL;
adamc@821 2817 else {
adamc@821 2818 uw_Basis_char *r = uw_malloc(ctx, 1);
adamc@821 2819 r[0] = s[0];
adamc@821 2820 return r;
adamc@821 2821 }
adamc@821 2822 }
adamc@821 2823
adamc@311 2824 uw_Basis_bool *uw_Basis_stringToBool(uw_context ctx, uw_Basis_string s) {
adamc@311 2825 static uw_Basis_bool true = uw_Basis_True;
adamc@311 2826 static uw_Basis_bool false = uw_Basis_False;
adamc@289 2827
adamc@289 2828 if (!strcasecmp (s, "True"))
adamc@289 2829 return &true;
adamc@289 2830 else if (!strcasecmp (s, "False"))
adamc@289 2831 return &false;
adamc@289 2832 else
adamc@289 2833 return NULL;
adamc@289 2834 }
adamc@292 2835
adamc@436 2836 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) {
adamc@438 2837 char *dot = strchr(s, '.'), *end = strchr(s, 0);
adam@1374 2838 struct tm stm = {};
adam@1392 2839 stm.tm_isdst = -1;
adamc@436 2840
adamc@439 2841 if (dot) {
adamc@439 2842 *dot = 0;
adamc@439 2843 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adamc@439 2844 *dot = '.';
adamc@439 2845 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adam@1365 2846 r->seconds = mktime(&stm);
adam@1365 2847 r->microseconds = 0;
adamc@439 2848 return r;
adamc@439 2849 }
adamc@439 2850 else {
adamc@439 2851 *dot = '.';
adamc@439 2852 return NULL;
adamc@439 2853 }
adamc@436 2854 }
adamc@439 2855 else {
adamc@439 2856 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adamc@439 2857 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adam@1365 2858 r->seconds = mktime(&stm);
adam@1365 2859 r->microseconds = 0;
adamc@439 2860 return r;
adamc@439 2861 }
adamc@439 2862 else if (strptime(s, TIME_FMT, &stm) == end) {
adamc@439 2863 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adam@1365 2864 r->seconds = mktime(&stm);
adam@1365 2865 r->microseconds = 0;
adamc@439 2866 return r;
adam@1632 2867 } else if (strptime(s, TIME_FMT_JS, &stm) == end) {
adam@1632 2868 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adam@1632 2869 r->seconds = mktime(&stm);
adam@1632 2870 r->microseconds = 0;
adam@1632 2871 return r;
adamc@439 2872 }
adamc@439 2873 else
adamc@439 2874 return NULL;
adamc@439 2875 }
adamc@436 2876 }
adamc@436 2877
adam@1352 2878 uw_Basis_time *uw_Basis_stringToTimef(uw_context ctx, const char *fmt, uw_Basis_string s) {
adam@1352 2879 char *end = strchr(s, 0);
adam@1374 2880 struct tm stm = {};
adam@1392 2881 stm.tm_isdst = -1;
adam@1352 2882
adam@1352 2883 if (strptime(s, fmt, &stm) == end) {
adam@1352 2884 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adam@1365 2885 r->seconds = mktime(&stm);
adam@1365 2886 r->microseconds = 0;
adam@1352 2887 return r;
adam@1352 2888 }
adam@1352 2889 else
adam@1352 2890 return NULL;
adam@1352 2891 }
adam@1352 2892
adamc@311 2893 uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) {
adamc@292 2894 char *endptr;
adamc@311 2895 uw_Basis_int n = strtoll(s, &endptr, 10);
adamc@292 2896
adamc@292 2897 if (*s != '\0' && *endptr == '\0')
adamc@292 2898 return n;
adamc@292 2899 else
adam@1444 2900 uw_error(ctx, FATAL, "Can't parse int: %s", uw_Basis_htmlifyString(ctx, s));
adamc@292 2901 }
adamc@293 2902
adamc@682 2903 #include <errno.h>
adamc@682 2904
adamc@678 2905 uw_Basis_channel uw_Basis_stringToChannel_error(uw_context ctx, uw_Basis_string s) {
adamc@682 2906 unsigned long long n;
adamc@682 2907
adamc@682 2908 if (sscanf(s, "%llu", &n) < 1)
adam@1444 2909 uw_error(ctx, FATAL, "Can't parse channel: %s", uw_Basis_htmlifyString(ctx, s));
adamc@682 2910 else {
adamc@682 2911 uw_Basis_channel ch = {n >> 32, n & ((1ull << 32) - 1)};
adamc@682 2912 return ch;
adamc@682 2913 }
adamc@682 2914 }
adamc@682 2915
adamc@682 2916 uw_Basis_client uw_Basis_stringToClient_error(uw_context ctx, uw_Basis_string s) {
adamc@678 2917 char *endptr;
adamc@682 2918 unsigned long n = strtoul(s, &endptr, 10);
adamc@678 2919
adamc@678 2920 if (*s != '\0' && *endptr == '\0')
adamc@678 2921 return n;
adamc@678 2922 else
adam@1444 2923 uw_error(ctx, FATAL, "Can't parse client: %s", uw_Basis_htmlifyString(ctx, s));
adamc@678 2924 }
adamc@678 2925
adamc@311 2926 uw_Basis_float uw_Basis_stringToFloat_error(uw_context ctx, uw_Basis_string s) {
adamc@293 2927 char *endptr;
adamc@311 2928 uw_Basis_float n = strtod(s, &endptr);
adamc@293 2929
adamc@293 2930 if (*s != '\0' && *endptr == '\0')
adamc@293 2931 return n;
adamc@293 2932 else
adam@1444 2933 uw_error(ctx, FATAL, "Can't parse float: %s", uw_Basis_htmlifyString(ctx, s));
adamc@293 2934 }
adamc@293 2935
adamc@821 2936 uw_Basis_char uw_Basis_stringToChar_error(uw_context ctx, uw_Basis_string s) {
adamc@821 2937 if (s[0] == 0)
adamc@821 2938 return 0;
adamc@821 2939 else if (s[1] != 0)
adam@1444 2940 uw_error(ctx, FATAL, "Can't parse char: %s", uw_Basis_htmlifyString(ctx, s));
adamc@821 2941 else
adamc@821 2942 return s[0];
adamc@821 2943 }
adamc@821 2944
adamc@311 2945 uw_Basis_bool uw_Basis_stringToBool_error(uw_context ctx, uw_Basis_string s) {
adamc@296 2946 if (!strcasecmp(s, "T") || !strcasecmp (s, "True"))
adamc@311 2947 return uw_Basis_True;
adamc@296 2948 else if (!strcasecmp(s, "F") || !strcasecmp (s, "False"))
adamc@311 2949 return uw_Basis_False;
adamc@293 2950 else
adam@1444 2951 uw_error(ctx, FATAL, "Can't parse bool: %s", uw_Basis_htmlifyString(ctx, s));
adamc@293 2952 }
adamc@436 2953
adam@1352 2954 uw_Basis_time uw_Basis_unsqlTime(uw_context ctx, uw_Basis_string s) {
adam@1352 2955 char *dot = strchr(s, '.'), *end = strchr(s, 0);
adam@1352 2956 struct tm stm = {};
adam@1392 2957 stm.tm_isdst = -1;
adam@1352 2958
adam@1352 2959 if (dot) {
adam@1352 2960 *dot = 0;
adam@1352 2961 if (strptime(s, TIME_FMT_PG, &stm)) {
adam@1352 2962 *dot = '.';
adam@1410 2963 char usec[] = "000000";
adam@1410 2964 int len = strlen(dot+1);
adam@1410 2965 memcpy(usec, dot+1, len < 6 ? len : 6);
adam@1410 2966 uw_Basis_time r = { mktime(&stm), atoi(usec) };
adam@1365 2967 return r;
adam@1352 2968 }
adam@1352 2969 else {
adam@1352 2970 *dot = '.';
adam@1444 2971 uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s));
adam@1352 2972 }
adam@1352 2973 }
adam@1352 2974 else {
adam@1352 2975 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adam@1365 2976 uw_Basis_time r = { mktime(&stm) };
adam@1365 2977 return r;
adam@1352 2978 } else if (strptime(s, TIME_FMT, &stm) == end) {
adam@1365 2979 uw_Basis_time r = { mktime(&stm) };
adam@1365 2980 return r;
adam@1352 2981 } else
adam@1444 2982 uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s));
adam@1352 2983 }
adam@1352 2984 }
adam@1352 2985
adamc@436 2986 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) {
adamc@438 2987 char *dot = strchr(s, '.'), *end = strchr(s, 0);
adamc@436 2988 struct tm stm = {};
adam@1392 2989 stm.tm_isdst = -1;
adamc@436 2990
adamc@438 2991 if (dot) {
adamc@438 2992 *dot = 0;
adamc@438 2993 if (strptime(s, TIME_FMT_PG, &stm)) {
adamc@438 2994 *dot = '.';
adam@1365 2995 {
adam@1365 2996 uw_Basis_time r = { mktime(&stm) };
adam@1365 2997 return r;
adam@1365 2998 }
adamc@438 2999 }
adamc@438 3000 else {
adamc@438 3001 *dot = '.';
adam@1444 3002 uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s));
adamc@438 3003 }
adamc@438 3004 }
adamc@438 3005 else {
adam@1365 3006 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adam@1365 3007 uw_Basis_time r = { mktime(&stm) };
adam@1365 3008 return r;
adam@1365 3009 } else if (strptime(s, TIME_FMT, &stm) == end) {
adam@1365 3010 uw_Basis_time r = { mktime(&stm) };
adam@1365 3011 return r;
adam@1632 3012 } else if (strptime(s, TIME_FMT_JS, &stm) == end) {
adam@1632 3013 uw_Basis_time r = { mktime(&stm) };
adam@1632 3014 return r;
adam@1365 3015 } else
adam@1444 3016 uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s));
adamc@438 3017 }
adamc@436 3018 }
adamc@457 3019
adam@1352 3020 uw_Basis_time uw_Basis_stringToTimef_error(uw_context ctx, const char *fmt, uw_Basis_string s) {
adam@1352 3021 char *end = strchr(s, 0);
adamc@939 3022 struct tm stm = {};
adam@1392 3023 stm.tm_isdst = -1;
adamc@939 3024
adam@1365 3025 if (strptime(s, fmt, &stm) == end) {
adam@1365 3026 uw_Basis_time r = { mktime(&stm) };
adam@1365 3027 return r;
adam@1365 3028 } else
adam@1444 3029 uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s));
adamc@939 3030 }
adamc@939 3031
adamc@742 3032 uw_Basis_blob uw_Basis_stringToBlob_error(uw_context ctx, uw_Basis_string s, size_t len) {
adamc@742 3033 char *r = ctx->heap.front;
adamc@742 3034 uw_Basis_blob b = {len, r};
adamc@742 3035
adamc@742 3036 uw_check_heap(ctx, len);
adamc@742 3037
adamc@742 3038 while (*s) {
adamc@742 3039 if (s[0] == '\\') {
adamc@742 3040 if (s[1] == '\\') {
adamc@742 3041 *r++ = '\\';
adamc@742 3042 s += 2;
adamc@1153 3043 } else if (isdigit((int)s[1]) && isdigit((int)s[2]) && isdigit((int)s[3])) {
adamc@742 3044 *r++ = (s[1] - '0') * 8 * 8 + ((s[2] - '0') * 8) + (s[3] - '0');
adamc@742 3045 s += 4;
adamc@742 3046 }
adamc@742 3047 else {
adamc@742 3048 *r++ = '\\';
adamc@742 3049 ++s;
adamc@742 3050 }
adamc@742 3051 } else {
adamc@742 3052 *r++ = s[0];
adamc@742 3053 ++s;
adamc@742 3054 }
adamc@742 3055 }
adamc@742 3056
adamc@742 3057 b.size = r - ctx->heap.front;
adamc@742 3058 ctx->heap.front = r;
adamc@742 3059 return b;
adamc@742 3060 }
adamc@742 3061
adam@1402 3062 #define THE_PAST "expires=Sat, 01-Jan-2011 00:00:00 GMT"
adamc@1052 3063
adamc@463 3064 uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) {
adamc@463 3065 int len = strlen(c);
adamc@854 3066 char *p = ctx->outHeaders.start;
adamc@474 3067
adamc@1134 3068 while ((p = strstr(p, "\nSet-Cookie: "))) {
adamc@474 3069 char *p2;
adamc@474 3070 p += 13;
adamc@474 3071 p2 = strchr(p, '=');
adamc@474 3072
adamc@474 3073 if (p2) {
adamc@474 3074 size_t sz = strcspn(p2+1, ";\r\n");
adamc@474 3075
adamc@474 3076 if (!strncasecmp(p, c, p2 - p)) {
adamc@1052 3077 if (sz == 0 && strstr(p2+2, THE_PAST))
adamc@1052 3078 return NULL;
adamc@1052 3079 else {
adamc@1052 3080 char *ret = uw_malloc(ctx, sz + 1);
adamc@1052 3081 memcpy(ret, p2+1, sz);
adamc@1052 3082 ret[sz] = 0;
adamc@1052 3083 return ret;
adamc@1052 3084 }
adamc@474 3085 }
adamc@474 3086 }
adamc@474 3087 }
adamc@463 3088
adamc@1134 3089 if ((p = uw_Basis_requestHeader(ctx, "Cookie"))) {
adamc@1051 3090 char *p2;
adamc@1051 3091
adamc@854 3092 while (1) {
adamc@1051 3093 if (!strncmp(p, c, len) && p[len] == '=') {
adamc@1134 3094 if ((p2 = strchr(p, ';'))) {
adamc@1051 3095 size_t n = p2 - (p + len);
adamc@1051 3096 char *r = uw_malloc(ctx, n);
adamc@1051 3097 memcpy(r, p + 1 + len, n-1);
adamc@1051 3098 r[n-1] = 0;
adamc@1051 3099 return r;
adamc@1051 3100 } else
adamc@1051 3101 return p + 1 + len;
adamc@1134 3102 } else if ((p = strchr(p, ';')))
adamc@854 3103 p += 2;
adamc@463 3104 else
adamc@463 3105 return NULL;
adamc@463 3106 }
adamc@463 3107 }
adamc@792 3108
adamc@792 3109 return NULL;
adamc@457 3110 }
adamc@462 3111
adam@1877 3112 static void set_cookie(uw_context ctx) {
adam@1877 3113 if (ctx->usedSig)
adam@1877 3114 ctx->needsResig = 1;
adam@1877 3115 }
adam@1877 3116
adamc@1050 3117 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 3118 uw_write_header(ctx, "Set-Cookie: ");
adamc@462 3119 uw_write_header(ctx, c);
adamc@462 3120 uw_write_header(ctx, "=");
adamc@462 3121 uw_write_header(ctx, v);
adamc@466 3122 uw_write_header(ctx, "; path=");
adamc@466 3123 uw_write_header(ctx, prefix);
adamc@1050 3124 if (expires) {
adamc@1050 3125 char formatted[30];
adam@1392 3126 struct tm tm = {};
adam@1392 3127 tm.tm_isdst = -1;
adamc@1050 3128
adam@1365 3129 gmtime_r(&expires->seconds, &tm);
adamc@1050 3130
adamc@1050 3131 strftime(formatted, sizeof formatted, "%a, %d-%b-%Y %T GMT", &tm);
adamc@1050 3132
adamc@1050 3133 uw_write_header(ctx, "; expires=");
adamc@1050 3134 uw_write_header(ctx, formatted);
adamc@1050 3135 }
adamc@1050 3136 if (secure)
adamc@1050 3137 uw_write_header(ctx, "; secure");
adamc@462 3138 uw_write_header(ctx, "\r\n");
adam@1877 3139 set_cookie(ctx);
adamc@462 3140
adamc@462 3141 return uw_unit_v;
adamc@462 3142 }
adamc@667 3143
adamc@1050 3144 uw_unit uw_Basis_clear_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c) {
adamc@1050 3145 uw_write_header(ctx, "Set-Cookie: ");
adamc@1050 3146 uw_write_header(ctx, c);
adamc@1050 3147 uw_write_header(ctx, "=; path=");
adamc@1050 3148 uw_write_header(ctx, prefix);
adamc@1052 3149 uw_write_header(ctx, "; " THE_PAST "\r\n");
adam@1877 3150 set_cookie(ctx);
adamc@1050 3151
adamc@1050 3152 return uw_unit_v;
adamc@1050 3153 }
adamc@1050 3154
adamc@1113 3155 size_t uw_deltas_max = SIZE_MAX;
adamc@1113 3156
adamc@682 3157 static delta *allocate_delta(uw_context ctx, unsigned client) {
adamc@682 3158 unsigned i;
adamc@682 3159 delta *d;
adamc@671 3160
adamc@682 3161 for (i = 0; i < ctx->used_deltas; ++i)
adamc@682 3162 if (ctx->deltas[i].client == client)
adamc@682 3163 return &ctx->deltas[i];
adamc@671 3164
adamc@682 3165 if (ctx->used_deltas >= ctx->n_deltas) {
adamc@1113 3166 if (ctx->n_deltas + 1 > uw_deltas_max)
adamc@1113 3167 uw_error(ctx, FATAL, "Exceeded limit on number of deltas");
adamc@1113 3168
adamc@682 3169 ctx->deltas = realloc(ctx->deltas, sizeof(delta) * ++ctx->n_deltas);
adam@1369 3170 uw_buffer_init(uw_messages_max, &ctx->deltas[ctx->n_deltas-1].msgs, 0);
adamc@682 3171 }
adamc@671 3172
adamc@682 3173 d = &ctx->deltas[ctx->used_deltas++];
adamc@682 3174 d->client = client;
adam@1369 3175 uw_buffer_reset(&d->msgs);
adamc@682 3176 return d;
adamc@671 3177 }
adamc@671 3178
adamc@668 3179 uw_Basis_channel uw_Basis_new_channel(uw_context ctx, uw_unit u) {
adamc@682 3180 if (ctx->client == NULL)
adamc@682 3181 uw_error(ctx, FATAL, "Attempt to create channel on request not associated with a persistent connection");
adamc@671 3182
adamc@682 3183 return new_channel(ctx->client);
adamc@668 3184 }
adamc@668 3185
adamc@668 3186 uw_unit uw_Basis_send(uw_context ctx, uw_Basis_channel chn, uw_Basis_string msg) {
adamc@682 3187 delta *d = allocate_delta(ctx, chn.cli);
adamc@682 3188 size_t len;
adamc@682 3189 int preLen;
adamc@682 3190 char pre[INTS_MAX + 2];
adamc@668 3191
adamc@682 3192 len = strlen(msg);
adamc@682 3193
adamc@682 3194 sprintf(pre, "%u\n%n", chn.chn, &preLen);
adamc@682 3195
adam@1369 3196 ctx_uw_buffer_append(ctx, "messages", &d->msgs, pre, preLen);
adam@1369 3197 ctx_uw_buffer_append(ctx, "messages", &d->msgs, msg, len);
adam@1369 3198 ctx_uw_buffer_append(ctx, "messages", &d->msgs, "\n", 1);
adamc@671 3199
adamc@671 3200 return uw_unit_v;
adamc@668 3201 }
adamc@671 3202
adam@1327 3203 int uw_rollback(uw_context ctx, int will_retry) {
adamc@1131 3204 int i;
adamc@1131 3205 cleanup *cl;
adamc@1131 3206
adamc@1131 3207 if (ctx->client)
adamc@1131 3208 release_client(ctx->client);
adamc@1131 3209
adamc@1131 3210 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@1131 3211 cl->func(cl->arg);
adamc@1131 3212
adamc@1131 3213 ctx->cleanup_front = ctx->cleanup;
adamc@1131 3214
adamc@1131 3215 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@1131 3216 if (ctx->transactionals[i].rollback != NULL)
adamc@1131 3217 ctx->transactionals[i].rollback(ctx->transactionals[i].data);
adamc@1131 3218
adamc@1131 3219 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@1131 3220 if (ctx->transactionals[i].free)
adam@1327 3221 ctx->transactionals[i].free(ctx->transactionals[i].data, will_retry);
adamc@1131 3222
adam@1931 3223 return (ctx->app && ctx->transaction_started) ? ctx->app->db_rollback(ctx) : 0;
adamc@1131 3224 }
adamc@1131 3225
adam@1728 3226 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 3227
adam@1877 3228 extern int uw_hash_blocksize;
adam@1877 3229
adam@1877 3230 static const char sig_intro[] = "<input type=\"hidden\" name=\"Sig\" value=\"";
adam@1877 3231
adam@1877 3232 static char *find_sig(char *haystack) {
adam@1877 3233 int i;
adam@1877 3234 char *s = strstr(haystack, sig_intro);
adam@1877 3235
adam@1877 3236 if (!s || strlen(haystack) - (s - haystack) - (sizeof sig_intro - 1) < uw_hash_blocksize*2+1)
adam@1877 3237 return NULL;
adam@1877 3238
adam@1877 3239 s += sizeof sig_intro - 1;
adam@1877 3240
adam@1877 3241 for (i = 0; i < uw_hash_blocksize*2; ++i)
adam@1877 3242 if (!isxdigit((int)s[i]))
adam@1877 3243 return NULL;
adam@1877 3244
adam@1877 3245 if (s[i] != '"')
adam@1877 3246 return NULL;
adam@1877 3247
adam@1877 3248 return s;
adam@1877 3249 }
adam@1877 3250
adamc@671 3251 void uw_commit(uw_context ctx) {
adamc@1104 3252 int i;
adam@1877 3253 char *sig;
adamc@1104 3254
adamc@1131 3255 if (uw_has_error(ctx)) {
adam@1327 3256 uw_rollback(ctx, 0);
adamc@1131 3257 return;
adamc@1131 3258 }
adamc@1131 3259
adamc@1104 3260 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@771 3261 if (ctx->transactionals[i].rollback != NULL)
adamc@1131 3262 if (ctx->transactionals[i].commit) {
adamc@1083 3263 ctx->transactionals[i].commit(ctx->transactionals[i].data);
adamc@1131 3264 if (uw_has_error(ctx)) {
adam@1327 3265 uw_rollback(ctx, 0);
adamc@1131 3266 return;
adamc@1131 3267 }
adamc@1131 3268 }
adamc@771 3269
adamc@1104 3270 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@771 3271 if (ctx->transactionals[i].rollback == NULL)
adamc@1131 3272 if (ctx->transactionals[i].commit) {
adamc@1083 3273 ctx->transactionals[i].commit(ctx->transactionals[i].data);
adamc@1131 3274 if (uw_has_error(ctx)) {
adam@1327 3275 uw_rollback(ctx, 0);
adamc@1131 3276 return;
adamc@1131 3277 }
adamc@1131 3278 }
adamc@1131 3279
adam@1931 3280 if (ctx->transaction_started && ctx->app->db_commit(ctx)) {
adamc@1131 3281 uw_set_error_message(ctx, "Error running SQL COMMIT");
adamc@1131 3282 return;
adamc@1131 3283 }
adamc@682 3284
adamc@682 3285 for (i = 0; i < ctx->used_deltas; ++i) {
adamc@682 3286 delta *d = &ctx->deltas[i];
adamc@682 3287 client *c = find_client(d->client);
adamc@682 3288
adam@1551 3289 assert (c != NULL);
adam@1551 3290 assert(c->mode == USED);
adamc@682 3291
adam@1447 3292 client_send(c, &d->msgs, ctx->script.start, uw_buffer_used(&ctx->script));
adamc@682 3293 }
adamc@682 3294
adamc@682 3295 if (ctx->client)
adamc@685 3296 release_client(ctx->client);
adamc@771 3297
adamc@1104 3298 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@1083 3299 if (ctx->transactionals[i].free)
adam@1327 3300 ctx->transactionals[i].free(ctx->transactionals[i].data, 0);
adamc@916 3301
vshabanoff@1726 3302 uw_check(ctx, 1);
vshabanoff@1726 3303 *ctx->page.front = 0;
adam@1413 3304
adam@1728 3305 if (!ctx->returning_indirectly && !strncmp(ctx->page.start, begin_xhtml, sizeof begin_xhtml - 1)) {
adam@1728 3306 char *s;
adam@1728 3307
adam@1728 3308 // Splice script data into appropriate part of page, also adding <head> if needed.
adam@1728 3309 s = ctx->page.start + sizeof begin_xhtml - 1;
adam@1728 3310 s = strchr(s, '<');
adam@1728 3311 if (s == NULL) {
adam@1728 3312 // Weird. Document has no tags!
adam@1728 3313
adam@1728 3314 uw_write(ctx, "<head></head><body></body>");
adam@1728 3315 uw_check(ctx, 1);
adam@1728 3316 *ctx->page.front = 0;
adam@1728 3317 } else if (!strncmp(s, "<head>", 6)) {
adam@1728 3318 // <head> is present. Let's add the <script> tags immediately after it.
adam@1728 3319
adam@1729 3320 // Any freeform JavaScript to include?
adam@1729 3321 if (uw_buffer_used(&ctx->script) > 0) {
adam@1729 3322 size_t lenH = strlen(ctx->script_header), len = uw_buffer_used(&ctx->script);
adam@1729 3323 size_t lenP = lenH + 40 + len;
adam@1729 3324 char *start = s + 6, *oldPage = ctx->page.start;
adam@1729 3325
adam@1729 3326 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenP);
adam@1729 3327 start += ctx->page.start - oldPage;
adam@1729 3328 memmove(start + lenP, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1);
adam@1729 3329 ctx->page.front += lenP;
adam@1729 3330 memcpy(start, ctx->script_header, lenH);
adam@1729 3331 memcpy(start + lenH, "<script type=\"text/javascript\">", 31);
adam@1729 3332 memcpy(start + lenH + 31, ctx->script.start, len);
adam@1729 3333 memcpy(start + lenH + 31 + len, "</script>", 9);
adam@1729 3334 } else {
adam@1729 3335 size_t lenH = strlen(ctx->script_header);
adam@1729 3336 char *start = s + 6, *oldPage = ctx->page.start;
adam@1729 3337
adam@1729 3338 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenH);
adam@1729 3339 start += ctx->page.start - oldPage;
adam@1729 3340 memmove(start + lenH, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1);
adam@1729 3341 ctx->page.front += lenH;
adam@1729 3342 memcpy(start, ctx->script_header, lenH);
adam@1729 3343 }
adam@1728 3344 } else {
adam@1728 3345 // No <head>. At this point, add it, with <script> tags inside.
adam@1728 3346
adam@1729 3347 if (uw_buffer_used(&ctx->script) > 0) {
adam@1729 3348 size_t lenH = strlen(ctx->script_header), len = uw_buffer_used(&ctx->script);
adam@1729 3349 size_t lenP = lenH + 53 + len;
adam@1729 3350 char *start = s, *oldPage = ctx->page.start;
adam@1729 3351
adam@1729 3352 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenP);
adam@1729 3353 start += ctx->page.start - oldPage;
adam@1729 3354 memmove(start + lenP, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1);
adam@1729 3355 ctx->page.front += lenP;
adam@1729 3356 memcpy(start, "<head>", 6);
adam@1729 3357 memcpy(start + 6, ctx->script_header, lenH);
adam@1729 3358 memcpy(start + 6 + lenH, "<script type=\"text/javascript\">", 31);
adam@1729 3359 memcpy(start + 6 + lenH + 31, ctx->script.start, len);
adam@1729 3360 memcpy(start + 6 + lenH + 31 + len, "</script></head>", 16);
adam@1729 3361 } else {
adam@1729 3362 size_t lenH = strlen(ctx->script_header);
adam@1729 3363 size_t lenP = lenH + 13;
adam@1729 3364 char *start = s, *oldPage = ctx->page.start;
adam@1729 3365
adam@1729 3366 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenP);
adam@1729 3367 start += ctx->page.start - oldPage;
adam@1729 3368 memmove(start + lenP, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1);
adam@1729 3369 ctx->page.front += lenP;
adam@1729 3370 memcpy(start, "<head>", 6);
adam@1729 3371 memcpy(start + 6, ctx->script_header, lenH);
adam@1729 3372 memcpy(start + 6 + lenH, "</head>", 7);
adam@1729 3373 }
adamc@916 3374 }
adamc@916 3375 }
adam@1877 3376
adam@1877 3377 if (ctx->needsResig) {
adam@1877 3378 sig = find_sig(ctx->page.start);
adam@1877 3379 if (sig) {
adam@1877 3380 char *realsig = ctx->app->cookie_sig(ctx);
adam@1877 3381
adam@1877 3382 do {
adam@1877 3383 memcpy(sig, realsig, 2*uw_hash_blocksize);
adam@1877 3384 sig = find_sig(sig);
adam@1877 3385 } while (sig);
adam@1877 3386 }
adam@1877 3387 }
adamc@671 3388 }
adamc@671 3389
adamc@683 3390
adamc@1113 3391 size_t uw_transactionals_max = SIZE_MAX;
adamc@1113 3392
adamc@771 3393 void uw_register_transactional(uw_context ctx, void *data, uw_callback commit, uw_callback rollback,
adam@1327 3394 uw_callback_with_retry free) {
adamc@771 3395 if (ctx->used_transactionals >= ctx->n_transactionals) {
adamc@1113 3396 if (ctx->used_transactionals+1 > uw_transactionals_max)
adamc@1113 3397 uw_error(ctx, FATAL, "Exceeded limit on number of transactionals");
adamc@1084 3398 ctx->transactionals = realloc(ctx->transactionals, sizeof(transactional) * (ctx->used_transactionals+1));
adamc@771 3399 ++ctx->n_transactionals;
adamc@771 3400 }
adamc@771 3401
adamc@771 3402 ctx->transactionals[ctx->used_transactionals].data = data;
adamc@771 3403 ctx->transactionals[ctx->used_transactionals].commit = commit;
adamc@771 3404 ctx->transactionals[ctx->used_transactionals].rollback = rollback;
adamc@771 3405 ctx->transactionals[ctx->used_transactionals++].free = free;
adamc@771 3406 }
adamc@771 3407
adamc@683 3408
adamc@683 3409 // "Garbage collection"
adamc@683 3410
adamc@1121 3411 void uw_do_expunge(uw_context ctx, uw_Basis_client cli, void *data);
adamc@1121 3412 void uw_post_expunge(uw_context ctx, void *data);
adamc@1121 3413
adamc@1121 3414 static failure_kind uw_expunge(uw_context ctx, uw_Basis_client cli, void *data) {
adamc@683 3415 int r = setjmp(ctx->jmp_buf);
adamc@683 3416
adamc@1121 3417 if (r == 0)
adamc@1121 3418 uw_do_expunge(ctx, cli, data);
adamc@1121 3419 else
adamc@1121 3420 ctx->app->db_rollback(ctx);
adamc@1121 3421
adamc@1121 3422 uw_post_expunge(ctx, data);
adamc@683 3423
adamc@683 3424 return r;
adamc@683 3425 }
adamc@683 3426
adamc@683 3427 void uw_prune_clients(uw_context ctx) {
adamc@683 3428 client *c, *next, *prev = NULL;
adamc@683 3429 time_t cutoff;
adamc@683 3430
adamc@1094 3431 cutoff = time(NULL) - ctx->app->timeout;
adamc@683 3432
adamc@683 3433 pthread_mutex_lock(&clients_mutex);
adam@1707 3434 pruning_thread = pthread_self();
adam@1707 3435 pruning_thread_initialized = 1;
adamc@683 3436
adamc@683 3437 for (c = clients_used; c; c = next) {
adamc@683 3438 next = c->next;
adamc@683 3439 pthread_mutex_lock(&c->lock);
adamc@685 3440 if (c->last_contact < cutoff && c->refcount == 0) {
adamc@683 3441 failure_kind fk = UNLIMITED_RETRY;
adamc@683 3442 if (prev)
adamc@683 3443 prev->next = next;
adamc@683 3444 else
adamc@683 3445 clients_used = next;
adamc@684 3446 uw_reset(ctx);
adamc@683 3447 while (fk == UNLIMITED_RETRY) {
adamc@1121 3448 fk = uw_expunge(ctx, c->id, c->data);
adamc@1121 3449 if (fk == UNLIMITED_RETRY)
adamc@684 3450 printf("Unlimited retry during expunge: %s\n", uw_error_message(ctx));
adamc@683 3451 }
adamc@684 3452 if (fk == SUCCESS)
adamc@684 3453 free_client(c);
adamc@1121 3454 else
adamc@856 3455 fprintf(stderr, "Expunge blocked by error: %s\n", uw_error_message(ctx));
adamc@683 3456 }
adamc@683 3457 else
adamc@683 3458 prev = c;
adamc@683 3459 pthread_mutex_unlock(&c->lock);
adamc@683 3460 }
adamc@683 3461
adamc@683 3462 pthread_mutex_unlock(&clients_mutex);
adamc@683 3463 }
adamc@687 3464
adamc@687 3465 failure_kind uw_initialize(uw_context ctx) {
adamc@687 3466 int r = setjmp(ctx->jmp_buf);
adamc@687 3467
adamc@687 3468 if (r == 0) {
adamc@1094 3469 if (ctx->app->db_begin(ctx))
adamc@687 3470 uw_error(ctx, FATAL, "Error running SQL BEGIN");
adam@1931 3471 ctx->transaction_started = 1;
adamc@1094 3472 ctx->app->initializer(ctx);
adamc@1094 3473 if (ctx->app->db_commit(ctx))
adamc@687 3474 uw_error(ctx, FATAL, "Error running SQL COMMIT");
adamc@687 3475 }
adamc@687 3476
adamc@687 3477 return r;
adamc@687 3478 }
adamc@717 3479
adamc@1065 3480 static int url_bad(uw_Basis_string s) {
adamc@1065 3481 for (; *s; ++s)
adamc@1153 3482 if (!isgraph((int)*s))
adamc@1065 3483 return 1;
adamc@1065 3484
adamc@1065 3485 return 0;
adamc@1065 3486 }
adamc@1065 3487
adamc@717 3488 uw_Basis_string uw_Basis_bless(uw_context ctx, uw_Basis_string s) {
adamc@1065 3489 if (url_bad(s))
adamc@1065 3490 uw_error(ctx, FATAL, "Invalid URL %s", uw_Basis_htmlifyString(ctx, s));
adamc@1094 3491 if (ctx->app->check_url(s))
adamc@770 3492 return s;
adamc@770 3493 else
adamc@770 3494 uw_error(ctx, FATAL, "Disallowed URL %s", uw_Basis_htmlifyString(ctx, s));
adamc@717 3495 }
adamc@734 3496
adamc@770 3497 uw_Basis_string uw_Basis_checkUrl(uw_context ctx, uw_Basis_string s) {
adamc@1065 3498 if (url_bad(s))
adamc@1065 3499 return NULL;
adamc@1094 3500 if (ctx->app->check_url(s))
adamc@770 3501 return s;
adamc@770 3502 else
adamc@770 3503 return NULL;
adamc@770 3504 }
adamc@770 3505
adam@1465 3506 static int mime_format(const char *s) {
adamc@770 3507 for (; *s; ++s)
adamc@1153 3508 if (!isalnum((int)*s) && *s != '/' && *s != '-' && *s != '.')
adamc@770 3509 return 0;
adamc@770 3510
adamc@770 3511 return 1;
adamc@770 3512 }
adamc@770 3513
adamc@741 3514 uw_Basis_string uw_Basis_blessMime(uw_context ctx, uw_Basis_string s) {
adamc@770 3515 if (!mime_format(s))
adamc@770 3516 uw_error(ctx, FATAL, "MIME type \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s));
adamc@770 3517
adamc@1094 3518 if (ctx->app->check_mime(s))
adamc@770 3519 return s;
adamc@770 3520 else
adamc@770 3521 uw_error(ctx, FATAL, "Disallowed MIME type %s", uw_Basis_htmlifyString(ctx, s));
adamc@770 3522 }
adamc@770 3523
adamc@770 3524 uw_Basis_string uw_Basis_checkMime(uw_context ctx, uw_Basis_string s) {
adamc@770 3525 if (!mime_format(s))
adamc@770 3526 return NULL;
adamc@770 3527
adamc@1094 3528 if (ctx->app->check_mime(s))
adamc@770 3529 return s;
adamc@770 3530 else
adamc@770 3531 return NULL;
adamc@741 3532 }
adamc@741 3533
adam@1465 3534 uw_Basis_string uw_Basis_blessRequestHeader(uw_context ctx, uw_Basis_string s) {
adam@1465 3535 if (!mime_format(s))
adam@1465 3536 uw_error(ctx, FATAL, "Request header \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s));
adam@1465 3537
adam@1465 3538 if (ctx->app->check_requestHeader(s))
adam@1465 3539 return s;
adam@1465 3540 else
adam@1465 3541 uw_error(ctx, FATAL, "Disallowed request header %s", uw_Basis_htmlifyString(ctx, s));
adam@1465 3542 }
adam@1465 3543
adam@1465 3544 uw_Basis_string uw_Basis_checkRequestHeader(uw_context ctx, uw_Basis_string s) {
adam@1465 3545 if (!mime_format(s))
adam@1465 3546 return NULL;
adam@1465 3547
adam@1465 3548 if (ctx->app->check_requestHeader(s))
adam@1465 3549 return s;
adam@1465 3550 else
adam@1465 3551 return NULL;
adam@1465 3552 }
adam@1465 3553
adam@1465 3554 uw_Basis_string uw_Basis_blessResponseHeader(uw_context ctx, uw_Basis_string s) {
adam@1465 3555 if (!mime_format(s))
adam@1465 3556 uw_error(ctx, FATAL, "Response header \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s));
adam@1465 3557
adam@1465 3558 if (ctx->app->check_responseHeader(s))
adam@1465 3559 return s;
adam@1465 3560 else
adam@1465 3561 uw_error(ctx, FATAL, "Disallowed response header %s", uw_Basis_htmlifyString(ctx, s));
adam@1465 3562 }
adam@1465 3563
adam@1799 3564 static int envVar_format(const char *s) {
adam@1799 3565 for (; *s; ++s)
adam@1799 3566 if (!isalnum((int)*s) && *s != '_' && *s != '.')
adam@1799 3567 return 0;
adam@1799 3568
adam@1799 3569 return 1;
adam@1799 3570 }
adam@1799 3571
adam@1465 3572 uw_Basis_string uw_Basis_checkResponseHeader(uw_context ctx, uw_Basis_string s) {
adam@1799 3573 if (!envVar_format(s))
adam@1465 3574 return NULL;
adam@1465 3575
adam@1465 3576 if (ctx->app->check_responseHeader(s))
adam@1465 3577 return s;
adam@1465 3578 else
adam@1465 3579 return NULL;
adam@1465 3580 }
adam@1465 3581
adam@1799 3582 uw_Basis_string uw_Basis_blessEnvVar(uw_context ctx, uw_Basis_string s) {
adam@1799 3583 if (!envVar_format(s))
adam@1799 3584 uw_error(ctx, FATAL, "Environment variable \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s));
adam@1799 3585
adam@1799 3586 if (ctx->app->check_envVar(s))
adam@1799 3587 return s;
adam@1799 3588 else
adam@1799 3589 uw_error(ctx, FATAL, "Disallowed environment variable %s", uw_Basis_htmlifyString(ctx, s));
adam@1799 3590 }
adam@1799 3591
adam@1799 3592 uw_Basis_string uw_Basis_checkEnvVar(uw_context ctx, uw_Basis_string s) {
adam@1799 3593 if (!mime_format(s))
adam@1799 3594 return NULL;
adam@1799 3595
adam@1799 3596 if (ctx->app->check_envVar(s))
adam@1799 3597 return s;
adam@1799 3598 else
adam@1799 3599 return NULL;
adam@1799 3600 }
adam@1799 3601
adam@1465 3602 uw_Basis_string uw_Basis_getHeader(uw_context ctx, uw_Basis_string name) {
adam@1465 3603 return uw_Basis_requestHeader(ctx, name);
adam@1465 3604 }
adam@1465 3605
adam@1465 3606 static int mime_value_format(const char *s) {
adam@1465 3607 for (; *s; ++s)
adam@1465 3608 if (*s == '\r' || *s == '\n')
adam@1465 3609 return 0;
adam@1465 3610
adam@1465 3611 return 1;
adam@1465 3612 }
adam@1465 3613
adam@1465 3614 uw_unit uw_Basis_setHeader(uw_context ctx, uw_Basis_string name, uw_Basis_string value) {
adam@1465 3615 if (!mime_value_format(value))
adam@1465 3616 uw_error(ctx, FATAL, "Invalid value for HTTP response header");
adam@1465 3617
adam@1465 3618 uw_write_header(ctx, name);
adam@1465 3619 uw_write_header(ctx, ": ");
adam@1465 3620 uw_write_header(ctx, value);
adam@1465 3621 uw_write_header(ctx, "\r\n");
adam@1465 3622
adam@1465 3623 return uw_unit_v;
adam@1465 3624 }
adam@1465 3625
adam@1799 3626 uw_Basis_string uw_Basis_getenv(uw_context ctx, uw_Basis_string name) {
adam@1799 3627 return ctx->get_env(ctx->get_env_data, name);
adam@1799 3628 }
adam@1799 3629
adamc@734 3630 uw_Basis_string uw_unnull(uw_Basis_string s) {
adamc@734 3631 return s ? s : "";
adamc@734 3632 }
adamc@734 3633
adamc@734 3634 uw_Basis_string uw_Basis_makeSigString(uw_context ctx, uw_Basis_string sig) {
adamc@734 3635 uw_Basis_string r = uw_malloc(ctx, 2 * uw_hash_blocksize + 1);
adamc@734 3636 int i;
adamc@734 3637
adamc@734 3638 for (i = 0; i < uw_hash_blocksize; ++i)
adamc@734 3639 sprintf(&r[2*i], "%.02X", ((unsigned char *)sig)[i]);
adamc@734 3640
adamc@734 3641 return r;
adamc@734 3642 }
adamc@734 3643
adam@1512 3644 /* This bit of crafty code is intended to prevent GCC from performing
adam@1512 3645 * optimizations that would enable timing attacks. See:
adam@1512 3646 * http://www.impredicative.com/pipermail/ur/2011-July/000659.html
adam@1512 3647 */
adam@1512 3648 int uw_streq(uw_Basis_string s1, uw_Basis_string s2) {
adam@1512 3649 int i, x = 0, len1 = strlen(s1);
adam@1512 3650
adam@1512 3651 if (len1 != strlen(s2)) return 0;
adam@1512 3652
adam@1512 3653 for (i = 0; i < len1; ++i) {
adam@1512 3654 __asm__ __volatile__ ("");
adam@1512 3655 x |= s1[i] ^ s2[i];
adam@1512 3656 }
adam@1512 3657
adam@1512 3658 return x == 0;
adam@1512 3659 }
adam@1512 3660
adamc@734 3661 uw_Basis_string uw_Basis_sigString(uw_context ctx, uw_unit u) {
adam@1877 3662 ctx->usedSig = 1;
adamc@1094 3663 return ctx->app->cookie_sig(ctx);
adamc@734 3664 }
adamc@737 3665
adamc@737 3666 uw_Basis_string uw_Basis_fileName(uw_context ctx, uw_Basis_file f) {
adamc@737 3667 return f.name;
adamc@737 3668 }
adamc@737 3669
adamc@740 3670 uw_Basis_string uw_Basis_fileMimeType(uw_context ctx, uw_Basis_file f) {
adamc@740 3671 return f.type;
adamc@740 3672 }
adamc@740 3673
adamc@745 3674 uw_Basis_int uw_Basis_blobSize(uw_context ctx, uw_Basis_blob b) {
adamc@745 3675 return b.size;
adamc@745 3676 }
adamc@745 3677
adamc@1119 3678 uw_Basis_blob uw_Basis_textBlob(uw_context ctx, uw_Basis_string s) {
adamc@1119 3679 uw_Basis_blob b = {strlen(s), s};
adamc@1119 3680
adamc@1119 3681 return b;
adamc@1119 3682 }
adamc@1119 3683
adamc@737 3684 uw_Basis_blob uw_Basis_fileData(uw_context ctx, uw_Basis_file f) {
adamc@737 3685 return f.data;
adamc@737 3686 }
adamc@741 3687
adam@1347 3688 uw_Basis_string uw_Basis_postType(uw_context ctx, uw_Basis_postBody pb) {
adam@1347 3689 return pb.type;
adam@1347 3690 }
adam@1347 3691
adam@1347 3692 uw_Basis_string uw_Basis_postData(uw_context ctx, uw_Basis_postBody pb) {
adam@1347 3693 return pb.data;
adam@1347 3694 }
adam@1347 3695
adam@1471 3696 static char *old_headers(uw_context ctx) {
adam@1471 3697 if (uw_buffer_used(&ctx->outHeaders) == 0)
adam@1471 3698 return NULL;
adam@1471 3699 else {
adam@1471 3700 char *s = strchr(ctx->outHeaders.start, '\n');
adam@1471 3701
adam@1471 3702 if (s == NULL || strncasecmp(s+1, "Content-type: ", 14))
adam@1471 3703 return NULL;
adam@1471 3704 else {
adam@1471 3705 s = strchr(s+15, '\n');
adam@1471 3706 if (s == NULL)
adam@1471 3707 return NULL;
adam@1471 3708 else
adam@1471 3709 return uw_strdup(ctx, s+1);
adam@1471 3710 }
adam@1471 3711 }
adam@1471 3712 }
adam@1471 3713
adamc@741 3714 __attribute__((noreturn)) void uw_return_blob(uw_context ctx, uw_Basis_blob b, uw_Basis_string mimeType) {
adamc@741 3715 cleanup *cl;
adamc@741 3716 int len;
adam@1471 3717 char *oldh;
adamc@741 3718
adam@1451 3719 if (!ctx->allowed_to_return_indirectly)
adam@1451 3720 uw_error(ctx, FATAL, "Tried to return a blob from an RPC");
adam@1451 3721
adamc@1065 3722 ctx->returning_indirectly = 1;
adam@1471 3723 oldh = old_headers(ctx);
adam@1369 3724 uw_buffer_reset(&ctx->outHeaders);
adam@1369 3725 uw_buffer_reset(&ctx->page);
adamc@741 3726
adamc@856 3727 uw_write_header(ctx, on_success);
adamc@856 3728 uw_write_header(ctx, "Content-Type: ");
adamc@741 3729 uw_write_header(ctx, mimeType);
adam@1934 3730 uw_write_header(ctx, "\r\nContent-length: ");
adam@1369 3731 ctx_uw_buffer_check(ctx, "headers", &ctx->outHeaders, INTS_MAX);
adamc@1134 3732 sprintf(ctx->outHeaders.front, "%lu%n", (unsigned long)b.size, &len);
adamc@741 3733 ctx->outHeaders.front += len;
adamc@1066 3734 uw_write_header(ctx, "\r\n");
adam@1471 3735 if (oldh) uw_write_header(ctx, oldh);
adamc@741 3736
adam@1369 3737 ctx_uw_buffer_append(ctx, "page", &ctx->page, b.data, b.size);
adamc@741 3738
adamc@741 3739 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@741 3740 cl->func(cl->arg);
adamc@741 3741
adamc@741 3742 ctx->cleanup_front = ctx->cleanup;
adamc@741 3743
adamc@1065 3744 longjmp(ctx->jmp_buf, RETURN_INDIRECTLY);
adamc@1065 3745 }
adamc@1065 3746
adam@1932 3747 __attribute__((noreturn)) void uw_return_blob_from_page(uw_context ctx, uw_Basis_string mimeType) {
adam@1932 3748 cleanup *cl;
adam@1932 3749 int len;
adam@1932 3750 char *oldh;
adam@1932 3751
adam@1932 3752 if (!ctx->allowed_to_return_indirectly)
adam@1932 3753 uw_error(ctx, FATAL, "Tried to return a blob from an RPC");
adam@1932 3754
adam@1932 3755 ctx->returning_indirectly = 1;
adam@1932 3756 oldh = old_headers(ctx);
adam@1932 3757 uw_buffer_reset(&ctx->outHeaders);
adam@1932 3758
adam@1932 3759 uw_write_header(ctx, on_success);
adam@1932 3760 uw_write_header(ctx, "Content-Type: ");
adam@1932 3761 uw_write_header(ctx, mimeType);
adam@1934 3762 uw_write_header(ctx, "\r\nContent-length: ");
adam@1932 3763 ctx_uw_buffer_check(ctx, "headers", &ctx->outHeaders, INTS_MAX);
adam@1932 3764 sprintf(ctx->outHeaders.front, "%lu%n", (unsigned long)uw_buffer_used(&ctx->page), &len);
adam@1932 3765 ctx->outHeaders.front += len;
adam@1932 3766 uw_write_header(ctx, "\r\n");
adam@1932 3767 if (oldh) uw_write_header(ctx, oldh);
adam@1932 3768
adam@1932 3769 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adam@1932 3770 cl->func(cl->arg);
adam@1932 3771
adam@1932 3772 ctx->cleanup_front = ctx->cleanup;
adam@1932 3773
adam@1932 3774 longjmp(ctx->jmp_buf, RETURN_INDIRECTLY);
adam@1932 3775 }
adam@1932 3776
adamc@1065 3777 __attribute__((noreturn)) void uw_redirect(uw_context ctx, uw_Basis_string url) {
adamc@1065 3778 cleanup *cl;
adamc@1066 3779 char *s;
adam@1471 3780 char *oldh;
adamc@1065 3781
adam@1451 3782 if (!ctx->allowed_to_return_indirectly)
adam@1451 3783 uw_error(ctx, FATAL, "Tried to redirect from an RPC");
adam@1451 3784
adamc@1065 3785 ctx->returning_indirectly = 1;
adam@1471 3786 oldh = old_headers(ctx);
adam@1369 3787 uw_buffer_reset(&ctx->page);
adam@1369 3788 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->outHeaders)+1);
adam@1369 3789 memcpy(ctx->page.start, ctx->outHeaders.start, uw_buffer_used(&ctx->outHeaders));
adam@1369 3790 ctx->page.start[uw_buffer_used(&ctx->outHeaders)] = 0;
adam@1369 3791 uw_buffer_reset(&ctx->outHeaders);
adamc@1065 3792
adam@1409 3793 if (on_success[0])
adam@1409 3794 uw_write_header(ctx, on_redirect);
adam@1409 3795 else
adam@1409 3796 uw_write_header(ctx, "Status: 303 See Other\r\n");
adam@1409 3797 s = on_success[0] ? strchr(ctx->page.start, '\n') : ctx->page.start;
adamc@1066 3798 if (s) {
adamc@1066 3799 char *s2;
adam@1409 3800 if (s[0] == '\n') ++s;
adam@1409 3801 for (; (s2 = strchr(s, '\n')); s = s2+1) {
adamc@1066 3802 *s2 = 0;
adamc@1066 3803 if (!strncmp(s, "Set-Cookie: ", 12)) {
adamc@1066 3804 uw_write_header(ctx, s);
adamc@1066 3805 uw_write_header(ctx, "\n");
adamc@1066 3806 }
adamc@1066 3807 }
adamc@1066 3808 }
adamc@1066 3809
adamc@1065 3810 uw_write_header(ctx, "Location: ");
adamc@1065 3811 uw_write_header(ctx, url);
adamc@1065 3812 uw_write_header(ctx, "\r\n\r\n");
adam@1471 3813 if (oldh) uw_write_header(ctx, oldh);
adamc@1065 3814
adamc@1065 3815 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@1065 3816 cl->func(cl->arg);
adamc@1065 3817
adamc@1065 3818 ctx->cleanup_front = ctx->cleanup;
adamc@1065 3819
adamc@1065 3820 longjmp(ctx->jmp_buf, RETURN_INDIRECTLY);
adamc@741 3821 }
adamc@874 3822
adamc@874 3823 uw_Basis_string uw_Basis_unAs(uw_context ctx, uw_Basis_string s) {
adam@1467 3824 uw_Basis_string ret = uw_malloc(ctx, strlen(s) + 1), r = ret;
adamc@874 3825
adamc@874 3826 for (; *s; ++s) {
adamc@874 3827 if (s[0] == '\'') {
adamc@874 3828 *r++ = '\'';
adamc@874 3829 for (++s; *s; ++s) {
adamc@874 3830 if (s[0] == '\'') {
adamc@874 3831 *r++ = '\'';
adamc@874 3832 break;
adamc@874 3833 } else if (s[0] == '\\') {
adam@1467 3834 *r++ = '\\';
adam@1467 3835 *r++ = s[1];
adam@1467 3836 ++s;
adamc@874 3837 } else
adamc@874 3838 *r++ = s[0];
adamc@874 3839 }
adamc@874 3840 if (*s == 0) break;
adam@1467 3841 } else if (s[0] == 'T' && s[1] == '_' && s[2] == 'T' && s[3] == '.')
adam@1467 3842 s += 3;
adamc@874 3843 else
adamc@874 3844 *r++ = s[0];
adamc@874 3845 }
adamc@874 3846
adam@1467 3847 *r = 0;
adam@1467 3848 return ret;
adamc@874 3849 }
adamc@874 3850
adamc@922 3851 uw_Basis_string uw_Basis_mstrcat(uw_context ctx, ...) {
adamc@922 3852 va_list ap;
adamc@922 3853 size_t len = 1;
adamc@922 3854 char *s, *r, *s2;
adamc@922 3855
adamc@922 3856 va_start(ap, ctx);
adamc@922 3857 for (s = va_arg(ap, char*); s; s = va_arg(ap, char*))
adamc@922 3858 len += strlen(s);
adamc@922 3859 va_end(ap);
adamc@922 3860
adamc@922 3861 r = uw_malloc(ctx, len);
adamc@922 3862 va_start(ap, ctx);
adamc@922 3863 for (s2 = r, s = va_arg(ap, char*); s; s = va_arg(ap, char*))
adamc@922 3864 while (*s)
adamc@922 3865 *s2++ = *s++;
adamc@922 3866 va_end(ap);
adamc@922 3867 *s2 = 0;
adamc@922 3868
adamc@922 3869 return r;
adamc@922 3870 }
adamc@1006 3871
adam@1365 3872 const uw_Basis_time uw_Basis_minTime = {};
adamc@1050 3873
adamc@1006 3874 uw_Basis_time uw_Basis_now(uw_context ctx) {
adam@1365 3875 uw_Basis_time r = { time(NULL) };
adam@1365 3876 return r;
adamc@1006 3877 }
adamc@1061 3878
adam@1369 3879 uw_Basis_time uw_Basis_addSeconds(uw_context ctx, uw_Basis_time tm, uw_Basis_int n) {
adam@1369 3880 tm.seconds += n;
adam@1365 3881 return tm;
adamc@1138 3882 }
adamc@1138 3883
adam@1514 3884 uw_Basis_int uw_Basis_diffInSeconds(uw_context ctx, uw_Basis_time tm1, uw_Basis_time tm2) {
adam@1514 3885 return difftime(tm2.seconds, tm1.seconds);
adam@1514 3886 }
adam@1514 3887
adam@1685 3888 uw_Basis_int uw_Basis_toMilliseconds(uw_context ctx, uw_Basis_time tm) {
adam@1685 3889 return tm.seconds * 1000 + tm.microseconds / 1000;
adam@1685 3890 }
adam@1685 3891
adam@1685 3892 uw_Basis_int uw_Basis_diffInMilliseconds(uw_context ctx, uw_Basis_time tm1, uw_Basis_time tm2) {
adam@1685 3893 return uw_Basis_toMilliseconds(ctx, tm2) - uw_Basis_toMilliseconds(ctx, tm1);
adam@1685 3894 }
adam@1685 3895
adam@1514 3896 uw_Basis_int uw_Basis_toSeconds(uw_context ctx, uw_Basis_time tm) {
adam@1514 3897 return tm.seconds;
adam@1514 3898 }
adam@1514 3899
adamc@1061 3900 void *uw_get_global(uw_context ctx, char *name) {
adamc@1061 3901 int i;
adamc@1061 3902
adamc@1061 3903 for (i = 0; i < ctx->n_globals; ++i)
adamc@1061 3904 if (!strcmp(name, ctx->globals[i].name))
adamc@1061 3905 return ctx->globals[i].data;
adamc@1061 3906
adamc@1061 3907 return NULL;
adamc@1061 3908 }
adamc@1061 3909
adamc@1113 3910 size_t uw_globals_max = SIZE_MAX;
adamc@1113 3911
adamc@1061 3912 void uw_set_global(uw_context ctx, char *name, void *data, void (*free)(void*)) {
adamc@1061 3913 int i;
adamc@1061 3914
adamc@1061 3915 for (i = 0; i < ctx->n_globals; ++i)
adamc@1061 3916 if (!strcmp(name, ctx->globals[i].name)) {
adamc@1089 3917 if (ctx->globals[i].free)
adamc@1061 3918 ctx->globals[i].free(ctx->globals[i].data);
adamc@1061 3919 ctx->globals[i].data = data;
adamc@1061 3920 ctx->globals[i].free = free;
adamc@1061 3921 return;
adamc@1061 3922 }
adamc@1089 3923
adamc@1113 3924 if (ctx->n_globals+1 > uw_globals_max)
adamc@1113 3925 uw_error(ctx, FATAL, "Exceeded limit on number of globals");
adamc@1113 3926
adamc@1061 3927 ++ctx->n_globals;
adamc@1061 3928 ctx->globals = realloc(ctx->globals, ctx->n_globals * sizeof(global));
adamc@1061 3929 ctx->globals[ctx->n_globals-1].name = name;
adamc@1061 3930 ctx->globals[ctx->n_globals-1].data = data;
adamc@1061 3931 ctx->globals[ctx->n_globals-1].free = free;
adamc@1061 3932 }
adamc@1061 3933
adamc@1061 3934 uw_Basis_bool uw_Basis_isalnum(uw_context ctx, uw_Basis_char c) {
adamc@1153 3935 return !!isalnum((int)c);
adamc@1061 3936 }
adamc@1061 3937
adamc@1061 3938 uw_Basis_bool uw_Basis_isalpha(uw_context ctx, uw_Basis_char c) {
adamc@1153 3939 return !!isalpha((int)c);
adamc@1061 3940 }
adamc@1061 3941
adamc@1061 3942 uw_Basis_bool uw_Basis_isblank(uw_context ctx, uw_Basis_char c) {
adamc@1153 3943 return !!isblank((int)c);
adamc@1061 3944 }
adamc@1061 3945
adamc@1061 3946 uw_Basis_bool uw_Basis_iscntrl(uw_context ctx, uw_Basis_char c) {
adamc@1153 3947 return !!iscntrl((int)c);
adamc@1061 3948 }
adamc@1061 3949
adamc@1061 3950 uw_Basis_bool uw_Basis_isdigit(uw_context ctx, uw_Basis_char c) {
adamc@1153 3951 return !!isdigit((int)c);
adamc@1061 3952 }
adamc@1061 3953
adamc@1061 3954 uw_Basis_bool uw_Basis_isgraph(uw_context ctx, uw_Basis_char c) {
adamc@1153 3955 return !!isgraph((int)c);
adamc@1061 3956 }
adamc@1061 3957
adamc@1061 3958 uw_Basis_bool uw_Basis_islower(uw_context ctx, uw_Basis_char c) {
adamc@1153 3959 return !!islower((int)c);
adamc@1061 3960 }
adamc@1061 3961
adamc@1061 3962 uw_Basis_bool uw_Basis_isprint(uw_context ctx, uw_Basis_char c) {
adamc@1153 3963 return !!isprint((int)c);
adamc@1061 3964 }
adamc@1061 3965
adamc@1061 3966 uw_Basis_bool uw_Basis_ispunct(uw_context ctx, uw_Basis_char c) {
adamc@1153 3967 return !!ispunct((int)c);
adamc@1061 3968 }
adamc@1061 3969
adamc@1061 3970 uw_Basis_bool uw_Basis_isspace(uw_context ctx, uw_Basis_char c) {
adamc@1153 3971 return !!isspace((int)c);
adamc@1061 3972 }
adamc@1063 3973
adamc@1061 3974 uw_Basis_bool uw_Basis_isupper(uw_context ctx, uw_Basis_char c) {
adamc@1153 3975 return !!isupper((int)c);
adamc@1061 3976 }
adamc@1061 3977
adamc@1061 3978 uw_Basis_bool uw_Basis_isxdigit(uw_context ctx, uw_Basis_char c) {
adamc@1153 3979 return !!isxdigit((int)c);
adamc@1061 3980 }
adamc@1061 3981
adamc@1061 3982 uw_Basis_char uw_Basis_tolower(uw_context ctx, uw_Basis_char c) {
adamc@1153 3983 return tolower((int)c);
adamc@1061 3984 }
adamc@1061 3985
adamc@1061 3986 uw_Basis_char uw_Basis_toupper(uw_context ctx, uw_Basis_char c) {
adamc@1153 3987 return toupper((int)c);
adamc@1061 3988 }
adamc@1066 3989
adamc@1128 3990 uw_Basis_int uw_Basis_ord(uw_context ctx, uw_Basis_char c) {
adamc@1128 3991 return (unsigned char)c;
adamc@1128 3992 }
adamc@1128 3993
adamc@1128 3994 uw_Basis_char uw_Basis_chr(uw_context ctx, uw_Basis_int n) {
adamc@1128 3995 return n;
adamc@1128 3996 }
adamc@1128 3997
adamc@1066 3998 uw_Basis_string uw_Basis_currentUrl(uw_context ctx) {
adamc@1066 3999 return ctx->current_url;
adamc@1066 4000 }
adamc@1066 4001
adamc@1066 4002 void uw_set_currentUrl(uw_context ctx, char *s) {
adamc@1066 4003 ctx->current_url = s;
adamc@1066 4004 }
adamc@1114 4005
adamc@1114 4006 void uw_set_deadline(uw_context ctx, int n) {
adamc@1114 4007 ctx->deadline = n;
adamc@1114 4008 }
adamc@1114 4009
adamc@1114 4010 void uw_check_deadline(uw_context ctx) {
adamc@1114 4011 if (uw_time > ctx->deadline)
adamc@1114 4012 uw_error(ctx, FATAL, "Maximum running time exceeded");
adamc@1114 4013 }
adamc@1115 4014
adamc@1115 4015 size_t uw_database_max = SIZE_MAX;
adamc@1120 4016
adam@1389 4017 uw_Basis_int uw_Basis_naughtyDebug(uw_context ctx, uw_Basis_string s) {
adam@1389 4018 fprintf(stderr, "%s\n", s);
adam@1389 4019 return 0;
adam@1389 4020 }
adam@1389 4021
adamc@1120 4022 uw_Basis_unit uw_Basis_debug(uw_context ctx, uw_Basis_string s) {
adam@1334 4023 if (ctx->log_debug)
adam@1334 4024 ctx->log_debug(ctx->logger_data, "%s\n", s);
adam@1334 4025 else
adam@1334 4026 fprintf(stderr, "%s\n", s);
adamc@1120 4027 return uw_unit_v;
adamc@1120 4028 }
adamc@1250 4029
adam@1933 4030 static pthread_mutex_t rand_mutex = PTHREAD_MUTEX_INITIALIZER;
adam@1933 4031
adamc@1250 4032 uw_Basis_int uw_Basis_rand(uw_context ctx) {
adam@1486 4033 uw_Basis_int ret;
adam@1933 4034 pthread_mutex_lock(&rand_mutex);
adam@1486 4035 int r = RAND_bytes((unsigned char *)&ret, sizeof ret);
adam@1933 4036 pthread_mutex_unlock(&rand_mutex);
adam@1486 4037
adam@1486 4038 if (r)
adam@1486 4039 return abs(ret);
adam@1486 4040 else
adam@1486 4041 uw_error(ctx, FATAL, "Random number generation failed");
adamc@1250 4042 }
adam@1347 4043
adam@1347 4044 void uw_noPostBody(uw_context ctx) {
adam@1347 4045 ctx->hasPostBody = 0;
adam@1347 4046 }
adam@1347 4047
adam@1347 4048 void uw_postBody(uw_context ctx, uw_Basis_postBody pb) {
adam@1347 4049 ctx->hasPostBody = 1;
adam@1347 4050 ctx->postBody = pb;
adam@1347 4051 }
adam@1347 4052
adam@1347 4053 int uw_hasPostBody(uw_context ctx) {
adam@1347 4054 return ctx->hasPostBody;
adam@1347 4055 }
adam@1347 4056
adam@1386 4057 void uw_isPost(uw_context ctx) {
adam@1386 4058 ctx->isPost = 1;
adam@1386 4059 }
adam@1386 4060
adam@1386 4061 uw_Basis_bool uw_Basis_currentUrlHasPost(uw_context ctx) {
adam@1386 4062 return ctx->isPost;
adam@1386 4063 }
adam@1386 4064
adam@1485 4065 uw_Basis_bool uw_Basis_currentUrlHasQueryString(uw_context ctx) {
adam@1485 4066 return ctx->queryString != NULL && ctx->queryString[0] != 0;
adam@1485 4067 }
adam@1485 4068
adam@1370 4069 void uw_setQueryString(uw_context ctx, uw_Basis_string s) {
adam@1370 4070 ctx->queryString = s;
adam@1370 4071 }
adam@1370 4072
adam@1370 4073 uw_Basis_string uw_queryString(uw_context ctx) {
adam@1370 4074 return ctx->queryString;
adam@1370 4075 }
adam@1370 4076
adam@1347 4077 uw_Basis_postBody uw_getPostBody(uw_context ctx) {
adam@1347 4078 if (ctx->hasPostBody)
adam@1347 4079 return ctx->postBody;
adam@1347 4080 else
adam@1347 4081 uw_error(ctx, FATAL, "Asked for POST body when none exists");
adam@1347 4082 }
adam@1349 4083
adam@1349 4084 failure_kind uw_runCallback(uw_context ctx, void (*callback)(uw_context)) {
adam@1349 4085 int r = setjmp(ctx->jmp_buf);
adam@1349 4086
adam@1349 4087 if (r == 0) {
adam@1418 4088 if (ctx->app->db_begin(ctx))
adam@1418 4089 uw_error(ctx, BOUNDED_RETRY, "Error running SQL BEGIN");
adam@1931 4090 ctx->transaction_started = 1;
adam@1418 4091
adam@1349 4092 callback(ctx);
adam@1349 4093 }
adam@1349 4094
adam@1349 4095 return r;
adam@1349 4096 }
adam@1360 4097
adam@1360 4098 uw_Basis_string uw_Basis_crypt(uw_context ctx, uw_Basis_string key, uw_Basis_string salt) {
adam@1397 4099 char buf[14];
adam@1397 4100 return uw_strdup(ctx, DES_fcrypt(key, salt, buf));
adam@1360 4101 }
adam@1365 4102
adam@1365 4103 uw_Basis_bool uw_Basis_eq_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_lt_time(uw_context ctx, uw_Basis_time t1, uw_Basis_time t2) {
adam@1365 4108 return !!(t1.seconds < t2.seconds || t1.microseconds < t2.microseconds);
adam@1365 4109 }
adam@1365 4110
adam@1365 4111 uw_Basis_bool uw_Basis_le_time(uw_context ctx, uw_Basis_time t1, uw_Basis_time t2) {
adam@1365 4112 return !!(uw_Basis_eq_time(ctx, t1, t2) || uw_Basis_lt_time(ctx, t1, t2));
adam@1365 4113 }
adam@1365 4114
adam@1372 4115 uw_Basis_time *uw_Basis_readUtc(uw_context ctx, uw_Basis_string s) {
adam@1374 4116 struct tm stm = {};
adam@1375 4117 char *end = strchr(s, 0);
adam@1449 4118 stm.tm_isdst = -1;
adam@1375 4119
adam@1632 4120 if (strptime(s, TIME_FMT_PG, &stm) == end || strptime(s, TIME_FMT, &stm) == end || strptime(s, TIME_FMT_JS, &stm) == end) {
adam@1373 4121 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adam@1375 4122
kkallio@1448 4123 r->seconds = timegm(&stm);
adam@1373 4124 r->microseconds = 0;
adam@1373 4125
adam@1373 4126 return r;
adam@1374 4127 }
adam@1374 4128 else
adam@1373 4129 return NULL;
adam@1372 4130 }
adam@1407 4131
adam@1407 4132 failure_kind uw_begin_onError(uw_context ctx, char *msg) {
adam@1407 4133 int r = setjmp(ctx->jmp_buf);
adam@1407 4134
adam@1407 4135 if (ctx->app->on_error) {
adam@1407 4136 if (r == 0) {
adam@1407 4137 if (ctx->app->db_begin(ctx))
adam@1407 4138 uw_error(ctx, BOUNDED_RETRY, "Error running SQL BEGIN");
adam@1931 4139 ctx->transaction_started = 1;
adam@1407 4140
adam@1409 4141 uw_buffer_reset(&ctx->outHeaders);
adam@1409 4142 if (on_success[0])
adam@1409 4143 uw_write_header(ctx, "HTTP/1.1 ");
adam@1409 4144 else
adam@1409 4145 uw_write_header(ctx, "Status: ");
adam@1409 4146 uw_write_header(ctx, "500 Internal Server Error\r\n");
adam@1919 4147 uw_write_header(ctx, "Content-type: text/html\r\n");
adam@1407 4148 uw_write(ctx, begin_xhtml);
adam@1407 4149 ctx->app->on_error(ctx, msg);
adam@1407 4150 uw_write(ctx, "</html>");
adam@1407 4151 }
adam@1407 4152
adam@1407 4153 return r;
adam@1407 4154 } else
adam@1407 4155 uw_error(ctx, FATAL, "Tried to run nonexistent onError handler");
adam@1407 4156 }
adam@1451 4157
adam@1451 4158 void uw_mayReturnIndirectly(uw_context ctx) {
adam@1451 4159 ctx->allowed_to_return_indirectly = 1;
adam@1451 4160 }
adam@1483 4161
adam@1556 4162 uw_Basis_string uw_Basis_fresh(uw_context ctx) {
adam@1703 4163 int len;
adam@1703 4164 char *r;
adam@1703 4165
adam@1703 4166 uw_check_heap(ctx, 2+INTS_MAX);
adam@1703 4167 r = ctx->heap.front;
adam@1703 4168 sprintf(r, "uw%u%n", ctx->nextId++, &len);
adam@1703 4169 ctx->heap.front += len+1;
adam@1703 4170 return r;
adam@1556 4171 }
adam@1571 4172
adam@1571 4173 uw_Basis_float uw_Basis_floatFromInt(uw_context ctx, uw_Basis_int n) {
adam@1571 4174 return n;
adam@1571 4175 }
adam@1571 4176
adam@1571 4177 uw_Basis_int uw_Basis_ceil(uw_context ctx, uw_Basis_float n) {
adam@1571 4178 return ceil(n);
adam@1571 4179 }
adam@1571 4180
adam@1571 4181 uw_Basis_int uw_Basis_trunc(uw_context ctx, uw_Basis_float n) {
adam@1571 4182 return trunc(n);
adam@1571 4183 }
adam@1571 4184
adam@1571 4185 uw_Basis_int uw_Basis_round(uw_context ctx, uw_Basis_float n) {
adam@1571 4186 return round(n);
adam@1571 4187 }
adam@1755 4188
adam@1755 4189 uw_Basis_string uw_Basis_atom(uw_context ctx, uw_Basis_string s) {
adam@1755 4190 char *p;
adam@1755 4191
adam@1755 4192 for (p = s; *p; ++p) {
adam@1755 4193 char c = *p;
adam@1801 4194 if (!isalnum((int)c) && c != '+' && c != '-' && c != '.' && c != '%' && c != '#')
adam@1755 4195 uw_error(ctx, FATAL, "Disallowed character in CSS atom");
adam@1755 4196 }
adam@1755 4197
adam@1755 4198 return s;
adam@1755 4199 }
adam@1755 4200
adam@1755 4201 uw_Basis_string uw_Basis_css_url(uw_context ctx, uw_Basis_string s) {
adam@1755 4202 char *p;
adam@1755 4203
adam@1755 4204 for (p = s; *p; ++p) {
adam@1755 4205 char c = *p;
adam@1801 4206 if (!isalnum((int)c) && c != ':' && c != '/' && c != '.' && c != '_' && c != '+'
adam@1755 4207 && c != '-' && c != '%' && c != '?' && c != '&' && c != '=' && c != '#')
adam@1755 4208 uw_error(ctx, FATAL, "Disallowed character in CSS URL");
adam@1755 4209 }
adam@1755 4210
adam@1755 4211 return s;
adam@1755 4212 }
adam@1755 4213
adam@1755 4214 uw_Basis_string uw_Basis_property(uw_context ctx, uw_Basis_string s) {
adam@1755 4215 char *p;
adam@1755 4216
adam@1755 4217 if (!*s)
adam@1755 4218 uw_error(ctx, FATAL, "Empty CSS property");
adam@1755 4219
adam@1801 4220 if (!islower((int)s[0]) && s[0] != '_')
adam@1755 4221 uw_error(ctx, FATAL, "Bad initial character in CSS property");
adam@1755 4222
adam@1755 4223 for (p = s; *p; ++p) {
adam@1755 4224 char c = *p;
adam@1801 4225 if (!islower((int)c) && !isdigit((int)c) && c != '_' && c != '-')
adam@1755 4226 uw_error(ctx, FATAL, "Disallowed character in CSS property");
adam@1755 4227 }
adam@1755 4228
adam@1755 4229 return s;
adam@1755 4230 }
adam@1787 4231
adam@1787 4232 uw_Basis_string uw_Basis_fieldName(uw_context ctx, uw_Basis_postField f) {
adam@1787 4233 return f.name;
adam@1787 4234 }
adam@1787 4235
adam@1787 4236 uw_Basis_string uw_Basis_fieldValue(uw_context ctx, uw_Basis_postField f) {
adam@1787 4237 return f.value;
adam@1787 4238 }
adam@1787 4239
adam@1787 4240 uw_Basis_string uw_Basis_remainingFields(uw_context ctx, uw_Basis_postField f) {
adam@1787 4241 return f.remaining;
adam@1787 4242 }
adam@1787 4243
adam@1787 4244 uw_Basis_postField *uw_Basis_firstFormField(uw_context ctx, uw_Basis_string s) {
adam@1788 4245 char *unurl;
adam@1787 4246 uw_Basis_postField *f;
adam@1787 4247
adam@1788 4248 if (!ctx->hasPostBody)
adam@1788 4249 uw_error(ctx, FATAL, "firstFormField called when there is no POST body");
adam@1788 4250
adam@1788 4251 if (s < ctx->postBody.data || s >= ctx->postBody.data + ctx->postBody.len)
adam@1787 4252 return NULL;
adam@1787 4253
adam@1787 4254 f = uw_malloc(ctx, sizeof(uw_Basis_postField));
adam@1788 4255 unurl = s;
adam@1787 4256 f->name = uw_Basis_unurlifyString(ctx, &unurl);
adam@1788 4257 s = strchr(s, 0);
adam@1788 4258 if (!s)
adam@1788 4259 uw_error(ctx, FATAL, "firstFormField: Missing null terminator");
adam@1788 4260 ++s;
adam@1788 4261 unurl = s;
adam@1788 4262 f->value = uw_Basis_unurlifyString(ctx, &unurl);
adam@1788 4263 s = strchr(s, 0);
adam@1788 4264 if (!s)
adam@1788 4265 uw_error(ctx, FATAL, "firstFormField: Missing null terminator");
adam@1788 4266 f->remaining = s+1;
adam@1787 4267
adam@1787 4268 return f;
adam@1787 4269 }