annotate src/c/urweb.c @ 671:729e65db2e2f

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