annotate src/c/urweb.c @ 1275:74150edf1134

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