annotate src/c/urweb.c @ 1114:01b6c7144a44

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