annotate src/c/urweb.c @ 1483:ebc30bb262d0

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