annotate src/c/urweb.c @ 1112:7a31e0cf25e9

Proper C-side deserialization; Shake.sliceDb
author Adam Chlipala <adamc@hcoop.net>
date Sun, 03 Jan 2010 12:47:27 -0500
parents 7fc4e0087e50
children 40d48a2b78a7
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@1110 1177 void uw_clear_headers(uw_context ctx) {
adamc@1110 1178 buf_reset(&ctx->outHeaders);
adamc@1110 1179 }
adamc@1110 1180
adamc@562 1181 static void uw_check_script(uw_context ctx, size_t extra) {
adamc@667 1182 buf_check(&ctx->script, extra);
adamc@562 1183 }
adamc@562 1184
adamc@562 1185 void uw_write_script(uw_context ctx, uw_Basis_string s) {
adamc@562 1186 int len = strlen(s);
adamc@562 1187
adamc@562 1188 uw_check_script(ctx, len + 1);
adamc@666 1189 strcpy(ctx->script.front, s);
adamc@666 1190 ctx->script.front += len;
adamc@562 1191 }
adamc@562 1192
adamc@645 1193 const char *uw_Basis_get_script(uw_context ctx, uw_unit u) {
adamc@916 1194 return "<sc>";
adamc@565 1195 }
adamc@565 1196
adamc@695 1197 uw_Basis_string uw_Basis_maybe_onload(uw_context ctx, uw_Basis_string s) {
adamc@695 1198 if (s[0] == 0)
adamc@695 1199 return "";
adamc@695 1200 else {
adamc@695 1201 char *r = uw_malloc(ctx, 11 + strlen(s));
adamc@695 1202 sprintf(r, " onload='%s'", s);
adamc@695 1203 return r;
adamc@695 1204 }
adamc@695 1205 }
adamc@695 1206
adamc@1042 1207 uw_Basis_string uw_Basis_maybe_onunload(uw_context ctx, uw_Basis_string s) {
adamc@1042 1208 if (ctx->script_header[0] == 0)
adamc@1042 1209 return "";
adamc@1042 1210 else {
adamc@1042 1211 char *r = uw_malloc(ctx, 22 + strlen(s));
adamc@1042 1212 sprintf(r, " onunload='unload();%s'", s);
adamc@1042 1213 return r;
adamc@1042 1214 }
adamc@1042 1215 }
adamc@1042 1216
adamc@694 1217 const char *uw_Basis_get_settings(uw_context ctx, uw_unit u) {
adamc@736 1218 if (ctx->client == NULL) {
adamc@736 1219 if (ctx->needs_sig) {
adamc@1094 1220 char *sig = ctx->app->cookie_sig(ctx);
adamc@736 1221 char *r = uw_malloc(ctx, strlen(sig) + 8);
adamc@736 1222 sprintf(r, "sig=\"%s\";", sig);
adamc@736 1223 return r;
adamc@736 1224 }
adamc@736 1225 else
adamc@736 1226 return "";
adamc@736 1227 } else {
adamc@1094 1228 char *sig = ctx->needs_sig ? ctx->app->cookie_sig(ctx) : "";
adamc@1094 1229 char *r = uw_malloc(ctx, 59 + 3 * INTS_MAX + strlen(ctx->app->url_prefix)
adamc@736 1230 + (ctx->needs_sig ? strlen(sig) + 7 : 0));
adamc@736 1231 sprintf(r, "client_id=%u;client_pass=%d;url_prefix=\"%s\";timeout=%d;%s%s%slistener();",
adamc@682 1232 ctx->client->id,
adamc@682 1233 ctx->client->pass,
adamc@1094 1234 ctx->app->url_prefix,
adamc@1094 1235 ctx->app->timeout,
adamc@736 1236 ctx->needs_sig ? "sig=\"" : "",
adamc@736 1237 sig,
adamc@736 1238 ctx->needs_sig ? "\";" : "");
adamc@679 1239 return r;
adamc@668 1240 }
adamc@667 1241 }
adamc@667 1242
adamc@574 1243 uw_Basis_string uw_Basis_jsifyString(uw_context ctx, uw_Basis_string s) {
adamc@574 1244 char *r, *s2;
adamc@574 1245
adamc@574 1246 uw_check_heap(ctx, strlen(s) * 4 + 2);
adamc@574 1247
adamc@666 1248 r = s2 = ctx->heap.front;
adamc@574 1249 *s2++ = '"';
adamc@574 1250
adamc@574 1251 for (; *s; s++) {
adamc@574 1252 char c = *s;
adamc@574 1253
adamc@574 1254 switch (c) {
adamc@574 1255 case '"':
adamc@574 1256 strcpy(s2, "\\\"");
adamc@574 1257 s2 += 2;
adamc@574 1258 break;
adamc@1027 1259 case '\'':
adamc@1027 1260 strcpy(s2, "\\047");
adamc@1027 1261 s2 += 4;
adamc@1027 1262 break;
adamc@574 1263 case '\\':
adamc@574 1264 strcpy(s2, "\\\\");
adamc@574 1265 s2 += 2;
adamc@574 1266 break;
adamc@574 1267 default:
adamc@574 1268 if (isprint(c))
adamc@574 1269 *s2++ = c;
adamc@574 1270 else {
adamc@574 1271 sprintf(s2, "\\%3o", c);
adamc@574 1272 s2 += 4;
adamc@574 1273 }
adamc@574 1274 }
adamc@574 1275 }
adamc@574 1276
adamc@574 1277 strcpy(s2, "\"");
adamc@910 1278 ctx->heap.front = s2 + 2;
adamc@574 1279 return r;
adamc@574 1280 }
adamc@574 1281
adamc@1025 1282 uw_Basis_string uw_Basis_jsifyChar(uw_context ctx, uw_Basis_char c) {
adamc@1025 1283 char *r, *s2;
adamc@1025 1284
adamc@1025 1285 uw_check_heap(ctx, 6);
adamc@1025 1286
adamc@1025 1287 r = s2 = ctx->heap.front;
adamc@1025 1288 *s2++ = '"';
adamc@1025 1289
adamc@1025 1290 switch (c) {
adamc@1025 1291 case '"':
adamc@1025 1292 strcpy(s2, "\\\"");
adamc@1025 1293 s2 += 2;
adamc@1025 1294 break;
adamc@1027 1295 case '\'':
adamc@1027 1296 strcpy(s2, "\\047");
adamc@1027 1297 s2 += 4;
adamc@1027 1298 break;
adamc@1025 1299 case '\\':
adamc@1025 1300 strcpy(s2, "\\\\");
adamc@1025 1301 s2 += 2;
adamc@1025 1302 break;
adamc@1025 1303 default:
adamc@1025 1304 if (isprint(c))
adamc@1025 1305 *s2++ = c;
adamc@1025 1306 else {
adamc@1025 1307 sprintf(s2, "\\%3o", c);
adamc@1025 1308 s2 += 4;
adamc@1025 1309 }
adamc@1025 1310 }
adamc@1025 1311
adamc@1025 1312 strcpy(s2, "\"");
adamc@1025 1313 ctx->heap.front = s2 + 2;
adamc@1025 1314 return r;
adamc@1025 1315 }
adamc@1025 1316
adamc@574 1317 uw_Basis_string uw_Basis_jsifyString_ws(uw_context ctx, uw_Basis_string s) {
adamc@574 1318 char *r, *s2;
adamc@574 1319
adamc@574 1320 uw_check_script(ctx, strlen(s) * 4 + 2);
adamc@574 1321
adamc@666 1322 r = s2 = ctx->script.front;
adamc@574 1323 *s2++ = '"';
adamc@574 1324
adamc@574 1325 for (; *s; s++) {
adamc@574 1326 char c = *s;
adamc@574 1327
adamc@574 1328 switch (c) {
adamc@577 1329 case '\'':
adamc@1027 1330 strcpy(s2, "\\");
adamc@574 1331 s2 += 2;
adamc@574 1332 break;
adamc@574 1333 case '\\':
adamc@574 1334 strcpy(s2, "\\\\");
adamc@574 1335 s2 += 2;
adamc@574 1336 break;
adamc@574 1337 default:
adamc@574 1338 if (isprint(c))
adamc@574 1339 *s2++ = c;
adamc@574 1340 else {
adamc@574 1341 sprintf(s2, "\\%3o", c);
adamc@574 1342 s2 += 4;
adamc@574 1343 }
adamc@574 1344 }
adamc@574 1345 }
adamc@574 1346
adamc@574 1347 strcpy(s2, "\"");
adamc@666 1348 ctx->script.front = s2 + 1;
adamc@574 1349 return r;
adamc@574 1350 }
adamc@574 1351
adamc@682 1352 char *uw_Basis_jsifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 1353 if (ctx->client == NULL || chn.cli != ctx->client->id)
adamc@682 1354 return "null";
adamc@682 1355 else {
adamc@682 1356 int len;
adamc@682 1357 char *r;
adamc@682 1358
adamc@682 1359 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 1360 r = ctx->heap.front;
adamc@682 1361 sprintf(r, "%u%n", chn.chn, &len);
adamc@682 1362 ctx->heap.front += len+1;
adamc@682 1363 return r;
adamc@682 1364 }
adamc@682 1365 }
adamc@682 1366
adamc@577 1367 uw_Basis_int uw_Basis_new_client_source(uw_context ctx, uw_Basis_string s) {
adamc@577 1368 int len;
adamc@577 1369 size_t s_len = strlen(s);
adamc@562 1370
adamc@970 1371 uw_check_script(ctx, 18 + INTS_MAX + s_len);
adamc@970 1372 sprintf(ctx->script.front, "var s%d=sc(exec(%n", ctx->source_count, &len);
adamc@666 1373 ctx->script.front += len;
adamc@666 1374 strcpy(ctx->script.front, s);
adamc@666 1375 ctx->script.front += s_len;
adamc@970 1376 strcpy(ctx->script.front, "));");
adamc@972 1377 ctx->script.front += 3;
adamc@562 1378
adamc@565 1379 return ctx->source_count++;
adamc@117 1380 }
adamc@117 1381
adamc@577 1382 uw_unit uw_Basis_set_client_source(uw_context ctx, uw_Basis_int n, uw_Basis_string s) {
adamc@577 1383 int len;
adamc@577 1384 size_t s_len = strlen(s);
adamc@577 1385
adamc@970 1386 uw_check_script(ctx, 12 + INTS_MAX + s_len);
adamc@970 1387 sprintf(ctx->script.front, "sv(s%d,exec(%n", (int)n, &len);
adamc@666 1388 ctx->script.front += len;
adamc@666 1389 strcpy(ctx->script.front, s);
adamc@666 1390 ctx->script.front += s_len;
adamc@970 1391 strcpy(ctx->script.front, "));");
adamc@972 1392 ctx->script.front += 3;
adamc@577 1393
adamc@577 1394 return uw_unit_v;
adamc@577 1395 }
adamc@577 1396
adamc@311 1397 static void uw_check(uw_context ctx, size_t extra) {
adamc@667 1398 buf_check(&ctx->page, extra);
adamc@117 1399 }
adamc@117 1400
adamc@311 1401 static void uw_writec_unsafe(uw_context ctx, char c) {
adamc@666 1402 *(ctx->page.front)++ = c;
adamc@117 1403 }
adamc@117 1404
adamc@311 1405 void uw_writec(uw_context ctx, char c) {
adamc@311 1406 uw_check(ctx, 1);
adamc@311 1407 uw_writec_unsafe(ctx, c);
adamc@117 1408 }
adamc@117 1409
adamc@311 1410 static void uw_write_unsafe(uw_context ctx, const char* s) {
adamc@117 1411 int len = strlen(s);
adamc@666 1412 memcpy(ctx->page.front, s, len);
adamc@666 1413 ctx->page.front += len;
adamc@117 1414 }
adamc@117 1415
adamc@311 1416 void uw_write(uw_context ctx, const char* s) {
adamc@311 1417 uw_check(ctx, strlen(s) + 1);
adamc@311 1418 uw_write_unsafe(ctx, s);
adamc@666 1419 *ctx->page.front = 0;
adamc@102 1420 }
adamc@106 1421
adamc@311 1422 char *uw_Basis_attrifyInt(uw_context ctx, uw_Basis_int n) {
adamc@136 1423 char *result;
adamc@136 1424 int len;
adamc@311 1425 uw_check_heap(ctx, INTS_MAX);
adamc@666 1426 result = ctx->heap.front;
adamc@276 1427 sprintf(result, "%lld%n", n, &len);
adamc@666 1428 ctx->heap.front += len+1;
adamc@136 1429 return result;
adamc@106 1430 }
adamc@106 1431
adamc@311 1432 char *uw_Basis_attrifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@136 1433 char *result;
adamc@136 1434 int len;
adamc@311 1435 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 1436 result = ctx->heap.front;
adamc@136 1437 sprintf(result, "%g%n", n, &len);
adamc@666 1438 ctx->heap.front += len+1;
adamc@136 1439 return result;
adamc@106 1440 }
adamc@106 1441
adamc@1054 1442 static int isCont(unsigned char ch) {
adamc@1054 1443 return ch / 64 == 2;
adamc@1054 1444 }
adamc@1054 1445
adamc@311 1446 char *uw_Basis_attrifyString(uw_context ctx, uw_Basis_string s) {
adamc@136 1447 int len = strlen(s);
adamc@136 1448 char *result, *p;
adamc@311 1449 uw_check_heap(ctx, len * 6 + 1);
adamc@136 1450
adamc@666 1451 result = p = ctx->heap.front;
adamc@136 1452
adamc@136 1453 for (; *s; s++) {
adamc@1054 1454 unsigned char c = *s;
adamc@136 1455
adamc@136 1456 if (c == '"') {
adamc@136 1457 strcpy(p, "&quot;");
adamc@136 1458 p += 6;
adamc@136 1459 } else if (c == '&') {
adamc@136 1460 strcpy(p, "&amp;");
adamc@136 1461 p += 5;
adamc@136 1462 }
adamc@1059 1463 else
adamc@136 1464 *p++ = c;
adamc@136 1465 }
adamc@136 1466
adamc@137 1467 *p++ = 0;
adamc@666 1468 ctx->heap.front = p;
adamc@136 1469 return result;
adamc@106 1470 }
adamc@106 1471
adamc@1011 1472 char *uw_Basis_attrifyChar(uw_context ctx, uw_Basis_char c) {
adamc@1011 1473 char *result, *p;
adamc@1011 1474 uw_check_heap(ctx, 7);
adamc@1011 1475
adamc@1011 1476 result = p = ctx->heap.front;
adamc@1011 1477
adamc@1011 1478 if (c == '"') {
adamc@1011 1479 strcpy(p, "&quot;");
adamc@1011 1480 p += 6;
adamc@1011 1481 } else if (c == '&') {
adamc@1011 1482 strcpy(p, "&amp;");
adamc@1011 1483 p += 5;
adamc@1011 1484 }
adamc@1059 1485 else
adamc@1011 1486 *p++ = c;
adamc@1011 1487
adamc@1011 1488 *p++ = 0;
adamc@1011 1489 ctx->heap.front = p;
adamc@1011 1490 return result;
adamc@1011 1491 }
adamc@1011 1492
adamc@721 1493 char *uw_Basis_attrifyCss_class(uw_context ctx, uw_Basis_css_class s) {
adamc@721 1494 return s;
adamc@721 1495 }
adamc@721 1496
adamc@311 1497 static void uw_Basis_attrifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
adamc@117 1498 int len;
adamc@117 1499
adamc@666 1500 sprintf(ctx->page.front, "%lld%n", n, &len);
adamc@666 1501 ctx->page.front += len;
adamc@106 1502 }
adamc@106 1503
adamc@428 1504 uw_unit uw_Basis_attrifyInt_w(uw_context ctx, uw_Basis_int n) {
adamc@311 1505 uw_check(ctx, INTS_MAX);
adamc@311 1506 uw_Basis_attrifyInt_w_unsafe(ctx, n);
adamc@428 1507
adamc@428 1508 return uw_unit_v;
adamc@106 1509 }
adamc@106 1510
adamc@428 1511 uw_unit uw_Basis_attrifyFloat_w(uw_context ctx, uw_Basis_float n) {
adamc@117 1512 int len;
adamc@117 1513
adamc@311 1514 uw_check(ctx, FLOATS_MAX);
adamc@666 1515 sprintf(ctx->page.front, "%g%n", n, &len);
adamc@666 1516 ctx->page.front += len;
adamc@428 1517
adamc@428 1518 return uw_unit_v;
adamc@117 1519 }
adamc@117 1520
adamc@428 1521 uw_unit uw_Basis_attrifyString_w(uw_context ctx, uw_Basis_string s) {
adamc@311 1522 uw_check(ctx, strlen(s) * 6);
adamc@117 1523
adamc@106 1524 for (; *s; s++) {
adamc@1054 1525 unsigned char c = *s;
adamc@106 1526
adamc@106 1527 if (c == '"')
adamc@311 1528 uw_write_unsafe(ctx, "&quot;");
adamc@136 1529 else if (c == '&')
adamc@311 1530 uw_write_unsafe(ctx, "&amp;");
adamc@1059 1531 else
adamc@311 1532 uw_writec_unsafe(ctx, c);
adamc@106 1533 }
adamc@428 1534
adamc@428 1535 return uw_unit_v;
adamc@106 1536 }
adamc@120 1537
adamc@1011 1538 uw_unit uw_Basis_attrifyChar_w(uw_context ctx, uw_Basis_char c) {
adamc@1011 1539 uw_check(ctx, 6);
adamc@1011 1540
adamc@1011 1541 if (c == '"')
adamc@1011 1542 uw_write_unsafe(ctx, "&quot;");
adamc@1011 1543 else if (c == '&')
adamc@1011 1544 uw_write_unsafe(ctx, "&amp;");
adamc@1059 1545 else
adamc@1011 1546 uw_writec_unsafe(ctx, c);
adamc@1011 1547
adamc@1011 1548 return uw_unit_v;
adamc@1011 1549 }
adamc@1011 1550
adamc@120 1551
adamc@311 1552 char *uw_Basis_urlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@137 1553 int len;
adamc@137 1554 char *r;
adamc@137 1555
adamc@311 1556 uw_check_heap(ctx, INTS_MAX);
adamc@666 1557 r = ctx->heap.front;
adamc@276 1558 sprintf(r, "%lld%n", n, &len);
adamc@666 1559 ctx->heap.front += len+1;
adamc@137 1560 return r;
adamc@120 1561 }
adamc@120 1562
adamc@682 1563 char *uw_Basis_urlifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 1564 if (ctx->client == NULL || chn.cli != ctx->client->id)
adamc@682 1565 return "";
adamc@682 1566 else {
adamc@682 1567 int len;
adamc@682 1568 char *r;
adamc@679 1569
adamc@682 1570 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 1571 r = ctx->heap.front;
adamc@682 1572 sprintf(r, "%u%n", chn.chn, &len);
adamc@682 1573 ctx->heap.front += len+1;
adamc@682 1574 return r;
adamc@682 1575 }
adamc@679 1576 }
adamc@679 1577
adamc@311 1578 char *uw_Basis_urlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@137 1579 int len;
adamc@137 1580 char *r;
adamc@137 1581
adamc@311 1582 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 1583 r = ctx->heap.front;
adamc@137 1584 sprintf(r, "%g%n", n, &len);
adamc@666 1585 ctx->heap.front += len+1;
adamc@137 1586 return r;
adamc@120 1587 }
adamc@120 1588
adamc@311 1589 char *uw_Basis_urlifyString(uw_context ctx, uw_Basis_string s) {
adamc@137 1590 char *r, *p;
adamc@137 1591
adamc@925 1592 if (s[0] == '\0')
adamc@925 1593 return "_";
adamc@925 1594
adamc@925 1595 uw_check_heap(ctx, strlen(s) * 3 + 1 + !!(s[0] == '_'));
adamc@925 1596
adamc@925 1597 r = p = ctx->heap.front;
adamc@925 1598 if (s[0] == '_')
adamc@925 1599 *p++ = '_';
adamc@925 1600
adamc@925 1601 for (; *s; s++) {
adamc@1087 1602 unsigned char c = *s;
adamc@137 1603
adamc@137 1604 if (c == ' ')
adamc@137 1605 *p++ = '+';
adamc@137 1606 else if (isalnum(c))
adamc@137 1607 *p++ = c;
adamc@137 1608 else {
adamc@137 1609 sprintf(p, "%%%02X", c);
adamc@137 1610 p += 3;
adamc@137 1611 }
adamc@137 1612 }
adamc@137 1613
adamc@137 1614 *p++ = 0;
adamc@666 1615 ctx->heap.front = p;
adamc@137 1616 return r;
adamc@120 1617 }
adamc@120 1618
adamc@311 1619 char *uw_Basis_urlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 1620 if (b == uw_Basis_False)
adamc@186 1621 return "0";
adamc@186 1622 else
adamc@186 1623 return "1";
adamc@186 1624 }
adamc@186 1625
adamc@311 1626 static void uw_Basis_urlifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
adamc@120 1627 int len;
adamc@120 1628
adamc@666 1629 sprintf(ctx->page.front, "%lld%n", n, &len);
adamc@666 1630 ctx->page.front += len;
adamc@120 1631 }
adamc@120 1632
adamc@428 1633 uw_unit uw_Basis_urlifyInt_w(uw_context ctx, uw_Basis_int n) {
adamc@311 1634 uw_check(ctx, INTS_MAX);
adamc@311 1635 uw_Basis_urlifyInt_w_unsafe(ctx, n);
adamc@428 1636
adamc@428 1637 return uw_unit_v;
adamc@120 1638 }
adamc@120 1639
adamc@682 1640 uw_unit uw_Basis_urlifyChannel_w(uw_context ctx, uw_Basis_channel chn) {
adamc@682 1641 if (ctx->client != NULL && chn.cli == ctx->client->id) {
adamc@682 1642 int len;
adamc@682 1643
adamc@682 1644 uw_check(ctx, INTS_MAX + 1);
adamc@682 1645 sprintf(ctx->page.front, "%u%n", chn.chn, &len);
adamc@682 1646 ctx->page.front += len;
adamc@682 1647 }
adamc@682 1648
adamc@679 1649 return uw_unit_v;
adamc@679 1650 }
adamc@679 1651
adamc@428 1652 uw_unit uw_Basis_urlifyFloat_w(uw_context ctx, uw_Basis_float n) {
adamc@120 1653 int len;
adamc@120 1654
adamc@311 1655 uw_check(ctx, FLOATS_MAX);
adamc@666 1656 sprintf(ctx->page.front, "%g%n", n, &len);
adamc@666 1657 ctx->page.front += len;
adamc@428 1658
adamc@428 1659 return uw_unit_v;
adamc@120 1660 }
adamc@120 1661
adamc@488 1662 uw_Basis_string uw_Basis_urlifyTime(uw_context ctx, uw_Basis_time t) {
adamc@488 1663 return uw_Basis_urlifyInt(ctx, t);
adamc@488 1664 }
adamc@488 1665
adamc@428 1666 uw_unit uw_Basis_urlifyString_w(uw_context ctx, uw_Basis_string s) {
adamc@925 1667 if (s[0] == '\0') {
adamc@925 1668 uw_check(ctx, 1);
adamc@925 1669 uw_writec_unsafe(ctx, '_');
adamc@925 1670 return uw_unit_v;
adamc@925 1671 }
adamc@925 1672
adamc@925 1673 uw_check(ctx, strlen(s) * 3 + !!(s[0] == '_'));
adamc@925 1674
adamc@925 1675 if (s[0] == '_')
adamc@925 1676 uw_writec_unsafe(ctx, '_');
adamc@120 1677
adamc@120 1678 for (; *s; s++) {
adamc@1087 1679 unsigned char c = *s;
adamc@120 1680
adamc@120 1681 if (c == ' ')
adamc@311 1682 uw_writec_unsafe(ctx, '+');
adamc@120 1683 else if (isalnum(c))
adamc@311 1684 uw_writec_unsafe(ctx, c);
adamc@120 1685 else {
adamc@666 1686 sprintf(ctx->page.front, "%%%02X", c);
adamc@666 1687 ctx->page.front += 3;
adamc@120 1688 }
adamc@120 1689 }
adamc@428 1690
adamc@428 1691 return uw_unit_v;
adamc@120 1692 }
adamc@120 1693
adamc@428 1694 uw_unit uw_Basis_urlifyBool_w(uw_context ctx, uw_Basis_bool b) {
adamc@311 1695 if (b == uw_Basis_False)
adamc@311 1696 uw_writec(ctx, '0');
adamc@186 1697 else
adamc@311 1698 uw_writec(ctx, '1');
adamc@428 1699
adamc@428 1700 return uw_unit_v;
adamc@186 1701 }
adamc@186 1702
adamc@120 1703
adamc@311 1704 static char *uw_unurlify_advance(char *s) {
adamc@144 1705 char *new_s = strchr(s, '/');
adamc@120 1706
adamc@120 1707 if (new_s)
adamc@120 1708 *new_s++ = 0;
adamc@120 1709 else
adamc@144 1710 new_s = strchr(s, 0);
adamc@144 1711
adamc@144 1712 return new_s;
adamc@144 1713 }
adamc@144 1714
adamc@311 1715 uw_Basis_int uw_Basis_unurlifyInt(uw_context ctx, char **s) {
adamc@311 1716 char *new_s = uw_unurlify_advance(*s);
adamc@311 1717 uw_Basis_int r;
adamc@120 1718
adamc@276 1719 r = atoll(*s);
adamc@120 1720 *s = new_s;
adamc@120 1721 return r;
adamc@120 1722 }
adamc@120 1723
adamc@311 1724 uw_Basis_float uw_Basis_unurlifyFloat(uw_context ctx, char **s) {
adamc@311 1725 char *new_s = uw_unurlify_advance(*s);
adamc@311 1726 uw_Basis_float r;
adamc@120 1727
adamc@120 1728 r = atof(*s);
adamc@120 1729 *s = new_s;
adamc@120 1730 return r;
adamc@120 1731 }
adamc@120 1732
adamc@488 1733 uw_Basis_time uw_Basis_unurlifyTime(uw_context ctx, char **s) {
adamc@488 1734 return uw_Basis_unurlifyInt(ctx, s);
adamc@488 1735 }
adamc@488 1736
adamc@1023 1737 static uw_Basis_string uw_unurlifyString_to(int fromClient, uw_context ctx, char *r, char *s) {
adamc@927 1738 char *s1, *s2 = s;
adamc@144 1739 int n;
adamc@136 1740
adamc@1023 1741 if (!fromClient) {
adamc@1023 1742 if (*s2 == '_')
adamc@1023 1743 ++s2;
adamc@1023 1744 else if (s2[0] == '%' && s2[1] == '5' && (s2[2] == 'f' || s2[2] == 'F'))
adamc@1023 1745 s2 += 3;
adamc@1023 1746 }
adamc@925 1747
adamc@927 1748 for (s1 = r; *s2; ++s1, ++s2) {
adamc@1087 1749 unsigned char c = *s2;
adamc@136 1750
adamc@136 1751 switch (c) {
adamc@136 1752 case '+':
adamc@136 1753 *s1 = ' ';
adamc@136 1754 break;
adamc@136 1755 case '%':
adamc@169 1756 if (s2[1] == 0)
adamc@311 1757 uw_error(ctx, FATAL, "Missing first character of escaped URL byte");
adamc@169 1758 if (s2[2] == 0)
adamc@311 1759 uw_error(ctx, FATAL, "Missing second character of escaped URL byte");
adamc@169 1760 if (sscanf(s2+1, "%02X", &n) != 1)
adamc@311 1761 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", s2);
adamc@136 1762 *s1 = n;
adamc@136 1763 s2 += 2;
adamc@136 1764 break;
adamc@136 1765 default:
adamc@136 1766 *s1 = c;
adamc@136 1767 }
adamc@136 1768 }
adamc@136 1769 *s1++ = 0;
adamc@144 1770 return s1;
adamc@144 1771 }
adamc@144 1772
adamc@311 1773 uw_Basis_bool uw_Basis_unurlifyBool(uw_context ctx, char **s) {
adamc@311 1774 char *new_s = uw_unurlify_advance(*s);
adamc@311 1775 uw_Basis_bool r;
adamc@186 1776
adamc@186 1777 if (*s[0] == 0 || !strcmp(*s, "0") || !strcmp(*s, "off"))
adamc@311 1778 r = uw_Basis_False;
adamc@186 1779 else
adamc@311 1780 r = uw_Basis_True;
adamc@186 1781
adamc@186 1782 *s = new_s;
adamc@186 1783 return r;
adamc@186 1784 }
adamc@186 1785
adamc@311 1786 uw_Basis_string uw_Basis_unurlifyString(uw_context ctx, char **s) {
adamc@311 1787 char *new_s = uw_unurlify_advance(*s);
adamc@144 1788 char *r, *s1, *s2;
adamc@144 1789 int len, n;
adamc@144 1790
adamc@200 1791 len = strlen(*s);
adamc@311 1792 uw_check_heap(ctx, len + 1);
adamc@144 1793
adamc@666 1794 r = ctx->heap.front;
adamc@1023 1795 ctx->heap.front = uw_unurlifyString_to(0, ctx, ctx->heap.front, *s);
adamc@1023 1796 *s = new_s;
adamc@1023 1797 return r;
adamc@1023 1798 }
adamc@1023 1799
adamc@1109 1800 uw_Basis_unit uw_Basis_unurlifyUnit(uw_context ctx, char **s) {
adamc@1109 1801 *s = uw_unurlify_advance(*s);
adamc@1109 1802 return uw_unit_v;
adamc@1109 1803 }
adamc@1109 1804
adamc@1023 1805 uw_Basis_string uw_Basis_unurlifyString_fromClient(uw_context ctx, char **s) {
adamc@1023 1806 char *new_s = uw_unurlify_advance(*s);
adamc@1023 1807 char *r, *s1, *s2;
adamc@1023 1808 int len, n;
adamc@1023 1809
adamc@1023 1810 len = strlen(*s);
adamc@1023 1811 uw_check_heap(ctx, len + 1);
adamc@1023 1812
adamc@1023 1813 r = ctx->heap.front;
adamc@1023 1814 ctx->heap.front = uw_unurlifyString_to(1, ctx, ctx->heap.front, *s);
adamc@136 1815 *s = new_s;
adamc@136 1816 return r;
adamc@120 1817 }
adamc@135 1818
adamc@135 1819
adamc@311 1820 char *uw_Basis_htmlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@286 1821 int len;
adamc@286 1822 char *r;
adamc@286 1823
adamc@311 1824 uw_check_heap(ctx, INTS_MAX);
adamc@666 1825 r = ctx->heap.front;
adamc@286 1826 sprintf(r, "%lld%n", n, &len);
adamc@666 1827 ctx->heap.front += len+1;
adamc@286 1828 return r;
adamc@286 1829 }
adamc@286 1830
adamc@428 1831 uw_unit uw_Basis_htmlifyInt_w(uw_context ctx, uw_Basis_int n) {
adamc@286 1832 int len;
adamc@286 1833
adamc@311 1834 uw_check(ctx, INTS_MAX);
adamc@666 1835 sprintf(ctx->page.front, "%lld%n", n, &len);
adamc@666 1836 ctx->page.front += len;
adamc@428 1837
adamc@428 1838 return uw_unit_v;
adamc@286 1839 }
adamc@286 1840
adamc@311 1841 char *uw_Basis_htmlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@286 1842 int len;
adamc@286 1843 char *r;
adamc@286 1844
adamc@311 1845 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 1846 r = ctx->heap.front;
adamc@286 1847 sprintf(r, "%g%n", n, &len);
adamc@666 1848 ctx->heap.front += len+1;
adamc@286 1849 return r;
adamc@286 1850 }
adamc@286 1851
adamc@428 1852 uw_unit uw_Basis_htmlifyFloat_w(uw_context ctx, uw_Basis_float n) {
adamc@286 1853 int len;
adamc@286 1854
adamc@311 1855 uw_check(ctx, FLOATS_MAX);
adamc@666 1856 sprintf(ctx->page.front, "%g%n", n, &len);
adamc@666 1857 ctx->page.front += len;
adamc@428 1858
adamc@428 1859 return uw_unit_v;
adamc@286 1860 }
adamc@286 1861
adamc@311 1862 char *uw_Basis_htmlifyString(uw_context ctx, uw_Basis_string s) {
adamc@137 1863 char *r, *s2;
adamc@137 1864
adamc@311 1865 uw_check_heap(ctx, strlen(s) * 5 + 1);
adamc@137 1866
adamc@666 1867 for (r = s2 = ctx->heap.front; *s; s++) {
adamc@1054 1868 unsigned char c = *s;
adamc@137 1869
adamc@137 1870 switch (c) {
adamc@137 1871 case '<':
adamc@137 1872 strcpy(s2, "&lt;");
adamc@137 1873 s2 += 4;
adamc@137 1874 break;
adamc@137 1875 case '&':
adamc@137 1876 strcpy(s2, "&amp;");
adamc@137 1877 s2 += 5;
adamc@137 1878 break;
adamc@137 1879 default:
adamc@1059 1880 *s2++ = c;
adamc@137 1881 }
adamc@137 1882 }
adamc@137 1883
adamc@137 1884 *s2++ = 0;
adamc@666 1885 ctx->heap.front = s2;
adamc@137 1886 return r;
adamc@135 1887 }
adamc@135 1888
adamc@428 1889 uw_unit uw_Basis_htmlifyString_w(uw_context ctx, uw_Basis_string s) {
adamc@321 1890 uw_check(ctx, strlen(s) * 6);
adamc@135 1891
adamc@135 1892 for (; *s; s++) {
adamc@1054 1893 unsigned char c = *s;
adamc@135 1894
adamc@135 1895 switch (c) {
adamc@135 1896 case '<':
adamc@311 1897 uw_write_unsafe(ctx, "&lt;");
adamc@135 1898 break;
adamc@135 1899 case '&':
adamc@311 1900 uw_write_unsafe(ctx, "&amp;");
adamc@135 1901 break;
adamc@135 1902 default:
adamc@1059 1903 uw_writec_unsafe(ctx, c);
adamc@135 1904 }
adamc@135 1905 }
adamc@428 1906
adamc@428 1907 return uw_unit_v;
adamc@135 1908 }
adamc@180 1909
adamc@311 1910 uw_Basis_string uw_Basis_htmlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 1911 if (b == uw_Basis_False)
adamc@286 1912 return "False";
adamc@286 1913 else
adamc@286 1914 return "True";
adamc@286 1915 }
adamc@286 1916
adamc@428 1917 uw_unit uw_Basis_htmlifyBool_w(uw_context ctx, uw_Basis_bool b) {
adamc@311 1918 if (b == uw_Basis_False) {
adamc@311 1919 uw_check(ctx, 6);
adamc@666 1920 strcpy(ctx->page.front, "False");
adamc@666 1921 ctx->page.front += 5;
adamc@286 1922 } else {
adamc@311 1923 uw_check(ctx, 5);
adamc@666 1924 strcpy(ctx->page.front, "True");
adamc@666 1925 ctx->page.front += 4;
adamc@286 1926 }
adamc@428 1927
adamc@428 1928 return uw_unit_v;
adamc@286 1929 }
adamc@286 1930
adamc@436 1931 #define TIME_FMT "%x %X"
adamc@438 1932 #define TIME_FMT_PG "%Y-%m-%d %T"
adamc@436 1933
adamc@436 1934 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) {
adamc@436 1935 size_t len;
adamc@436 1936 char *r;
adamc@436 1937 struct tm stm;
adamc@436 1938
adamc@436 1939 if (localtime_r(&t, &stm)) {
adamc@436 1940 uw_check_heap(ctx, TIMES_MAX);
adamc@666 1941 r = ctx->heap.front;
adamc@436 1942 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 1943 ctx->heap.front += len+1;
adamc@436 1944 return r;
adamc@436 1945 } else
adamc@436 1946 return "<i>Invalid time</i>";
adamc@436 1947 }
adamc@436 1948
adamc@436 1949 uw_unit uw_Basis_htmlifyTime_w(uw_context ctx, uw_Basis_time t) {
adamc@436 1950 size_t len;
adamc@436 1951 char *r;
adamc@436 1952 struct tm stm;
adamc@436 1953
adamc@436 1954 if (localtime_r(&t, &stm)) {
adamc@436 1955 uw_check(ctx, TIMES_MAX);
adamc@666 1956 r = ctx->page.front;
adamc@436 1957 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 1958 ctx->page.front += len;
adamc@436 1959 } else {
adamc@436 1960 uw_check(ctx, 20);
adamc@666 1961 strcpy(ctx->page.front, "<i>Invalid time</i>");
adamc@666 1962 ctx->page.front += 19;
adamc@436 1963 }
adamc@436 1964
adamc@436 1965 return uw_unit_v;
adamc@436 1966 }
adamc@436 1967
adamc@821 1968 uw_Basis_char uw_Basis_strsub(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
adamc@821 1969 if (n >= 0 && n < strlen(s))
adamc@821 1970 return s[n];
adamc@821 1971 else
adamc@821 1972 uw_error(ctx, FATAL, "Out-of-bounds strsub");
adamc@821 1973 }
adamc@821 1974
adamc@821 1975 uw_Basis_string uw_Basis_strsuffix(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
adamc@821 1976 if (n >= 0 && n < strlen(s))
adamc@821 1977 return &s[n];
adamc@821 1978 else
adamc@821 1979 uw_error(ctx, FATAL, "Out-of-bounds strsuffix");
adamc@821 1980 }
adamc@821 1981
adamc@828 1982 uw_Basis_int uw_Basis_strlen(uw_context ctx, uw_Basis_string s) {
adamc@828 1983 return strlen(s);
adamc@828 1984 }
adamc@828 1985
adamc@829 1986 uw_Basis_string uw_Basis_strchr(uw_context ctx, uw_Basis_string s, uw_Basis_char ch) {
adamc@829 1987 return strchr(s, ch);
adamc@829 1988 }
adamc@829 1989
adamc@831 1990 uw_Basis_int uw_Basis_strcspn(uw_context ctx, uw_Basis_string s, uw_Basis_string chs) {
adamc@831 1991 return strcspn(s, chs);
adamc@831 1992 }
adamc@831 1993
adamc@829 1994 uw_Basis_int *uw_Basis_strindex(uw_context ctx, uw_Basis_string s, uw_Basis_char ch) {
adamc@829 1995 uw_Basis_string r = strchr(s, ch);
adamc@829 1996 if (r == NULL)
adamc@829 1997 return NULL;
adamc@829 1998 else {
adamc@829 1999 uw_Basis_int *nr = uw_malloc(ctx, sizeof(uw_Basis_int));
adamc@829 2000 *nr = r - s;
adamc@829 2001 return nr;
adamc@829 2002 }
adamc@829 2003 }
adamc@829 2004
adamc@311 2005 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) {
adamc@828 2006 int len = uw_Basis_strlen(ctx, s1) + uw_Basis_strlen(ctx, s2) + 1;
adamc@180 2007 char *s;
adamc@180 2008
adamc@311 2009 uw_check_heap(ctx, len);
adamc@180 2010
adamc@666 2011 s = ctx->heap.front;
adamc@180 2012
adamc@180 2013 strcpy(s, s1);
adamc@180 2014 strcat(s, s2);
adamc@666 2015 ctx->heap.front += len;
adamc@180 2016
adamc@180 2017 return s;
adamc@180 2018 }
adamc@278 2019
adamc@829 2020 uw_Basis_string uw_Basis_substring(uw_context ctx, uw_Basis_string s, uw_Basis_int start, uw_Basis_int len) {
adamc@829 2021 size_t full_len = uw_Basis_strlen(ctx, s);
adamc@829 2022
adamc@829 2023 if (start < 0)
adamc@829 2024 uw_error(ctx, FATAL, "substring: Negative start index");
adamc@829 2025 if (len < 0)
adamc@829 2026 uw_error(ctx, FATAL, "substring: Negative length");
adamc@829 2027 if (start + len > full_len)
adamc@829 2028 uw_error(ctx, FATAL, "substring: Start index plus length is too large");
adamc@829 2029
adamc@829 2030 if (start + len == full_len)
adamc@829 2031 return &s[start];
adamc@829 2032 else {
adamc@829 2033 uw_Basis_string r = uw_malloc(ctx, len+1);
adamc@959 2034 memcpy(r, s+start, len);
adamc@829 2035 r[len] = 0;
adamc@829 2036 return r;
adamc@829 2037 }
adamc@829 2038
adamc@829 2039 }
adamc@829 2040
adamc@1023 2041 uw_Basis_string uw_Basis_str1(uw_context ctx, uw_Basis_char ch) {
adamc@1023 2042 char *r;
adamc@1023 2043
adamc@1023 2044 uw_check_heap(ctx, 2);
adamc@1023 2045 r = ctx->heap.front;
adamc@1023 2046 r[0] = ch;
adamc@1023 2047 r[1] = 0;
adamc@1023 2048
adamc@1023 2049 ctx->heap.front += 2;
adamc@1023 2050
adamc@1023 2051 return r;
adamc@1023 2052 }
adamc@1023 2053
adamc@737 2054 uw_Basis_string uw_strdup(uw_context ctx, uw_Basis_string s1) {
adamc@828 2055 int len = uw_Basis_strlen(ctx, s1) + 1;
adamc@278 2056 char *s;
adamc@278 2057
adamc@311 2058 uw_check_heap(ctx, len);
adamc@278 2059
adamc@666 2060 s = ctx->heap.front;
adamc@278 2061
adamc@278 2062 strcpy(s, s1);
adamc@666 2063 ctx->heap.front += len;
adamc@278 2064
adamc@278 2065 return s;
adamc@278 2066 }
adamc@280 2067
adamc@737 2068 uw_Basis_string uw_maybe_strdup(uw_context ctx, uw_Basis_string s1) {
adamc@493 2069 if (s1)
adamc@737 2070 return uw_strdup(ctx, s1);
adamc@493 2071 else
adamc@493 2072 return NULL;
adamc@493 2073 }
adamc@493 2074
adamc@742 2075 char *uw_memdup(uw_context ctx, const char *p, size_t len) {
adamc@742 2076 char *r = uw_malloc(ctx, len);
adamc@742 2077 memcpy(r, p, len);
adamc@742 2078 return r;
adamc@742 2079 }
adamc@280 2080
adamc@879 2081 char *uw_sqlfmtInt = "%lld::int8%n";
adamc@879 2082
adamc@311 2083 char *uw_Basis_sqlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@281 2084 int len;
adamc@281 2085 char *r;
adamc@281 2086
adamc@311 2087 uw_check_heap(ctx, INTS_MAX + 6);
adamc@666 2088 r = ctx->heap.front;
adamc@879 2089 sprintf(r, uw_sqlfmtInt, n, &len);
adamc@666 2090 ctx->heap.front += len+1;
adamc@281 2091 return r;
adamc@281 2092 }
adamc@281 2093
adamc@467 2094 char *uw_Basis_sqlifyIntN(uw_context ctx, uw_Basis_int *n) {
adamc@467 2095 if (n == NULL)
adamc@467 2096 return "NULL";
adamc@467 2097 else
adamc@467 2098 return uw_Basis_sqlifyInt(ctx, *n);
adamc@467 2099 }
adamc@467 2100
adamc@879 2101 char *uw_sqlfmtFloat = "%g::float8%n";
adamc@879 2102
adamc@311 2103 char *uw_Basis_sqlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@281 2104 int len;
adamc@281 2105 char *r;
adamc@281 2106
adamc@311 2107 uw_check_heap(ctx, FLOATS_MAX + 8);
adamc@666 2108 r = ctx->heap.front;
adamc@879 2109 sprintf(r, uw_sqlfmtFloat, n, &len);
adamc@666 2110 ctx->heap.front += len+1;
adamc@281 2111 return r;
adamc@281 2112 }
adamc@281 2113
adamc@467 2114 char *uw_Basis_sqlifyFloatN(uw_context ctx, uw_Basis_float *n) {
adamc@467 2115 if (n == NULL)
adamc@467 2116 return "NULL";
adamc@467 2117 else
adamc@467 2118 return uw_Basis_sqlifyFloat(ctx, *n);
adamc@467 2119 }
adamc@467 2120
adamc@879 2121 int uw_Estrings = 1;
adamc@879 2122 char *uw_sqlsuffixString = "::text";
adamc@1011 2123 char *uw_sqlsuffixChar = "::char";
adamc@281 2124
adamc@311 2125 uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) {
adamc@280 2126 char *r, *s2;
adamc@280 2127
adamc@879 2128 uw_check_heap(ctx, strlen(s) * 2 + 3 + uw_Estrings + strlen(uw_sqlsuffixString));
adamc@280 2129
adamc@666 2130 r = s2 = ctx->heap.front;
adamc@879 2131 if (uw_Estrings)
adamc@879 2132 *s2++ = 'E';
adamc@280 2133 *s2++ = '\'';
adamc@280 2134
adamc@280 2135 for (; *s; s++) {
adamc@280 2136 char c = *s;
adamc@280 2137
adamc@280 2138 switch (c) {
adamc@280 2139 case '\'':
adamc@885 2140 if (uw_Estrings)
adamc@885 2141 strcpy(s2, "\\'");
adamc@885 2142 else
adamc@885 2143 strcpy(s2, "''");
adamc@280 2144 s2 += 2;
adamc@280 2145 break;
adamc@280 2146 case '\\':
adamc@885 2147 if (uw_Estrings) {
adamc@885 2148 strcpy(s2, "\\\\");
adamc@885 2149 s2 += 2;
adamc@885 2150 } else
adamc@885 2151 *s2++ = '\\';
adamc@280 2152 break;
adamc@280 2153 default:
adamc@280 2154 if (isprint(c))
adamc@280 2155 *s2++ = c;
adamc@879 2156 else if (uw_Estrings) {
adamc@737 2157 sprintf(s2, "\\%03o", c);
adamc@280 2158 s2 += 4;
adamc@280 2159 }
adamc@879 2160 else
adamc@879 2161 uw_error(ctx, FATAL, "Non-printable character %u in string to SQLify", c);
adamc@280 2162 }
adamc@280 2163 }
adamc@280 2164
adamc@879 2165 *s2++ = '\'';
adamc@879 2166 strcpy(s2, uw_sqlsuffixString);
adamc@879 2167 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixString);
adamc@280 2168 return r;
adamc@280 2169 }
adamc@281 2170
adamc@1011 2171 uw_Basis_string uw_Basis_sqlifyChar(uw_context ctx, uw_Basis_char c) {
adamc@1011 2172 char *r, *s2;
adamc@1011 2173
adamc@1011 2174 uw_check_heap(ctx, 5 + uw_Estrings + strlen(uw_sqlsuffixChar));
adamc@1011 2175
adamc@1011 2176 r = s2 = ctx->heap.front;
adamc@1011 2177 if (uw_Estrings)
adamc@1011 2178 *s2++ = 'E';
adamc@1011 2179 *s2++ = '\'';
adamc@1011 2180
adamc@1011 2181 switch (c) {
adamc@1011 2182 case '\'':
adamc@1011 2183 if (uw_Estrings)
adamc@1011 2184 strcpy(s2, "\\'");
adamc@1011 2185 else
adamc@1011 2186 strcpy(s2, "''");
adamc@1011 2187 s2 += 2;
adamc@1011 2188 break;
adamc@1011 2189 case '\\':
adamc@1011 2190 if (uw_Estrings) {
adamc@1011 2191 strcpy(s2, "\\\\");
adamc@1011 2192 s2 += 2;
adamc@1011 2193 } else
adamc@1011 2194 *s2++ = '\\';
adamc@1011 2195 break;
adamc@1011 2196 default:
adamc@1011 2197 if (isprint(c))
adamc@1011 2198 *s2++ = c;
adamc@1011 2199 else if (uw_Estrings) {
adamc@1011 2200 sprintf(s2, "\\%03o", c);
adamc@1011 2201 s2 += 4;
adamc@1011 2202 }
adamc@1011 2203 else
adamc@1011 2204 uw_error(ctx, FATAL, "Non-printable character %u in char to SQLify", c);
adamc@1011 2205 }
adamc@1011 2206
adamc@1011 2207 *s2++ = '\'';
adamc@1011 2208 strcpy(s2, uw_sqlsuffixChar);
adamc@1011 2209 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixChar);
adamc@1011 2210 return r;
adamc@1011 2211 }
adamc@1011 2212
adamc@879 2213 char *uw_sqlsuffixBlob = "::bytea";
adamc@879 2214
adamc@737 2215 uw_Basis_string uw_Basis_sqlifyBlob(uw_context ctx, uw_Basis_blob b) {
adamc@737 2216 char *r, *s2;
adamc@737 2217 size_t i;
adamc@737 2218
adamc@879 2219 uw_check_heap(ctx, b.size * 5 + 3 + uw_Estrings + strlen(uw_sqlsuffixBlob));
adamc@737 2220
adamc@737 2221 r = s2 = ctx->heap.front;
adamc@879 2222 if (uw_Estrings)
adamc@879 2223 *s2++ = 'E';
adamc@737 2224 *s2++ = '\'';
adamc@737 2225
adamc@737 2226 for (i = 0; i < b.size; ++i) {
adamc@737 2227 char c = b.data[i];
adamc@737 2228
adamc@737 2229 switch (c) {
adamc@737 2230 case '\'':
adamc@885 2231 if (uw_Estrings)
adamc@885 2232 strcpy(s2, "\\'");
adamc@885 2233 else
adamc@885 2234 strcpy(s2, "''");
adamc@737 2235 s2 += 2;
adamc@737 2236 break;
adamc@737 2237 case '\\':
adamc@885 2238 if (uw_Estrings) {
adamc@885 2239 strcpy(s2, "\\\\\\\\");
adamc@885 2240 s2 += 4;
adamc@885 2241 } else
adamc@885 2242 *s2++ = '\\';
adamc@737 2243 break;
adamc@737 2244 default:
adamc@737 2245 if (isprint(c))
adamc@737 2246 *s2++ = c;
adamc@879 2247 else if (uw_Estrings) {
adamc@737 2248 sprintf(s2, "\\\\%03o", c);
adamc@737 2249 s2 += 5;
adamc@737 2250 }
adamc@879 2251 else
adamc@879 2252 uw_error(ctx, FATAL, "Non-printable character %u in blob to SQLify", c);
adamc@737 2253 }
adamc@737 2254 }
adamc@737 2255
adamc@879 2256 *s2++ = '\'';
adamc@879 2257 strcpy(s2, uw_sqlsuffixBlob);
adamc@879 2258 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixBlob);
adamc@737 2259 return r;
adamc@737 2260 }
adamc@737 2261
adamc@682 2262 char *uw_Basis_sqlifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 2263 int len;
adamc@682 2264 char *r;
adamc@682 2265 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
adamc@682 2266
adamc@682 2267 uw_check_heap(ctx, INTS_MAX + 7);
adamc@682 2268 r = ctx->heap.front;
adamc@879 2269 sprintf(r, uw_sqlfmtInt, combo, &len);
adamc@682 2270 ctx->heap.front += len+1;
adamc@682 2271 return r;
adamc@682 2272 }
adamc@682 2273
adamc@682 2274 char *uw_Basis_attrifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 2275 int len;
adamc@682 2276 char *r;
adamc@682 2277 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
adamc@682 2278
adamc@682 2279 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 2280 r = ctx->heap.front;
adamc@682 2281 sprintf(r, "%lld%n", combo, &len);
adamc@682 2282 ctx->heap.front += len+1;
adamc@682 2283 return r;
adamc@682 2284 }
adamc@682 2285
adamc@879 2286 char *uw_sqlfmtUint4 = "%u::int4%n";
adamc@879 2287
adamc@682 2288 char *uw_Basis_sqlifyClient(uw_context ctx, uw_Basis_client cli) {
adamc@682 2289 int len;
adamc@682 2290 char *r;
adamc@682 2291
adamc@682 2292 uw_check_heap(ctx, INTS_MAX + 7);
adamc@682 2293 r = ctx->heap.front;
adamc@879 2294 sprintf(r, uw_sqlfmtUint4, cli, &len);
adamc@682 2295 ctx->heap.front += len+1;
adamc@682 2296 return r;
adamc@682 2297 }
adamc@682 2298
adamc@682 2299 char *uw_Basis_attrifyClient(uw_context ctx, uw_Basis_client cli) {
adamc@682 2300 int len;
adamc@682 2301 char *r;
adamc@682 2302
adamc@682 2303 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 2304 r = ctx->heap.front;
adamc@682 2305 sprintf(r, "%u%n", cli, &len);
adamc@682 2306 ctx->heap.front += len+1;
adamc@682 2307 return r;
adamc@682 2308 }
adamc@682 2309
adamc@467 2310 uw_Basis_string uw_Basis_sqlifyStringN(uw_context ctx, uw_Basis_string s) {
adamc@467 2311 if (s == NULL)
adamc@467 2312 return "NULL";
adamc@467 2313 else
adamc@467 2314 return uw_Basis_sqlifyString(ctx, s);
adamc@467 2315 }
adamc@467 2316
adamc@311 2317 char *uw_Basis_sqlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 2318 if (b == uw_Basis_False)
adamc@281 2319 return "FALSE";
adamc@281 2320 else
adamc@281 2321 return "TRUE";
adamc@281 2322 }
adamc@282 2323
adamc@467 2324 char *uw_Basis_sqlifyBoolN(uw_context ctx, uw_Basis_bool *b) {
adamc@467 2325 if (b == NULL)
adamc@467 2326 return "NULL";
adamc@467 2327 else
adamc@467 2328 return uw_Basis_sqlifyBool(ctx, *b);
adamc@467 2329 }
adamc@467 2330
adamc@439 2331 char *uw_Basis_sqlifyTime(uw_context ctx, uw_Basis_time t) {
adamc@439 2332 size_t len;
adamc@682 2333 char *r, *s;
adamc@682 2334 struct tm stm;
adamc@682 2335
adamc@682 2336 if (localtime_r(&t, &stm)) {
adamc@682 2337 s = uw_malloc(ctx, TIMES_MAX);
adamc@995 2338 --stm.tm_hour;
adamc@682 2339 len = strftime(s, TIMES_MAX, TIME_FMT, &stm);
adamc@682 2340 r = uw_malloc(ctx, len + 14);
adamc@682 2341 sprintf(r, "'%s'::timestamp", s);
adamc@682 2342 return r;
adamc@682 2343 } else
adamc@682 2344 return "<Invalid time>";
adamc@682 2345 }
adamc@682 2346
adamc@682 2347 char *uw_Basis_attrifyTime(uw_context ctx, uw_Basis_time t) {
adamc@682 2348 size_t len;
adamc@439 2349 char *r;
adamc@439 2350 struct tm stm;
adamc@439 2351
adamc@439 2352 if (localtime_r(&t, &stm)) {
adamc@439 2353 uw_check_heap(ctx, TIMES_MAX);
adamc@666 2354 r = ctx->heap.front;
adamc@439 2355 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 2356 ctx->heap.front += len+1;
adamc@439 2357 return r;
adamc@439 2358 } else
adamc@439 2359 return "<Invalid time>";
adamc@439 2360 }
adamc@439 2361
adamc@467 2362 char *uw_Basis_sqlifyTimeN(uw_context ctx, uw_Basis_time *t) {
adamc@467 2363 if (t == NULL)
adamc@467 2364 return "NULL";
adamc@467 2365 else
adamc@467 2366 return uw_Basis_sqlifyTime(ctx, *t);
adamc@467 2367 }
adamc@467 2368
adamc@311 2369 char *uw_Basis_ensqlBool(uw_Basis_bool b) {
adamc@311 2370 static uw_Basis_int true = 1;
adamc@311 2371 static uw_Basis_int false = 0;
adamc@282 2372
adamc@311 2373 if (b == uw_Basis_False)
adamc@282 2374 return (char *)&false;
adamc@282 2375 else
adamc@282 2376 return (char *)&true;
adamc@282 2377 }
adamc@284 2378
adamc@311 2379 uw_Basis_string uw_Basis_intToString(uw_context ctx, uw_Basis_int n) {
adamc@284 2380 int len;
adamc@284 2381 char *r;
adamc@284 2382
adamc@311 2383 uw_check_heap(ctx, INTS_MAX);
adamc@666 2384 r = ctx->heap.front;
adamc@284 2385 sprintf(r, "%lld%n", n, &len);
adamc@666 2386 ctx->heap.front += len+1;
adamc@284 2387 return r;
adamc@284 2388 }
adamc@285 2389
adamc@311 2390 uw_Basis_string uw_Basis_floatToString(uw_context ctx, uw_Basis_float n) {
adamc@285 2391 int len;
adamc@285 2392 char *r;
adamc@285 2393
adamc@311 2394 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 2395 r = ctx->heap.front;
adamc@285 2396 sprintf(r, "%g%n", n, &len);
adamc@666 2397 ctx->heap.front += len+1;
adamc@285 2398 return r;
adamc@285 2399 }
adamc@285 2400
adamc@821 2401 uw_Basis_string uw_Basis_charToString(uw_context ctx, uw_Basis_char ch) {
adamc@821 2402 char *r = uw_malloc(ctx, 2);
adamc@821 2403 r[0] = ch;
adamc@821 2404 r[1] = 0;
adamc@821 2405 return r;
adamc@821 2406 }
adamc@821 2407
adamc@311 2408 uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) {
adamc@311 2409 if (b == uw_Basis_False)
adamc@285 2410 return "False";
adamc@285 2411 else
adamc@285 2412 return "True";
adamc@285 2413 }
adamc@288 2414
adamc@436 2415 uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) {
adamc@436 2416 size_t len;
adamc@436 2417 char *r;
adamc@436 2418 struct tm stm;
adamc@436 2419
adamc@436 2420 if (localtime_r(&t, &stm)) {
adamc@436 2421 uw_check_heap(ctx, TIMES_MAX);
adamc@666 2422 r = ctx->heap.front;
adamc@436 2423 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 2424 ctx->heap.front += len+1;
adamc@436 2425 return r;
adamc@436 2426 } else
adamc@436 2427 return "<Invalid time>";
adamc@436 2428 }
adamc@288 2429
adamc@311 2430 uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) {
adamc@288 2431 char *endptr;
adamc@311 2432 uw_Basis_int n = strtoll(s, &endptr, 10);
adamc@288 2433
adamc@288 2434 if (*s != '\0' && *endptr == '\0') {
adamc@311 2435 uw_Basis_int *r = uw_malloc(ctx, sizeof(uw_Basis_int));
adamc@288 2436 *r = n;
adamc@288 2437 return r;
adamc@288 2438 } else
adamc@288 2439 return NULL;
adamc@288 2440 }
adamc@289 2441
adamc@311 2442 uw_Basis_float *uw_Basis_stringToFloat(uw_context ctx, uw_Basis_string s) {
adamc@289 2443 char *endptr;
adamc@311 2444 uw_Basis_float n = strtod(s, &endptr);
adamc@289 2445
adamc@289 2446 if (*s != '\0' && *endptr == '\0') {
adamc@311 2447 uw_Basis_float *r = uw_malloc(ctx, sizeof(uw_Basis_float));
adamc@289 2448 *r = n;
adamc@289 2449 return r;
adamc@289 2450 } else
adamc@289 2451 return NULL;
adamc@289 2452 }
adamc@289 2453
adamc@821 2454 uw_Basis_char *uw_Basis_stringToChar(uw_context ctx, uw_Basis_string s) {
adamc@821 2455 if (s[0] == 0) {
adamc@821 2456 uw_Basis_char *r = uw_malloc(ctx, 1);
adamc@821 2457 r[0] = 0;
adamc@821 2458 return r;
adamc@821 2459 } else if (s[1] != 0)
adamc@821 2460 return NULL;
adamc@821 2461 else {
adamc@821 2462 uw_Basis_char *r = uw_malloc(ctx, 1);
adamc@821 2463 r[0] = s[0];
adamc@821 2464 return r;
adamc@821 2465 }
adamc@821 2466 }
adamc@821 2467
adamc@311 2468 uw_Basis_bool *uw_Basis_stringToBool(uw_context ctx, uw_Basis_string s) {
adamc@311 2469 static uw_Basis_bool true = uw_Basis_True;
adamc@311 2470 static uw_Basis_bool false = uw_Basis_False;
adamc@289 2471
adamc@289 2472 if (!strcasecmp (s, "True"))
adamc@289 2473 return &true;
adamc@289 2474 else if (!strcasecmp (s, "False"))
adamc@289 2475 return &false;
adamc@289 2476 else
adamc@289 2477 return NULL;
adamc@289 2478 }
adamc@292 2479
adamc@436 2480 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) {
adamc@438 2481 char *dot = strchr(s, '.'), *end = strchr(s, 0);
adamc@436 2482 struct tm stm;
adamc@436 2483
adamc@439 2484 if (dot) {
adamc@439 2485 *dot = 0;
adamc@439 2486 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adamc@439 2487 *dot = '.';
adamc@439 2488 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adamc@439 2489 *r = mktime(&stm);
adamc@439 2490 return r;
adamc@439 2491 }
adamc@439 2492 else {
adamc@439 2493 *dot = '.';
adamc@439 2494 return NULL;
adamc@439 2495 }
adamc@436 2496 }
adamc@439 2497 else {
adamc@439 2498 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adamc@439 2499 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adamc@439 2500 *r = mktime(&stm);
adamc@439 2501 return r;
adamc@439 2502 }
adamc@439 2503 else if (strptime(s, TIME_FMT, &stm) == end) {
adamc@439 2504 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adamc@439 2505 *r = mktime(&stm);
adamc@439 2506 return r;
adamc@439 2507 }
adamc@439 2508 else
adamc@439 2509 return NULL;
adamc@439 2510 }
adamc@436 2511 }
adamc@436 2512
adamc@311 2513 uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) {
adamc@292 2514 char *endptr;
adamc@311 2515 uw_Basis_int n = strtoll(s, &endptr, 10);
adamc@292 2516
adamc@292 2517 if (*s != '\0' && *endptr == '\0')
adamc@292 2518 return n;
adamc@292 2519 else
adamc@311 2520 uw_error(ctx, FATAL, "Can't parse int: %s", s);
adamc@292 2521 }
adamc@293 2522
adamc@682 2523 #include <errno.h>
adamc@682 2524
adamc@678 2525 uw_Basis_channel uw_Basis_stringToChannel_error(uw_context ctx, uw_Basis_string s) {
adamc@682 2526 unsigned long long n;
adamc@682 2527
adamc@682 2528 if (sscanf(s, "%llu", &n) < 1)
adamc@682 2529 uw_error(ctx, FATAL, "Can't parse channel: %s", s);
adamc@682 2530 else {
adamc@682 2531 uw_Basis_channel ch = {n >> 32, n & ((1ull << 32) - 1)};
adamc@682 2532 return ch;
adamc@682 2533 }
adamc@682 2534 }
adamc@682 2535
adamc@682 2536 uw_Basis_client uw_Basis_stringToClient_error(uw_context ctx, uw_Basis_string s) {
adamc@678 2537 char *endptr;
adamc@682 2538 unsigned long n = strtoul(s, &endptr, 10);
adamc@678 2539
adamc@678 2540 if (*s != '\0' && *endptr == '\0')
adamc@678 2541 return n;
adamc@678 2542 else
adamc@682 2543 uw_error(ctx, FATAL, "Can't parse client: %s", s);
adamc@678 2544 }
adamc@678 2545
adamc@311 2546 uw_Basis_float uw_Basis_stringToFloat_error(uw_context ctx, uw_Basis_string s) {
adamc@293 2547 char *endptr;
adamc@311 2548 uw_Basis_float n = strtod(s, &endptr);
adamc@293 2549
adamc@293 2550 if (*s != '\0' && *endptr == '\0')
adamc@293 2551 return n;
adamc@293 2552 else
adamc@311 2553 uw_error(ctx, FATAL, "Can't parse float: %s", s);
adamc@293 2554 }
adamc@293 2555
adamc@821 2556 uw_Basis_char uw_Basis_stringToChar_error(uw_context ctx, uw_Basis_string s) {
adamc@821 2557 if (s[0] == 0)
adamc@821 2558 return 0;
adamc@821 2559 else if (s[1] != 0)
adamc@821 2560 uw_error(ctx, FATAL, "Can't parse char: %s", s);
adamc@821 2561 else
adamc@821 2562 return s[0];
adamc@821 2563 }
adamc@821 2564
adamc@311 2565 uw_Basis_bool uw_Basis_stringToBool_error(uw_context ctx, uw_Basis_string s) {
adamc@296 2566 if (!strcasecmp(s, "T") || !strcasecmp (s, "True"))
adamc@311 2567 return uw_Basis_True;
adamc@296 2568 else if (!strcasecmp(s, "F") || !strcasecmp (s, "False"))
adamc@311 2569 return uw_Basis_False;
adamc@293 2570 else
adamc@311 2571 uw_error(ctx, FATAL, "Can't parse bool: %s", s);
adamc@293 2572 }
adamc@436 2573
adamc@436 2574 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) {
adamc@438 2575 char *dot = strchr(s, '.'), *end = strchr(s, 0);
adamc@436 2576 struct tm stm = {};
adamc@436 2577
adamc@438 2578 if (dot) {
adamc@438 2579 *dot = 0;
adamc@438 2580 if (strptime(s, TIME_FMT_PG, &stm)) {
adamc@438 2581 *dot = '.';
adamc@438 2582 return mktime(&stm);
adamc@438 2583 }
adamc@438 2584 else {
adamc@438 2585 *dot = '.';
adamc@438 2586 uw_error(ctx, FATAL, "Can't parse time: %s", s);
adamc@438 2587 }
adamc@438 2588 }
adamc@438 2589 else {
adamc@439 2590 if (strptime(s, TIME_FMT_PG, &stm) == end)
adamc@439 2591 return mktime(&stm);
adamc@439 2592 else if (strptime(s, TIME_FMT, &stm) == end)
adamc@438 2593 return mktime(&stm);
adamc@438 2594 else
adamc@438 2595 uw_error(ctx, FATAL, "Can't parse time: %s", s);
adamc@438 2596 }
adamc@436 2597 }
adamc@457 2598
adamc@939 2599 uw_Basis_time uw_Basis_unsqlTime(uw_context ctx, uw_Basis_string s) {
adamc@939 2600 char *dot = strchr(s, '.'), *end = strchr(s, 0);
adamc@939 2601 struct tm stm = {};
adamc@939 2602
adamc@939 2603 if (dot) {
adamc@939 2604 *dot = 0;
adamc@939 2605 if (strptime(s, TIME_FMT_PG, &stm)) {
adamc@939 2606 *dot = '.';
adamc@939 2607 return mktime(&stm);
adamc@939 2608 }
adamc@939 2609 else {
adamc@939 2610 *dot = '.';
adamc@939 2611 uw_error(ctx, FATAL, "Can't parse time: %s", s);
adamc@939 2612 }
adamc@939 2613 }
adamc@939 2614 else {
adamc@939 2615 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adamc@939 2616 return mktime(&stm);
adamc@939 2617 } else if (strptime(s, TIME_FMT, &stm) == end) {
adamc@939 2618 return mktime(&stm);
adamc@939 2619 } else
adamc@939 2620 uw_error(ctx, FATAL, "Can't parse time: %s", s);
adamc@939 2621 }
adamc@939 2622 }
adamc@939 2623
adamc@742 2624 uw_Basis_blob uw_Basis_stringToBlob_error(uw_context ctx, uw_Basis_string s, size_t len) {
adamc@742 2625 char *r = ctx->heap.front;
adamc@742 2626 uw_Basis_blob b = {len, r};
adamc@742 2627
adamc@742 2628 uw_check_heap(ctx, len);
adamc@742 2629
adamc@742 2630 while (*s) {
adamc@742 2631 if (s[0] == '\\') {
adamc@742 2632 if (s[1] == '\\') {
adamc@742 2633 *r++ = '\\';
adamc@742 2634 s += 2;
adamc@742 2635 } else if (isdigit(s[1]) && isdigit(s[2]) && isdigit(s[3])) {
adamc@742 2636 *r++ = (s[1] - '0') * 8 * 8 + ((s[2] - '0') * 8) + (s[3] - '0');
adamc@742 2637 s += 4;
adamc@742 2638 }
adamc@742 2639 else {
adamc@742 2640 *r++ = '\\';
adamc@742 2641 ++s;
adamc@742 2642 }
adamc@742 2643 } else {
adamc@742 2644 *r++ = s[0];
adamc@742 2645 ++s;
adamc@742 2646 }
adamc@742 2647 }
adamc@742 2648
adamc@742 2649 b.size = r - ctx->heap.front;
adamc@742 2650 ctx->heap.front = r;
adamc@742 2651 return b;
adamc@742 2652 }
adamc@742 2653
adamc@1052 2654 #define THE_PAST "expires=Mon, 01-01-1970 00:00:00 GMT"
adamc@1052 2655
adamc@463 2656 uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) {
adamc@463 2657 int len = strlen(c);
adamc@854 2658 char *p = ctx->outHeaders.start;
adamc@474 2659
adamc@474 2660 while (p = strstr(p, "\nSet-Cookie: ")) {
adamc@474 2661 char *p2;
adamc@474 2662 p += 13;
adamc@474 2663 p2 = strchr(p, '=');
adamc@474 2664
adamc@474 2665 if (p2) {
adamc@474 2666 size_t sz = strcspn(p2+1, ";\r\n");
adamc@474 2667
adamc@474 2668 if (!strncasecmp(p, c, p2 - p)) {
adamc@1052 2669 if (sz == 0 && strstr(p2+2, THE_PAST))
adamc@1052 2670 return NULL;
adamc@1052 2671 else {
adamc@1052 2672 char *ret = uw_malloc(ctx, sz + 1);
adamc@1052 2673 memcpy(ret, p2+1, sz);
adamc@1052 2674 ret[sz] = 0;
adamc@1052 2675 return ret;
adamc@1052 2676 }
adamc@474 2677 }
adamc@474 2678 }
adamc@474 2679 }
adamc@463 2680
adamc@854 2681 if (p = uw_Basis_requestHeader(ctx, "Cookie")) {
adamc@1051 2682 char *p2;
adamc@1051 2683
adamc@854 2684 while (1) {
adamc@1051 2685 if (!strncmp(p, c, len) && p[len] == '=') {
adamc@1051 2686 if (p2 = strchr(p, ';')) {
adamc@1051 2687 size_t n = p2 - (p + len);
adamc@1051 2688 char *r = uw_malloc(ctx, n);
adamc@1051 2689 memcpy(r, p + 1 + len, n-1);
adamc@1051 2690 r[n-1] = 0;
adamc@1051 2691 return r;
adamc@1051 2692 } else
adamc@1051 2693 return p + 1 + len;
adamc@1051 2694 } else if (p = strchr(p, ';'))
adamc@854 2695 p += 2;
adamc@463 2696 else
adamc@463 2697 return NULL;
adamc@463 2698 }
adamc@463 2699 }
adamc@792 2700
adamc@792 2701 return NULL;
adamc@457 2702 }
adamc@462 2703
adamc@1050 2704 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 2705 uw_write_header(ctx, "Set-Cookie: ");
adamc@462 2706 uw_write_header(ctx, c);
adamc@462 2707 uw_write_header(ctx, "=");
adamc@462 2708 uw_write_header(ctx, v);
adamc@466 2709 uw_write_header(ctx, "; path=");
adamc@466 2710 uw_write_header(ctx, prefix);
adamc@1050 2711 if (expires) {
adamc@1050 2712 char formatted[30];
adamc@1050 2713 struct tm tm;
adamc@1050 2714
adamc@1050 2715 gmtime_r(expires, &tm);
adamc@1050 2716
adamc@1050 2717 strftime(formatted, sizeof formatted, "%a, %d-%b-%Y %T GMT", &tm);
adamc@1050 2718
adamc@1050 2719 uw_write_header(ctx, "; expires=");
adamc@1050 2720 uw_write_header(ctx, formatted);
adamc@1050 2721 }
adamc@1050 2722 if (secure)
adamc@1050 2723 uw_write_header(ctx, "; secure");
adamc@462 2724 uw_write_header(ctx, "\r\n");
adamc@462 2725
adamc@462 2726 return uw_unit_v;
adamc@462 2727 }
adamc@667 2728
adamc@1050 2729 uw_unit uw_Basis_clear_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c) {
adamc@1050 2730 uw_write_header(ctx, "Set-Cookie: ");
adamc@1050 2731 uw_write_header(ctx, c);
adamc@1050 2732 uw_write_header(ctx, "=; path=");
adamc@1050 2733 uw_write_header(ctx, prefix);
adamc@1052 2734 uw_write_header(ctx, "; " THE_PAST "\r\n");
adamc@1050 2735
adamc@1050 2736 return uw_unit_v;
adamc@1050 2737 }
adamc@1050 2738
adamc@682 2739 static delta *allocate_delta(uw_context ctx, unsigned client) {
adamc@682 2740 unsigned i;
adamc@682 2741 delta *d;
adamc@671 2742
adamc@682 2743 for (i = 0; i < ctx->used_deltas; ++i)
adamc@682 2744 if (ctx->deltas[i].client == client)
adamc@682 2745 return &ctx->deltas[i];
adamc@671 2746
adamc@682 2747 if (ctx->used_deltas >= ctx->n_deltas) {
adamc@682 2748 ctx->deltas = realloc(ctx->deltas, sizeof(delta) * ++ctx->n_deltas);
adamc@682 2749 buf_init(&ctx->deltas[ctx->n_deltas-1].msgs, 0);
adamc@682 2750 }
adamc@671 2751
adamc@682 2752 d = &ctx->deltas[ctx->used_deltas++];
adamc@682 2753 d->client = client;
adamc@682 2754 buf_reset(&d->msgs);
adamc@682 2755 return d;
adamc@671 2756 }
adamc@671 2757
adamc@668 2758 uw_Basis_channel uw_Basis_new_channel(uw_context ctx, uw_unit u) {
adamc@682 2759 if (ctx->client == NULL)
adamc@682 2760 uw_error(ctx, FATAL, "Attempt to create channel on request not associated with a persistent connection");
adamc@671 2761
adamc@682 2762 return new_channel(ctx->client);
adamc@668 2763 }
adamc@668 2764
adamc@668 2765 uw_unit uw_Basis_send(uw_context ctx, uw_Basis_channel chn, uw_Basis_string msg) {
adamc@682 2766 delta *d = allocate_delta(ctx, chn.cli);
adamc@682 2767 size_t len;
adamc@682 2768 int preLen;
adamc@682 2769 char pre[INTS_MAX + 2];
adamc@668 2770
adamc@682 2771 len = strlen(msg);
adamc@682 2772
adamc@682 2773 sprintf(pre, "%u\n%n", chn.chn, &preLen);
adamc@682 2774
adamc@682 2775 buf_append(&d->msgs, pre, preLen);
adamc@682 2776 buf_append(&d->msgs, msg, len);
adamc@682 2777 buf_append(&d->msgs, "\n", 1);
adamc@671 2778
adamc@671 2779 return uw_unit_v;
adamc@668 2780 }
adamc@671 2781
adamc@671 2782 void uw_commit(uw_context ctx) {
adamc@1104 2783 int i;
adamc@1104 2784
adamc@1104 2785 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@771 2786 if (ctx->transactionals[i].rollback != NULL)
adamc@1083 2787 if (ctx->transactionals[i].commit)
adamc@1083 2788 ctx->transactionals[i].commit(ctx->transactionals[i].data);
adamc@771 2789
adamc@1104 2790 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@771 2791 if (ctx->transactionals[i].rollback == NULL)
adamc@1083 2792 if (ctx->transactionals[i].commit)
adamc@1083 2793 ctx->transactionals[i].commit(ctx->transactionals[i].data);
adamc@771 2794
adamc@1094 2795 if (ctx->app->db_commit(ctx))
adamc@671 2796 uw_error(ctx, FATAL, "Error running SQL COMMIT");
adamc@682 2797
adamc@682 2798 for (i = 0; i < ctx->used_deltas; ++i) {
adamc@682 2799 delta *d = &ctx->deltas[i];
adamc@682 2800 client *c = find_client(d->client);
adamc@682 2801
adamc@682 2802 assert (c != NULL && c->mode == USED);
adamc@682 2803
adamc@685 2804 client_send(c, &d->msgs);
adamc@682 2805 }
adamc@682 2806
adamc@682 2807 if (ctx->client)
adamc@685 2808 release_client(ctx->client);
adamc@771 2809
adamc@1104 2810 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@1083 2811 if (ctx->transactionals[i].free)
adamc@1083 2812 ctx->transactionals[i].free(ctx->transactionals[i].data);
adamc@916 2813
adamc@916 2814 // Splice script data into appropriate part of page
adamc@1065 2815 if (ctx->returning_indirectly || ctx->script_header[0] == 0) {
adamc@995 2816 char *start = strstr(ctx->page.start, "<sc>");
adamc@995 2817 if (start) {
adamc@995 2818 memmove(start, start + 4, buf_used(&ctx->page) - (start - ctx->page.start) - 4);
adamc@995 2819 ctx->page.front -= 4;
adamc@995 2820 }
adamc@995 2821 } else if (buf_used(&ctx->script) == 0) {
adamc@916 2822 size_t len = strlen(ctx->script_header);
adamc@916 2823 char *start = strstr(ctx->page.start, "<sc>");
adamc@916 2824 if (start) {
adamc@916 2825 buf_check(&ctx->page, buf_used(&ctx->page) - 4 + len);
adamc@917 2826 start = strstr(ctx->page.start, "<sc>");
adamc@916 2827 memmove(start + len, start + 4, buf_used(&ctx->page) - (start - ctx->page.start) - 3);
adamc@916 2828 ctx->page.front += len - 4;
adamc@916 2829 memcpy(start, ctx->script_header, len);
adamc@916 2830 }
adamc@916 2831 } else {
adamc@916 2832 size_t lenH = strlen(ctx->script_header), len = buf_used(&ctx->script);
adamc@916 2833 size_t lenP = lenH + 40 + len;
adamc@916 2834 char *start = strstr(ctx->page.start, "<sc>");
adamc@916 2835 if (start) {
adamc@916 2836 buf_check(&ctx->page, buf_used(&ctx->page) - 4 + lenP);
adamc@917 2837 start = strstr(ctx->page.start, "<sc>");
adamc@916 2838 memmove(start + lenP, start + 4, buf_used(&ctx->page) - (start - ctx->page.start) - 3);
adamc@916 2839 ctx->page.front += lenP - 4;
adamc@916 2840 memcpy(start, ctx->script_header, lenH);
adamc@916 2841 memcpy(start + lenH, "<script type=\"text/javascript\">", 31);
adamc@916 2842 memcpy(start + lenH + 31, ctx->script.start, len);
adamc@916 2843 memcpy(start + lenH + 31 + len, "</script>", 9);
adamc@916 2844 }
adamc@916 2845 }
adamc@671 2846 }
adamc@671 2847
adamc@671 2848 int uw_rollback(uw_context ctx) {
adamc@1104 2849 int i;
adamc@885 2850 cleanup *cl;
adamc@771 2851
adamc@682 2852 if (ctx->client)
adamc@685 2853 release_client(ctx->client);
adamc@671 2854
adamc@885 2855 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@885 2856 cl->func(cl->arg);
adamc@885 2857
adamc@885 2858 ctx->cleanup_front = ctx->cleanup;
adamc@885 2859
adamc@1104 2860 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@771 2861 if (ctx->transactionals[i].rollback != NULL)
adamc@771 2862 ctx->transactionals[i].rollback(ctx->transactionals[i].data);
adamc@771 2863
adamc@1104 2864 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@1084 2865 if (ctx->transactionals[i].free)
adamc@1084 2866 ctx->transactionals[i].free(ctx->transactionals[i].data);
adamc@771 2867
adamc@1094 2868 return ctx->app->db_rollback(ctx);
adamc@671 2869 }
adamc@683 2870
adamc@771 2871 void uw_register_transactional(uw_context ctx, void *data, uw_callback commit, uw_callback rollback,
adamc@771 2872 uw_callback free) {
adamc@771 2873 if (ctx->used_transactionals >= ctx->n_transactionals) {
adamc@1084 2874 ctx->transactionals = realloc(ctx->transactionals, sizeof(transactional) * (ctx->used_transactionals+1));
adamc@771 2875 ++ctx->n_transactionals;
adamc@771 2876 }
adamc@771 2877
adamc@771 2878 ctx->transactionals[ctx->used_transactionals].data = data;
adamc@771 2879 ctx->transactionals[ctx->used_transactionals].commit = commit;
adamc@771 2880 ctx->transactionals[ctx->used_transactionals].rollback = rollback;
adamc@771 2881 ctx->transactionals[ctx->used_transactionals++].free = free;
adamc@771 2882 }
adamc@771 2883
adamc@683 2884
adamc@683 2885 // "Garbage collection"
adamc@683 2886
adamc@683 2887 static failure_kind uw_expunge(uw_context ctx, uw_Basis_client cli) {
adamc@683 2888 int r = setjmp(ctx->jmp_buf);
adamc@683 2889
adamc@684 2890 if (r == 0) {
adamc@1094 2891 if (ctx->app->db_begin(ctx))
adamc@684 2892 uw_error(ctx, FATAL, "Error running SQL BEGIN");
adamc@1094 2893 ctx->app->expunger(ctx, cli);
adamc@1094 2894 if (ctx->app->db_commit(ctx))
adamc@684 2895 uw_error(ctx, FATAL, "Error running SQL COMMIT");
adamc@684 2896 }
adamc@683 2897
adamc@683 2898 return r;
adamc@683 2899 }
adamc@683 2900
adamc@683 2901 void uw_prune_clients(uw_context ctx) {
adamc@683 2902 client *c, *next, *prev = NULL;
adamc@683 2903 time_t cutoff;
adamc@683 2904
adamc@1094 2905 cutoff = time(NULL) - ctx->app->timeout;
adamc@683 2906
adamc@683 2907 pthread_mutex_lock(&clients_mutex);
adamc@683 2908
adamc@683 2909 for (c = clients_used; c; c = next) {
adamc@683 2910 next = c->next;
adamc@683 2911 pthread_mutex_lock(&c->lock);
adamc@685 2912 if (c->last_contact < cutoff && c->refcount == 0) {
adamc@683 2913 failure_kind fk = UNLIMITED_RETRY;
adamc@683 2914 if (prev)
adamc@683 2915 prev->next = next;
adamc@683 2916 else
adamc@683 2917 clients_used = next;
adamc@684 2918 uw_reset(ctx);
adamc@683 2919 while (fk == UNLIMITED_RETRY) {
adamc@683 2920 fk = uw_expunge(ctx, c->id);
adamc@684 2921 if (fk == UNLIMITED_RETRY) {
adamc@1094 2922 ctx->app->db_rollback(ctx);
adamc@684 2923 printf("Unlimited retry during expunge: %s\n", uw_error_message(ctx));
adamc@683 2924 }
adamc@683 2925 }
adamc@684 2926 if (fk == SUCCESS)
adamc@684 2927 free_client(c);
adamc@684 2928 else {
adamc@1094 2929 ctx->app->db_rollback(ctx);
adamc@856 2930 fprintf(stderr, "Expunge blocked by error: %s\n", uw_error_message(ctx));
adamc@684 2931 }
adamc@683 2932 }
adamc@683 2933 else
adamc@683 2934 prev = c;
adamc@683 2935 pthread_mutex_unlock(&c->lock);
adamc@683 2936 }
adamc@683 2937
adamc@683 2938 pthread_mutex_unlock(&clients_mutex);
adamc@683 2939 }
adamc@687 2940
adamc@687 2941 failure_kind uw_initialize(uw_context ctx) {
adamc@687 2942 int r = setjmp(ctx->jmp_buf);
adamc@687 2943
adamc@687 2944 if (r == 0) {
adamc@1094 2945 if (ctx->app->db_begin(ctx))
adamc@687 2946 uw_error(ctx, FATAL, "Error running SQL BEGIN");
adamc@1094 2947 ctx->app->initializer(ctx);
adamc@1094 2948 if (ctx->app->db_commit(ctx))
adamc@687 2949 uw_error(ctx, FATAL, "Error running SQL COMMIT");
adamc@687 2950 }
adamc@687 2951
adamc@687 2952 return r;
adamc@687 2953 }
adamc@717 2954
adamc@1065 2955 static int url_bad(uw_Basis_string s) {
adamc@1065 2956 for (; *s; ++s)
adamc@1065 2957 if (!isgraph(*s))
adamc@1065 2958 return 1;
adamc@1065 2959
adamc@1065 2960 return 0;
adamc@1065 2961 }
adamc@1065 2962
adamc@717 2963 uw_Basis_string uw_Basis_bless(uw_context ctx, uw_Basis_string s) {
adamc@1065 2964 if (url_bad(s))
adamc@1065 2965 uw_error(ctx, FATAL, "Invalid URL %s", uw_Basis_htmlifyString(ctx, s));
adamc@1094 2966 if (ctx->app->check_url(s))
adamc@770 2967 return s;
adamc@770 2968 else
adamc@770 2969 uw_error(ctx, FATAL, "Disallowed URL %s", uw_Basis_htmlifyString(ctx, s));
adamc@717 2970 }
adamc@734 2971
adamc@770 2972 uw_Basis_string uw_Basis_checkUrl(uw_context ctx, uw_Basis_string s) {
adamc@1065 2973 if (url_bad(s))
adamc@1065 2974 return NULL;
adamc@1094 2975 if (ctx->app->check_url(s))
adamc@770 2976 return s;
adamc@770 2977 else
adamc@770 2978 return NULL;
adamc@770 2979 }
adamc@770 2980
adamc@770 2981 int mime_format(const char *s) {
adamc@770 2982 for (; *s; ++s)
adamc@770 2983 if (!isalnum(*s) && *s != '/' && *s != '-' && *s != '.')
adamc@770 2984 return 0;
adamc@770 2985
adamc@770 2986 return 1;
adamc@770 2987 }
adamc@770 2988
adamc@741 2989 uw_Basis_string uw_Basis_blessMime(uw_context ctx, uw_Basis_string s) {
adamc@770 2990 if (!mime_format(s))
adamc@770 2991 uw_error(ctx, FATAL, "MIME type \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s));
adamc@770 2992
adamc@1094 2993 if (ctx->app->check_mime(s))
adamc@770 2994 return s;
adamc@770 2995 else
adamc@770 2996 uw_error(ctx, FATAL, "Disallowed MIME type %s", uw_Basis_htmlifyString(ctx, s));
adamc@770 2997 }
adamc@770 2998
adamc@770 2999 uw_Basis_string uw_Basis_checkMime(uw_context ctx, uw_Basis_string s) {
adamc@770 3000 if (!mime_format(s))
adamc@770 3001 return NULL;
adamc@770 3002
adamc@1094 3003 if (ctx->app->check_mime(s))
adamc@770 3004 return s;
adamc@770 3005 else
adamc@770 3006 return NULL;
adamc@741 3007 }
adamc@741 3008
adamc@734 3009 uw_Basis_string uw_unnull(uw_Basis_string s) {
adamc@734 3010 return s ? s : "";
adamc@734 3011 }
adamc@734 3012
adamc@734 3013 extern int uw_hash_blocksize;
adamc@734 3014
adamc@734 3015 uw_Basis_string uw_Basis_makeSigString(uw_context ctx, uw_Basis_string sig) {
adamc@734 3016 uw_Basis_string r = uw_malloc(ctx, 2 * uw_hash_blocksize + 1);
adamc@734 3017 int i;
adamc@734 3018
adamc@734 3019 for (i = 0; i < uw_hash_blocksize; ++i)
adamc@734 3020 sprintf(&r[2*i], "%.02X", ((unsigned char *)sig)[i]);
adamc@734 3021
adamc@734 3022 return r;
adamc@734 3023 }
adamc@734 3024
adamc@734 3025 uw_Basis_string uw_Basis_sigString(uw_context ctx, uw_unit u) {
adamc@1094 3026 return ctx->app->cookie_sig(ctx);
adamc@734 3027 }
adamc@737 3028
adamc@737 3029 uw_Basis_string uw_Basis_fileName(uw_context ctx, uw_Basis_file f) {
adamc@737 3030 return f.name;
adamc@737 3031 }
adamc@737 3032
adamc@740 3033 uw_Basis_string uw_Basis_fileMimeType(uw_context ctx, uw_Basis_file f) {
adamc@740 3034 return f.type;
adamc@740 3035 }
adamc@740 3036
adamc@745 3037 uw_Basis_int uw_Basis_blobSize(uw_context ctx, uw_Basis_blob b) {
adamc@745 3038 return b.size;
adamc@745 3039 }
adamc@745 3040
adamc@737 3041 uw_Basis_blob uw_Basis_fileData(uw_context ctx, uw_Basis_file f) {
adamc@737 3042 return f.data;
adamc@737 3043 }
adamc@741 3044
adamc@741 3045 __attribute__((noreturn)) void uw_return_blob(uw_context ctx, uw_Basis_blob b, uw_Basis_string mimeType) {
adamc@741 3046 cleanup *cl;
adamc@741 3047 int len;
adamc@741 3048
adamc@1065 3049 ctx->returning_indirectly = 1;
adamc@741 3050 buf_reset(&ctx->outHeaders);
adamc@741 3051 buf_reset(&ctx->page);
adamc@741 3052
adamc@856 3053 uw_write_header(ctx, on_success);
adamc@856 3054 uw_write_header(ctx, "Content-Type: ");
adamc@741 3055 uw_write_header(ctx, mimeType);
adamc@741 3056 uw_write_header(ctx, "\r\nContent-Length: ");
adamc@741 3057 buf_check(&ctx->outHeaders, INTS_MAX);
adamc@741 3058 sprintf(ctx->outHeaders.front, "%d%n", b.size, &len);
adamc@741 3059 ctx->outHeaders.front += len;
adamc@1066 3060 uw_write_header(ctx, "\r\n");
adamc@741 3061
adamc@741 3062 buf_append(&ctx->page, b.data, b.size);
adamc@741 3063
adamc@741 3064 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@741 3065 cl->func(cl->arg);
adamc@741 3066
adamc@741 3067 ctx->cleanup_front = ctx->cleanup;
adamc@741 3068
adamc@1065 3069 longjmp(ctx->jmp_buf, RETURN_INDIRECTLY);
adamc@1065 3070 }
adamc@1065 3071
adamc@1065 3072 __attribute__((noreturn)) void uw_redirect(uw_context ctx, uw_Basis_string url) {
adamc@1065 3073 cleanup *cl;
adamc@1065 3074 int len;
adamc@1066 3075 char *s;
adamc@1065 3076
adamc@1065 3077 ctx->returning_indirectly = 1;
adamc@1066 3078 buf_reset(&ctx->page);
adamc@1066 3079 buf_check(&ctx->page, buf_used(&ctx->outHeaders)+1);
adamc@1066 3080 memcpy(ctx->page.start, ctx->outHeaders.start, buf_used(&ctx->outHeaders));
adamc@1066 3081 ctx->page.start[buf_used(&ctx->outHeaders)] = 0;
adamc@1065 3082 buf_reset(&ctx->outHeaders);
adamc@1065 3083
adamc@1065 3084 uw_write_header(ctx, on_redirect);
adamc@1066 3085
adamc@1066 3086 s = strchr(ctx->page.start, '\n');
adamc@1066 3087 if (s) {
adamc@1066 3088 char *s2;
adamc@1066 3089 for (++s; s2 = strchr(s, '\n'); s = s2+1) {
adamc@1066 3090 *s2 = 0;
adamc@1066 3091 if (!strncmp(s, "Set-Cookie: ", 12)) {
adamc@1066 3092 uw_write_header(ctx, s);
adamc@1066 3093 uw_write_header(ctx, "\n");
adamc@1066 3094 }
adamc@1066 3095 }
adamc@1066 3096 }
adamc@1066 3097
adamc@1065 3098 uw_write_header(ctx, "Location: ");
adamc@1065 3099 uw_write_header(ctx, url);
adamc@1065 3100 uw_write_header(ctx, "\r\n\r\n");
adamc@1065 3101
adamc@1065 3102 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@1065 3103 cl->func(cl->arg);
adamc@1065 3104
adamc@1065 3105 ctx->cleanup_front = ctx->cleanup;
adamc@1065 3106
adamc@1065 3107 longjmp(ctx->jmp_buf, RETURN_INDIRECTLY);
adamc@741 3108 }
adamc@874 3109
adamc@874 3110 uw_Basis_string uw_Basis_unAs(uw_context ctx, uw_Basis_string s) {
adamc@874 3111 uw_Basis_string r = uw_malloc(ctx, strlen(s) + 1);
adamc@874 3112
adamc@874 3113 for (; *s; ++s) {
adamc@874 3114 if (s[0] == '\'') {
adamc@874 3115 *r++ = '\'';
adamc@874 3116 for (++s; *s; ++s) {
adamc@874 3117 if (s[0] == '\'') {
adamc@874 3118 *r++ = '\'';
adamc@874 3119 break;
adamc@874 3120 } else if (s[0] == '\\') {
adamc@874 3121 if (s[1] == '\\') {
adamc@874 3122 *r++ = '\\';
adamc@874 3123 *r++ = '\\';
adamc@874 3124 ++s;
adamc@874 3125 } else if (s[1] == '\'') {
adamc@874 3126 *r++ = '\\';
adamc@874 3127 *r++ = '\'';
adamc@874 3128 ++s;
adamc@874 3129 } else
adamc@874 3130 *r++ = '\'';
adamc@874 3131 } else
adamc@874 3132 *r++ = s[0];
adamc@874 3133 }
adamc@874 3134 if (*s == 0) break;
adamc@874 3135 } else if (s[0] == 'T' && s[1] == '.')
adamc@874 3136 ++s;
adamc@874 3137 else
adamc@874 3138 *r++ = s[0];
adamc@874 3139 }
adamc@874 3140
adamc@874 3141 return r;
adamc@874 3142 }
adamc@874 3143
adamc@922 3144 uw_Basis_string uw_Basis_mstrcat(uw_context ctx, ...) {
adamc@922 3145 va_list ap;
adamc@922 3146 size_t len = 1;
adamc@922 3147 char *s, *r, *s2;
adamc@922 3148
adamc@922 3149 va_start(ap, ctx);
adamc@922 3150 for (s = va_arg(ap, char*); s; s = va_arg(ap, char*))
adamc@922 3151 len += strlen(s);
adamc@922 3152 va_end(ap);
adamc@922 3153
adamc@922 3154 r = uw_malloc(ctx, len);
adamc@922 3155 va_start(ap, ctx);
adamc@922 3156 for (s2 = r, s = va_arg(ap, char*); s; s = va_arg(ap, char*))
adamc@922 3157 while (*s)
adamc@922 3158 *s2++ = *s++;
adamc@922 3159 va_end(ap);
adamc@922 3160 *s2 = 0;
adamc@922 3161
adamc@922 3162 return r;
adamc@922 3163 }
adamc@1006 3164
adamc@1050 3165 const uw_Basis_time minTime = 0;
adamc@1050 3166
adamc@1006 3167 uw_Basis_time uw_Basis_now(uw_context ctx) {
adamc@1006 3168 return time(NULL);
adamc@1006 3169 }
adamc@1061 3170
adamc@1061 3171 void *uw_get_global(uw_context ctx, char *name) {
adamc@1061 3172 int i;
adamc@1061 3173
adamc@1061 3174 for (i = 0; i < ctx->n_globals; ++i)
adamc@1061 3175 if (!strcmp(name, ctx->globals[i].name))
adamc@1061 3176 return ctx->globals[i].data;
adamc@1061 3177
adamc@1061 3178 return NULL;
adamc@1061 3179 }
adamc@1061 3180
adamc@1061 3181 void uw_set_global(uw_context ctx, char *name, void *data, void (*free)(void*)) {
adamc@1061 3182 int i;
adamc@1061 3183
adamc@1061 3184 for (i = 0; i < ctx->n_globals; ++i)
adamc@1061 3185 if (!strcmp(name, ctx->globals[i].name)) {
adamc@1089 3186 if (ctx->globals[i].free)
adamc@1061 3187 ctx->globals[i].free(ctx->globals[i].data);
adamc@1061 3188 ctx->globals[i].data = data;
adamc@1061 3189 ctx->globals[i].free = free;
adamc@1061 3190 return;
adamc@1061 3191 }
adamc@1089 3192
adamc@1061 3193 ++ctx->n_globals;
adamc@1061 3194 ctx->globals = realloc(ctx->globals, ctx->n_globals * sizeof(global));
adamc@1061 3195 ctx->globals[ctx->n_globals-1].name = name;
adamc@1061 3196 ctx->globals[ctx->n_globals-1].data = data;
adamc@1061 3197 ctx->globals[ctx->n_globals-1].free = free;
adamc@1061 3198 }
adamc@1061 3199
adamc@1061 3200 uw_Basis_bool uw_Basis_isalnum(uw_context ctx, uw_Basis_char c) {
adamc@1063 3201 return !!isalnum(c);
adamc@1061 3202 }
adamc@1061 3203
adamc@1061 3204 uw_Basis_bool uw_Basis_isalpha(uw_context ctx, uw_Basis_char c) {
adamc@1063 3205 return !!isalpha(c);
adamc@1061 3206 }
adamc@1061 3207
adamc@1061 3208 uw_Basis_bool uw_Basis_isblank(uw_context ctx, uw_Basis_char c) {
adamc@1063 3209 return !!isblank(c);
adamc@1061 3210 }
adamc@1061 3211
adamc@1061 3212 uw_Basis_bool uw_Basis_iscntrl(uw_context ctx, uw_Basis_char c) {
adamc@1063 3213 return !!iscntrl(c);
adamc@1061 3214 }
adamc@1061 3215
adamc@1061 3216 uw_Basis_bool uw_Basis_isdigit(uw_context ctx, uw_Basis_char c) {
adamc@1063 3217 return !!isdigit(c);
adamc@1061 3218 }
adamc@1061 3219
adamc@1061 3220 uw_Basis_bool uw_Basis_isgraph(uw_context ctx, uw_Basis_char c) {
adamc@1063 3221 return !!isgraph(c);
adamc@1061 3222 }
adamc@1061 3223
adamc@1061 3224 uw_Basis_bool uw_Basis_islower(uw_context ctx, uw_Basis_char c) {
adamc@1063 3225 return !!islower(c);
adamc@1061 3226 }
adamc@1061 3227
adamc@1061 3228 uw_Basis_bool uw_Basis_isprint(uw_context ctx, uw_Basis_char c) {
adamc@1063 3229 return !!isprint(c);
adamc@1061 3230 }
adamc@1061 3231
adamc@1061 3232 uw_Basis_bool uw_Basis_ispunct(uw_context ctx, uw_Basis_char c) {
adamc@1063 3233 return !!ispunct(c);
adamc@1061 3234 }
adamc@1061 3235
adamc@1061 3236 uw_Basis_bool uw_Basis_isspace(uw_context ctx, uw_Basis_char c) {
adamc@1063 3237 return !!isspace(c);
adamc@1061 3238 }
adamc@1063 3239
adamc@1061 3240 uw_Basis_bool uw_Basis_isupper(uw_context ctx, uw_Basis_char c) {
adamc@1063 3241 return !!isupper(c);
adamc@1061 3242 }
adamc@1061 3243
adamc@1061 3244 uw_Basis_bool uw_Basis_isxdigit(uw_context ctx, uw_Basis_char c) {
adamc@1063 3245 return !!isxdigit(c);
adamc@1061 3246 }
adamc@1061 3247
adamc@1061 3248 uw_Basis_char uw_Basis_tolower(uw_context ctx, uw_Basis_char c) {
adamc@1061 3249 return tolower(c);
adamc@1061 3250 }
adamc@1061 3251
adamc@1061 3252 uw_Basis_char uw_Basis_toupper(uw_context ctx, uw_Basis_char c) {
adamc@1061 3253 return toupper(c);
adamc@1061 3254 }
adamc@1066 3255
adamc@1066 3256 uw_Basis_string uw_Basis_currentUrl(uw_context ctx) {
adamc@1066 3257 return ctx->current_url;
adamc@1066 3258 }
adamc@1066 3259
adamc@1066 3260 void uw_set_currentUrl(uw_context ctx, char *s) {
adamc@1066 3261 ctx->current_url = s;
adamc@1066 3262 }