annotate src/c/urweb.c @ 1105:a5c160636832

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