annotate src/c/urweb.c @ 678:5ff1ff38e2db

Preliminary work supporting channels in databases
author Adam Chlipala <adamc@hcoop.net>
date Thu, 26 Mar 2009 16:22:34 -0400
parents a8effb6159c2
children 44f23712020d
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@678 1002 char *uw_Basis_attrifyChannel(uw_context ctx, uw_Basis_channel n) {
adamc@678 1003 char *result;
adamc@678 1004 int len;
adamc@678 1005 uw_check_heap(ctx, INTS_MAX);
adamc@678 1006 result = ctx->heap.front;
adamc@678 1007 sprintf(result, "%lld%n", (long long)n, &len);
adamc@678 1008 ctx->heap.front += len+1;
adamc@678 1009 return result;
adamc@678 1010 }
adamc@678 1011
adamc@311 1012 char *uw_Basis_attrifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@136 1013 char *result;
adamc@136 1014 int len;
adamc@311 1015 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 1016 result = ctx->heap.front;
adamc@136 1017 sprintf(result, "%g%n", n, &len);
adamc@666 1018 ctx->heap.front += len+1;
adamc@136 1019 return result;
adamc@106 1020 }
adamc@106 1021
adamc@311 1022 char *uw_Basis_attrifyString(uw_context ctx, uw_Basis_string s) {
adamc@136 1023 int len = strlen(s);
adamc@136 1024 char *result, *p;
adamc@311 1025 uw_check_heap(ctx, len * 6 + 1);
adamc@136 1026
adamc@666 1027 result = p = ctx->heap.front;
adamc@136 1028
adamc@136 1029 for (; *s; s++) {
adamc@136 1030 char c = *s;
adamc@136 1031
adamc@136 1032 if (c == '"') {
adamc@136 1033 strcpy(p, "&quot;");
adamc@136 1034 p += 6;
adamc@136 1035 } else if (c == '&') {
adamc@136 1036 strcpy(p, "&amp;");
adamc@136 1037 p += 5;
adamc@136 1038 }
adamc@136 1039 else if (isprint(c))
adamc@136 1040 *p++ = c;
adamc@136 1041 else {
adamc@136 1042 int len2;
adamc@136 1043 sprintf(p, "&#%d;%n", c, &len2);
adamc@136 1044 p += len2;
adamc@136 1045 }
adamc@136 1046 }
adamc@136 1047
adamc@137 1048 *p++ = 0;
adamc@666 1049 ctx->heap.front = p;
adamc@136 1050 return result;
adamc@106 1051 }
adamc@106 1052
adamc@311 1053 static void uw_Basis_attrifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
adamc@117 1054 int len;
adamc@117 1055
adamc@666 1056 sprintf(ctx->page.front, "%lld%n", n, &len);
adamc@666 1057 ctx->page.front += len;
adamc@106 1058 }
adamc@106 1059
adamc@428 1060 uw_unit uw_Basis_attrifyInt_w(uw_context ctx, uw_Basis_int n) {
adamc@311 1061 uw_check(ctx, INTS_MAX);
adamc@311 1062 uw_Basis_attrifyInt_w_unsafe(ctx, n);
adamc@428 1063
adamc@428 1064 return uw_unit_v;
adamc@106 1065 }
adamc@106 1066
adamc@428 1067 uw_unit uw_Basis_attrifyFloat_w(uw_context ctx, uw_Basis_float n) {
adamc@117 1068 int len;
adamc@117 1069
adamc@311 1070 uw_check(ctx, FLOATS_MAX);
adamc@666 1071 sprintf(ctx->page.front, "%g%n", n, &len);
adamc@666 1072 ctx->page.front += len;
adamc@428 1073
adamc@428 1074 return uw_unit_v;
adamc@117 1075 }
adamc@117 1076
adamc@428 1077 uw_unit uw_Basis_attrifyString_w(uw_context ctx, uw_Basis_string s) {
adamc@311 1078 uw_check(ctx, strlen(s) * 6);
adamc@117 1079
adamc@106 1080 for (; *s; s++) {
adamc@106 1081 char c = *s;
adamc@106 1082
adamc@106 1083 if (c == '"')
adamc@311 1084 uw_write_unsafe(ctx, "&quot;");
adamc@136 1085 else if (c == '&')
adamc@311 1086 uw_write_unsafe(ctx, "&amp;");
adamc@106 1087 else if (isprint(c))
adamc@311 1088 uw_writec_unsafe(ctx, c);
adamc@106 1089 else {
adamc@311 1090 uw_write_unsafe(ctx, "&#");
adamc@311 1091 uw_Basis_attrifyInt_w_unsafe(ctx, c);
adamc@311 1092 uw_writec_unsafe(ctx, ';');
adamc@106 1093 }
adamc@106 1094 }
adamc@428 1095
adamc@428 1096 return uw_unit_v;
adamc@106 1097 }
adamc@120 1098
adamc@120 1099
adamc@311 1100 char *uw_Basis_urlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@137 1101 int len;
adamc@137 1102 char *r;
adamc@137 1103
adamc@311 1104 uw_check_heap(ctx, INTS_MAX);
adamc@666 1105 r = ctx->heap.front;
adamc@276 1106 sprintf(r, "%lld%n", n, &len);
adamc@666 1107 ctx->heap.front += len+1;
adamc@137 1108 return r;
adamc@120 1109 }
adamc@120 1110
adamc@311 1111 char *uw_Basis_urlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@137 1112 int len;
adamc@137 1113 char *r;
adamc@137 1114
adamc@311 1115 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 1116 r = ctx->heap.front;
adamc@137 1117 sprintf(r, "%g%n", n, &len);
adamc@666 1118 ctx->heap.front += len+1;
adamc@137 1119 return r;
adamc@120 1120 }
adamc@120 1121
adamc@311 1122 char *uw_Basis_urlifyString(uw_context ctx, uw_Basis_string s) {
adamc@137 1123 char *r, *p;
adamc@137 1124
adamc@311 1125 uw_check_heap(ctx, strlen(s) * 3 + 1);
adamc@137 1126
adamc@666 1127 for (r = p = ctx->heap.front; *s; s++) {
adamc@137 1128 char c = *s;
adamc@137 1129
adamc@137 1130 if (c == ' ')
adamc@137 1131 *p++ = '+';
adamc@137 1132 else if (isalnum(c))
adamc@137 1133 *p++ = c;
adamc@137 1134 else {
adamc@137 1135 sprintf(p, "%%%02X", c);
adamc@137 1136 p += 3;
adamc@137 1137 }
adamc@137 1138 }
adamc@137 1139
adamc@137 1140 *p++ = 0;
adamc@666 1141 ctx->heap.front = p;
adamc@137 1142 return r;
adamc@120 1143 }
adamc@120 1144
adamc@311 1145 char *uw_Basis_urlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 1146 if (b == uw_Basis_False)
adamc@186 1147 return "0";
adamc@186 1148 else
adamc@186 1149 return "1";
adamc@186 1150 }
adamc@186 1151
adamc@311 1152 static void uw_Basis_urlifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
adamc@120 1153 int len;
adamc@120 1154
adamc@666 1155 sprintf(ctx->page.front, "%lld%n", n, &len);
adamc@666 1156 ctx->page.front += len;
adamc@120 1157 }
adamc@120 1158
adamc@428 1159 uw_unit uw_Basis_urlifyInt_w(uw_context ctx, uw_Basis_int n) {
adamc@311 1160 uw_check(ctx, INTS_MAX);
adamc@311 1161 uw_Basis_urlifyInt_w_unsafe(ctx, n);
adamc@428 1162
adamc@428 1163 return uw_unit_v;
adamc@120 1164 }
adamc@120 1165
adamc@428 1166 uw_unit uw_Basis_urlifyFloat_w(uw_context ctx, uw_Basis_float n) {
adamc@120 1167 int len;
adamc@120 1168
adamc@311 1169 uw_check(ctx, FLOATS_MAX);
adamc@666 1170 sprintf(ctx->page.front, "%g%n", n, &len);
adamc@666 1171 ctx->page.front += len;
adamc@428 1172
adamc@428 1173 return uw_unit_v;
adamc@120 1174 }
adamc@120 1175
adamc@488 1176 uw_Basis_string uw_Basis_urlifyTime(uw_context ctx, uw_Basis_time t) {
adamc@488 1177 return uw_Basis_urlifyInt(ctx, t);
adamc@488 1178 }
adamc@488 1179
adamc@428 1180 uw_unit uw_Basis_urlifyString_w(uw_context ctx, uw_Basis_string s) {
adamc@311 1181 uw_check(ctx, strlen(s) * 3);
adamc@120 1182
adamc@120 1183 for (; *s; s++) {
adamc@120 1184 char c = *s;
adamc@120 1185
adamc@120 1186 if (c == ' ')
adamc@311 1187 uw_writec_unsafe(ctx, '+');
adamc@120 1188 else if (isalnum(c))
adamc@311 1189 uw_writec_unsafe(ctx, c);
adamc@120 1190 else {
adamc@666 1191 sprintf(ctx->page.front, "%%%02X", c);
adamc@666 1192 ctx->page.front += 3;
adamc@120 1193 }
adamc@120 1194 }
adamc@428 1195
adamc@428 1196 return uw_unit_v;
adamc@120 1197 }
adamc@120 1198
adamc@428 1199 uw_unit uw_Basis_urlifyBool_w(uw_context ctx, uw_Basis_bool b) {
adamc@311 1200 if (b == uw_Basis_False)
adamc@311 1201 uw_writec(ctx, '0');
adamc@186 1202 else
adamc@311 1203 uw_writec(ctx, '1');
adamc@428 1204
adamc@428 1205 return uw_unit_v;
adamc@186 1206 }
adamc@186 1207
adamc@120 1208
adamc@311 1209 static char *uw_unurlify_advance(char *s) {
adamc@144 1210 char *new_s = strchr(s, '/');
adamc@120 1211
adamc@120 1212 if (new_s)
adamc@120 1213 *new_s++ = 0;
adamc@120 1214 else
adamc@144 1215 new_s = strchr(s, 0);
adamc@144 1216
adamc@144 1217 return new_s;
adamc@144 1218 }
adamc@144 1219
adamc@311 1220 uw_Basis_int uw_Basis_unurlifyInt(uw_context ctx, char **s) {
adamc@311 1221 char *new_s = uw_unurlify_advance(*s);
adamc@311 1222 uw_Basis_int r;
adamc@120 1223
adamc@276 1224 r = atoll(*s);
adamc@120 1225 *s = new_s;
adamc@120 1226 return r;
adamc@120 1227 }
adamc@120 1228
adamc@668 1229 uw_Basis_channel uw_Basis_unurlifyChannel(uw_context ctx, char **s) {
adamc@668 1230 return uw_Basis_unurlifyInt(ctx, s);
adamc@668 1231 }
adamc@668 1232
adamc@311 1233 uw_Basis_float uw_Basis_unurlifyFloat(uw_context ctx, char **s) {
adamc@311 1234 char *new_s = uw_unurlify_advance(*s);
adamc@311 1235 uw_Basis_float r;
adamc@120 1236
adamc@120 1237 r = atof(*s);
adamc@120 1238 *s = new_s;
adamc@120 1239 return r;
adamc@120 1240 }
adamc@120 1241
adamc@488 1242 uw_Basis_time uw_Basis_unurlifyTime(uw_context ctx, char **s) {
adamc@488 1243 return uw_Basis_unurlifyInt(ctx, s);
adamc@488 1244 }
adamc@488 1245
adamc@311 1246 static uw_Basis_string uw_unurlifyString_to(uw_context ctx, char *r, char *s) {
adamc@144 1247 char *s1, *s2;
adamc@144 1248 int n;
adamc@136 1249
adamc@144 1250 for (s1 = r, s2 = s; *s2; ++s1, ++s2) {
adamc@136 1251 char c = *s2;
adamc@136 1252
adamc@136 1253 switch (c) {
adamc@136 1254 case '+':
adamc@136 1255 *s1 = ' ';
adamc@136 1256 break;
adamc@136 1257 case '%':
adamc@169 1258 if (s2[1] == 0)
adamc@311 1259 uw_error(ctx, FATAL, "Missing first character of escaped URL byte");
adamc@169 1260 if (s2[2] == 0)
adamc@311 1261 uw_error(ctx, FATAL, "Missing second character of escaped URL byte");
adamc@169 1262 if (sscanf(s2+1, "%02X", &n) != 1)
adamc@311 1263 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", s2);
adamc@136 1264 *s1 = n;
adamc@136 1265 s2 += 2;
adamc@136 1266 break;
adamc@136 1267 default:
adamc@136 1268 *s1 = c;
adamc@136 1269 }
adamc@136 1270 }
adamc@136 1271 *s1++ = 0;
adamc@144 1272 return s1;
adamc@144 1273 }
adamc@144 1274
adamc@311 1275 uw_Basis_bool uw_Basis_unurlifyBool(uw_context ctx, char **s) {
adamc@311 1276 char *new_s = uw_unurlify_advance(*s);
adamc@311 1277 uw_Basis_bool r;
adamc@186 1278
adamc@186 1279 if (*s[0] == 0 || !strcmp(*s, "0") || !strcmp(*s, "off"))
adamc@311 1280 r = uw_Basis_False;
adamc@186 1281 else
adamc@311 1282 r = uw_Basis_True;
adamc@186 1283
adamc@186 1284 *s = new_s;
adamc@186 1285 return r;
adamc@186 1286 }
adamc@186 1287
adamc@311 1288 uw_Basis_string uw_Basis_unurlifyString(uw_context ctx, char **s) {
adamc@311 1289 char *new_s = uw_unurlify_advance(*s);
adamc@144 1290 char *r, *s1, *s2;
adamc@144 1291 int len, n;
adamc@144 1292
adamc@200 1293 len = strlen(*s);
adamc@311 1294 uw_check_heap(ctx, len + 1);
adamc@144 1295
adamc@666 1296 r = ctx->heap.front;
adamc@666 1297 ctx->heap.front = uw_unurlifyString_to(ctx, ctx->heap.front, *s);
adamc@136 1298 *s = new_s;
adamc@136 1299 return r;
adamc@120 1300 }
adamc@135 1301
adamc@135 1302
adamc@311 1303 char *uw_Basis_htmlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@286 1304 int len;
adamc@286 1305 char *r;
adamc@286 1306
adamc@311 1307 uw_check_heap(ctx, INTS_MAX);
adamc@666 1308 r = ctx->heap.front;
adamc@286 1309 sprintf(r, "%lld%n", n, &len);
adamc@666 1310 ctx->heap.front += len+1;
adamc@286 1311 return r;
adamc@286 1312 }
adamc@286 1313
adamc@428 1314 uw_unit uw_Basis_htmlifyInt_w(uw_context ctx, uw_Basis_int n) {
adamc@286 1315 int len;
adamc@286 1316
adamc@311 1317 uw_check(ctx, INTS_MAX);
adamc@666 1318 sprintf(ctx->page.front, "%lld%n", n, &len);
adamc@666 1319 ctx->page.front += len;
adamc@428 1320
adamc@428 1321 return uw_unit_v;
adamc@286 1322 }
adamc@286 1323
adamc@668 1324 char *uw_Basis_htmlifyChannel(uw_context ctx, uw_Basis_channel ch) {
adamc@668 1325 return uw_Basis_htmlifyInt(ctx, ch);
adamc@668 1326 }
adamc@668 1327
adamc@311 1328 char *uw_Basis_htmlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@286 1329 int len;
adamc@286 1330 char *r;
adamc@286 1331
adamc@311 1332 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 1333 r = ctx->heap.front;
adamc@286 1334 sprintf(r, "%g%n", n, &len);
adamc@666 1335 ctx->heap.front += len+1;
adamc@286 1336 return r;
adamc@286 1337 }
adamc@286 1338
adamc@428 1339 uw_unit uw_Basis_htmlifyFloat_w(uw_context ctx, uw_Basis_float n) {
adamc@286 1340 int len;
adamc@286 1341
adamc@311 1342 uw_check(ctx, FLOATS_MAX);
adamc@666 1343 sprintf(ctx->page.front, "%g%n", n, &len);
adamc@666 1344 ctx->page.front += len;
adamc@428 1345
adamc@428 1346 return uw_unit_v;
adamc@286 1347 }
adamc@286 1348
adamc@311 1349 char *uw_Basis_htmlifyString(uw_context ctx, uw_Basis_string s) {
adamc@137 1350 char *r, *s2;
adamc@137 1351
adamc@311 1352 uw_check_heap(ctx, strlen(s) * 5 + 1);
adamc@137 1353
adamc@666 1354 for (r = s2 = ctx->heap.front; *s; s++) {
adamc@137 1355 char c = *s;
adamc@137 1356
adamc@137 1357 switch (c) {
adamc@137 1358 case '<':
adamc@137 1359 strcpy(s2, "&lt;");
adamc@137 1360 s2 += 4;
adamc@137 1361 break;
adamc@137 1362 case '&':
adamc@137 1363 strcpy(s2, "&amp;");
adamc@137 1364 s2 += 5;
adamc@137 1365 break;
adamc@137 1366 default:
adamc@137 1367 if (isprint(c))
adamc@137 1368 *s2++ = c;
adamc@137 1369 else {
adamc@137 1370 int len2;
adamc@137 1371 sprintf(s2, "&#%d;%n", c, &len2);
adamc@137 1372 s2 += len2;
adamc@137 1373 }
adamc@137 1374 }
adamc@137 1375 }
adamc@137 1376
adamc@137 1377 *s2++ = 0;
adamc@666 1378 ctx->heap.front = s2;
adamc@137 1379 return r;
adamc@135 1380 }
adamc@135 1381
adamc@428 1382 uw_unit uw_Basis_htmlifyString_w(uw_context ctx, uw_Basis_string s) {
adamc@321 1383 uw_check(ctx, strlen(s) * 6);
adamc@135 1384
adamc@135 1385 for (; *s; s++) {
adamc@135 1386 char c = *s;
adamc@135 1387
adamc@135 1388 switch (c) {
adamc@135 1389 case '<':
adamc@311 1390 uw_write_unsafe(ctx, "&lt;");
adamc@135 1391 break;
adamc@135 1392 case '&':
adamc@311 1393 uw_write_unsafe(ctx, "&amp;");
adamc@135 1394 break;
adamc@135 1395 default:
adamc@135 1396 if (isprint(c))
adamc@311 1397 uw_writec_unsafe(ctx, c);
adamc@135 1398 else {
adamc@311 1399 uw_write_unsafe(ctx, "&#");
adamc@311 1400 uw_Basis_attrifyInt_w_unsafe(ctx, c);
adamc@311 1401 uw_writec_unsafe(ctx, ';');
adamc@135 1402 }
adamc@135 1403 }
adamc@135 1404 }
adamc@428 1405
adamc@428 1406 return uw_unit_v;
adamc@135 1407 }
adamc@180 1408
adamc@311 1409 uw_Basis_string uw_Basis_htmlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 1410 if (b == uw_Basis_False)
adamc@286 1411 return "False";
adamc@286 1412 else
adamc@286 1413 return "True";
adamc@286 1414 }
adamc@286 1415
adamc@428 1416 uw_unit uw_Basis_htmlifyBool_w(uw_context ctx, uw_Basis_bool b) {
adamc@311 1417 if (b == uw_Basis_False) {
adamc@311 1418 uw_check(ctx, 6);
adamc@666 1419 strcpy(ctx->page.front, "False");
adamc@666 1420 ctx->page.front += 5;
adamc@286 1421 } else {
adamc@311 1422 uw_check(ctx, 5);
adamc@666 1423 strcpy(ctx->page.front, "True");
adamc@666 1424 ctx->page.front += 4;
adamc@286 1425 }
adamc@428 1426
adamc@428 1427 return uw_unit_v;
adamc@286 1428 }
adamc@286 1429
adamc@436 1430 #define TIME_FMT "%x %X"
adamc@438 1431 #define TIME_FMT_PG "%Y-%m-%d %T"
adamc@436 1432
adamc@436 1433 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) {
adamc@436 1434 size_t len;
adamc@436 1435 char *r;
adamc@436 1436 struct tm stm;
adamc@436 1437
adamc@436 1438 if (localtime_r(&t, &stm)) {
adamc@436 1439 uw_check_heap(ctx, TIMES_MAX);
adamc@666 1440 r = ctx->heap.front;
adamc@436 1441 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 1442 ctx->heap.front += len+1;
adamc@436 1443 return r;
adamc@436 1444 } else
adamc@436 1445 return "<i>Invalid time</i>";
adamc@436 1446 }
adamc@436 1447
adamc@436 1448 uw_unit uw_Basis_htmlifyTime_w(uw_context ctx, uw_Basis_time t) {
adamc@436 1449 size_t len;
adamc@436 1450 char *r;
adamc@436 1451 struct tm stm;
adamc@436 1452
adamc@436 1453 if (localtime_r(&t, &stm)) {
adamc@436 1454 uw_check(ctx, TIMES_MAX);
adamc@666 1455 r = ctx->page.front;
adamc@436 1456 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 1457 ctx->page.front += len;
adamc@436 1458 } else {
adamc@436 1459 uw_check(ctx, 20);
adamc@666 1460 strcpy(ctx->page.front, "<i>Invalid time</i>");
adamc@666 1461 ctx->page.front += 19;
adamc@436 1462 }
adamc@436 1463
adamc@436 1464 return uw_unit_v;
adamc@436 1465 }
adamc@436 1466
adamc@311 1467 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) {
adamc@180 1468 int len = strlen(s1) + strlen(s2) + 1;
adamc@180 1469 char *s;
adamc@180 1470
adamc@311 1471 uw_check_heap(ctx, len);
adamc@180 1472
adamc@666 1473 s = ctx->heap.front;
adamc@180 1474
adamc@180 1475 strcpy(s, s1);
adamc@180 1476 strcat(s, s2);
adamc@666 1477 ctx->heap.front += len;
adamc@180 1478
adamc@180 1479 return s;
adamc@180 1480 }
adamc@278 1481
adamc@311 1482 uw_Basis_string uw_Basis_strdup(uw_context ctx, uw_Basis_string s1) {
adamc@278 1483 int len = strlen(s1) + 1;
adamc@278 1484 char *s;
adamc@278 1485
adamc@311 1486 uw_check_heap(ctx, len);
adamc@278 1487
adamc@666 1488 s = ctx->heap.front;
adamc@278 1489
adamc@278 1490 strcpy(s, s1);
adamc@666 1491 ctx->heap.front += len;
adamc@278 1492
adamc@278 1493 return s;
adamc@278 1494 }
adamc@280 1495
adamc@493 1496 uw_Basis_string uw_Basis_maybe_strdup(uw_context ctx, uw_Basis_string s1) {
adamc@493 1497 if (s1)
adamc@493 1498 return uw_Basis_strdup(ctx, s1);
adamc@493 1499 else
adamc@493 1500 return NULL;
adamc@493 1501 }
adamc@493 1502
adamc@280 1503
adamc@311 1504 char *uw_Basis_sqlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@281 1505 int len;
adamc@281 1506 char *r;
adamc@281 1507
adamc@311 1508 uw_check_heap(ctx, INTS_MAX + 6);
adamc@666 1509 r = ctx->heap.front;
adamc@281 1510 sprintf(r, "%lld::int8%n", n, &len);
adamc@666 1511 ctx->heap.front += len+1;
adamc@281 1512 return r;
adamc@281 1513 }
adamc@281 1514
adamc@678 1515 char *uw_Basis_sqlifyChannel(uw_context ctx, uw_Basis_channel n) {
adamc@678 1516 int len;
adamc@678 1517 char *r;
adamc@678 1518
adamc@678 1519 uw_check_heap(ctx, INTS_MAX + 6);
adamc@678 1520 r = ctx->heap.front;
adamc@678 1521 sprintf(r, "%lld::int4%n", (long long)n, &len);
adamc@678 1522 ctx->heap.front += len+1;
adamc@678 1523 return r;
adamc@678 1524 }
adamc@678 1525
adamc@467 1526 char *uw_Basis_sqlifyIntN(uw_context ctx, uw_Basis_int *n) {
adamc@467 1527 if (n == NULL)
adamc@467 1528 return "NULL";
adamc@467 1529 else
adamc@467 1530 return uw_Basis_sqlifyInt(ctx, *n);
adamc@467 1531 }
adamc@467 1532
adamc@311 1533 char *uw_Basis_sqlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@281 1534 int len;
adamc@281 1535 char *r;
adamc@281 1536
adamc@311 1537 uw_check_heap(ctx, FLOATS_MAX + 8);
adamc@666 1538 r = ctx->heap.front;
adamc@281 1539 sprintf(r, "%g::float8%n", n, &len);
adamc@666 1540 ctx->heap.front += len+1;
adamc@281 1541 return r;
adamc@281 1542 }
adamc@281 1543
adamc@467 1544 char *uw_Basis_sqlifyFloatN(uw_context ctx, uw_Basis_float *n) {
adamc@467 1545 if (n == NULL)
adamc@467 1546 return "NULL";
adamc@467 1547 else
adamc@467 1548 return uw_Basis_sqlifyFloat(ctx, *n);
adamc@467 1549 }
adamc@467 1550
adamc@281 1551
adamc@311 1552 uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) {
adamc@280 1553 char *r, *s2;
adamc@280 1554
adamc@311 1555 uw_check_heap(ctx, strlen(s) * 2 + 10);
adamc@280 1556
adamc@666 1557 r = s2 = ctx->heap.front;
adamc@280 1558 *s2++ = 'E';
adamc@280 1559 *s2++ = '\'';
adamc@280 1560
adamc@280 1561 for (; *s; s++) {
adamc@280 1562 char c = *s;
adamc@280 1563
adamc@280 1564 switch (c) {
adamc@280 1565 case '\'':
adamc@280 1566 strcpy(s2, "\\'");
adamc@280 1567 s2 += 2;
adamc@280 1568 break;
adamc@280 1569 case '\\':
adamc@280 1570 strcpy(s2, "\\\\");
adamc@280 1571 s2 += 2;
adamc@280 1572 break;
adamc@280 1573 default:
adamc@280 1574 if (isprint(c))
adamc@280 1575 *s2++ = c;
adamc@280 1576 else {
adamc@280 1577 sprintf(s2, "\\%3o", c);
adamc@280 1578 s2 += 4;
adamc@280 1579 }
adamc@280 1580 }
adamc@280 1581 }
adamc@280 1582
adamc@281 1583 strcpy(s2, "'::text");
adamc@666 1584 ctx->heap.front = s2 + 8;
adamc@280 1585 return r;
adamc@280 1586 }
adamc@281 1587
adamc@467 1588 uw_Basis_string uw_Basis_sqlifyStringN(uw_context ctx, uw_Basis_string s) {
adamc@467 1589 if (s == NULL)
adamc@467 1590 return "NULL";
adamc@467 1591 else
adamc@467 1592 return uw_Basis_sqlifyString(ctx, s);
adamc@467 1593 }
adamc@467 1594
adamc@311 1595 char *uw_Basis_sqlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 1596 if (b == uw_Basis_False)
adamc@281 1597 return "FALSE";
adamc@281 1598 else
adamc@281 1599 return "TRUE";
adamc@281 1600 }
adamc@282 1601
adamc@467 1602 char *uw_Basis_sqlifyBoolN(uw_context ctx, uw_Basis_bool *b) {
adamc@467 1603 if (b == NULL)
adamc@467 1604 return "NULL";
adamc@467 1605 else
adamc@467 1606 return uw_Basis_sqlifyBool(ctx, *b);
adamc@467 1607 }
adamc@467 1608
adamc@439 1609 char *uw_Basis_sqlifyTime(uw_context ctx, uw_Basis_time t) {
adamc@439 1610 size_t len;
adamc@439 1611 char *r;
adamc@439 1612 struct tm stm;
adamc@439 1613
adamc@439 1614 if (localtime_r(&t, &stm)) {
adamc@439 1615 uw_check_heap(ctx, TIMES_MAX);
adamc@666 1616 r = ctx->heap.front;
adamc@439 1617 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 1618 ctx->heap.front += len+1;
adamc@439 1619 return r;
adamc@439 1620 } else
adamc@439 1621 return "<Invalid time>";
adamc@439 1622 }
adamc@439 1623
adamc@467 1624 char *uw_Basis_sqlifyTimeN(uw_context ctx, uw_Basis_time *t) {
adamc@467 1625 if (t == NULL)
adamc@467 1626 return "NULL";
adamc@467 1627 else
adamc@467 1628 return uw_Basis_sqlifyTime(ctx, *t);
adamc@467 1629 }
adamc@467 1630
adamc@311 1631 char *uw_Basis_ensqlBool(uw_Basis_bool b) {
adamc@311 1632 static uw_Basis_int true = 1;
adamc@311 1633 static uw_Basis_int false = 0;
adamc@282 1634
adamc@311 1635 if (b == uw_Basis_False)
adamc@282 1636 return (char *)&false;
adamc@282 1637 else
adamc@282 1638 return (char *)&true;
adamc@282 1639 }
adamc@284 1640
adamc@311 1641 uw_Basis_string uw_Basis_intToString(uw_context ctx, uw_Basis_int n) {
adamc@284 1642 int len;
adamc@284 1643 char *r;
adamc@284 1644
adamc@311 1645 uw_check_heap(ctx, INTS_MAX);
adamc@666 1646 r = ctx->heap.front;
adamc@284 1647 sprintf(r, "%lld%n", n, &len);
adamc@666 1648 ctx->heap.front += len+1;
adamc@284 1649 return r;
adamc@284 1650 }
adamc@285 1651
adamc@311 1652 uw_Basis_string uw_Basis_floatToString(uw_context ctx, uw_Basis_float n) {
adamc@285 1653 int len;
adamc@285 1654 char *r;
adamc@285 1655
adamc@311 1656 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 1657 r = ctx->heap.front;
adamc@285 1658 sprintf(r, "%g%n", n, &len);
adamc@666 1659 ctx->heap.front += len+1;
adamc@285 1660 return r;
adamc@285 1661 }
adamc@285 1662
adamc@311 1663 uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) {
adamc@311 1664 if (b == uw_Basis_False)
adamc@285 1665 return "False";
adamc@285 1666 else
adamc@285 1667 return "True";
adamc@285 1668 }
adamc@288 1669
adamc@436 1670 uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) {
adamc@436 1671 size_t len;
adamc@436 1672 char *r;
adamc@436 1673 struct tm stm;
adamc@436 1674
adamc@436 1675 if (localtime_r(&t, &stm)) {
adamc@436 1676 uw_check_heap(ctx, TIMES_MAX);
adamc@666 1677 r = ctx->heap.front;
adamc@436 1678 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 1679 ctx->heap.front += len+1;
adamc@436 1680 return r;
adamc@436 1681 } else
adamc@436 1682 return "<Invalid time>";
adamc@436 1683 }
adamc@288 1684
adamc@311 1685 uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) {
adamc@288 1686 char *endptr;
adamc@311 1687 uw_Basis_int n = strtoll(s, &endptr, 10);
adamc@288 1688
adamc@288 1689 if (*s != '\0' && *endptr == '\0') {
adamc@311 1690 uw_Basis_int *r = uw_malloc(ctx, sizeof(uw_Basis_int));
adamc@288 1691 *r = n;
adamc@288 1692 return r;
adamc@288 1693 } else
adamc@288 1694 return NULL;
adamc@288 1695 }
adamc@289 1696
adamc@678 1697 uw_Basis_channel *uw_Basis_stringToChannel(uw_context ctx, uw_Basis_string s) {
adamc@678 1698 char *endptr;
adamc@678 1699 uw_Basis_channel n = strtoll(s, &endptr, 10);
adamc@678 1700
adamc@678 1701 if (*s != '\0' && *endptr == '\0') {
adamc@678 1702 uw_Basis_channel *r = uw_malloc(ctx, sizeof(uw_Basis_channel));
adamc@678 1703 *r = n;
adamc@678 1704 return r;
adamc@678 1705 } else
adamc@678 1706 return NULL;
adamc@678 1707 }
adamc@678 1708
adamc@311 1709 uw_Basis_float *uw_Basis_stringToFloat(uw_context ctx, uw_Basis_string s) {
adamc@289 1710 char *endptr;
adamc@311 1711 uw_Basis_float n = strtod(s, &endptr);
adamc@289 1712
adamc@289 1713 if (*s != '\0' && *endptr == '\0') {
adamc@311 1714 uw_Basis_float *r = uw_malloc(ctx, sizeof(uw_Basis_float));
adamc@289 1715 *r = n;
adamc@289 1716 return r;
adamc@289 1717 } else
adamc@289 1718 return NULL;
adamc@289 1719 }
adamc@289 1720
adamc@311 1721 uw_Basis_bool *uw_Basis_stringToBool(uw_context ctx, uw_Basis_string s) {
adamc@311 1722 static uw_Basis_bool true = uw_Basis_True;
adamc@311 1723 static uw_Basis_bool false = uw_Basis_False;
adamc@289 1724
adamc@289 1725 if (!strcasecmp (s, "True"))
adamc@289 1726 return &true;
adamc@289 1727 else if (!strcasecmp (s, "False"))
adamc@289 1728 return &false;
adamc@289 1729 else
adamc@289 1730 return NULL;
adamc@289 1731 }
adamc@292 1732
adamc@436 1733 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) {
adamc@438 1734 char *dot = strchr(s, '.'), *end = strchr(s, 0);
adamc@436 1735 struct tm stm;
adamc@436 1736
adamc@439 1737 if (dot) {
adamc@439 1738 *dot = 0;
adamc@439 1739 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adamc@439 1740 *dot = '.';
adamc@439 1741 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adamc@439 1742 *r = mktime(&stm);
adamc@439 1743 return r;
adamc@439 1744 }
adamc@439 1745 else {
adamc@439 1746 *dot = '.';
adamc@439 1747 return NULL;
adamc@439 1748 }
adamc@436 1749 }
adamc@439 1750 else {
adamc@439 1751 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adamc@439 1752 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adamc@439 1753 *r = mktime(&stm);
adamc@439 1754 return r;
adamc@439 1755 }
adamc@439 1756 else if (strptime(s, TIME_FMT, &stm) == end) {
adamc@439 1757 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adamc@439 1758 *r = mktime(&stm);
adamc@439 1759 return r;
adamc@439 1760 }
adamc@439 1761 else
adamc@439 1762 return NULL;
adamc@439 1763 }
adamc@436 1764 }
adamc@436 1765
adamc@311 1766 uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) {
adamc@292 1767 char *endptr;
adamc@311 1768 uw_Basis_int n = strtoll(s, &endptr, 10);
adamc@292 1769
adamc@292 1770 if (*s != '\0' && *endptr == '\0')
adamc@292 1771 return n;
adamc@292 1772 else
adamc@311 1773 uw_error(ctx, FATAL, "Can't parse int: %s", s);
adamc@292 1774 }
adamc@293 1775
adamc@678 1776 uw_Basis_channel uw_Basis_stringToChannel_error(uw_context ctx, uw_Basis_string s) {
adamc@678 1777 char *endptr;
adamc@678 1778 uw_Basis_channel n = strtoll(s, &endptr, 10);
adamc@678 1779
adamc@678 1780 if (*s != '\0' && *endptr == '\0')
adamc@678 1781 return n;
adamc@678 1782 else
adamc@678 1783 uw_error(ctx, FATAL, "Can't parse channel int: %s", s);
adamc@678 1784 }
adamc@678 1785
adamc@311 1786 uw_Basis_float uw_Basis_stringToFloat_error(uw_context ctx, uw_Basis_string s) {
adamc@293 1787 char *endptr;
adamc@311 1788 uw_Basis_float n = strtod(s, &endptr);
adamc@293 1789
adamc@293 1790 if (*s != '\0' && *endptr == '\0')
adamc@293 1791 return n;
adamc@293 1792 else
adamc@311 1793 uw_error(ctx, FATAL, "Can't parse float: %s", s);
adamc@293 1794 }
adamc@293 1795
adamc@311 1796 uw_Basis_bool uw_Basis_stringToBool_error(uw_context ctx, uw_Basis_string s) {
adamc@296 1797 if (!strcasecmp(s, "T") || !strcasecmp (s, "True"))
adamc@311 1798 return uw_Basis_True;
adamc@296 1799 else if (!strcasecmp(s, "F") || !strcasecmp (s, "False"))
adamc@311 1800 return uw_Basis_False;
adamc@293 1801 else
adamc@311 1802 uw_error(ctx, FATAL, "Can't parse bool: %s", s);
adamc@293 1803 }
adamc@436 1804
adamc@436 1805 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) {
adamc@438 1806 char *dot = strchr(s, '.'), *end = strchr(s, 0);
adamc@436 1807 struct tm stm = {};
adamc@436 1808
adamc@438 1809 if (dot) {
adamc@438 1810 *dot = 0;
adamc@438 1811 if (strptime(s, TIME_FMT_PG, &stm)) {
adamc@438 1812 *dot = '.';
adamc@438 1813 return mktime(&stm);
adamc@438 1814 }
adamc@438 1815 else {
adamc@438 1816 *dot = '.';
adamc@438 1817 uw_error(ctx, FATAL, "Can't parse time: %s", s);
adamc@438 1818 }
adamc@438 1819 }
adamc@438 1820 else {
adamc@439 1821 if (strptime(s, TIME_FMT_PG, &stm) == end)
adamc@439 1822 return mktime(&stm);
adamc@439 1823 else if (strptime(s, TIME_FMT, &stm) == end)
adamc@438 1824 return mktime(&stm);
adamc@438 1825 else
adamc@438 1826 uw_error(ctx, FATAL, "Can't parse time: %s", s);
adamc@438 1827 }
adamc@436 1828 }
adamc@457 1829
adamc@457 1830 uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) {
adamc@457 1831 int len = strlen(h);
adamc@457 1832 char *s = ctx->headers, *p;
adamc@457 1833
adamc@457 1834 while (p = strchr(s, ':')) {
adamc@457 1835 if (p - s == len && !strncasecmp(s, h, len)) {
adamc@458 1836 return p + 2;
adamc@457 1837 } else {
adamc@458 1838 if ((s = strchr(p, 0)) && s < ctx->headers_end)
adamc@458 1839 s += 2;
adamc@457 1840 else
adamc@457 1841 return NULL;
adamc@457 1842 }
adamc@457 1843 }
adamc@463 1844 }
adamc@458 1845
adamc@463 1846 uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) {
adamc@463 1847 int len = strlen(c);
adamc@666 1848 char *s = ctx->headers, *p = ctx->outHeaders.start;
adamc@474 1849
adamc@474 1850 while (p = strstr(p, "\nSet-Cookie: ")) {
adamc@474 1851 char *p2;
adamc@474 1852 p += 13;
adamc@474 1853 p2 = strchr(p, '=');
adamc@474 1854
adamc@474 1855 if (p2) {
adamc@474 1856 size_t sz = strcspn(p2+1, ";\r\n");
adamc@474 1857
adamc@474 1858 if (!strncasecmp(p, c, p2 - p)) {
adamc@474 1859 char *ret = uw_malloc(ctx, sz + 1);
adamc@474 1860 memcpy(ret, p2+1, sz);
adamc@474 1861 ret[sz] = 0;
adamc@474 1862 return ret;
adamc@474 1863 }
adamc@474 1864 }
adamc@474 1865 }
adamc@463 1866
adamc@463 1867 while (p = strchr(s, ':')) {
adamc@476 1868 if (!strncasecmp(s, "Cookie: ", 8)) {
adamc@476 1869 p += 2;
adamc@476 1870 while (1) {
adamc@476 1871 if (!strncmp(p, c, len)
adamc@476 1872 && p + len < ctx->headers_end && p[len] == '=')
adamc@476 1873 return p + 1 + len;
adamc@476 1874 else if (p = strchr(p, ';'))
adamc@476 1875 p += 2;
adamc@476 1876 else if ((s = strchr(s, 0)) && s < ctx->headers_end) {
adamc@476 1877 s += 2;
adamc@476 1878 break;
adamc@476 1879 }
adamc@476 1880 else
adamc@476 1881 return NULL;
adamc@476 1882 }
adamc@463 1883 } else {
adamc@463 1884 if ((s = strchr(p, 0)) && s < ctx->headers_end)
adamc@463 1885 s += 2;
adamc@463 1886 else
adamc@463 1887 return NULL;
adamc@463 1888 }
adamc@463 1889 }
adamc@457 1890 }
adamc@462 1891
adamc@466 1892 uw_unit uw_Basis_set_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c, uw_Basis_string v) {
adamc@462 1893 uw_write_header(ctx, "Set-Cookie: ");
adamc@462 1894 uw_write_header(ctx, c);
adamc@462 1895 uw_write_header(ctx, "=");
adamc@462 1896 uw_write_header(ctx, v);
adamc@466 1897 uw_write_header(ctx, "; path=");
adamc@466 1898 uw_write_header(ctx, prefix);
adamc@462 1899 uw_write_header(ctx, "\r\n");
adamc@462 1900
adamc@462 1901 return uw_unit_v;
adamc@462 1902 }
adamc@667 1903
adamc@671 1904 static channel_delta *allocate_delta(uw_context ctx, channel *ch) {
adamc@671 1905 size_t i;
adamc@671 1906 channel_delta *cd;
adamc@671 1907
adamc@671 1908 for (i = 0; i < ctx->n_deltas && ctx->deltas[i].mode == USED && ctx->deltas[i].ch != ch; ++i);
adamc@671 1909
adamc@671 1910 if (i < ctx->n_deltas && ctx->deltas[i].mode == USED && ctx->deltas[i].ch == ch)
adamc@671 1911 return &ctx->deltas[i];
adamc@671 1912
adamc@671 1913 if (i < ctx->n_deltas)
adamc@671 1914 cd = &ctx->deltas[i];
adamc@671 1915 else {
adamc@671 1916 ++ctx->n_deltas;
adamc@671 1917 ctx->deltas = realloc(ctx->deltas, sizeof(channel_delta) * ctx->n_deltas);
adamc@671 1918 cd = &ctx->deltas[ctx->n_deltas-1];
adamc@671 1919 }
adamc@671 1920
adamc@671 1921 cd->mode = USED;
adamc@671 1922 cd->newness = OLD;
adamc@671 1923 cd->ch = ch;
adamc@671 1924 if (cd->n_subscribed > 0)
adamc@671 1925 cd->subscribed[0] = NULL;
adamc@671 1926 buf_reset(&cd->msgs);
adamc@671 1927 return cd;
adamc@671 1928 }
adamc@671 1929
adamc@668 1930 uw_Basis_channel uw_Basis_new_channel(uw_context ctx, uw_unit u) {
adamc@671 1931 size_t i;
adamc@671 1932 channel *ch = uw_new_channel();
adamc@671 1933 ++ch->data.used.refcount;
adamc@671 1934 channel_delta *cd = allocate_delta(ctx, ch);
adamc@671 1935
adamc@671 1936 cd->newness = NEW;
adamc@671 1937
adamc@671 1938 return ch->id;
adamc@671 1939 }
adamc@671 1940
adamc@671 1941 static int delta_used(channel_delta *cd) {
adamc@671 1942 return cd->newness == NEW || buf_used(&cd->msgs) > 0 || (cd->n_subscribed > 0 && cd->subscribed[0]);
adamc@668 1943 }
adamc@667 1944
adamc@668 1945 uw_unit uw_Basis_subscribe(uw_context ctx, uw_Basis_channel chn) {
adamc@668 1946 channel *ch = uw_find_channel(chn);
adamc@668 1947
adamc@668 1948 if (ch == NULL)
adamc@668 1949 uw_error(ctx, FATAL, "Bad channel ID %d", (int)chn);
adamc@668 1950 else {
adamc@668 1951 size_t id = atoi(uw_Basis_requestHeader(ctx, "UrWeb-Client"));
adamc@668 1952 int pass = atoi(uw_Basis_requestHeader(ctx, "UrWeb-Pass"));
adamc@668 1953 client *c = uw_find_client(id);
adamc@668 1954
adamc@671 1955 if (c == NULL) {
adamc@671 1956 uw_release_channel(ch);
adamc@668 1957 uw_error(ctx, FATAL, "Unknown client ID in subscription request");
adamc@671 1958 } else if (c->data.used.pass != pass) {
adamc@671 1959 uw_release_channel(ch);
adamc@671 1960 uw_release_client(c);
adamc@668 1961 uw_error(ctx, FATAL, "Wrong client password in subscription request");
adamc@671 1962 } else {
adamc@671 1963 size_t i;
adamc@671 1964 channel_delta *cd = allocate_delta(ctx, ch);
adamc@671 1965
adamc@671 1966 if (delta_used(cd))
adamc@671 1967 uw_release_channel(ch);
adamc@671 1968
adamc@671 1969 for (i = 0; i < cd->n_subscribed && cd->subscribed[i]; ++i);
adamc@671 1970
adamc@671 1971 if (i < cd->n_subscribed)
adamc@671 1972 cd->subscribed[i] = c;
adamc@671 1973 else {
adamc@671 1974 ++cd->n_subscribed;
adamc@671 1975 cd->subscribed = realloc(cd->subscribed, sizeof(int) * cd->n_subscribed);
adamc@671 1976 cd->subscribed[cd->n_subscribed-1] = c;
adamc@671 1977 }
adamc@671 1978 }
adamc@668 1979 }
adamc@671 1980
adamc@671 1981 return uw_unit_v;
adamc@668 1982 }
adamc@668 1983
adamc@668 1984 uw_unit uw_Basis_send(uw_context ctx, uw_Basis_channel chn, uw_Basis_string msg) {
adamc@668 1985 channel *ch = uw_find_channel(chn);
adamc@668 1986
adamc@668 1987 if (ch == NULL)
adamc@668 1988 uw_error(ctx, FATAL, "Bad channel ID %d", (int)chn);
adamc@671 1989 else {
adamc@671 1990 channel_delta *cd = allocate_delta(ctx, ch);
adamc@671 1991 if (delta_used(cd))
adamc@671 1992 uw_release_channel(ch);
adamc@671 1993 buf_append(&cd->msgs, msg, strlen(msg));
adamc@671 1994 }
adamc@671 1995
adamc@671 1996 return uw_unit_v;
adamc@668 1997 }
adamc@671 1998
adamc@671 1999 int uw_db_commit(uw_context);
adamc@671 2000 int uw_db_rollback(uw_context);
adamc@671 2001
adamc@671 2002 void uw_commit(uw_context ctx) {
adamc@671 2003 size_t i, j;
adamc@671 2004
adamc@671 2005 for (i = 0; i < ctx->n_deltas && ctx->deltas[i].mode == USED; ++i) {
adamc@671 2006 channel *ch = ctx->deltas[i].ch;
adamc@671 2007
adamc@671 2008 for (j = 0; j < ctx->deltas[i].n_subscribed && ctx->deltas[i].subscribed[j]; ++j) {
adamc@671 2009 client *c = ctx->deltas[i].subscribed[j];
adamc@671 2010
adamc@671 2011 uw_subscribe(ch, c);
adamc@671 2012 uw_release_client(c);
adamc@671 2013 }
adamc@671 2014
adamc@671 2015 if (buf_used(&ctx->deltas[i].msgs) > 0) {
adamc@671 2016 uw_channel_send(ch, ctx->deltas[i].msgs.start);
adamc@671 2017 }
adamc@671 2018
adamc@671 2019 uw_release_channel(ch);
adamc@671 2020 }
adamc@671 2021
adamc@671 2022 if (uw_db_commit(ctx))
adamc@671 2023 uw_error(ctx, FATAL, "Error running SQL COMMIT");
adamc@671 2024 }
adamc@671 2025
adamc@671 2026 int uw_rollback(uw_context ctx) {
adamc@671 2027 size_t i, j;
adamc@671 2028
adamc@671 2029 for (i = 0; i < ctx->n_deltas && ctx->deltas[i].mode == USED; ++i) {
adamc@671 2030 channel *ch = ctx->deltas[i].ch;
adamc@671 2031
adamc@671 2032 for (j = 0; j < ctx->deltas[i].n_subscribed && ctx->deltas[i].subscribed[j]; ++j) {
adamc@671 2033 client *c = ctx->deltas[i].subscribed[j];
adamc@671 2034
adamc@671 2035 uw_release_client(c);
adamc@671 2036 }
adamc@671 2037
adamc@671 2038 if (ctx->deltas[i].newness == NEW)
adamc@671 2039 uw_free_channel(ch);
adamc@671 2040 else
adamc@671 2041 uw_release_channel(ch);
adamc@671 2042 }
adamc@671 2043
adamc@671 2044 return uw_db_rollback(ctx);
adamc@671 2045 }