annotate src/c/urweb.c @ 1796:0de0daab5fbb

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