annotate src/c/urweb.c @ 1702:06791667937e

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