annotate src/c/urweb.c @ 1334:a1aa62b472cf

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