annotate src/c/urweb.c @ 1992:7075acda4456

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