annotate src/c/urweb.c @ 1094:db52c32dbe42

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