annotate src/c/urweb.c @ 1152:e6eabf0c5ccc

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