annotate src/c/urweb.c @ 1286:829da30fb808

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