annotate src/c/urweb.c @ 672:df6eb58de040

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