annotate src/c/urweb.c @ 2224:5709482a2afd

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