annotate src/c/urweb.c @ 2084:0d48cfb59b29

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