annotate src/c/urweb.c @ 2171:7187068f56f5

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