annotate src/c/urweb.c @ 854:158d980889ac

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