annotate src/c/urweb.c @ 1117:31ef567fa772

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