annotate src/c/urweb.c @ 780:0084af7af35a

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