annotate src/c/urweb.c @ 679:44f23712020d

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