annotate src/c/urweb.c @ 1113:40d48a2b78a7

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