annotate src/c/urweb.c @ 804:10fe57e4a8c2

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