annotate src/c/urweb.c @ 2178:c76a9712334c

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