annotate src/c/urweb.c @ 1925:15802a00816c

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