annotate src/c/urweb.c @ 1104:72670131dace

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