annotate src/c/urweb.c @ 1867:216a3a67ebe3

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