annotate src/c/urweb.c @ 2186:d87d72b6e299

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