annotate src/c/urweb.c @ 1089:f1647f16097d

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