annotate src/c/urweb.c @ 756:8ce31c052dce

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