annotate src/c/urweb.c @ 2288:98f96a976ede

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