annotate src/c/urweb.c @ 1673:dae2c6fbf16c

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