annotate src/c/urweb.c @ 673:a8effb6159c2

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