annotate src/c/urweb.c @ 860:a738002d5b4d

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