annotate src/c/urweb.c @ 1109:631a3597c065

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