annotate src/c/urweb.c @ 1408:56ba9c442a2d

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