annotate src/c/urweb.c @ 1265:e8d68fd8ed4b

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