annotate src/c/urweb.c @ 668:b0c1a46b1f15

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