annotate src/c/urweb.c @ 2177:00cf8214c2e3

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