annotate src/c/urweb.c @ 2220:794017f378de

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