annotate src/c/urweb.c @ 1433:66092ce45a76

Ignore JavaScript events in Effectize; allow extra spaces for 'jsFunc'; eat carriage returns at line ends in .urp files
author Adam Chlipala <adam@chlipala.net>
date Thu, 10 Mar 2011 20:22:03 -0500
parents 4a6f84092399
children 6ff10428de4a
rev   line source
adamc@1268 1 #include "config.h"
adamc@1268 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@682 10 #include <assert.h>
adamc@741 11 #include <ctype.h>
adamc@1114 12 #include <limits.h>
adamc@1113 13 #include <stdint.h>
adamc@1094 14 #include <sys/types.h>
adamc@1094 15 #include <sys/socket.h>
adam@1397 16 #include <openssl/des.h>
adam@1374 17 #include <time.h>
adamc@102 18
adamc@667 19 #include <pthread.h>
adamc@667 20
adamc@102 21 #include "types.h"
adamc@102 22
adam@1431 23 uw_unit uw_unit_v = 0;
adamc@102 24
adamc@667 25
adamc@667 26 // Socket extras
adamc@667 27
adamc@863 28 int uw_really_send(int sock, const void *buf, ssize_t len) {
adamc@667 29 while (len > 0) {
adamc@856 30 ssize_t n = send(sock, buf, len, 0);
adamc@856 31
adamc@856 32 if (n < 0)
adamc@856 33 return n;
adamc@856 34
adamc@856 35 buf += n;
adamc@856 36 len -= n;
adamc@856 37 }
adamc@856 38
adamc@856 39 return 0;
adamc@856 40 }
adamc@856 41
adamc@856 42 int uw_really_write(int fd, const void *buf, size_t len) {
adamc@856 43 while (len > 0) {
adamc@856 44 ssize_t n = write(fd, buf, len);
adamc@667 45
adamc@667 46 if (n < 0)
adamc@667 47 return n;
adamc@667 48
adamc@667 49 buf += n;
adamc@667 50 len -= n;
adamc@667 51 }
adamc@667 52
adamc@667 53 return 0;
adamc@667 54 }
adamc@667 55
adamc@667 56
adamc@667 57 // Buffers
adamc@667 58
adam@1369 59 void uw_buffer_init(size_t max, uw_buffer *b, size_t s) {
adamc@1113 60 b->max = max;
adamc@667 61 b->front = b->start = malloc(s);
adamc@667 62 b->back = b->front + s;
adamc@667 63 }
adamc@667 64
adam@1369 65 void uw_buffer_free(uw_buffer *b) {
adamc@667 66 free(b->start);
adamc@667 67 }
adamc@667 68
adam@1369 69 void uw_buffer_reset(uw_buffer *b) {
adamc@667 70 b->front = b->start;
adamc@667 71 }
adamc@667 72
adam@1369 73 int uw_buffer_check(uw_buffer *b, size_t extra) {
adamc@667 74 if (b->back - b->front < extra) {
adamc@667 75 size_t desired = b->front - b->start + extra, next;
adamc@667 76 char *new_heap;
adamc@667 77
adamc@667 78 next = b->back - b->start;
adamc@667 79 if (next == 0)
adamc@667 80 next = 1;
adamc@667 81 for (; next < desired; next *= 2);
adamc@667 82
adamc@1134 83 if (next > b->max) {
adamc@1113 84 if (desired <= b->max)
adamc@1113 85 next = desired;
adamc@1113 86 else
adamc@1113 87 return 1;
adamc@1134 88 }
adamc@1113 89
adamc@667 90 new_heap = realloc(b->start, next);
adamc@667 91 b->front = new_heap + (b->front - b->start);
adamc@667 92 b->back = new_heap + next;
adamc@667 93 b->start = new_heap;
adamc@667 94 }
adamc@1113 95
adamc@1113 96 return 0;
adamc@1113 97 }
adamc@1113 98
adamc@1113 99 __attribute__((noreturn)) void uw_error(uw_context, failure_kind, const char *, ...);
adamc@1113 100
adam@1369 101 static void ctx_uw_buffer_check(uw_context ctx, const char *kind, uw_buffer *b, size_t extra) {
adam@1369 102 if (uw_buffer_check(b, extra))
adamc@1113 103 uw_error(ctx, FATAL, "Memory limit exceeded (%s)", kind);
adamc@667 104 }
adamc@667 105
adam@1369 106 size_t uw_buffer_used(uw_buffer *b) {
adamc@667 107 return b->front - b->start;
adamc@667 108 }
adamc@667 109
adam@1369 110 size_t uw_buffer_avail(uw_buffer *b) {
adamc@667 111 return b->back - b->start;
adamc@667 112 }
adamc@667 113
adam@1369 114 int uw_buffer_append(uw_buffer *b, const char *s, size_t len) {
adam@1369 115 if (uw_buffer_check(b, len+1))
adamc@1113 116 return 1;
adamc@1113 117
adamc@1113 118 memcpy(b->front, s, len);
adamc@1113 119 b->front += len;
adamc@1113 120 *b->front = 0;
adamc@1113 121
adamc@1113 122 return 0;
adamc@1113 123 }
adamc@1113 124
adam@1369 125 static void ctx_uw_buffer_append(uw_context ctx, const char *kind, uw_buffer *b, const char *s, size_t len) {
adam@1369 126 ctx_uw_buffer_check(ctx, kind, b, len+1);
adamc@1113 127
adamc@668 128 memcpy(b->front, s, len);
adamc@668 129 b->front += len;
adamc@672 130 *b->front = 0;
adamc@668 131 }
adamc@667 132
adamc@668 133
adamc@672 134 // Persistent state types
adamc@667 135
adamc@667 136 typedef enum { UNUSED, USED } usage;
adamc@667 137
adamc@667 138 typedef struct client {
adamc@682 139 unsigned id;
adamc@667 140 usage mode;
adamc@682 141 int pass;
adamc@682 142 struct client *next;
adamc@689 143 pthread_mutex_t lock, pull_lock;
adam@1369 144 uw_buffer msgs;
adamc@682 145 int sock;
adamc@863 146 int (*send)(int sockfd, const void *buf, ssize_t len);
adamc@863 147 int (*close)(int fd);
adamc@682 148 time_t last_contact;
adamc@682 149 unsigned n_channels;
adamc@685 150 unsigned refcount;
adamc@1121 151 void *data;
adamc@667 152 } client;
adamc@667 153
adamc@672 154
adamc@672 155 // Persistent client state
adamc@672 156
adamc@682 157 static client **clients, *clients_free, *clients_used;
adamc@682 158 static unsigned n_clients;
adamc@667 159
adamc@667 160 static pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER;
adamc@667 161
adamc@1113 162 size_t uw_messages_max = SIZE_MAX;
adamc@1113 163 size_t uw_clients_max = SIZE_MAX;
adamc@1113 164
adamc@1121 165 void *uw_init_client_data();
adamc@1121 166 void uw_free_client_data(void *);
adamc@1121 167 void uw_copy_client_data(void *dst, void *src);
adamc@1121 168
adamc@682 169 static client *new_client() {
adamc@667 170 client *c;
adamc@667 171
adamc@667 172 pthread_mutex_lock(&clients_mutex);
adamc@667 173
adamc@667 174 if (clients_free) {
adamc@667 175 c = clients_free;
adamc@682 176 clients_free = clients_free->next;
adamc@667 177 }
adamc@1113 178 else if (n_clients >= uw_clients_max)
adamc@1113 179 return NULL;
adamc@667 180 else {
adamc@667 181 ++n_clients;
adamc@667 182 clients = realloc(clients, sizeof(client) * n_clients);
adamc@667 183 c = malloc(sizeof(client));
adamc@667 184 c->id = n_clients-1;
adamc@682 185 pthread_mutex_init(&c->lock, NULL);
adamc@689 186 pthread_mutex_init(&c->pull_lock, NULL);
adam@1369 187 uw_buffer_init(uw_messages_max, &c->msgs, 0);
adamc@667 188 clients[n_clients-1] = c;
adamc@667 189 }
adamc@667 190
adamc@682 191 pthread_mutex_lock(&c->lock);
adamc@667 192 c->mode = USED;
adamc@682 193 c->pass = rand();
adamc@682 194 c->sock = -1;
adamc@682 195 c->last_contact = time(NULL);
adam@1369 196 uw_buffer_reset(&c->msgs);
adamc@682 197 c->n_channels = 0;
adamc@685 198 c->refcount = 0;
adamc@1121 199 c->data = uw_init_client_data();
adamc@682 200 pthread_mutex_unlock(&c->lock);
adamc@682 201
adamc@682 202 c->next = clients_used;
adamc@682 203 clients_used = c;
adamc@667 204
adamc@667 205 pthread_mutex_unlock(&clients_mutex);
adamc@667 206
adamc@667 207 return c;
adamc@667 208 }
adamc@667 209
adamc@685 210 static void use_client(client *c) {
adamc@685 211 pthread_mutex_lock(&c->lock);
adamc@685 212 ++c->refcount;
adamc@685 213 pthread_mutex_unlock(&c->lock);
adamc@689 214 pthread_mutex_lock(&c->pull_lock);
adamc@685 215 }
adamc@685 216
adamc@685 217 static void release_client(client *c) {
adamc@689 218 pthread_mutex_unlock(&c->pull_lock);
adamc@685 219 pthread_mutex_lock(&c->lock);
adamc@685 220 --c->refcount;
adamc@685 221 pthread_mutex_unlock(&c->lock);
adamc@689 222
adamc@685 223 }
adamc@685 224
adamc@856 225 static const char begin_msgs[] = "Content-type: text/plain\r\n\r\n";
adamc@667 226
adamc@682 227 static client *find_client(unsigned id) {
adamc@667 228 client *c;
adamc@667 229
adamc@667 230 pthread_mutex_lock(&clients_mutex);
adamc@667 231
adamc@667 232 if (id >= n_clients) {
adamc@667 233 pthread_mutex_unlock(&clients_mutex);
adamc@668 234 return NULL;
adamc@667 235 }
adamc@667 236
adamc@667 237 c = clients[id];
adamc@667 238
adamc@668 239 pthread_mutex_unlock(&clients_mutex);
adamc@668 240 return c;
adamc@668 241 }
adamc@668 242
adamc@856 243 static char *on_success = "HTTP/1.1 200 OK\r\n";
adam@1409 244 static char *on_redirect = "HTTP/1.1 303 See Other\r\n";
adamc@856 245
adamc@856 246 void uw_set_on_success(char *s) {
adamc@856 247 on_success = s;
adamc@856 248 }
adamc@856 249
adamc@863 250 void uw_client_connect(unsigned id, int pass, int sock,
adamc@863 251 int (*send)(int sockfd, const void *buf, ssize_t len),
adamc@864 252 int (*close)(int fd),
adamc@864 253 void *logger_data, uw_logger log_error) {
adamc@682 254 client *c = find_client(id);
adamc@668 255
adamc@668 256 if (c == NULL) {
adamc@667 257 close(sock);
adamc@864 258 log_error(logger_data, "Out-of-bounds client request (%u)\n", id);
adamc@667 259 return;
adamc@667 260 }
adamc@667 261
adamc@682 262 pthread_mutex_lock(&c->lock);
adamc@671 263
adamc@682 264 if (c->mode != USED) {
adamc@682 265 pthread_mutex_unlock(&c->lock);
adamc@667 266 close(sock);
adamc@864 267 log_error(logger_data, "Client request for unused slot (%u)\n", id);
adamc@667 268 return;
adamc@667 269 }
adamc@667 270
adamc@682 271 if (pass != c->pass) {
adamc@682 272 pthread_mutex_unlock(&c->lock);
adamc@682 273 close(sock);
adamc@864 274 log_error(logger_data, "Wrong client password (%u, %d)\n", id, pass);
adamc@682 275 return;
adamc@667 276 }
adamc@667 277
adamc@682 278 if (c->sock != -1) {
adamc@863 279 c->close(c->sock);
adamc@682 280 c->sock = -1;
adamc@682 281 }
adamc@667 282
adamc@682 283 c->last_contact = time(NULL);
adamc@682 284
adam@1369 285 if (uw_buffer_used(&c->msgs) > 0) {
adamc@863 286 send(sock, on_success, strlen(on_success));
adamc@863 287 send(sock, begin_msgs, sizeof(begin_msgs) - 1);
adam@1369 288 send(sock, c->msgs.start, uw_buffer_used(&c->msgs));
adam@1369 289 uw_buffer_reset(&c->msgs);
adamc@667 290 close(sock);
adamc@667 291 }
adamc@863 292 else {
adamc@682 293 c->sock = sock;
adamc@863 294 c->send = send;
adamc@863 295 c->close = close;
adamc@863 296 }
adamc@667 297
adamc@682 298 pthread_mutex_unlock(&c->lock);
adamc@667 299 }
adamc@667 300
adamc@682 301 static void free_client(client *c) {
adamc@682 302 c->mode = UNUSED;
adamc@682 303 c->pass = -1;
adamc@667 304
adamc@682 305 c->next = clients_free;
adamc@682 306 clients_free = c;
adamc@667 307 }
adamc@667 308
adamc@682 309 static uw_Basis_channel new_channel(client *c) {
adamc@682 310 uw_Basis_channel ch = {c->id, c->n_channels++};
adamc@668 311 return ch;
adamc@668 312 }
adamc@668 313
adam@1369 314 static void client_send(client *c, uw_buffer *msg) {
adamc@685 315 pthread_mutex_lock(&c->lock);
adamc@671 316
adamc@682 317 if (c->sock != -1) {
adamc@863 318 c->send(c->sock, on_success, strlen(on_success));
adamc@863 319 c->send(c->sock, begin_msgs, sizeof(begin_msgs) - 1);
adam@1369 320 c->send(c->sock, msg->start, uw_buffer_used(msg));
adamc@863 321 c->close(c->sock);
adamc@682 322 c->sock = -1;
adam@1369 323 } else if (uw_buffer_append(&c->msgs, msg->start, uw_buffer_used(msg)))
adamc@1113 324 fprintf(stderr, "Client message buffer size exceeded");
adamc@671 325
adamc@685 326 pthread_mutex_unlock(&c->lock);
adamc@668 327 }
adamc@668 328
adamc@668 329
adamc@668 330 // Global entry points
adamc@668 331
adamc@1164 332 extern void uw_global_custom();
adamc@1104 333 extern void uw_init_crypto();
adamc@1104 334
adamc@668 335 void uw_global_init() {
adamc@668 336 srand(time(NULL) ^ getpid());
adamc@668 337
adamc@668 338 clients = malloc(0);
adamc@1104 339
adamc@1164 340 uw_global_custom();
adamc@1104 341 uw_init_crypto();
adamc@1094 342 }
adamc@1094 343
adamc@1094 344 void uw_app_init(uw_app *app) {
adamc@1094 345 app->client_init();
adamc@668 346 }
adamc@668 347
adam@1332 348 int uw_time = 0, uw_time_max = 0, uw_min_heap = 0;
adamc@1114 349
adamc@668 350
adamc@667 351 // Single-request state
adamc@667 352
adamc@323 353 typedef struct regions {
adamc@323 354 struct regions *next;
adamc@323 355 } regions;
adamc@323 356
adamc@425 357 typedef struct {
adamc@425 358 void (*func)(void*);
adamc@425 359 void *arg;
adamc@425 360 } cleanup;
adamc@425 361
adamc@671 362 typedef struct {
adamc@682 363 unsigned client;
adam@1369 364 uw_buffer msgs;
adamc@682 365 } delta;
adamc@671 366
adamc@737 367 typedef enum {
adamc@759 368 UNSET, NORMAL, FIL, SUBFORM, SUBFORMS, ENTRY
adamc@737 369 } input_kind;
adamc@737 370
adamc@756 371 typedef struct input {
adamc@737 372 input_kind kind;
adamc@737 373 union {
adamc@737 374 char *normal;
adamc@739 375 uw_Basis_file file;
adamc@756 376 struct {
adamc@759 377 struct input *fields, *parent;
adamc@756 378 } subform;
adamc@759 379 struct {
adamc@759 380 struct input *entries, *parent;
adamc@759 381 } subforms;
adamc@759 382 struct {
adamc@759 383 struct input *fields, *next, *parent;
adamc@759 384 } entry;
adamc@737 385 } data;
adamc@737 386 } input;
adamc@737 387
adamc@771 388 typedef struct {
adamc@771 389 void *data;
adam@1327 390 uw_callback commit, rollback;
adam@1327 391 uw_callback_with_retry free;
adamc@771 392 } transactional;
adamc@771 393
adamc@1061 394 typedef struct {
adamc@1061 395 char *name;
adamc@1061 396 void *data;
adamc@1061 397 void (*free)(void*);
adamc@1061 398 } global;
adamc@1061 399
adamc@311 400 struct uw_context {
adamc@1094 401 uw_app *app;
adamc@1094 402
adamc@854 403 char *(*get_header)(void *, const char *);
adamc@854 404 void *get_header_data;
adamc@457 405
adam@1369 406 uw_buffer outHeaders, page, heap, script;
adamc@1065 407 int returning_indirectly;
adamc@759 408 input *inputs, *subinputs, *cur_container;
adamc@1094 409 size_t sz_inputs, n_subinputs, used_subinputs;
adamc@167 410
adamc@565 411 int source_count;
adamc@562 412
adamc@272 413 void *db;
adamc@272 414
adamc@167 415 jmp_buf jmp_buf;
adamc@167 416
adamc@323 417 regions *regions;
adamc@323 418
adamc@425 419 cleanup *cleanup, *cleanup_front, *cleanup_back;
adamc@425 420
adamc@1094 421 const char *script_header;
adamc@643 422
adamc@736 423 int needs_push, needs_sig;
adamc@693 424
adamc@682 425 size_t n_deltas, used_deltas;
adamc@682 426 delta *deltas;
adamc@671 427
adamc@682 428 client *client;
adamc@682 429
adamc@771 430 transactional *transactionals;
adamc@771 431 size_t n_transactionals, used_transactionals;
adamc@771 432
adamc@1061 433 global *globals;
adamc@1061 434 size_t n_globals;
adamc@1061 435
adamc@1066 436 char *current_url;
adamc@1066 437
adamc@1114 438 int deadline;
adamc@1114 439
adamc@1121 440 void *client_data;
adamc@1121 441
adam@1334 442 void *logger_data;
adam@1334 443 uw_logger log_debug;
adam@1334 444
adam@1386 445 int isPost, hasPostBody;
adam@1347 446 uw_Basis_postBody postBody;
adam@1370 447 uw_Basis_string queryString;
adam@1347 448
adamc@167 449 char error_message[ERROR_BUF_LEN];
adamc@117 450 };
adamc@117 451
adamc@1113 452 size_t uw_headers_max = SIZE_MAX;
adamc@1113 453 size_t uw_page_max = SIZE_MAX;
adamc@1113 454 size_t uw_heap_max = SIZE_MAX;
adamc@1113 455 size_t uw_script_max = SIZE_MAX;
adamc@1113 456
adam@1334 457 uw_context uw_init(void *logger_data, uw_logger log_debug) {
adamc@311 458 uw_context ctx = malloc(sizeof(struct uw_context));
adamc@136 459
adamc@1094 460 ctx->app = NULL;
adamc@1094 461
adamc@854 462 ctx->get_header = NULL;
adamc@854 463 ctx->get_header_data = NULL;
adamc@457 464
adam@1413 465 uw_buffer_init(uw_headers_max, &ctx->outHeaders, 1);
adam@1413 466 ctx->outHeaders.start[0] = 0;
adam@1413 467 uw_buffer_init(uw_page_max, &ctx->page, 1);
adam@1413 468 ctx->page.start[0] = 0;
adamc@1065 469 ctx->returning_indirectly = 0;
adam@1369 470 uw_buffer_init(uw_heap_max, &ctx->heap, uw_min_heap);
adam@1369 471 uw_buffer_init(uw_script_max, &ctx->script, 1);
adamc@667 472 ctx->script.start[0] = 0;
adamc@136 473
adamc@1094 474 ctx->inputs = malloc(0);
adamc@759 475 ctx->cur_container = NULL;
adamc@756 476 ctx->subinputs = malloc(0);
adamc@1094 477 ctx->sz_inputs = ctx->n_subinputs = ctx->used_subinputs = 0;
adamc@144 478
adamc@272 479 ctx->db = NULL;
adamc@272 480
adamc@323 481 ctx->regions = NULL;
adamc@323 482
adamc@425 483 ctx->cleanup_front = ctx->cleanup_back = ctx->cleanup = malloc(0);
adamc@425 484
adamc@643 485 ctx->script_header = "";
adamc@693 486 ctx->needs_push = 0;
adamc@736 487 ctx->needs_sig = 0;
adamc@1104 488
adamc@565 489 ctx->source_count = 0;
adamc@562 490
adamc@682 491 ctx->n_deltas = ctx->used_deltas = 0;
adamc@671 492 ctx->deltas = malloc(0);
adamc@671 493
adamc@700 494 ctx->client = NULL;
adamc@700 495
adamc@700 496 ctx->error_message[0] = 0;
adamc@700 497
adamc@771 498 ctx->transactionals = malloc(0);
adamc@771 499 ctx->n_transactionals = ctx->used_transactionals = 0;
adamc@771 500
adamc@1061 501 ctx->globals = malloc(0);
adamc@1061 502 ctx->n_globals = 0;
adamc@1061 503
adamc@1066 504 ctx->current_url = "";
adamc@1066 505
adamc@1114 506 ctx->deadline = INT_MAX;
adamc@1114 507
adamc@1121 508 ctx->client_data = uw_init_client_data();
adamc@1121 509
adam@1334 510 ctx->logger_data = logger_data;
adam@1334 511 ctx->log_debug = log_debug;
adam@1334 512
adam@1386 513 ctx->isPost = ctx->hasPostBody = 0;
adam@1347 514
adam@1370 515 ctx->queryString = NULL;
adam@1370 516
adamc@117 517 return ctx;
adamc@106 518 }
adamc@106 519
adamc@1113 520 size_t uw_inputs_max = SIZE_MAX;
adamc@1113 521
adamc@1121 522 uw_app *uw_get_app(uw_context ctx) {
adamc@1121 523 return ctx->app;
adamc@1121 524 }
adamc@1121 525
adamc@1113 526 int uw_set_app(uw_context ctx, uw_app *app) {
adamc@1094 527 ctx->app = app;
adamc@1094 528
adamc@1105 529 if (app && app->inputs_len > ctx->sz_inputs) {
adamc@1113 530 if (app->inputs_len > uw_inputs_max)
adamc@1113 531 return 1;
adamc@1113 532
adamc@1094 533 ctx->sz_inputs = app->inputs_len;
adamc@1094 534 ctx->inputs = realloc(ctx->inputs, ctx->sz_inputs * sizeof(input));
adamc@1094 535 memset(ctx->inputs, 0, ctx->sz_inputs * sizeof(input));
adamc@1094 536 }
adamc@1134 537
adamc@1134 538 return 0;
adamc@1094 539 }
adamc@1094 540
adamc@1121 541 void uw_set_client_data(uw_context ctx, void *data) {
adamc@1121 542 uw_copy_client_data(ctx->client_data, data);
adamc@1121 543 }
adamc@1121 544
adamc@311 545 void uw_set_db(uw_context ctx, void *db) {
adamc@272 546 ctx->db = db;
adamc@272 547 }
adamc@272 548
adamc@311 549 void *uw_get_db(uw_context ctx) {
adamc@272 550 return ctx->db;
adamc@272 551 }
adamc@272 552
adamc@311 553 void uw_free(uw_context ctx) {
adamc@671 554 size_t i;
adamc@671 555
adam@1369 556 uw_buffer_free(&ctx->outHeaders);
adam@1369 557 uw_buffer_free(&ctx->script);
adam@1369 558 uw_buffer_free(&ctx->page);
adam@1369 559 uw_buffer_free(&ctx->heap);
adamc@144 560 free(ctx->inputs);
adamc@756 561 free(ctx->subinputs);
adamc@425 562 free(ctx->cleanup);
adamc@771 563 free(ctx->transactionals);
adamc@1121 564 uw_free_client_data(ctx->client_data);
adamc@671 565
adamc@682 566 for (i = 0; i < ctx->n_deltas; ++i)
adam@1369 567 uw_buffer_free(&ctx->deltas[i].msgs);
marco-oweber@1330 568 free(ctx->deltas);
adamc@671 569
adamc@1061 570 for (i = 0; i < ctx->n_globals; ++i)
adamc@1089 571 if (ctx->globals[i].free)
adamc@1089 572 ctx->globals[i].free(ctx->globals[i].data);
marco-oweber@1330 573 free(ctx->globals);
adamc@1061 574
adamc@136 575 free(ctx);
adamc@136 576 }
adamc@136 577
adamc@562 578 void uw_reset_keep_error_message(uw_context ctx) {
adam@1369 579 uw_buffer_reset(&ctx->outHeaders);
adam@1369 580 uw_buffer_reset(&ctx->script);
adamc@667 581 ctx->script.start[0] = 0;
adam@1369 582 uw_buffer_reset(&ctx->page);
adamc@1065 583 ctx->returning_indirectly = 0;
adam@1369 584 uw_buffer_reset(&ctx->heap);
adamc@323 585 ctx->regions = NULL;
adamc@425 586 ctx->cleanup_front = ctx->cleanup;
adamc@565 587 ctx->source_count = 0;
adamc@682 588 ctx->used_deltas = 0;
adamc@682 589 ctx->client = NULL;
adamc@759 590 ctx->cur_container = NULL;
adamc@771 591 ctx->used_transactionals = 0;
adamc@1117 592 ctx->script_header = "";
adam@1417 593 ctx->hasPostBody = 0;
adam@1370 594 ctx->queryString = NULL;
adamc@167 595 }
adamc@167 596
adamc@562 597 void uw_reset_keep_request(uw_context ctx) {
adamc@562 598 uw_reset_keep_error_message(ctx);
adamc@562 599 ctx->error_message[0] = 0;
adamc@167 600 }
adamc@167 601
adamc@311 602 void uw_reset(uw_context ctx) {
adamc@311 603 uw_reset_keep_request(ctx);
adamc@1105 604 if (ctx->app)
adamc@1105 605 memset(ctx->inputs, 0, ctx->app->inputs_len * sizeof(input));
adamc@756 606 memset(ctx->subinputs, 0, ctx->n_subinputs * sizeof(input));
adamc@756 607 ctx->used_subinputs = 0;
adam@1417 608 ctx->isPost = 0;
adamc@144 609 }
adamc@144 610
adamc@311 611 failure_kind uw_begin_init(uw_context ctx) {
adamc@272 612 int r = setjmp(ctx->jmp_buf);
adamc@272 613
adamc@1105 614 if (r == 0 && ctx->app)
adamc@1094 615 ctx->app->db_init(ctx);
adamc@272 616
adamc@272 617 return r;
adamc@272 618 }
adamc@272 619
adamc@1118 620 void uw_close(uw_context ctx) {
adamc@1118 621 ctx->app->db_close(ctx);
adamc@1118 622 }
adamc@1118 623
adamc@854 624 void uw_set_headers(uw_context ctx, char *(*get_header)(void *, const char *), void *get_header_data) {
adamc@854 625 ctx->get_header = get_header;
adamc@854 626 ctx->get_header_data = get_header_data;
adamc@742 627 }
adamc@742 628
adamc@801 629 static void uw_set_error(uw_context ctx, const char *fmt, ...) {
adamc@801 630 va_list ap;
adamc@801 631 va_start(ap, fmt);
adamc@801 632
adamc@801 633 vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap);
adamc@801 634 }
adamc@801 635
adamc@1131 636 int uw_has_error(uw_context ctx) {
adamc@1131 637 return ctx->error_message[0] != 0;
adamc@1131 638 }
adamc@1131 639
adamc@311 640 __attribute__((noreturn)) void uw_error(uw_context ctx, failure_kind fk, const char *fmt, ...) {
adamc@425 641 cleanup *cl;
adamc@425 642
adamc@167 643 va_list ap;
adamc@167 644 va_start(ap, fmt);
adamc@167 645
adamc@167 646 vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap);
adamc@167 647
adamc@425 648 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@425 649 cl->func(cl->arg);
adamc@425 650
adamc@425 651 ctx->cleanup_front = ctx->cleanup;
adamc@425 652
adamc@190 653 longjmp(ctx->jmp_buf, fk);
adamc@167 654 }
adamc@167 655
adamc@1113 656 size_t uw_cleanup_max = SIZE_MAX;
adamc@1113 657
adamc@425 658 void uw_push_cleanup(uw_context ctx, void (*func)(void *), void *arg) {
adamc@425 659 if (ctx->cleanup_front >= ctx->cleanup_back) {
adamc@425 660 int len = ctx->cleanup_back - ctx->cleanup, newLen;
adamc@425 661 if (len == 0)
adamc@425 662 newLen = 1;
adamc@425 663 else
adamc@428 664 newLen = len * 2;
adamc@1113 665
adamc@1134 666 if (newLen > uw_cleanup_max) {
adamc@1113 667 if (len+1 <= uw_cleanup_max)
adamc@1113 668 newLen = uw_cleanup_max;
adamc@1113 669 else
adamc@1113 670 uw_error(ctx, FATAL, "Exceeded limit on number of cleanup handlers");
adamc@1134 671 }
adamc@1113 672
adamc@470 673 ctx->cleanup = realloc(ctx->cleanup, newLen * sizeof(cleanup));
adamc@425 674 ctx->cleanup_front = ctx->cleanup + len;
adamc@425 675 ctx->cleanup_back = ctx->cleanup + newLen;
adamc@425 676 }
adamc@425 677
adamc@425 678 ctx->cleanup_front->func = func;
adamc@425 679 ctx->cleanup_front->arg = arg;
adamc@425 680 ++ctx->cleanup_front;
adamc@425 681 }
adamc@425 682
adamc@682 683 uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) {
adamc@854 684 return ctx->get_header(ctx->get_header_data, h);
adamc@682 685 }
adamc@682 686
adamc@682 687 void uw_login(uw_context ctx) {
adamc@693 688 if (ctx->needs_push) {
adamc@682 689 char *id_s, *pass_s;
adamc@682 690
adamc@682 691 if ((id_s = uw_Basis_requestHeader(ctx, "UrWeb-Client"))
adamc@682 692 && (pass_s = uw_Basis_requestHeader(ctx, "UrWeb-Pass"))) {
adamc@682 693 unsigned id = atoi(id_s);
adamc@682 694 int pass = atoi(pass_s);
adamc@682 695 client *c = find_client(id);
adamc@682 696
adamc@682 697 if (c == NULL)
adamc@682 698 uw_error(ctx, FATAL, "Unknown client ID in HTTP headers (%s, %s)", id_s, pass_s);
adamc@682 699 else {
adamc@685 700 use_client(c);
adamc@682 701 ctx->client = c;
adamc@682 702
adamc@682 703 if (c->mode != USED)
adamc@682 704 uw_error(ctx, FATAL, "Stale client ID (%u) in subscription request", id);
adamc@682 705 if (c->pass != pass)
adamc@682 706 uw_error(ctx, FATAL, "Wrong client password (%u, %d) in subscription request", id, pass);
adamc@682 707 }
adamc@682 708 } else {
adamc@682 709 client *c = new_client();
adamc@1113 710
adamc@1113 711 if (c == NULL)
adamc@1113 712 uw_error(ctx, FATAL, "Limit exceeded on number of message-passing clients");
adamc@1113 713
adamc@685 714 use_client(c);
adamc@1121 715 uw_copy_client_data(c->data, ctx->client_data);
adamc@682 716 ctx->client = c;
adamc@864 717 }
adamc@682 718 }
adamc@682 719 }
adamc@682 720
adamc@671 721 failure_kind uw_begin(uw_context ctx, char *path) {
adamc@671 722 int r = setjmp(ctx->jmp_buf);
adamc@671 723
adamc@671 724 if (r == 0) {
adamc@1094 725 if (ctx->app->db_begin(ctx))
adamc@671 726 uw_error(ctx, BOUNDED_RETRY, "Error running SQL BEGIN");
adamc@682 727
adamc@1094 728 ctx->app->handle(ctx, path);
adamc@671 729 }
adamc@671 730
adamc@671 731 return r;
adamc@671 732 }
adamc@671 733
adamc@772 734 uw_Basis_client uw_Basis_self(uw_context ctx) {
adamc@682 735 if (ctx->client == NULL)
adamc@682 736 uw_error(ctx, FATAL, "Call to Basis.self() from page that has only server-side code");
adamc@682 737
adamc@682 738 return ctx->client->id;
adamc@682 739 }
adamc@682 740
adamc@425 741 void uw_pop_cleanup(uw_context ctx) {
adamc@425 742 if (ctx->cleanup_front == ctx->cleanup)
adamc@425 743 uw_error(ctx, FATAL, "Attempt to pop from empty cleanup action stack");
adamc@425 744
adamc@425 745 --ctx->cleanup_front;
adamc@425 746 ctx->cleanup_front->func(ctx->cleanup_front->arg);
adamc@425 747 }
adamc@425 748
adamc@311 749 char *uw_error_message(uw_context ctx) {
adamc@167 750 return ctx->error_message;
adamc@167 751 }
adamc@167 752
adamc@1115 753 void uw_set_error_message(uw_context ctx, const char *msg) {
adamc@1115 754 strncpy(ctx->error_message, msg, sizeof(ctx->error_message));
adamc@1115 755 ctx->error_message[sizeof(ctx->error_message)-1] = 0;
adamc@1115 756 }
adamc@1115 757
adamc@759 758 static input *INP(uw_context ctx) {
adamc@759 759 if (ctx->cur_container == NULL)
adamc@759 760 return ctx->inputs;
adamc@759 761 else if (ctx->cur_container->kind == SUBFORM)
adamc@759 762 return ctx->cur_container->data.subform.fields;
adamc@759 763 else if (ctx->cur_container->kind == ENTRY)
adamc@759 764 return ctx->cur_container->data.entry.fields;
adamc@759 765 else
adamc@760 766 uw_error(ctx, FATAL, "INP: Wrong kind (%d, %p)", ctx->cur_container->kind, ctx->cur_container);
adamc@759 767 }
adamc@759 768
adamc@760 769 static void adjust_pointer(input **ptr, input *old_start, input *new_start, size_t len) {
adamc@760 770 if (*ptr != NULL && *ptr >= old_start && *ptr < old_start + len)
adamc@760 771 *ptr += new_start - old_start;
adamc@760 772 }
adamc@760 773
adamc@760 774 static void adjust_input(input *x, input *old_start, input *new_start, size_t len) {
adamc@759 775 switch (x->kind) {
adamc@759 776 case SUBFORM:
adamc@760 777 adjust_pointer(&x->data.subform.fields, old_start, new_start, len);
adamc@760 778 adjust_pointer(&x->data.subform.parent, old_start, new_start, len);
adamc@759 779 break;
adamc@759 780 case SUBFORMS:
adamc@760 781 adjust_pointer(&x->data.subforms.entries, old_start, new_start, len);
adamc@760 782 adjust_pointer(&x->data.subforms.parent, old_start, new_start, len);
adamc@759 783 break;
adamc@759 784 case ENTRY:
adamc@760 785 adjust_pointer(&x->data.entry.fields, old_start, new_start, len);
adamc@760 786 adjust_pointer(&x->data.entry.next, old_start, new_start, len);
adamc@760 787 adjust_pointer(&x->data.entry.parent, old_start, new_start, len);
adamc@1134 788 break;
adamc@1134 789 default:
adamc@1134 790 break;
adamc@759 791 }
adamc@759 792 }
adamc@759 793
adamc@1113 794 size_t uw_subinputs_max = SIZE_MAX;
adamc@1113 795
adamc@759 796 static input *check_input_space(uw_context ctx, size_t len) {
adamc@759 797 size_t i;
adamc@759 798 input *r;
adamc@759 799
adamc@759 800 if (ctx->used_subinputs + len >= ctx->n_subinputs) {
adamc@1113 801 if (ctx->used_subinputs + len > uw_subinputs_max)
adamc@1113 802 uw_error(ctx, FATAL, "Exceeded limit on number of subinputs");
adamc@1113 803
adamc@759 804 input *new_subinputs = realloc(ctx->subinputs, sizeof(input) * (ctx->used_subinputs + len));
adamc@759 805
adamc@760 806 if (ctx->subinputs != new_subinputs) {
adamc@760 807 for (i = 0; i < ctx->used_subinputs; ++i)
adamc@760 808 adjust_input(&new_subinputs[i], ctx->subinputs, new_subinputs, ctx->used_subinputs);
adamc@1094 809 for (i = 0; i < ctx->app->inputs_len; ++i)
adamc@760 810 adjust_input(&ctx->inputs[i], ctx->subinputs, new_subinputs, ctx->used_subinputs);
adamc@760 811
adamc@760 812 adjust_pointer(&ctx->cur_container, ctx->subinputs, new_subinputs, ctx->used_subinputs);
adamc@760 813
adamc@760 814 ctx->n_subinputs = ctx->used_subinputs + len;
adamc@760 815 ctx->subinputs = new_subinputs;
adamc@760 816 }
adamc@759 817 }
adamc@759 818
adamc@759 819 r = &ctx->subinputs[ctx->used_subinputs];
adamc@759 820
adamc@759 821 for (i = 0; i < len; ++i)
adamc@759 822 ctx->subinputs[ctx->used_subinputs++].kind = UNUSED;
adamc@759 823
adamc@759 824 return r;
adamc@759 825 }
adamc@756 826
adamc@801 827 int uw_set_input(uw_context ctx, const char *name, char *value) {
adamc@1050 828 //printf("Input name %s\n", name);
adamc@1038 829
adamc@756 830 if (!strcasecmp(name, ".b")) {
adamc@1094 831 int n = ctx->app->input_num(value);
adamc@756 832 input *inps;
adamc@756 833
adamc@801 834 if (n < 0) {
adamc@801 835 uw_set_error(ctx, "Bad subform name %s", value);
adamc@801 836 return -1;
adamc@801 837 }
adamc@801 838
adamc@1094 839 if (n >= ctx->app->inputs_len) {
adamc@801 840 uw_set_error(ctx, "For subform name %s, index %d is out of range", value, n);
adamc@801 841 return -1;
adamc@801 842 }
adamc@756 843
adamc@1094 844 inps = check_input_space(ctx, ctx->app->inputs_len);
adamc@759 845
adamc@759 846 INP(ctx)[n].kind = SUBFORM;
adamc@759 847 INP(ctx)[n].data.subform.parent = ctx->cur_container;
adamc@759 848 INP(ctx)[n].data.subform.fields = inps;
adamc@759 849 ctx->cur_container = &INP(ctx)[n];
adamc@756 850 } else if (!strcasecmp(name, ".e")) {
adamc@756 851 input *tmp;
adamc@756 852
adamc@801 853 if (ctx->cur_container == NULL) {
adamc@801 854 uw_set_error(ctx, "Unmatched subform closer");
adamc@801 855 return -1;
adamc@801 856 }
adamc@756 857
adamc@759 858 tmp = ctx->cur_container;
adamc@759 859 switch (tmp->kind) {
adamc@759 860 case SUBFORM:
adamc@759 861 ctx->cur_container = tmp->data.subform.parent;
adamc@759 862 tmp->data.subform.parent = NULL;
adamc@759 863 break;
adamc@759 864 case SUBFORMS:
adamc@759 865 ctx->cur_container = tmp->data.subforms.parent;
adamc@759 866 tmp->data.subforms.parent = NULL;
adamc@759 867 break;
adamc@759 868 case ENTRY:
adamc@759 869 ctx->cur_container = tmp->data.entry.parent;
adamc@759 870 break;
adamc@759 871 default:
adamc@801 872 uw_set_error(ctx, "uw_set_input: Wrong kind");
adamc@801 873 return -1;
adamc@759 874 }
adamc@759 875 } else if (!strcasecmp(name, ".s")) {
adamc@1094 876 int n = ctx->app->input_num(value);
adamc@759 877
adamc@801 878 if (n < 0) {
adamc@801 879 uw_set_error(ctx, "Bad subforms name %s", value);
adamc@801 880 return -1;
adamc@801 881 }
adamc@801 882
adamc@1094 883 if (n >= ctx->app->inputs_len) {
adamc@801 884 uw_set_error(ctx, "For subforms name %s, index %d is out of range", value, n);
adamc@801 885 return -1;
adamc@801 886 }
adamc@759 887
adamc@759 888 INP(ctx)[n].kind = SUBFORMS;
adamc@759 889 INP(ctx)[n].data.subforms.parent = ctx->cur_container;
adamc@759 890 INP(ctx)[n].data.subforms.entries = NULL;
adamc@759 891 ctx->cur_container = &INP(ctx)[n];
adamc@759 892 } else if (!strcasecmp(name, ".i")) {
adamc@759 893 input *inps;
adamc@759 894
adamc@801 895 if (!ctx->cur_container) {
adamc@801 896 uw_set_error(ctx, "New entry without container");
adamc@801 897 return -1;
adamc@801 898 }
adamc@801 899
adamc@801 900 if (ctx->cur_container->kind != SUBFORMS) {
adamc@801 901 uw_set_error(ctx, "Bad kind for entry parent");
adamc@801 902 return -1;
adamc@801 903 }
adamc@759 904
adamc@1094 905 inps = check_input_space(ctx, ctx->app->inputs_len + 1);
adamc@759 906
adamc@759 907 inps->kind = ENTRY;
adamc@759 908 inps->data.entry.parent = ctx->cur_container;
adamc@759 909 inps->data.entry.next = ctx->cur_container->data.subforms.entries;
adamc@759 910 ctx->cur_container->data.subforms.entries = inps;
adamc@759 911
adamc@759 912 inps->data.entry.fields = inps+1;
adamc@759 913 ctx->cur_container = inps;
adamc@756 914 } else {
adamc@1094 915 int n = ctx->app->input_num(name);
adamc@756 916
adam@1384 917 if (n < 0)
adam@1384 918 return 0;
adamc@801 919
adamc@1094 920 if (n >= ctx->app->inputs_len) {
adamc@801 921 uw_set_error(ctx, "For input name %s, index %d is out of range", name, n);
adamc@801 922 return -1;
adamc@801 923 }
adamc@756 924
adamc@756 925 INP(ctx)[n].kind = NORMAL;
adamc@756 926 INP(ctx)[n].data.normal = value;
adamc@756 927 }
adamc@801 928
adamc@801 929 return 0;
adamc@144 930 }
adamc@144 931
adamc@311 932 char *uw_get_input(uw_context ctx, int n) {
adamc@169 933 if (n < 0)
adamc@311 934 uw_error(ctx, FATAL, "Negative input index %d", n);
adamc@1094 935 if (n >= ctx->app->inputs_len)
adamc@311 936 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
adamc@737 937
adamc@756 938 switch (INP(ctx)[n].kind) {
adamc@737 939 case UNSET:
adamc@737 940 return NULL;
adamc@739 941 case FIL:
adamc@739 942 uw_error(ctx, FATAL, "Tried to read a file form input as normal");
adamc@756 943 case SUBFORM:
adamc@756 944 uw_error(ctx, FATAL, "Tried to read a subform form input as normal");
adamc@759 945 case SUBFORMS:
adamc@759 946 uw_error(ctx, FATAL, "Tried to read a subforms form input as normal");
adamc@759 947 case ENTRY:
adamc@759 948 uw_error(ctx, FATAL, "Tried to read an entry form input as normal");
adamc@737 949 case NORMAL:
adamc@756 950 return INP(ctx)[n].data.normal;
adamc@737 951 default:
adamc@737 952 uw_error(ctx, FATAL, "Impossible input kind");
adamc@737 953 }
adamc@138 954 }
adamc@138 955
adamc@311 956 char *uw_get_optional_input(uw_context ctx, int n) {
adamc@190 957 if (n < 0)
adamc@311 958 uw_error(ctx, FATAL, "Negative input index %d", n);
adamc@1094 959 if (n >= ctx->app->inputs_len)
adamc@311 960 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
adamc@737 961
adamc@756 962 switch (INP(ctx)[n].kind) {
adamc@737 963 case UNSET:
adamc@737 964 return "";
adamc@739 965 case FIL:
adamc@739 966 uw_error(ctx, FATAL, "Tried to read a file form input as normal");
adamc@756 967 case SUBFORM:
adamc@756 968 uw_error(ctx, FATAL, "Tried to read a subform form input as normal");
adamc@759 969 case SUBFORMS:
adamc@759 970 uw_error(ctx, FATAL, "Tried to read a subforms form input as normal");
adamc@759 971 case ENTRY:
adamc@759 972 uw_error(ctx, FATAL, "Tried to read an entry form input as normal");
adamc@737 973 case NORMAL:
adamc@756 974 return INP(ctx)[n].data.normal;
adamc@737 975 default:
adamc@737 976 uw_error(ctx, FATAL, "Impossible input kind");
adamc@737 977 }
adamc@737 978 }
adamc@737 979
adamc@801 980 int uw_set_file_input(uw_context ctx, const char *name, uw_Basis_file f) {
adamc@1094 981 int n = ctx->app->input_num(name);
adamc@737 982
adamc@801 983 if (n < 0) {
adamc@801 984 uw_set_error(ctx, "Bad file input name %s", name);
adamc@801 985 return -1;
adamc@801 986 }
adamc@801 987
adamc@1094 988 if (n >= ctx->app->inputs_len) {
adamc@801 989 uw_set_error(ctx, "For file input name %s, index %d is out of range", name, n);
adamc@801 990 return -1;
adamc@801 991 }
adamc@737 992
adamc@739 993 ctx->inputs[n].kind = FIL;
adamc@739 994 ctx->inputs[n].data.file = f;
adamc@801 995
adamc@801 996 return 0;
adamc@737 997 }
adamc@737 998
adamc@739 999 void *uw_malloc(uw_context ctx, size_t len);
adamc@739 1000
adamc@739 1001 uw_Basis_file uw_get_file_input(uw_context ctx, int n) {
adamc@737 1002 if (n < 0)
adamc@737 1003 uw_error(ctx, FATAL, "Negative file input index %d", n);
adamc@1094 1004 if (n >= ctx->app->inputs_len)
adamc@737 1005 uw_error(ctx, FATAL, "Out-of-bounds file input index %d", n);
adamc@737 1006
adamc@756 1007 switch (INP(ctx)[n].kind) {
adamc@737 1008 case UNSET:
adamc@737 1009 {
adamc@739 1010 char *data = uw_malloc(ctx, 0);
adamc@740 1011 uw_Basis_file f = {NULL, "", {0, data}};
adamc@739 1012 return f;
adamc@737 1013 }
adamc@739 1014 case FIL:
adamc@756 1015 return INP(ctx)[n].data.file;
adamc@737 1016 case NORMAL:
adamc@737 1017 uw_error(ctx, FATAL, "Tried to read a normal form input as files");
adamc@756 1018 case SUBFORM:
adamc@756 1019 uw_error(ctx, FATAL, "Tried to read a subform form input as files");
adamc@759 1020 case SUBFORMS:
adamc@759 1021 uw_error(ctx, FATAL, "Tried to read a subforms form input as files");
adamc@759 1022 case ENTRY:
adamc@759 1023 uw_error(ctx, FATAL, "Tried to read an entry form input as files");
adamc@737 1024 default:
adamc@737 1025 uw_error(ctx, FATAL, "Impossible input kind");
adamc@737 1026 }
adamc@190 1027 }
adamc@190 1028
adamc@756 1029 void uw_enter_subform(uw_context ctx, int n) {
adamc@756 1030 if (n < 0)
adamc@756 1031 uw_error(ctx, FATAL, "Negative subform index %d", n);
adamc@1094 1032 if (n >= ctx->app->inputs_len)
adamc@756 1033 uw_error(ctx, FATAL, "Out-of-bounds subform index %d", n);
adamc@756 1034
adamc@756 1035 switch (INP(ctx)[n].kind) {
adamc@756 1036 case UNSET:
adamc@756 1037 uw_error(ctx, FATAL, "Missing subform");
adamc@756 1038 case FIL:
adamc@756 1039 uw_error(ctx, FATAL, "Tried to read a file form input as subform");
adamc@756 1040 case NORMAL:
adamc@756 1041 uw_error(ctx, FATAL, "Tried to read a normal form input as subform");
adamc@759 1042 case SUBFORMS:
adamc@759 1043 uw_error(ctx, FATAL, "Tried to read a subforms form input as subform");
adamc@759 1044 case ENTRY:
adamc@759 1045 uw_error(ctx, FATAL, "Tried to read an entry form input as subform");
adamc@756 1046 case SUBFORM:
adamc@759 1047 INP(ctx)[n].data.subform.parent = ctx->cur_container;
adamc@760 1048 ctx->cur_container = &INP(ctx)[n];
adamc@756 1049 return;
adamc@756 1050 default:
adamc@756 1051 uw_error(ctx, FATAL, "Impossible input kind");
adamc@756 1052 }
adamc@756 1053 }
adamc@756 1054
adamc@756 1055 void uw_leave_subform(uw_context ctx) {
adamc@756 1056 input *tmp;
adamc@756 1057
adamc@759 1058 if (ctx->cur_container == NULL)
adamc@756 1059 uw_error(ctx, FATAL, "Unmatched uw_leave_subform");
adamc@756 1060
adamc@759 1061 tmp = ctx->cur_container;
adamc@759 1062 ctx->cur_container = tmp->data.subform.parent;
adamc@759 1063 tmp->data.subform.parent = NULL;
adamc@759 1064 }
adamc@759 1065
adamc@759 1066 int uw_enter_subforms(uw_context ctx, int n) {
adamc@759 1067 input *inps;
adamc@759 1068
adamc@759 1069 if (n < 0)
adamc@759 1070 uw_error(ctx, FATAL, "Negative subforms index %d", n);
adamc@1094 1071 if (n >= ctx->app->inputs_len)
adamc@759 1072 uw_error(ctx, FATAL, "Out-of-bounds subforms index %d", n);
adamc@759 1073
adamc@759 1074 switch (INP(ctx)[n].kind) {
adamc@759 1075 case UNSET:
adamc@759 1076 uw_error(ctx, FATAL, "Missing subforms");
adamc@759 1077 case FIL:
adamc@759 1078 uw_error(ctx, FATAL, "Tried to read a file form input as subforms");
adamc@759 1079 case NORMAL:
adamc@759 1080 uw_error(ctx, FATAL, "Tried to read a normal form input %p as subforms", &INP(ctx)[n]);
adamc@759 1081 case SUBFORM:
adamc@759 1082 uw_error(ctx, FATAL, "Tried to read a subform form input as subforms");
adamc@759 1083 case ENTRY:
adamc@759 1084 uw_error(ctx, FATAL, "Tried to read an entry form input as subforms");
adamc@759 1085 case SUBFORMS:
adamc@759 1086 inps = INP(ctx)[n].data.subforms.entries;
adamc@759 1087 if (inps) {
adamc@759 1088 INP(ctx)[n].data.subforms.parent = ctx->cur_container;
adamc@759 1089 ctx->cur_container = INP(ctx)[n].data.subforms.entries;
adamc@759 1090 return 1;
adamc@759 1091 } else
adamc@759 1092 return 0;
adamc@759 1093 default:
adamc@759 1094 uw_error(ctx, FATAL, "Impossible input kind");
adamc@759 1095 }
adamc@759 1096 }
adamc@759 1097
adamc@759 1098 int uw_next_entry(uw_context ctx) {
adamc@759 1099 if (ctx->cur_container == NULL)
adamc@759 1100 uw_error(ctx, FATAL, "uw_next_entry(NULL)");
adamc@759 1101
adamc@759 1102 switch (ctx->cur_container->kind) {
adamc@759 1103 case UNSET:
adamc@759 1104 uw_error(ctx, FATAL, "Missing entry");
adamc@759 1105 case FIL:
adamc@759 1106 uw_error(ctx, FATAL, "Tried to read a file form input as entry");
adamc@759 1107 case NORMAL:
adamc@759 1108 uw_error(ctx, FATAL, "Tried to read a normal form input as entry");
adamc@759 1109 case SUBFORM:
adamc@759 1110 uw_error(ctx, FATAL, "Tried to read a subform form input as entry");
adamc@759 1111 case SUBFORMS:
adamc@759 1112 uw_error(ctx, FATAL, "Tried to read a subforms form input as entry");
adamc@759 1113 case ENTRY:
adamc@759 1114 if (ctx->cur_container->data.entry.next) {
adamc@759 1115 ctx->cur_container = ctx->cur_container->data.entry.next;
adamc@759 1116 return 1;
adamc@759 1117 } else {
adamc@759 1118 ctx->cur_container = ctx->cur_container->data.entry.parent->data.subforms.parent;
adamc@759 1119 return 0;
adamc@759 1120 }
adamc@759 1121 default:
adamc@759 1122 uw_error(ctx, FATAL, "Impossible input kind");
adamc@759 1123 }
adamc@756 1124 }
adamc@756 1125
adamc@643 1126 void uw_set_script_header(uw_context ctx, const char *s) {
adamc@643 1127 ctx->script_header = s;
adamc@643 1128 }
adamc@643 1129
adamc@1094 1130 const char *uw_get_url_prefix(uw_context ctx) {
adamc@1094 1131 return ctx->app->url_prefix;
adamc@667 1132 }
adamc@667 1133
adamc@693 1134 void uw_set_needs_push(uw_context ctx, int n) {
adamc@693 1135 ctx->needs_push = n;
adamc@693 1136 }
adamc@693 1137
adamc@736 1138 void uw_set_needs_sig(uw_context ctx, int n) {
adamc@736 1139 ctx->needs_sig = n;
adamc@736 1140 }
adamc@736 1141
adamc@667 1142
adam@1369 1143 static void uw_buffer_check_ctx(uw_context ctx, const char *kind, uw_buffer *b, size_t extra, const char *desc) {
adamc@666 1144 if (b->back - b->front < extra) {
adamc@666 1145 size_t desired = b->front - b->start + extra, next;
adamc@136 1146 char *new_heap;
adamc@136 1147
adamc@666 1148 next = b->back - b->start;
adamc@317 1149 if (next == 0)
adamc@317 1150 next = 1;
adamc@317 1151 for (; next < desired; next *= 2);
adamc@136 1152
adamc@1134 1153 if (next > b->max) {
adamc@1113 1154 if (desired <= b->max)
adamc@1113 1155 next = desired;
adamc@1113 1156 else
adamc@1113 1157 uw_error(ctx, FATAL, "Memory limit exceeded (%s)", kind);
adamc@1134 1158 }
adamc@1113 1159
adamc@666 1160 new_heap = realloc(b->start, next);
adamc@666 1161 b->front = new_heap + (b->front - b->start);
adamc@666 1162 b->back = new_heap + next;
adamc@136 1163
adamc@667 1164 if (new_heap != b->start) {
adamc@666 1165 b->start = new_heap;
adam@1389 1166 uw_error(ctx, UNLIMITED_RETRY, "Couldn't allocate new %s contiguously; increasing size to %llu", desc, (unsigned long long)next);
adamc@136 1167 }
adamc@136 1168
adamc@666 1169 b->start = new_heap;
adamc@136 1170 }
adamc@136 1171 }
adamc@136 1172
adamc@832 1173 void uw_check_heap(uw_context ctx, size_t extra) {
adam@1369 1174 uw_buffer_check_ctx(ctx, "heap", &ctx->heap, extra, "heap chunk");
adamc@666 1175 }
adamc@666 1176
adamc@832 1177 char *uw_heap_front(uw_context ctx) {
adamc@832 1178 return ctx->heap.front;
adamc@832 1179 }
adamc@832 1180
adamc@832 1181 void uw_set_heap_front(uw_context ctx, char *fr) {
adamc@832 1182 ctx->heap.front = fr;
adamc@832 1183 }
adamc@832 1184
adamc@311 1185 void *uw_malloc(uw_context ctx, size_t len) {
adamc@136 1186 void *result;
adamc@136 1187
adamc@311 1188 uw_check_heap(ctx, len);
adamc@136 1189
adamc@666 1190 result = ctx->heap.front;
adamc@666 1191 ctx->heap.front += len;
adamc@136 1192 return result;
adamc@117 1193 }
adamc@117 1194
adamc@323 1195 void uw_begin_region(uw_context ctx) {
adamc@666 1196 regions *r = (regions *) ctx->heap.front;
adamc@323 1197
adamc@323 1198 uw_check_heap(ctx, sizeof(regions));
adamc@323 1199
adamc@666 1200 ctx->heap.front += sizeof(regions);
adamc@323 1201
adamc@323 1202 r->next = ctx->regions;
adamc@323 1203 ctx->regions = r;
adamc@323 1204 }
adamc@323 1205
adamc@323 1206 void uw_end_region(uw_context ctx) {
adamc@323 1207 regions *r = ctx->regions;
adamc@323 1208
adamc@323 1209 if (r == NULL)
adamc@323 1210 uw_error(ctx, FATAL, "Region stack underflow");
adamc@323 1211
adamc@666 1212 ctx->heap.front = (char *) r;
adamc@323 1213 ctx->regions = r->next;
adamc@323 1214 }
adamc@323 1215
adamc@324 1216 void uw_memstats(uw_context ctx) {
adam@1369 1217 printf("Headers: %lu/%lu\n", (unsigned long)uw_buffer_used(&ctx->outHeaders), (unsigned long)uw_buffer_avail(&ctx->outHeaders));
adam@1369 1218 printf("Script: %lu/%lu\n", (unsigned long)uw_buffer_used(&ctx->script), (unsigned long)uw_buffer_avail(&ctx->script));
adam@1369 1219 printf("Page: %lu/%lu\n", (unsigned long)uw_buffer_used(&ctx->page), (unsigned long)uw_buffer_avail(&ctx->page));
adam@1369 1220 printf("Heap: %lu/%lu\n", (unsigned long)uw_buffer_used(&ctx->heap), (unsigned long)uw_buffer_avail(&ctx->heap));
adamc@324 1221 }
adamc@324 1222
adamc@311 1223 int uw_send(uw_context ctx, int sock) {
adamc@666 1224 int n = uw_really_send(sock, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);
adamc@462 1225
adamc@462 1226 if (n < 0)
adamc@462 1227 return n;
adamc@462 1228
adamc@462 1229 n = uw_really_send(sock, "\r\n", 2);
adamc@462 1230
adamc@462 1231 if (n < 0)
adamc@462 1232 return n;
adamc@462 1233
adamc@666 1234 return uw_really_send(sock, ctx->page.start, ctx->page.front - ctx->page.start);
adamc@462 1235 }
adamc@462 1236
adamc@856 1237 int uw_print(uw_context ctx, int fd) {
adamc@856 1238 int n = uw_really_write(fd, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);
adamc@856 1239
adamc@856 1240 if (n < 0)
adamc@856 1241 return n;
adamc@856 1242
adamc@856 1243 n = uw_really_write(fd, "\r\n", 2);
adamc@856 1244
adamc@856 1245 if (n < 0)
adamc@856 1246 return n;
adamc@856 1247
adamc@856 1248 return uw_really_write(fd, ctx->page.start, ctx->page.front - ctx->page.start);
adamc@856 1249 }
adamc@856 1250
adamc@860 1251 int uw_output(uw_context ctx, int (*output)(void *data, char *buf, size_t len), void *data) {
adamc@860 1252 int n = output(data, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);
adamc@860 1253
adamc@860 1254 if (n < 0)
adamc@860 1255 return n;
adamc@860 1256
adamc@860 1257 n = output(data, "\r\n", 2);
adamc@860 1258
adamc@860 1259 if (n < 0)
adamc@860 1260 return n;
adamc@860 1261
adamc@860 1262 return output(data, ctx->page.start, ctx->page.front - ctx->page.start);
adamc@860 1263 }
adamc@860 1264
adamc@462 1265 static void uw_check_headers(uw_context ctx, size_t extra) {
adam@1369 1266 ctx_uw_buffer_check(ctx, "headers", &ctx->outHeaders, extra);
adamc@462 1267 }
adamc@462 1268
adamc@462 1269 void uw_write_header(uw_context ctx, uw_Basis_string s) {
adamc@462 1270 int len = strlen(s);
adamc@462 1271
adamc@462 1272 uw_check_headers(ctx, len + 1);
adamc@666 1273 strcpy(ctx->outHeaders.front, s);
adamc@666 1274 ctx->outHeaders.front += len;
adamc@562 1275 }
adamc@562 1276
adamc@1110 1277 void uw_clear_headers(uw_context ctx) {
adam@1369 1278 uw_buffer_reset(&ctx->outHeaders);
adamc@1110 1279 }
adamc@1110 1280
adamc@562 1281 static void uw_check_script(uw_context ctx, size_t extra) {
adam@1369 1282 ctx_uw_buffer_check(ctx, "script", &ctx->script, extra);
adamc@562 1283 }
adamc@562 1284
adamc@562 1285 void uw_write_script(uw_context ctx, uw_Basis_string s) {
adamc@562 1286 int len = strlen(s);
adamc@562 1287
adamc@562 1288 uw_check_script(ctx, len + 1);
adamc@666 1289 strcpy(ctx->script.front, s);
adamc@666 1290 ctx->script.front += len;
adamc@562 1291 }
adamc@562 1292
adamc@645 1293 const char *uw_Basis_get_script(uw_context ctx, uw_unit u) {
adamc@916 1294 return "<sc>";
adamc@565 1295 }
adamc@565 1296
adamc@695 1297 uw_Basis_string uw_Basis_maybe_onload(uw_context ctx, uw_Basis_string s) {
adamc@695 1298 if (s[0] == 0)
adamc@695 1299 return "";
adamc@695 1300 else {
adamc@695 1301 char *r = uw_malloc(ctx, 11 + strlen(s));
adamc@695 1302 sprintf(r, " onload='%s'", s);
adamc@695 1303 return r;
adamc@695 1304 }
adamc@695 1305 }
adamc@695 1306
adamc@1042 1307 uw_Basis_string uw_Basis_maybe_onunload(uw_context ctx, uw_Basis_string s) {
adamc@1042 1308 if (ctx->script_header[0] == 0)
adamc@1042 1309 return "";
adamc@1042 1310 else {
adamc@1042 1311 char *r = uw_malloc(ctx, 22 + strlen(s));
adamc@1042 1312 sprintf(r, " onunload='unload();%s'", s);
adamc@1042 1313 return r;
adamc@1042 1314 }
adamc@1042 1315 }
adamc@1042 1316
adamc@694 1317 const char *uw_Basis_get_settings(uw_context ctx, uw_unit u) {
adamc@736 1318 if (ctx->client == NULL) {
adamc@736 1319 if (ctx->needs_sig) {
adamc@1094 1320 char *sig = ctx->app->cookie_sig(ctx);
adamc@736 1321 char *r = uw_malloc(ctx, strlen(sig) + 8);
adamc@736 1322 sprintf(r, "sig=\"%s\";", sig);
adamc@736 1323 return r;
adamc@736 1324 }
adamc@736 1325 else
adamc@736 1326 return "";
adamc@736 1327 } else {
adamc@1094 1328 char *sig = ctx->needs_sig ? ctx->app->cookie_sig(ctx) : "";
adamc@1094 1329 char *r = uw_malloc(ctx, 59 + 3 * INTS_MAX + strlen(ctx->app->url_prefix)
adamc@736 1330 + (ctx->needs_sig ? strlen(sig) + 7 : 0));
adamc@736 1331 sprintf(r, "client_id=%u;client_pass=%d;url_prefix=\"%s\";timeout=%d;%s%s%slistener();",
adamc@682 1332 ctx->client->id,
adamc@682 1333 ctx->client->pass,
adamc@1094 1334 ctx->app->url_prefix,
adamc@1094 1335 ctx->app->timeout,
adamc@736 1336 ctx->needs_sig ? "sig=\"" : "",
adamc@736 1337 sig,
adamc@736 1338 ctx->needs_sig ? "\";" : "");
adamc@679 1339 return r;
adamc@668 1340 }
adamc@667 1341 }
adamc@667 1342
adamc@574 1343 uw_Basis_string uw_Basis_jsifyString(uw_context ctx, uw_Basis_string s) {
adamc@574 1344 char *r, *s2;
adamc@574 1345
adamc@574 1346 uw_check_heap(ctx, strlen(s) * 4 + 2);
adamc@574 1347
adamc@666 1348 r = s2 = ctx->heap.front;
adamc@574 1349 *s2++ = '"';
adamc@574 1350
adamc@574 1351 for (; *s; s++) {
adam@1286 1352 unsigned char c = *s;
adamc@574 1353
adamc@574 1354 switch (c) {
adamc@574 1355 case '"':
adamc@574 1356 strcpy(s2, "\\\"");
adamc@574 1357 s2 += 2;
adamc@574 1358 break;
adamc@1027 1359 case '\'':
adamc@1027 1360 strcpy(s2, "\\047");
adamc@1027 1361 s2 += 4;
adamc@1027 1362 break;
adamc@574 1363 case '\\':
adamc@574 1364 strcpy(s2, "\\\\");
adamc@574 1365 s2 += 2;
adamc@574 1366 break;
adamc@574 1367 default:
adam@1286 1368 if (isprint((int)c) || c >= 128)
adamc@574 1369 *s2++ = c;
adamc@574 1370 else {
adam@1429 1371 sprintf(s2, "\\%03o", c);
adamc@574 1372 s2 += 4;
adamc@574 1373 }
adamc@574 1374 }
adamc@574 1375 }
adamc@574 1376
adamc@574 1377 strcpy(s2, "\"");
adamc@910 1378 ctx->heap.front = s2 + 2;
adamc@574 1379 return r;
adamc@574 1380 }
adamc@574 1381
adam@1286 1382 uw_Basis_string uw_Basis_jsifyChar(uw_context ctx, uw_Basis_char c1) {
adam@1286 1383 unsigned char c = c1;
adamc@1025 1384 char *r, *s2;
adamc@1025 1385
adamc@1025 1386 uw_check_heap(ctx, 6);
adamc@1025 1387
adamc@1025 1388 r = s2 = ctx->heap.front;
adamc@1025 1389 *s2++ = '"';
adamc@1025 1390
adamc@1025 1391 switch (c) {
adamc@1025 1392 case '"':
adamc@1025 1393 strcpy(s2, "\\\"");
adamc@1025 1394 s2 += 2;
adamc@1025 1395 break;
adamc@1027 1396 case '\'':
adamc@1027 1397 strcpy(s2, "\\047");
adamc@1027 1398 s2 += 4;
adamc@1027 1399 break;
adamc@1025 1400 case '\\':
adamc@1025 1401 strcpy(s2, "\\\\");
adamc@1025 1402 s2 += 2;
adamc@1025 1403 break;
adamc@1025 1404 default:
adam@1286 1405 if (isprint((int)c) || c >= 128)
adamc@1025 1406 *s2++ = c;
adamc@1025 1407 else {
adam@1429 1408 sprintf(s2, "\\%03o", (unsigned char)c);
adamc@1025 1409 s2 += 4;
adamc@1025 1410 }
adamc@1025 1411 }
adamc@1025 1412
adamc@1025 1413 strcpy(s2, "\"");
adamc@1025 1414 ctx->heap.front = s2 + 2;
adamc@1025 1415 return r;
adamc@1025 1416 }
adamc@1025 1417
adamc@574 1418 uw_Basis_string uw_Basis_jsifyString_ws(uw_context ctx, uw_Basis_string s) {
adamc@574 1419 char *r, *s2;
adamc@574 1420
adamc@574 1421 uw_check_script(ctx, strlen(s) * 4 + 2);
adamc@574 1422
adamc@666 1423 r = s2 = ctx->script.front;
adamc@574 1424 *s2++ = '"';
adamc@574 1425
adamc@574 1426 for (; *s; s++) {
adam@1286 1427 unsigned char c = *s;
adamc@574 1428
adamc@574 1429 switch (c) {
adamc@577 1430 case '\'':
adamc@1027 1431 strcpy(s2, "\\");
adamc@574 1432 s2 += 2;
adamc@574 1433 break;
adamc@574 1434 case '\\':
adamc@574 1435 strcpy(s2, "\\\\");
adamc@574 1436 s2 += 2;
adamc@574 1437 break;
adamc@574 1438 default:
adam@1286 1439 if (isprint((int)c) || c >= 128)
adamc@574 1440 *s2++ = c;
adamc@574 1441 else {
adam@1429 1442 sprintf(s2, "\\%03o", c);
adamc@574 1443 s2 += 4;
adamc@574 1444 }
adamc@574 1445 }
adamc@574 1446 }
adamc@574 1447
adamc@574 1448 strcpy(s2, "\"");
adamc@666 1449 ctx->script.front = s2 + 1;
adamc@574 1450 return r;
adamc@574 1451 }
adamc@574 1452
adamc@682 1453 char *uw_Basis_jsifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 1454 if (ctx->client == NULL || chn.cli != ctx->client->id)
adamc@682 1455 return "null";
adamc@682 1456 else {
adamc@682 1457 int len;
adamc@682 1458 char *r;
adamc@682 1459
adamc@682 1460 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 1461 r = ctx->heap.front;
adamc@682 1462 sprintf(r, "%u%n", chn.chn, &len);
adamc@682 1463 ctx->heap.front += len+1;
adamc@682 1464 return r;
adamc@682 1465 }
adamc@682 1466 }
adamc@682 1467
adamc@577 1468 uw_Basis_int uw_Basis_new_client_source(uw_context ctx, uw_Basis_string s) {
adamc@577 1469 int len;
adamc@577 1470 size_t s_len = strlen(s);
adamc@562 1471
adamc@970 1472 uw_check_script(ctx, 18 + INTS_MAX + s_len);
adamc@970 1473 sprintf(ctx->script.front, "var s%d=sc(exec(%n", ctx->source_count, &len);
adamc@666 1474 ctx->script.front += len;
adamc@666 1475 strcpy(ctx->script.front, s);
adamc@666 1476 ctx->script.front += s_len;
adamc@970 1477 strcpy(ctx->script.front, "));");
adamc@972 1478 ctx->script.front += 3;
adamc@562 1479
adamc@565 1480 return ctx->source_count++;
adamc@117 1481 }
adamc@117 1482
adamc@577 1483 uw_unit uw_Basis_set_client_source(uw_context ctx, uw_Basis_int n, uw_Basis_string s) {
adamc@577 1484 int len;
adamc@577 1485 size_t s_len = strlen(s);
adamc@577 1486
adamc@970 1487 uw_check_script(ctx, 12 + INTS_MAX + s_len);
adamc@970 1488 sprintf(ctx->script.front, "sv(s%d,exec(%n", (int)n, &len);
adamc@666 1489 ctx->script.front += len;
adamc@666 1490 strcpy(ctx->script.front, s);
adamc@666 1491 ctx->script.front += s_len;
adamc@970 1492 strcpy(ctx->script.front, "));");
adamc@972 1493 ctx->script.front += 3;
adamc@577 1494
adamc@577 1495 return uw_unit_v;
adamc@577 1496 }
adamc@577 1497
adamc@311 1498 static void uw_check(uw_context ctx, size_t extra) {
adam@1369 1499 ctx_uw_buffer_check(ctx, "page", &ctx->page, extra);
adamc@117 1500 }
adamc@117 1501
adamc@311 1502 static void uw_writec_unsafe(uw_context ctx, char c) {
adamc@666 1503 *(ctx->page.front)++ = c;
adam@1413 1504 *ctx->page.front = 0;
adamc@117 1505 }
adamc@117 1506
adamc@311 1507 void uw_writec(uw_context ctx, char c) {
adam@1413 1508 uw_check(ctx, 2);
adamc@311 1509 uw_writec_unsafe(ctx, c);
adamc@117 1510 }
adamc@117 1511
adamc@311 1512 static void uw_write_unsafe(uw_context ctx, const char* s) {
adamc@117 1513 int len = strlen(s);
adamc@666 1514 memcpy(ctx->page.front, s, len);
adamc@666 1515 ctx->page.front += len;
adamc@117 1516 }
adamc@117 1517
adamc@311 1518 void uw_write(uw_context ctx, const char* s) {
adamc@311 1519 uw_check(ctx, strlen(s) + 1);
adamc@311 1520 uw_write_unsafe(ctx, s);
adamc@666 1521 *ctx->page.front = 0;
adamc@102 1522 }
adamc@106 1523
adamc@311 1524 char *uw_Basis_attrifyInt(uw_context ctx, uw_Basis_int n) {
adamc@136 1525 char *result;
adamc@136 1526 int len;
adamc@311 1527 uw_check_heap(ctx, INTS_MAX);
adamc@666 1528 result = ctx->heap.front;
adamc@276 1529 sprintf(result, "%lld%n", n, &len);
adamc@666 1530 ctx->heap.front += len+1;
adamc@136 1531 return result;
adamc@106 1532 }
adamc@106 1533
adamc@311 1534 char *uw_Basis_attrifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@136 1535 char *result;
adamc@136 1536 int len;
adamc@311 1537 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 1538 result = ctx->heap.front;
adamc@136 1539 sprintf(result, "%g%n", n, &len);
adamc@666 1540 ctx->heap.front += len+1;
adamc@136 1541 return result;
adamc@106 1542 }
adamc@106 1543
adamc@311 1544 char *uw_Basis_attrifyString(uw_context ctx, uw_Basis_string s) {
adamc@136 1545 int len = strlen(s);
adamc@136 1546 char *result, *p;
adamc@311 1547 uw_check_heap(ctx, len * 6 + 1);
adamc@136 1548
adamc@666 1549 result = p = ctx->heap.front;
adamc@136 1550
adamc@136 1551 for (; *s; s++) {
adamc@1054 1552 unsigned char c = *s;
adamc@136 1553
adamc@136 1554 if (c == '"') {
adamc@136 1555 strcpy(p, "&quot;");
adamc@136 1556 p += 6;
adamc@136 1557 } else if (c == '&') {
adamc@136 1558 strcpy(p, "&amp;");
adamc@136 1559 p += 5;
adamc@136 1560 }
adamc@1059 1561 else
adamc@136 1562 *p++ = c;
adamc@136 1563 }
adamc@136 1564
adamc@137 1565 *p++ = 0;
adamc@666 1566 ctx->heap.front = p;
adamc@136 1567 return result;
adamc@106 1568 }
adamc@106 1569
adamc@1011 1570 char *uw_Basis_attrifyChar(uw_context ctx, uw_Basis_char c) {
adamc@1011 1571 char *result, *p;
adamc@1011 1572 uw_check_heap(ctx, 7);
adamc@1011 1573
adamc@1011 1574 result = p = ctx->heap.front;
adamc@1011 1575
adamc@1011 1576 if (c == '"') {
adamc@1011 1577 strcpy(p, "&quot;");
adamc@1011 1578 p += 6;
adamc@1011 1579 } else if (c == '&') {
adamc@1011 1580 strcpy(p, "&amp;");
adamc@1011 1581 p += 5;
adamc@1011 1582 }
adamc@1059 1583 else
adamc@1011 1584 *p++ = c;
adamc@1011 1585
adamc@1011 1586 *p++ = 0;
adamc@1011 1587 ctx->heap.front = p;
adamc@1011 1588 return result;
adamc@1011 1589 }
adamc@1011 1590
adamc@721 1591 char *uw_Basis_attrifyCss_class(uw_context ctx, uw_Basis_css_class s) {
adamc@721 1592 return s;
adamc@721 1593 }
adamc@721 1594
adamc@311 1595 static void uw_Basis_attrifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
adamc@117 1596 int len;
adamc@117 1597
adamc@666 1598 sprintf(ctx->page.front, "%lld%n", n, &len);
adamc@666 1599 ctx->page.front += len;
adamc@106 1600 }
adamc@106 1601
adamc@428 1602 uw_unit uw_Basis_attrifyInt_w(uw_context ctx, uw_Basis_int n) {
adamc@311 1603 uw_check(ctx, INTS_MAX);
adamc@311 1604 uw_Basis_attrifyInt_w_unsafe(ctx, n);
adamc@428 1605
adamc@428 1606 return uw_unit_v;
adamc@106 1607 }
adamc@106 1608
adamc@428 1609 uw_unit uw_Basis_attrifyFloat_w(uw_context ctx, uw_Basis_float n) {
adamc@117 1610 int len;
adamc@117 1611
adamc@311 1612 uw_check(ctx, FLOATS_MAX);
adamc@666 1613 sprintf(ctx->page.front, "%g%n", n, &len);
adamc@666 1614 ctx->page.front += len;
adamc@428 1615
adamc@428 1616 return uw_unit_v;
adamc@117 1617 }
adamc@117 1618
adamc@428 1619 uw_unit uw_Basis_attrifyString_w(uw_context ctx, uw_Basis_string s) {
adamc@311 1620 uw_check(ctx, strlen(s) * 6);
adamc@117 1621
adamc@106 1622 for (; *s; s++) {
adamc@1054 1623 unsigned char c = *s;
adamc@106 1624
adamc@106 1625 if (c == '"')
adamc@311 1626 uw_write_unsafe(ctx, "&quot;");
adamc@136 1627 else if (c == '&')
adamc@311 1628 uw_write_unsafe(ctx, "&amp;");
adamc@1059 1629 else
adamc@311 1630 uw_writec_unsafe(ctx, c);
adamc@106 1631 }
adamc@428 1632
adamc@428 1633 return uw_unit_v;
adamc@106 1634 }
adamc@120 1635
adamc@1011 1636 uw_unit uw_Basis_attrifyChar_w(uw_context ctx, uw_Basis_char c) {
adamc@1011 1637 uw_check(ctx, 6);
adamc@1011 1638
adamc@1011 1639 if (c == '"')
adamc@1011 1640 uw_write_unsafe(ctx, "&quot;");
adamc@1011 1641 else if (c == '&')
adamc@1011 1642 uw_write_unsafe(ctx, "&amp;");
adamc@1059 1643 else
adamc@1011 1644 uw_writec_unsafe(ctx, c);
adamc@1011 1645
adamc@1011 1646 return uw_unit_v;
adamc@1011 1647 }
adamc@1011 1648
adamc@120 1649
adamc@311 1650 char *uw_Basis_urlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@137 1651 int len;
adamc@137 1652 char *r;
adamc@137 1653
adamc@311 1654 uw_check_heap(ctx, INTS_MAX);
adamc@666 1655 r = ctx->heap.front;
adamc@276 1656 sprintf(r, "%lld%n", n, &len);
adamc@666 1657 ctx->heap.front += len+1;
adamc@137 1658 return r;
adamc@120 1659 }
adamc@120 1660
adamc@682 1661 char *uw_Basis_urlifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 1662 if (ctx->client == NULL || chn.cli != ctx->client->id)
adamc@682 1663 return "";
adamc@682 1664 else {
adamc@682 1665 int len;
adamc@682 1666 char *r;
adamc@679 1667
adamc@682 1668 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 1669 r = ctx->heap.front;
adamc@682 1670 sprintf(r, "%u%n", chn.chn, &len);
adamc@682 1671 ctx->heap.front += len+1;
adamc@682 1672 return r;
adamc@682 1673 }
adamc@679 1674 }
adamc@679 1675
adamc@311 1676 char *uw_Basis_urlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@137 1677 int len;
adamc@137 1678 char *r;
adamc@137 1679
adamc@311 1680 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 1681 r = ctx->heap.front;
adamc@137 1682 sprintf(r, "%g%n", n, &len);
adamc@666 1683 ctx->heap.front += len+1;
adamc@137 1684 return r;
adamc@120 1685 }
adamc@120 1686
adamc@311 1687 char *uw_Basis_urlifyString(uw_context ctx, uw_Basis_string s) {
adamc@137 1688 char *r, *p;
adamc@137 1689
adamc@925 1690 if (s[0] == '\0')
adamc@925 1691 return "_";
adamc@925 1692
adamc@925 1693 uw_check_heap(ctx, strlen(s) * 3 + 1 + !!(s[0] == '_'));
adamc@925 1694
adamc@925 1695 r = p = ctx->heap.front;
adamc@925 1696 if (s[0] == '_')
adamc@925 1697 *p++ = '_';
adamc@925 1698
adamc@925 1699 for (; *s; s++) {
adamc@1087 1700 unsigned char c = *s;
adamc@137 1701
adamc@137 1702 if (c == ' ')
adamc@137 1703 *p++ = '+';
adamc@137 1704 else if (isalnum(c))
adamc@137 1705 *p++ = c;
adamc@137 1706 else {
adamc@1259 1707 sprintf(p, ".%02X", c);
adamc@137 1708 p += 3;
adamc@137 1709 }
adamc@137 1710 }
adamc@137 1711
adamc@137 1712 *p++ = 0;
adamc@666 1713 ctx->heap.front = p;
adamc@137 1714 return r;
adamc@120 1715 }
adamc@120 1716
adamc@311 1717 char *uw_Basis_urlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 1718 if (b == uw_Basis_False)
adamc@186 1719 return "0";
adamc@186 1720 else
adamc@186 1721 return "1";
adamc@186 1722 }
adamc@186 1723
adamc@311 1724 static void uw_Basis_urlifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
adamc@120 1725 int len;
adamc@120 1726
adamc@666 1727 sprintf(ctx->page.front, "%lld%n", n, &len);
adamc@666 1728 ctx->page.front += len;
adamc@120 1729 }
adamc@120 1730
adamc@428 1731 uw_unit uw_Basis_urlifyInt_w(uw_context ctx, uw_Basis_int n) {
adamc@311 1732 uw_check(ctx, INTS_MAX);
adamc@311 1733 uw_Basis_urlifyInt_w_unsafe(ctx, n);
adamc@428 1734
adamc@428 1735 return uw_unit_v;
adamc@120 1736 }
adamc@120 1737
adamc@682 1738 uw_unit uw_Basis_urlifyChannel_w(uw_context ctx, uw_Basis_channel chn) {
adamc@682 1739 if (ctx->client != NULL && chn.cli == ctx->client->id) {
adamc@682 1740 int len;
adamc@682 1741
adamc@682 1742 uw_check(ctx, INTS_MAX + 1);
adamc@682 1743 sprintf(ctx->page.front, "%u%n", chn.chn, &len);
adamc@682 1744 ctx->page.front += len;
adamc@682 1745 }
adamc@682 1746
adamc@679 1747 return uw_unit_v;
adamc@679 1748 }
adamc@679 1749
adamc@428 1750 uw_unit uw_Basis_urlifyFloat_w(uw_context ctx, uw_Basis_float n) {
adamc@120 1751 int len;
adamc@120 1752
adamc@311 1753 uw_check(ctx, FLOATS_MAX);
adamc@666 1754 sprintf(ctx->page.front, "%g%n", n, &len);
adamc@666 1755 ctx->page.front += len;
adamc@428 1756
adamc@428 1757 return uw_unit_v;
adamc@120 1758 }
adamc@120 1759
adamc@488 1760 uw_Basis_string uw_Basis_urlifyTime(uw_context ctx, uw_Basis_time t) {
adamc@1376 1761 return uw_Basis_urlifyInt(ctx, (uw_Basis_int)t.seconds * 1000000 + t.microseconds);
adamc@488 1762 }
adamc@488 1763
adam@1404 1764 uw_unit uw_Basis_urlifyTime_w(uw_context ctx, uw_Basis_time t) {
adam@1404 1765 return uw_Basis_urlifyInt_w(ctx, (uw_Basis_int)t.seconds * 1000000 + t.microseconds);
adam@1404 1766 }
adam@1404 1767
adamc@428 1768 uw_unit uw_Basis_urlifyString_w(uw_context ctx, uw_Basis_string s) {
adamc@925 1769 if (s[0] == '\0') {
adamc@925 1770 uw_check(ctx, 1);
adamc@925 1771 uw_writec_unsafe(ctx, '_');
adamc@925 1772 return uw_unit_v;
adamc@925 1773 }
adamc@925 1774
adamc@925 1775 uw_check(ctx, strlen(s) * 3 + !!(s[0] == '_'));
adamc@925 1776
adamc@925 1777 if (s[0] == '_')
adamc@925 1778 uw_writec_unsafe(ctx, '_');
adamc@120 1779
adamc@120 1780 for (; *s; s++) {
adamc@1087 1781 unsigned char c = *s;
adamc@120 1782
adamc@120 1783 if (c == ' ')
adamc@311 1784 uw_writec_unsafe(ctx, '+');
adamc@120 1785 else if (isalnum(c))
adamc@311 1786 uw_writec_unsafe(ctx, c);
adamc@120 1787 else {
adamc@1259 1788 sprintf(ctx->page.front, ".%02X", c);
adamc@666 1789 ctx->page.front += 3;
adamc@120 1790 }
adamc@120 1791 }
adamc@428 1792
adamc@428 1793 return uw_unit_v;
adamc@120 1794 }
adamc@120 1795
adamc@428 1796 uw_unit uw_Basis_urlifyBool_w(uw_context ctx, uw_Basis_bool b) {
adamc@311 1797 if (b == uw_Basis_False)
adamc@311 1798 uw_writec(ctx, '0');
adamc@186 1799 else
adamc@311 1800 uw_writec(ctx, '1');
adamc@428 1801
adamc@428 1802 return uw_unit_v;
adamc@186 1803 }
adamc@186 1804
adamc@120 1805
adamc@311 1806 static char *uw_unurlify_advance(char *s) {
adamc@144 1807 char *new_s = strchr(s, '/');
adamc@120 1808
adamc@120 1809 if (new_s)
adamc@120 1810 *new_s++ = 0;
adamc@120 1811 else
adamc@144 1812 new_s = strchr(s, 0);
adamc@144 1813
adamc@144 1814 return new_s;
adamc@144 1815 }
adamc@144 1816
adamc@311 1817 uw_Basis_int uw_Basis_unurlifyInt(uw_context ctx, char **s) {
adamc@311 1818 char *new_s = uw_unurlify_advance(*s);
adamc@311 1819 uw_Basis_int r;
adamc@120 1820
adamc@276 1821 r = atoll(*s);
adamc@120 1822 *s = new_s;
adamc@120 1823 return r;
adamc@120 1824 }
adamc@120 1825
adamc@311 1826 uw_Basis_float uw_Basis_unurlifyFloat(uw_context ctx, char **s) {
adamc@311 1827 char *new_s = uw_unurlify_advance(*s);
adamc@311 1828 uw_Basis_float r;
adamc@120 1829
adamc@120 1830 r = atof(*s);
adamc@120 1831 *s = new_s;
adamc@120 1832 return r;
adamc@120 1833 }
adamc@120 1834
adamc@488 1835 uw_Basis_time uw_Basis_unurlifyTime(uw_context ctx, char **s) {
adam@1365 1836 uw_Basis_int n = uw_Basis_unurlifyInt(ctx, s);
adam@1365 1837 uw_Basis_time r = {n / 1000000, n % 1000000};
adam@1365 1838 return r;
adamc@488 1839 }
adamc@488 1840
adamc@1023 1841 static uw_Basis_string uw_unurlifyString_to(int fromClient, uw_context ctx, char *r, char *s) {
adamc@927 1842 char *s1, *s2 = s;
adamc@144 1843 int n;
adamc@136 1844
adamc@1023 1845 if (!fromClient) {
adamc@1023 1846 if (*s2 == '_')
adamc@1023 1847 ++s2;
adamc@1259 1848 else if ((s2[0] == '%' || s2[0] == '.') && s2[1] == '5' && (s2[2] == 'f' || s2[2] == 'F'))
adamc@1023 1849 s2 += 3;
adamc@1023 1850 }
adamc@925 1851
adamc@927 1852 for (s1 = r; *s2; ++s1, ++s2) {
adamc@1087 1853 unsigned char c = *s2;
adamc@136 1854
adamc@136 1855 switch (c) {
adamc@136 1856 case '+':
adamc@136 1857 *s1 = ' ';
adamc@136 1858 break;
adamc@136 1859 case '%':
adamc@169 1860 if (s2[1] == 0)
adamc@311 1861 uw_error(ctx, FATAL, "Missing first character of escaped URL byte");
adamc@169 1862 if (s2[2] == 0)
adamc@311 1863 uw_error(ctx, FATAL, "Missing second character of escaped URL byte");
adamc@169 1864 if (sscanf(s2+1, "%02X", &n) != 1)
adamc@311 1865 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", s2);
adamc@136 1866 *s1 = n;
adamc@136 1867 s2 += 2;
adamc@136 1868 break;
adamc@1259 1869 case '.':
adamc@1259 1870 if (!fromClient) {
adamc@1259 1871 if (s2[1] == 0)
adamc@1259 1872 uw_error(ctx, FATAL, "Missing first character of escaped URL byte");
adamc@1259 1873 if (s2[2] == 0)
adamc@1259 1874 uw_error(ctx, FATAL, "Missing second character of escaped URL byte");
adamc@1259 1875 if (sscanf(s2+1, "%02X", &n) != 1)
adamc@1259 1876 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", s2);
adamc@1259 1877 *s1 = n;
adamc@1259 1878 s2 += 2;
adamc@1259 1879 break;
adamc@1259 1880 }
adamc@136 1881 default:
adamc@136 1882 *s1 = c;
adamc@136 1883 }
adamc@136 1884 }
adamc@136 1885 *s1++ = 0;
adamc@144 1886 return s1;
adamc@144 1887 }
adamc@144 1888
adamc@311 1889 uw_Basis_bool uw_Basis_unurlifyBool(uw_context ctx, char **s) {
adamc@311 1890 char *new_s = uw_unurlify_advance(*s);
adamc@311 1891 uw_Basis_bool r;
adamc@186 1892
adamc@186 1893 if (*s[0] == 0 || !strcmp(*s, "0") || !strcmp(*s, "off"))
adamc@311 1894 r = uw_Basis_False;
adamc@186 1895 else
adamc@311 1896 r = uw_Basis_True;
adamc@186 1897
adamc@186 1898 *s = new_s;
adamc@186 1899 return r;
adamc@186 1900 }
adamc@186 1901
adamc@311 1902 uw_Basis_string uw_Basis_unurlifyString(uw_context ctx, char **s) {
adamc@311 1903 char *new_s = uw_unurlify_advance(*s);
adamc@1134 1904 char *r;
adamc@1134 1905 int len;
adamc@144 1906
adamc@200 1907 len = strlen(*s);
adamc@311 1908 uw_check_heap(ctx, len + 1);
adamc@144 1909
adamc@666 1910 r = ctx->heap.front;
adamc@1023 1911 ctx->heap.front = uw_unurlifyString_to(0, ctx, ctx->heap.front, *s);
adamc@1023 1912 *s = new_s;
adamc@1023 1913 return r;
adamc@1023 1914 }
adamc@1023 1915
adamc@1109 1916 uw_Basis_unit uw_Basis_unurlifyUnit(uw_context ctx, char **s) {
adamc@1109 1917 *s = uw_unurlify_advance(*s);
adamc@1109 1918 return uw_unit_v;
adamc@1109 1919 }
adamc@1109 1920
adamc@1023 1921 uw_Basis_string uw_Basis_unurlifyString_fromClient(uw_context ctx, char **s) {
adamc@1023 1922 char *new_s = uw_unurlify_advance(*s);
adamc@1134 1923 char *r;
adamc@1134 1924 int len;
adamc@1023 1925
adamc@1023 1926 len = strlen(*s);
adamc@1023 1927 uw_check_heap(ctx, len + 1);
adamc@1023 1928
adamc@1023 1929 r = ctx->heap.front;
adamc@1023 1930 ctx->heap.front = uw_unurlifyString_to(1, ctx, ctx->heap.front, *s);
adamc@136 1931 *s = new_s;
adamc@136 1932 return r;
adamc@120 1933 }
adamc@135 1934
adamc@135 1935
adamc@311 1936 char *uw_Basis_htmlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@286 1937 int len;
adamc@286 1938 char *r;
adamc@286 1939
adamc@311 1940 uw_check_heap(ctx, INTS_MAX);
adamc@666 1941 r = ctx->heap.front;
adamc@286 1942 sprintf(r, "%lld%n", n, &len);
adamc@666 1943 ctx->heap.front += len+1;
adamc@286 1944 return r;
adamc@286 1945 }
adamc@286 1946
adamc@428 1947 uw_unit uw_Basis_htmlifyInt_w(uw_context ctx, uw_Basis_int n) {
adamc@286 1948 int len;
adamc@286 1949
adamc@311 1950 uw_check(ctx, INTS_MAX);
adamc@666 1951 sprintf(ctx->page.front, "%lld%n", n, &len);
adamc@666 1952 ctx->page.front += len;
adamc@428 1953
adamc@428 1954 return uw_unit_v;
adamc@286 1955 }
adamc@286 1956
adam@1358 1957 char *uw_Basis_htmlifySpecialChar(uw_context ctx, unsigned char ch) {
adam@1358 1958 unsigned int n = ch;
adam@1358 1959 int len;
adam@1358 1960 char *r;
adam@1358 1961
adam@1358 1962 uw_check_heap(ctx, INTS_MAX+3);
adam@1358 1963 r = ctx->heap.front;
adam@1358 1964 sprintf(r, "&#%u;%n", n, &len);
adam@1358 1965 ctx->heap.front += len+1;
adam@1358 1966 return r;
adam@1358 1967 }
adam@1358 1968
adam@1358 1969 uw_unit uw_Basis_htmlifySpecialChar_w(uw_context ctx, unsigned char ch) {
adam@1358 1970 unsigned int n = ch;
adam@1358 1971 int len;
adam@1358 1972
adam@1358 1973 uw_check(ctx, INTS_MAX+3);
adam@1358 1974 sprintf(ctx->page.front, "&#%u;%n", n, &len);
adam@1358 1975 ctx->page.front += len;
adam@1358 1976 return uw_unit_v;
adam@1358 1977 }
adam@1358 1978
adamc@311 1979 char *uw_Basis_htmlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@286 1980 int len;
adamc@286 1981 char *r;
adamc@286 1982
adamc@311 1983 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 1984 r = ctx->heap.front;
adamc@286 1985 sprintf(r, "%g%n", n, &len);
adamc@666 1986 ctx->heap.front += len+1;
adamc@286 1987 return r;
adamc@286 1988 }
adamc@286 1989
adamc@428 1990 uw_unit uw_Basis_htmlifyFloat_w(uw_context ctx, uw_Basis_float n) {
adamc@286 1991 int len;
adamc@286 1992
adamc@311 1993 uw_check(ctx, FLOATS_MAX);
adamc@666 1994 sprintf(ctx->page.front, "%g%n", n, &len);
adamc@666 1995 ctx->page.front += len;
adamc@428 1996
adamc@428 1997 return uw_unit_v;
adamc@286 1998 }
adamc@286 1999
adam@1365 2000 char *uw_Basis_jsifyTime(uw_context ctx, uw_Basis_time t) {
adam@1360 2001 int len;
adam@1360 2002 char *r;
adam@1360 2003
adam@1360 2004 uw_check_heap(ctx, INTS_MAX);
adam@1360 2005 r = ctx->heap.front;
adamc@1377 2006 sprintf(r, "%lld%n", (uw_Basis_int)t.seconds * 1000000 + t.microseconds, &len);
adam@1360 2007 ctx->heap.front += len+1;
adam@1360 2008 return r;
adam@1360 2009 }
adam@1360 2010
adam@1365 2011 uw_unit uw_Basis_jsifyInt_w(uw_context ctx, uw_Basis_int n) {
adam@1360 2012 int len;
adam@1360 2013
adam@1360 2014 uw_check(ctx, INTS_MAX);
adam@1360 2015 sprintf(ctx->page.front, "%lld%n", (uw_Basis_int)n, &len);
adam@1360 2016 ctx->page.front += len;
adam@1360 2017
adam@1360 2018 return uw_unit_v;
adam@1360 2019 }
adam@1360 2020
adamc@311 2021 char *uw_Basis_htmlifyString(uw_context ctx, uw_Basis_string s) {
adamc@137 2022 char *r, *s2;
adamc@137 2023
adamc@311 2024 uw_check_heap(ctx, strlen(s) * 5 + 1);
adamc@137 2025
adamc@666 2026 for (r = s2 = ctx->heap.front; *s; s++) {
adamc@1054 2027 unsigned char c = *s;
adamc@137 2028
adamc@137 2029 switch (c) {
adamc@137 2030 case '<':
adamc@137 2031 strcpy(s2, "&lt;");
adamc@137 2032 s2 += 4;
adamc@137 2033 break;
adamc@137 2034 case '&':
adamc@137 2035 strcpy(s2, "&amp;");
adamc@137 2036 s2 += 5;
adamc@137 2037 break;
adamc@137 2038 default:
adamc@1059 2039 *s2++ = c;
adamc@137 2040 }
adamc@137 2041 }
adamc@137 2042
adamc@137 2043 *s2++ = 0;
adamc@666 2044 ctx->heap.front = s2;
adamc@137 2045 return r;
adamc@135 2046 }
adamc@135 2047
adamc@428 2048 uw_unit uw_Basis_htmlifyString_w(uw_context ctx, uw_Basis_string s) {
adamc@321 2049 uw_check(ctx, strlen(s) * 6);
adamc@135 2050
adamc@135 2051 for (; *s; s++) {
adamc@1054 2052 unsigned char c = *s;
adamc@135 2053
adamc@135 2054 switch (c) {
adamc@135 2055 case '<':
adamc@311 2056 uw_write_unsafe(ctx, "&lt;");
adamc@135 2057 break;
adamc@135 2058 case '&':
adamc@311 2059 uw_write_unsafe(ctx, "&amp;");
adamc@135 2060 break;
adamc@135 2061 default:
adamc@1059 2062 uw_writec_unsafe(ctx, c);
adamc@135 2063 }
adamc@135 2064 }
adamc@428 2065
adamc@428 2066 return uw_unit_v;
adamc@135 2067 }
adamc@180 2068
adamc@311 2069 uw_Basis_string uw_Basis_htmlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 2070 if (b == uw_Basis_False)
adamc@286 2071 return "False";
adamc@286 2072 else
adamc@286 2073 return "True";
adamc@286 2074 }
adamc@286 2075
adamc@428 2076 uw_unit uw_Basis_htmlifyBool_w(uw_context ctx, uw_Basis_bool b) {
adamc@311 2077 if (b == uw_Basis_False) {
adamc@311 2078 uw_check(ctx, 6);
adamc@666 2079 strcpy(ctx->page.front, "False");
adamc@666 2080 ctx->page.front += 5;
adamc@286 2081 } else {
adamc@311 2082 uw_check(ctx, 5);
adamc@666 2083 strcpy(ctx->page.front, "True");
adamc@666 2084 ctx->page.front += 4;
adamc@286 2085 }
adamc@428 2086
adamc@428 2087 return uw_unit_v;
adamc@286 2088 }
adamc@286 2089
adamc@436 2090 #define TIME_FMT "%x %X"
adamc@438 2091 #define TIME_FMT_PG "%Y-%m-%d %T"
adamc@436 2092
adamc@436 2093 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) {
adamc@436 2094 size_t len;
adamc@436 2095 char *r;
adam@1392 2096 struct tm stm = {};
adam@1392 2097 stm.tm_isdst = -1;
adamc@436 2098
adam@1365 2099 if (localtime_r(&t.seconds, &stm)) {
adamc@436 2100 uw_check_heap(ctx, TIMES_MAX);
adamc@666 2101 r = ctx->heap.front;
adamc@436 2102 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 2103 ctx->heap.front += len+1;
adamc@436 2104 return r;
adamc@436 2105 } else
adamc@436 2106 return "<i>Invalid time</i>";
adamc@436 2107 }
adamc@436 2108
adamc@436 2109 uw_unit uw_Basis_htmlifyTime_w(uw_context ctx, uw_Basis_time t) {
adamc@436 2110 size_t len;
adamc@436 2111 char *r;
adam@1392 2112 struct tm stm = {};
adam@1392 2113 stm.tm_isdst = -1;
adamc@436 2114
adam@1365 2115 if (localtime_r(&t.seconds, &stm)) {
adamc@436 2116 uw_check(ctx, TIMES_MAX);
adamc@666 2117 r = ctx->page.front;
adamc@436 2118 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 2119 ctx->page.front += len;
adamc@436 2120 } else {
adamc@436 2121 uw_check(ctx, 20);
adamc@666 2122 strcpy(ctx->page.front, "<i>Invalid time</i>");
adamc@666 2123 ctx->page.front += 19;
adamc@436 2124 }
adamc@436 2125
adamc@436 2126 return uw_unit_v;
adamc@436 2127 }
adamc@436 2128
adamc@821 2129 uw_Basis_char uw_Basis_strsub(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
adam@1388 2130 while (n >= 0) {
adam@1388 2131 if (*s == 0)
adam@1388 2132 uw_error(ctx, FATAL, "Out-of-bounds strsub");
adam@1388 2133
adam@1388 2134 if (n == 0)
adam@1388 2135 return *s;
adam@1388 2136
adam@1388 2137 --n;
adam@1388 2138 ++s;
adam@1388 2139 }
adam@1388 2140
adam@1388 2141 uw_error(ctx, FATAL, "Negative strsub bound");
adamc@821 2142 }
adamc@821 2143
adamc@821 2144 uw_Basis_string uw_Basis_strsuffix(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
adam@1388 2145 while (n >= 0) {
adam@1388 2146 if (*s == 0 || n == 0)
adam@1388 2147 return s;
adam@1388 2148
adam@1388 2149 --n;
adam@1388 2150 ++s;
adam@1388 2151 }
adam@1388 2152
adam@1388 2153 uw_error(ctx, FATAL, "Negative strsuffix bound");
adamc@821 2154 }
adamc@821 2155
adamc@828 2156 uw_Basis_int uw_Basis_strlen(uw_context ctx, uw_Basis_string s) {
adamc@828 2157 return strlen(s);
adamc@828 2158 }
adamc@828 2159
adam@1388 2160 uw_Basis_bool uw_Basis_strlenGe(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
adam@1388 2161 while (n > 0) {
adam@1388 2162 if (*s == 0)
adam@1388 2163 return uw_Basis_False;
adam@1388 2164
adam@1388 2165 --n;
adam@1388 2166 ++s;
adam@1388 2167 }
adam@1388 2168
adam@1388 2169 return uw_Basis_True;
adam@1388 2170 }
adam@1388 2171
adamc@829 2172 uw_Basis_string uw_Basis_strchr(uw_context ctx, uw_Basis_string s, uw_Basis_char ch) {
adamc@829 2173 return strchr(s, ch);
adamc@829 2174 }
adamc@829 2175
adamc@831 2176 uw_Basis_int uw_Basis_strcspn(uw_context ctx, uw_Basis_string s, uw_Basis_string chs) {
adamc@831 2177 return strcspn(s, chs);
adamc@831 2178 }
adamc@831 2179
adamc@829 2180 uw_Basis_int *uw_Basis_strindex(uw_context ctx, uw_Basis_string s, uw_Basis_char ch) {
adamc@829 2181 uw_Basis_string r = strchr(s, ch);
adamc@829 2182 if (r == NULL)
adamc@829 2183 return NULL;
adamc@829 2184 else {
adamc@829 2185 uw_Basis_int *nr = uw_malloc(ctx, sizeof(uw_Basis_int));
adamc@829 2186 *nr = r - s;
adamc@829 2187 return nr;
adamc@829 2188 }
adamc@829 2189 }
adamc@829 2190
adam@1390 2191 uw_Basis_int *uw_Basis_strsindex(uw_context ctx, const char *haystack, const char *needle) {
adam@1390 2192 uw_Basis_string r = strstr(haystack, needle);
adam@1390 2193 if (r == NULL)
adam@1390 2194 return NULL;
adam@1390 2195 else {
adam@1390 2196 uw_Basis_int *nr = uw_malloc(ctx, sizeof(uw_Basis_int));
adam@1390 2197 *nr = r - haystack;
adam@1390 2198 return nr;
adam@1390 2199 }
adam@1390 2200 }
adam@1390 2201
adamc@311 2202 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) {
adamc@828 2203 int len = uw_Basis_strlen(ctx, s1) + uw_Basis_strlen(ctx, s2) + 1;
adamc@180 2204 char *s;
adamc@180 2205
adamc@311 2206 uw_check_heap(ctx, len);
adamc@180 2207
adamc@666 2208 s = ctx->heap.front;
adamc@180 2209
adamc@180 2210 strcpy(s, s1);
adamc@180 2211 strcat(s, s2);
adamc@666 2212 ctx->heap.front += len;
adamc@180 2213
adamc@180 2214 return s;
adamc@180 2215 }
adamc@278 2216
adamc@829 2217 uw_Basis_string uw_Basis_substring(uw_context ctx, uw_Basis_string s, uw_Basis_int start, uw_Basis_int len) {
adamc@829 2218 size_t full_len = uw_Basis_strlen(ctx, s);
adamc@829 2219
adamc@829 2220 if (start < 0)
adamc@829 2221 uw_error(ctx, FATAL, "substring: Negative start index");
adamc@829 2222 if (len < 0)
adamc@829 2223 uw_error(ctx, FATAL, "substring: Negative length");
adamc@829 2224 if (start + len > full_len)
adamc@829 2225 uw_error(ctx, FATAL, "substring: Start index plus length is too large");
adamc@829 2226
adamc@829 2227 if (start + len == full_len)
adamc@829 2228 return &s[start];
adamc@829 2229 else {
adamc@829 2230 uw_Basis_string r = uw_malloc(ctx, len+1);
adamc@959 2231 memcpy(r, s+start, len);
adamc@829 2232 r[len] = 0;
adamc@829 2233 return r;
adamc@829 2234 }
adamc@829 2235
adamc@829 2236 }
adamc@829 2237
adamc@1023 2238 uw_Basis_string uw_Basis_str1(uw_context ctx, uw_Basis_char ch) {
adamc@1023 2239 char *r;
adamc@1023 2240
adamc@1023 2241 uw_check_heap(ctx, 2);
adamc@1023 2242 r = ctx->heap.front;
adamc@1023 2243 r[0] = ch;
adamc@1023 2244 r[1] = 0;
adamc@1023 2245
adamc@1023 2246 ctx->heap.front += 2;
adamc@1023 2247
adamc@1023 2248 return r;
adamc@1023 2249 }
adamc@1023 2250
adamc@737 2251 uw_Basis_string uw_strdup(uw_context ctx, uw_Basis_string s1) {
adamc@828 2252 int len = uw_Basis_strlen(ctx, s1) + 1;
adamc@278 2253 char *s;
adamc@278 2254
adamc@311 2255 uw_check_heap(ctx, len);
adamc@278 2256
adamc@666 2257 s = ctx->heap.front;
adamc@278 2258
adamc@278 2259 strcpy(s, s1);
adamc@666 2260 ctx->heap.front += len;
adamc@278 2261
adamc@278 2262 return s;
adamc@278 2263 }
adamc@280 2264
adam@1295 2265 uw_Basis_string uw_dup_and_clear_error_message(uw_context ctx) {
adam@1295 2266 if (ctx->error_message[0]) {
adam@1295 2267 char *s = uw_strdup(ctx, ctx->error_message);
adam@1295 2268 ctx->error_message[0] = 0;
adam@1295 2269 return s;
adam@1295 2270 } else
adam@1295 2271 return NULL;
adam@1295 2272 }
adam@1295 2273
adamc@737 2274 uw_Basis_string uw_maybe_strdup(uw_context ctx, uw_Basis_string s1) {
adamc@493 2275 if (s1)
adamc@737 2276 return uw_strdup(ctx, s1);
adamc@493 2277 else
adamc@493 2278 return NULL;
adamc@493 2279 }
adamc@493 2280
adamc@742 2281 char *uw_memdup(uw_context ctx, const char *p, size_t len) {
adamc@742 2282 char *r = uw_malloc(ctx, len);
adamc@742 2283 memcpy(r, p, len);
adamc@742 2284 return r;
adamc@742 2285 }
adamc@280 2286
adamc@879 2287 char *uw_sqlfmtInt = "%lld::int8%n";
adamc@879 2288
adamc@311 2289 char *uw_Basis_sqlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@281 2290 int len;
adamc@281 2291 char *r;
adamc@281 2292
adamc@311 2293 uw_check_heap(ctx, INTS_MAX + 6);
adamc@666 2294 r = ctx->heap.front;
adamc@879 2295 sprintf(r, uw_sqlfmtInt, n, &len);
adamc@666 2296 ctx->heap.front += len+1;
adamc@281 2297 return r;
adamc@281 2298 }
adamc@281 2299
adamc@467 2300 char *uw_Basis_sqlifyIntN(uw_context ctx, uw_Basis_int *n) {
adamc@467 2301 if (n == NULL)
adamc@467 2302 return "NULL";
adamc@467 2303 else
adamc@467 2304 return uw_Basis_sqlifyInt(ctx, *n);
adamc@467 2305 }
adamc@467 2306
adamc@879 2307 char *uw_sqlfmtFloat = "%g::float8%n";
adamc@879 2308
adamc@311 2309 char *uw_Basis_sqlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@281 2310 int len;
adamc@281 2311 char *r;
adamc@281 2312
adamc@311 2313 uw_check_heap(ctx, FLOATS_MAX + 8);
adamc@666 2314 r = ctx->heap.front;
adamc@879 2315 sprintf(r, uw_sqlfmtFloat, n, &len);
adamc@666 2316 ctx->heap.front += len+1;
adamc@281 2317 return r;
adamc@281 2318 }
adamc@281 2319
adamc@467 2320 char *uw_Basis_sqlifyFloatN(uw_context ctx, uw_Basis_float *n) {
adamc@467 2321 if (n == NULL)
adamc@467 2322 return "NULL";
adamc@467 2323 else
adamc@467 2324 return uw_Basis_sqlifyFloat(ctx, *n);
adamc@467 2325 }
adamc@467 2326
adamc@879 2327 int uw_Estrings = 1;
adamc@879 2328 char *uw_sqlsuffixString = "::text";
adamc@1011 2329 char *uw_sqlsuffixChar = "::char";
adamc@281 2330
adamc@311 2331 uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) {
adamc@280 2332 char *r, *s2;
adamc@280 2333
adamc@879 2334 uw_check_heap(ctx, strlen(s) * 2 + 3 + uw_Estrings + strlen(uw_sqlsuffixString));
adamc@280 2335
adamc@666 2336 r = s2 = ctx->heap.front;
adamc@879 2337 if (uw_Estrings)
adamc@879 2338 *s2++ = 'E';
adamc@280 2339 *s2++ = '\'';
adamc@280 2340
adamc@280 2341 for (; *s; s++) {
adamc@280 2342 char c = *s;
adamc@280 2343
adamc@280 2344 switch (c) {
adamc@280 2345 case '\'':
adamc@885 2346 if (uw_Estrings)
adamc@885 2347 strcpy(s2, "\\'");
adamc@885 2348 else
adamc@885 2349 strcpy(s2, "''");
adamc@280 2350 s2 += 2;
adamc@280 2351 break;
adamc@280 2352 case '\\':
adamc@885 2353 if (uw_Estrings) {
adamc@885 2354 strcpy(s2, "\\\\");
adamc@885 2355 s2 += 2;
adamc@885 2356 } else
adamc@885 2357 *s2++ = '\\';
adamc@280 2358 break;
adamc@280 2359 default:
adamc@1150 2360 if (isprint((int)c))
adamc@280 2361 *s2++ = c;
adamc@879 2362 else if (uw_Estrings) {
adamc@737 2363 sprintf(s2, "\\%03o", c);
adamc@280 2364 s2 += 4;
adamc@280 2365 }
adamc@879 2366 else
adamc@879 2367 uw_error(ctx, FATAL, "Non-printable character %u in string to SQLify", c);
adamc@280 2368 }
adamc@280 2369 }
adamc@280 2370
adamc@879 2371 *s2++ = '\'';
adamc@879 2372 strcpy(s2, uw_sqlsuffixString);
adamc@879 2373 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixString);
adamc@280 2374 return r;
adamc@280 2375 }
adamc@281 2376
adamc@1011 2377 uw_Basis_string uw_Basis_sqlifyChar(uw_context ctx, uw_Basis_char c) {
adamc@1011 2378 char *r, *s2;
adamc@1011 2379
adamc@1011 2380 uw_check_heap(ctx, 5 + uw_Estrings + strlen(uw_sqlsuffixChar));
adamc@1011 2381
adamc@1011 2382 r = s2 = ctx->heap.front;
adamc@1011 2383 if (uw_Estrings)
adamc@1011 2384 *s2++ = 'E';
adamc@1011 2385 *s2++ = '\'';
adamc@1011 2386
adamc@1011 2387 switch (c) {
adamc@1011 2388 case '\'':
adamc@1011 2389 if (uw_Estrings)
adamc@1011 2390 strcpy(s2, "\\'");
adamc@1011 2391 else
adamc@1011 2392 strcpy(s2, "''");
adamc@1011 2393 s2 += 2;
adamc@1011 2394 break;
adamc@1011 2395 case '\\':
adamc@1011 2396 if (uw_Estrings) {
adamc@1011 2397 strcpy(s2, "\\\\");
adamc@1011 2398 s2 += 2;
adamc@1011 2399 } else
adamc@1011 2400 *s2++ = '\\';
adamc@1011 2401 break;
adamc@1011 2402 default:
adamc@1150 2403 if (isprint((int)c))
adamc@1011 2404 *s2++ = c;
adamc@1011 2405 else if (uw_Estrings) {
adamc@1011 2406 sprintf(s2, "\\%03o", c);
adamc@1011 2407 s2 += 4;
adamc@1011 2408 }
adamc@1011 2409 else
adamc@1011 2410 uw_error(ctx, FATAL, "Non-printable character %u in char to SQLify", c);
adamc@1011 2411 }
adamc@1011 2412
adamc@1011 2413 *s2++ = '\'';
adamc@1011 2414 strcpy(s2, uw_sqlsuffixChar);
adamc@1011 2415 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixChar);
adamc@1011 2416 return r;
adamc@1011 2417 }
adamc@1011 2418
adamc@879 2419 char *uw_sqlsuffixBlob = "::bytea";
adamc@879 2420
adamc@737 2421 uw_Basis_string uw_Basis_sqlifyBlob(uw_context ctx, uw_Basis_blob b) {
adamc@737 2422 char *r, *s2;
adamc@737 2423 size_t i;
adamc@737 2424
adamc@879 2425 uw_check_heap(ctx, b.size * 5 + 3 + uw_Estrings + strlen(uw_sqlsuffixBlob));
adamc@737 2426
adamc@737 2427 r = s2 = ctx->heap.front;
adamc@879 2428 if (uw_Estrings)
adamc@879 2429 *s2++ = 'E';
adamc@737 2430 *s2++ = '\'';
adamc@737 2431
adamc@737 2432 for (i = 0; i < b.size; ++i) {
adamc@737 2433 char c = b.data[i];
adamc@737 2434
adamc@737 2435 switch (c) {
adamc@737 2436 case '\'':
adamc@885 2437 if (uw_Estrings)
adamc@885 2438 strcpy(s2, "\\'");
adamc@885 2439 else
adamc@885 2440 strcpy(s2, "''");
adamc@737 2441 s2 += 2;
adamc@737 2442 break;
adamc@737 2443 case '\\':
adamc@885 2444 if (uw_Estrings) {
adamc@885 2445 strcpy(s2, "\\\\\\\\");
adamc@885 2446 s2 += 4;
adamc@885 2447 } else
adamc@885 2448 *s2++ = '\\';
adamc@737 2449 break;
adamc@737 2450 default:
adamc@1150 2451 if (isprint((int)c))
adamc@737 2452 *s2++ = c;
adamc@879 2453 else if (uw_Estrings) {
adamc@737 2454 sprintf(s2, "\\\\%03o", c);
adamc@737 2455 s2 += 5;
adamc@737 2456 }
adamc@879 2457 else
adamc@879 2458 uw_error(ctx, FATAL, "Non-printable character %u in blob to SQLify", c);
adamc@737 2459 }
adamc@737 2460 }
adamc@737 2461
adamc@879 2462 *s2++ = '\'';
adamc@879 2463 strcpy(s2, uw_sqlsuffixBlob);
adamc@879 2464 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixBlob);
adamc@737 2465 return r;
adamc@737 2466 }
adamc@737 2467
adamc@682 2468 char *uw_Basis_sqlifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 2469 int len;
adamc@682 2470 char *r;
adamc@682 2471 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
adamc@682 2472
adamc@682 2473 uw_check_heap(ctx, INTS_MAX + 7);
adamc@682 2474 r = ctx->heap.front;
adamc@879 2475 sprintf(r, uw_sqlfmtInt, combo, &len);
adamc@682 2476 ctx->heap.front += len+1;
adamc@682 2477 return r;
adamc@682 2478 }
adamc@682 2479
adamc@682 2480 char *uw_Basis_attrifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 2481 int len;
adamc@682 2482 char *r;
adamc@682 2483 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
adamc@682 2484
adamc@682 2485 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 2486 r = ctx->heap.front;
adamc@682 2487 sprintf(r, "%lld%n", combo, &len);
adamc@682 2488 ctx->heap.front += len+1;
adamc@682 2489 return r;
adamc@682 2490 }
adamc@682 2491
adamc@879 2492 char *uw_sqlfmtUint4 = "%u::int4%n";
adamc@879 2493
adamc@682 2494 char *uw_Basis_sqlifyClient(uw_context ctx, uw_Basis_client cli) {
adamc@682 2495 int len;
adamc@682 2496 char *r;
adamc@682 2497
adamc@682 2498 uw_check_heap(ctx, INTS_MAX + 7);
adamc@682 2499 r = ctx->heap.front;
adamc@879 2500 sprintf(r, uw_sqlfmtUint4, cli, &len);
adamc@682 2501 ctx->heap.front += len+1;
adamc@682 2502 return r;
adamc@682 2503 }
adamc@682 2504
adamc@682 2505 char *uw_Basis_attrifyClient(uw_context ctx, uw_Basis_client cli) {
adamc@682 2506 int len;
adamc@682 2507 char *r;
adamc@682 2508
adamc@682 2509 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 2510 r = ctx->heap.front;
adamc@682 2511 sprintf(r, "%u%n", cli, &len);
adamc@682 2512 ctx->heap.front += len+1;
adamc@682 2513 return r;
adamc@682 2514 }
adamc@682 2515
adamc@467 2516 uw_Basis_string uw_Basis_sqlifyStringN(uw_context ctx, uw_Basis_string s) {
adamc@467 2517 if (s == NULL)
adamc@467 2518 return "NULL";
adamc@467 2519 else
adamc@467 2520 return uw_Basis_sqlifyString(ctx, s);
adamc@467 2521 }
adamc@467 2522
adamc@311 2523 char *uw_Basis_sqlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 2524 if (b == uw_Basis_False)
adamc@281 2525 return "FALSE";
adamc@281 2526 else
adamc@281 2527 return "TRUE";
adamc@281 2528 }
adamc@282 2529
adamc@467 2530 char *uw_Basis_sqlifyBoolN(uw_context ctx, uw_Basis_bool *b) {
adamc@467 2531 if (b == NULL)
adamc@467 2532 return "NULL";
adamc@467 2533 else
adamc@467 2534 return uw_Basis_sqlifyBool(ctx, *b);
adamc@467 2535 }
adamc@467 2536
adamc@439 2537 char *uw_Basis_sqlifyTime(uw_context ctx, uw_Basis_time t) {
adamc@439 2538 size_t len;
adamc@682 2539 char *r, *s;
adam@1392 2540 struct tm stm = {};
adam@1392 2541 stm.tm_isdst = -1;
adamc@682 2542
adam@1365 2543 if (localtime_r(&t.seconds, &stm)) {
adamc@682 2544 s = uw_malloc(ctx, TIMES_MAX);
adam@1364 2545 len = strftime(s, TIMES_MAX, TIME_FMT_PG, &stm);
adam@1367 2546 if (t.microseconds) {
adam@1367 2547 r = uw_malloc(ctx, len + 21);
adam@1367 2548 sprintf(r, "'%s.%06u'::timestamp", s, t.microseconds);
adam@1367 2549 } else {
adam@1367 2550 r = uw_malloc(ctx, len + 14);
adam@1367 2551 sprintf(r, "'%s'::timestamp", s);
adam@1367 2552 }
adamc@682 2553 return r;
adamc@682 2554 } else
adamc@682 2555 return "<Invalid time>";
adamc@682 2556 }
adamc@682 2557
adamc@682 2558 char *uw_Basis_attrifyTime(uw_context ctx, uw_Basis_time t) {
adamc@682 2559 size_t len;
adamc@439 2560 char *r;
adam@1392 2561 struct tm stm = {};
adam@1392 2562 stm.tm_isdst = -1;
adamc@439 2563
adam@1365 2564 if (localtime_r(&t.seconds, &stm)) {
adamc@439 2565 uw_check_heap(ctx, TIMES_MAX);
adamc@666 2566 r = ctx->heap.front;
adamc@439 2567 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 2568 ctx->heap.front += len+1;
adamc@439 2569 return r;
adamc@439 2570 } else
adamc@439 2571 return "<Invalid time>";
adamc@439 2572 }
adamc@439 2573
adam@1364 2574 char *uw_Basis_ensqlTime(uw_context ctx, uw_Basis_time t) {
adam@1364 2575 size_t len;
adam@1364 2576 char *r;
adam@1392 2577 struct tm stm = {};
adam@1392 2578 stm.tm_isdst = -1;
adam@1364 2579
adam@1365 2580 if (localtime_r(&t.seconds, &stm)) {
adam@1364 2581 uw_check_heap(ctx, TIMES_MAX);
adam@1364 2582 r = ctx->heap.front;
adam@1365 2583 len = strftime(r, TIMES_MAX-7, TIME_FMT_PG, &stm);
adam@1365 2584 ctx->heap.front += len;
adam@1365 2585 sprintf(ctx->heap.front, ".%06u", t.microseconds);
adam@1365 2586 ctx->heap.front += 8;
adam@1364 2587 return r;
adam@1364 2588 } else
adam@1364 2589 return "<Invalid time>";
adam@1364 2590 }
adam@1364 2591
adamc@467 2592 char *uw_Basis_sqlifyTimeN(uw_context ctx, uw_Basis_time *t) {
adamc@467 2593 if (t == NULL)
adamc@467 2594 return "NULL";
adamc@467 2595 else
adamc@467 2596 return uw_Basis_sqlifyTime(ctx, *t);
adamc@467 2597 }
adamc@467 2598
adamc@311 2599 char *uw_Basis_ensqlBool(uw_Basis_bool b) {
adamc@311 2600 static uw_Basis_int true = 1;
adamc@311 2601 static uw_Basis_int false = 0;
adamc@282 2602
adamc@311 2603 if (b == uw_Basis_False)
adamc@282 2604 return (char *)&false;
adamc@282 2605 else
adamc@282 2606 return (char *)&true;
adamc@282 2607 }
adamc@284 2608
adamc@311 2609 uw_Basis_string uw_Basis_intToString(uw_context ctx, uw_Basis_int n) {
adamc@284 2610 int len;
adamc@284 2611 char *r;
adamc@284 2612
adamc@311 2613 uw_check_heap(ctx, INTS_MAX);
adamc@666 2614 r = ctx->heap.front;
adamc@284 2615 sprintf(r, "%lld%n", n, &len);
adamc@666 2616 ctx->heap.front += len+1;
adamc@284 2617 return r;
adamc@284 2618 }
adamc@285 2619
adamc@311 2620 uw_Basis_string uw_Basis_floatToString(uw_context ctx, uw_Basis_float n) {
adamc@285 2621 int len;
adamc@285 2622 char *r;
adamc@285 2623
adamc@311 2624 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 2625 r = ctx->heap.front;
adamc@285 2626 sprintf(r, "%g%n", n, &len);
adamc@666 2627 ctx->heap.front += len+1;
adamc@285 2628 return r;
adamc@285 2629 }
adamc@285 2630
adamc@821 2631 uw_Basis_string uw_Basis_charToString(uw_context ctx, uw_Basis_char ch) {
adamc@821 2632 char *r = uw_malloc(ctx, 2);
adamc@821 2633 r[0] = ch;
adamc@821 2634 r[1] = 0;
adamc@821 2635 return r;
adamc@821 2636 }
adamc@821 2637
adamc@311 2638 uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) {
adamc@311 2639 if (b == uw_Basis_False)
adamc@285 2640 return "False";
adamc@285 2641 else
adamc@285 2642 return "True";
adamc@285 2643 }
adamc@288 2644
adamc@436 2645 uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) {
adamc@436 2646 size_t len;
adamc@436 2647 char *r;
adam@1392 2648 struct tm stm = {};
adam@1392 2649 stm.tm_isdst = -1;
adamc@436 2650
adam@1365 2651 if (localtime_r(&t.seconds, &stm)) {
adamc@436 2652 uw_check_heap(ctx, TIMES_MAX);
adamc@666 2653 r = ctx->heap.front;
adamc@436 2654 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 2655 ctx->heap.front += len+1;
adamc@436 2656 return r;
adamc@436 2657 } else
adamc@436 2658 return "<Invalid time>";
adamc@436 2659 }
adamc@288 2660
adam@1359 2661 uw_Basis_string uw_Basis_timef(uw_context ctx, const char *fmt, uw_Basis_time t) {
adam@1352 2662 size_t len;
adam@1352 2663 char *r;
adam@1392 2664 struct tm stm = {};
adam@1392 2665 stm.tm_isdst = -1;
adam@1352 2666
adam@1365 2667 if (localtime_r(&t.seconds, &stm)) {
adam@1352 2668 uw_check_heap(ctx, TIMES_MAX);
adam@1352 2669 r = ctx->heap.front;
adam@1352 2670 len = strftime(r, TIMES_MAX, fmt, &stm);
adam@1352 2671 ctx->heap.front += len+1;
adam@1352 2672 return r;
adam@1352 2673 } else
adam@1352 2674 return "<Invalid time>";
adam@1352 2675 }
adam@1352 2676
adamc@311 2677 uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) {
adamc@288 2678 char *endptr;
adamc@311 2679 uw_Basis_int n = strtoll(s, &endptr, 10);
adamc@288 2680
adamc@288 2681 if (*s != '\0' && *endptr == '\0') {
adamc@311 2682 uw_Basis_int *r = uw_malloc(ctx, sizeof(uw_Basis_int));
adamc@288 2683 *r = n;
adamc@288 2684 return r;
adamc@288 2685 } else
adamc@288 2686 return NULL;
adamc@288 2687 }
adamc@289 2688
adamc@311 2689 uw_Basis_float *uw_Basis_stringToFloat(uw_context ctx, uw_Basis_string s) {
adamc@289 2690 char *endptr;
adamc@311 2691 uw_Basis_float n = strtod(s, &endptr);
adamc@289 2692
adamc@289 2693 if (*s != '\0' && *endptr == '\0') {
adamc@311 2694 uw_Basis_float *r = uw_malloc(ctx, sizeof(uw_Basis_float));
adamc@289 2695 *r = n;
adamc@289 2696 return r;
adamc@289 2697 } else
adamc@289 2698 return NULL;
adamc@289 2699 }
adamc@289 2700
adamc@821 2701 uw_Basis_char *uw_Basis_stringToChar(uw_context ctx, uw_Basis_string s) {
adamc@821 2702 if (s[0] == 0) {
adamc@821 2703 uw_Basis_char *r = uw_malloc(ctx, 1);
adamc@821 2704 r[0] = 0;
adamc@821 2705 return r;
adamc@821 2706 } else if (s[1] != 0)
adamc@821 2707 return NULL;
adamc@821 2708 else {
adamc@821 2709 uw_Basis_char *r = uw_malloc(ctx, 1);
adamc@821 2710 r[0] = s[0];
adamc@821 2711 return r;
adamc@821 2712 }
adamc@821 2713 }
adamc@821 2714
adamc@311 2715 uw_Basis_bool *uw_Basis_stringToBool(uw_context ctx, uw_Basis_string s) {
adamc@311 2716 static uw_Basis_bool true = uw_Basis_True;
adamc@311 2717 static uw_Basis_bool false = uw_Basis_False;
adamc@289 2718
adamc@289 2719 if (!strcasecmp (s, "True"))
adamc@289 2720 return &true;
adamc@289 2721 else if (!strcasecmp (s, "False"))
adamc@289 2722 return &false;
adamc@289 2723 else
adamc@289 2724 return NULL;
adamc@289 2725 }
adamc@292 2726
adamc@436 2727 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) {
adamc@438 2728 char *dot = strchr(s, '.'), *end = strchr(s, 0);
adam@1374 2729 struct tm stm = {};
adam@1392 2730 stm.tm_isdst = -1;
adamc@436 2731
adamc@439 2732 if (dot) {
adamc@439 2733 *dot = 0;
adamc@439 2734 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adamc@439 2735 *dot = '.';
adamc@439 2736 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adam@1365 2737 r->seconds = mktime(&stm);
adam@1365 2738 r->microseconds = 0;
adamc@439 2739 return r;
adamc@439 2740 }
adamc@439 2741 else {
adamc@439 2742 *dot = '.';
adamc@439 2743 return NULL;
adamc@439 2744 }
adamc@436 2745 }
adamc@439 2746 else {
adamc@439 2747 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adamc@439 2748 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adam@1365 2749 r->seconds = mktime(&stm);
adam@1365 2750 r->microseconds = 0;
adamc@439 2751 return r;
adamc@439 2752 }
adamc@439 2753 else if (strptime(s, TIME_FMT, &stm) == end) {
adamc@439 2754 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adam@1365 2755 r->seconds = mktime(&stm);
adam@1365 2756 r->microseconds = 0;
adamc@439 2757 return r;
adamc@439 2758 }
adamc@439 2759 else
adamc@439 2760 return NULL;
adamc@439 2761 }
adamc@436 2762 }
adamc@436 2763
adam@1352 2764 uw_Basis_time *uw_Basis_stringToTimef(uw_context ctx, const char *fmt, uw_Basis_string s) {
adam@1352 2765 char *end = strchr(s, 0);
adam@1374 2766 struct tm stm = {};
adam@1392 2767 stm.tm_isdst = -1;
adam@1352 2768
adam@1352 2769 if (strptime(s, fmt, &stm) == end) {
adam@1352 2770 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adam@1365 2771 r->seconds = mktime(&stm);
adam@1365 2772 r->microseconds = 0;
adam@1352 2773 return r;
adam@1352 2774 }
adam@1352 2775 else
adam@1352 2776 return NULL;
adam@1352 2777 }
adam@1352 2778
adamc@311 2779 uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) {
adamc@292 2780 char *endptr;
adamc@311 2781 uw_Basis_int n = strtoll(s, &endptr, 10);
adamc@292 2782
adamc@292 2783 if (*s != '\0' && *endptr == '\0')
adamc@292 2784 return n;
adamc@292 2785 else
adamc@311 2786 uw_error(ctx, FATAL, "Can't parse int: %s", s);
adamc@292 2787 }
adamc@293 2788
adamc@682 2789 #include <errno.h>
adamc@682 2790
adamc@678 2791 uw_Basis_channel uw_Basis_stringToChannel_error(uw_context ctx, uw_Basis_string s) {
adamc@682 2792 unsigned long long n;
adamc@682 2793
adamc@682 2794 if (sscanf(s, "%llu", &n) < 1)
adamc@682 2795 uw_error(ctx, FATAL, "Can't parse channel: %s", s);
adamc@682 2796 else {
adamc@682 2797 uw_Basis_channel ch = {n >> 32, n & ((1ull << 32) - 1)};
adamc@682 2798 return ch;
adamc@682 2799 }
adamc@682 2800 }
adamc@682 2801
adamc@682 2802 uw_Basis_client uw_Basis_stringToClient_error(uw_context ctx, uw_Basis_string s) {
adamc@678 2803 char *endptr;
adamc@682 2804 unsigned long n = strtoul(s, &endptr, 10);
adamc@678 2805
adamc@678 2806 if (*s != '\0' && *endptr == '\0')
adamc@678 2807 return n;
adamc@678 2808 else
adamc@682 2809 uw_error(ctx, FATAL, "Can't parse client: %s", s);
adamc@678 2810 }
adamc@678 2811
adamc@311 2812 uw_Basis_float uw_Basis_stringToFloat_error(uw_context ctx, uw_Basis_string s) {
adamc@293 2813 char *endptr;
adamc@311 2814 uw_Basis_float n = strtod(s, &endptr);
adamc@293 2815
adamc@293 2816 if (*s != '\0' && *endptr == '\0')
adamc@293 2817 return n;
adamc@293 2818 else
adamc@311 2819 uw_error(ctx, FATAL, "Can't parse float: %s", s);
adamc@293 2820 }
adamc@293 2821
adamc@821 2822 uw_Basis_char uw_Basis_stringToChar_error(uw_context ctx, uw_Basis_string s) {
adamc@821 2823 if (s[0] == 0)
adamc@821 2824 return 0;
adamc@821 2825 else if (s[1] != 0)
adamc@821 2826 uw_error(ctx, FATAL, "Can't parse char: %s", s);
adamc@821 2827 else
adamc@821 2828 return s[0];
adamc@821 2829 }
adamc@821 2830
adamc@311 2831 uw_Basis_bool uw_Basis_stringToBool_error(uw_context ctx, uw_Basis_string s) {
adamc@296 2832 if (!strcasecmp(s, "T") || !strcasecmp (s, "True"))
adamc@311 2833 return uw_Basis_True;
adamc@296 2834 else if (!strcasecmp(s, "F") || !strcasecmp (s, "False"))
adamc@311 2835 return uw_Basis_False;
adamc@293 2836 else
adamc@311 2837 uw_error(ctx, FATAL, "Can't parse bool: %s", s);
adamc@293 2838 }
adamc@436 2839
adam@1352 2840 uw_Basis_time uw_Basis_unsqlTime(uw_context ctx, uw_Basis_string s) {
adam@1352 2841 char *dot = strchr(s, '.'), *end = strchr(s, 0);
adam@1352 2842 struct tm stm = {};
adam@1392 2843 stm.tm_isdst = -1;
adam@1352 2844
adam@1352 2845 if (dot) {
adam@1352 2846 *dot = 0;
adam@1352 2847 if (strptime(s, TIME_FMT_PG, &stm)) {
adam@1352 2848 *dot = '.';
adam@1410 2849 char usec[] = "000000";
adam@1410 2850 int len = strlen(dot+1);
adam@1410 2851 memcpy(usec, dot+1, len < 6 ? len : 6);
adam@1410 2852 uw_Basis_time r = { mktime(&stm), atoi(usec) };
adam@1365 2853 return r;
adam@1352 2854 }
adam@1352 2855 else {
adam@1352 2856 *dot = '.';
adam@1352 2857 uw_error(ctx, FATAL, "Can't parse time: %s", s);
adam@1352 2858 }
adam@1352 2859 }
adam@1352 2860 else {
adam@1352 2861 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adam@1365 2862 uw_Basis_time r = { mktime(&stm) };
adam@1365 2863 return r;
adam@1352 2864 } else if (strptime(s, TIME_FMT, &stm) == end) {
adam@1365 2865 uw_Basis_time r = { mktime(&stm) };
adam@1365 2866 return r;
adam@1352 2867 } else
adam@1352 2868 uw_error(ctx, FATAL, "Can't parse time: %s", s);
adam@1352 2869 }
adam@1352 2870 }
adam@1352 2871
adamc@436 2872 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) {
adamc@438 2873 char *dot = strchr(s, '.'), *end = strchr(s, 0);
adamc@436 2874 struct tm stm = {};
adam@1392 2875 stm.tm_isdst = -1;
adamc@436 2876
adamc@438 2877 if (dot) {
adamc@438 2878 *dot = 0;
adamc@438 2879 if (strptime(s, TIME_FMT_PG, &stm)) {
adamc@438 2880 *dot = '.';
adam@1365 2881 {
adam@1365 2882 uw_Basis_time r = { mktime(&stm) };
adam@1365 2883 return r;
adam@1365 2884 }
adamc@438 2885 }
adamc@438 2886 else {
adamc@438 2887 *dot = '.';
adamc@438 2888 uw_error(ctx, FATAL, "Can't parse time: %s", s);
adamc@438 2889 }
adamc@438 2890 }
adamc@438 2891 else {
adam@1365 2892 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adam@1365 2893 uw_Basis_time r = { mktime(&stm) };
adam@1365 2894 return r;
adam@1365 2895 } else if (strptime(s, TIME_FMT, &stm) == end) {
adam@1365 2896 uw_Basis_time r = { mktime(&stm) };
adam@1365 2897 return r;
adam@1365 2898 } else
adamc@438 2899 uw_error(ctx, FATAL, "Can't parse time: %s", s);
adamc@438 2900 }
adamc@436 2901 }
adamc@457 2902
adam@1352 2903 uw_Basis_time uw_Basis_stringToTimef_error(uw_context ctx, const char *fmt, uw_Basis_string s) {
adam@1352 2904 char *end = strchr(s, 0);
adamc@939 2905 struct tm stm = {};
adam@1392 2906 stm.tm_isdst = -1;
adamc@939 2907
adam@1365 2908 if (strptime(s, fmt, &stm) == end) {
adam@1365 2909 uw_Basis_time r = { mktime(&stm) };
adam@1365 2910 return r;
adam@1365 2911 } else
adam@1352 2912 uw_error(ctx, FATAL, "Can't parse time: %s", s);
adamc@939 2913 }
adamc@939 2914
adamc@742 2915 uw_Basis_blob uw_Basis_stringToBlob_error(uw_context ctx, uw_Basis_string s, size_t len) {
adamc@742 2916 char *r = ctx->heap.front;
adamc@742 2917 uw_Basis_blob b = {len, r};
adamc@742 2918
adamc@742 2919 uw_check_heap(ctx, len);
adamc@742 2920
adamc@742 2921 while (*s) {
adamc@742 2922 if (s[0] == '\\') {
adamc@742 2923 if (s[1] == '\\') {
adamc@742 2924 *r++ = '\\';
adamc@742 2925 s += 2;
adamc@1153 2926 } else if (isdigit((int)s[1]) && isdigit((int)s[2]) && isdigit((int)s[3])) {
adamc@742 2927 *r++ = (s[1] - '0') * 8 * 8 + ((s[2] - '0') * 8) + (s[3] - '0');
adamc@742 2928 s += 4;
adamc@742 2929 }
adamc@742 2930 else {
adamc@742 2931 *r++ = '\\';
adamc@742 2932 ++s;
adamc@742 2933 }
adamc@742 2934 } else {
adamc@742 2935 *r++ = s[0];
adamc@742 2936 ++s;
adamc@742 2937 }
adamc@742 2938 }
adamc@742 2939
adamc@742 2940 b.size = r - ctx->heap.front;
adamc@742 2941 ctx->heap.front = r;
adamc@742 2942 return b;
adamc@742 2943 }
adamc@742 2944
adam@1402 2945 #define THE_PAST "expires=Sat, 01-Jan-2011 00:00:00 GMT"
adamc@1052 2946
adamc@463 2947 uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) {
adamc@463 2948 int len = strlen(c);
adamc@854 2949 char *p = ctx->outHeaders.start;
adamc@474 2950
adamc@1134 2951 while ((p = strstr(p, "\nSet-Cookie: "))) {
adamc@474 2952 char *p2;
adamc@474 2953 p += 13;
adamc@474 2954 p2 = strchr(p, '=');
adamc@474 2955
adamc@474 2956 if (p2) {
adamc@474 2957 size_t sz = strcspn(p2+1, ";\r\n");
adamc@474 2958
adamc@474 2959 if (!strncasecmp(p, c, p2 - p)) {
adamc@1052 2960 if (sz == 0 && strstr(p2+2, THE_PAST))
adamc@1052 2961 return NULL;
adamc@1052 2962 else {
adamc@1052 2963 char *ret = uw_malloc(ctx, sz + 1);
adamc@1052 2964 memcpy(ret, p2+1, sz);
adamc@1052 2965 ret[sz] = 0;
adamc@1052 2966 return ret;
adamc@1052 2967 }
adamc@474 2968 }
adamc@474 2969 }
adamc@474 2970 }
adamc@463 2971
adamc@1134 2972 if ((p = uw_Basis_requestHeader(ctx, "Cookie"))) {
adamc@1051 2973 char *p2;
adamc@1051 2974
adamc@854 2975 while (1) {
adamc@1051 2976 if (!strncmp(p, c, len) && p[len] == '=') {
adamc@1134 2977 if ((p2 = strchr(p, ';'))) {
adamc@1051 2978 size_t n = p2 - (p + len);
adamc@1051 2979 char *r = uw_malloc(ctx, n);
adamc@1051 2980 memcpy(r, p + 1 + len, n-1);
adamc@1051 2981 r[n-1] = 0;
adamc@1051 2982 return r;
adamc@1051 2983 } else
adamc@1051 2984 return p + 1 + len;
adamc@1134 2985 } else if ((p = strchr(p, ';')))
adamc@854 2986 p += 2;
adamc@463 2987 else
adamc@463 2988 return NULL;
adamc@463 2989 }
adamc@463 2990 }
adamc@792 2991
adamc@792 2992 return NULL;
adamc@457 2993 }
adamc@462 2994
adamc@1050 2995 uw_unit uw_Basis_set_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c, uw_Basis_string v, uw_Basis_time *expires, uw_Basis_bool secure) {
adamc@462 2996 uw_write_header(ctx, "Set-Cookie: ");
adamc@462 2997 uw_write_header(ctx, c);
adamc@462 2998 uw_write_header(ctx, "=");
adamc@462 2999 uw_write_header(ctx, v);
adamc@466 3000 uw_write_header(ctx, "; path=");
adamc@466 3001 uw_write_header(ctx, prefix);
adamc@1050 3002 if (expires) {
adamc@1050 3003 char formatted[30];
adam@1392 3004 struct tm tm = {};
adam@1392 3005 tm.tm_isdst = -1;
adamc@1050 3006
adam@1365 3007 gmtime_r(&expires->seconds, &tm);
adamc@1050 3008
adamc@1050 3009 strftime(formatted, sizeof formatted, "%a, %d-%b-%Y %T GMT", &tm);
adamc@1050 3010
adamc@1050 3011 uw_write_header(ctx, "; expires=");
adamc@1050 3012 uw_write_header(ctx, formatted);
adamc@1050 3013 }
adamc@1050 3014 if (secure)
adamc@1050 3015 uw_write_header(ctx, "; secure");
adamc@462 3016 uw_write_header(ctx, "\r\n");
adamc@462 3017
adamc@462 3018 return uw_unit_v;
adamc@462 3019 }
adamc@667 3020
adamc@1050 3021 uw_unit uw_Basis_clear_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c) {
adamc@1050 3022 uw_write_header(ctx, "Set-Cookie: ");
adamc@1050 3023 uw_write_header(ctx, c);
adamc@1050 3024 uw_write_header(ctx, "=; path=");
adamc@1050 3025 uw_write_header(ctx, prefix);
adamc@1052 3026 uw_write_header(ctx, "; " THE_PAST "\r\n");
adamc@1050 3027
adamc@1050 3028 return uw_unit_v;
adamc@1050 3029 }
adamc@1050 3030
adamc@1113 3031 size_t uw_deltas_max = SIZE_MAX;
adamc@1113 3032
adamc@682 3033 static delta *allocate_delta(uw_context ctx, unsigned client) {
adamc@682 3034 unsigned i;
adamc@682 3035 delta *d;
adamc@671 3036
adamc@682 3037 for (i = 0; i < ctx->used_deltas; ++i)
adamc@682 3038 if (ctx->deltas[i].client == client)
adamc@682 3039 return &ctx->deltas[i];
adamc@671 3040
adamc@682 3041 if (ctx->used_deltas >= ctx->n_deltas) {
adamc@1113 3042 if (ctx->n_deltas + 1 > uw_deltas_max)
adamc@1113 3043 uw_error(ctx, FATAL, "Exceeded limit on number of deltas");
adamc@1113 3044
adamc@682 3045 ctx->deltas = realloc(ctx->deltas, sizeof(delta) * ++ctx->n_deltas);
adam@1369 3046 uw_buffer_init(uw_messages_max, &ctx->deltas[ctx->n_deltas-1].msgs, 0);
adamc@682 3047 }
adamc@671 3048
adamc@682 3049 d = &ctx->deltas[ctx->used_deltas++];
adamc@682 3050 d->client = client;
adam@1369 3051 uw_buffer_reset(&d->msgs);
adamc@682 3052 return d;
adamc@671 3053 }
adamc@671 3054
adamc@668 3055 uw_Basis_channel uw_Basis_new_channel(uw_context ctx, uw_unit u) {
adamc@682 3056 if (ctx->client == NULL)
adamc@682 3057 uw_error(ctx, FATAL, "Attempt to create channel on request not associated with a persistent connection");
adamc@671 3058
adamc@682 3059 return new_channel(ctx->client);
adamc@668 3060 }
adamc@668 3061
adamc@668 3062 uw_unit uw_Basis_send(uw_context ctx, uw_Basis_channel chn, uw_Basis_string msg) {
adamc@682 3063 delta *d = allocate_delta(ctx, chn.cli);
adamc@682 3064 size_t len;
adamc@682 3065 int preLen;
adamc@682 3066 char pre[INTS_MAX + 2];
adamc@668 3067
adamc@682 3068 len = strlen(msg);
adamc@682 3069
adamc@682 3070 sprintf(pre, "%u\n%n", chn.chn, &preLen);
adamc@682 3071
adam@1369 3072 ctx_uw_buffer_append(ctx, "messages", &d->msgs, pre, preLen);
adam@1369 3073 ctx_uw_buffer_append(ctx, "messages", &d->msgs, msg, len);
adam@1369 3074 ctx_uw_buffer_append(ctx, "messages", &d->msgs, "\n", 1);
adamc@671 3075
adamc@671 3076 return uw_unit_v;
adamc@668 3077 }
adamc@671 3078
adam@1327 3079 int uw_rollback(uw_context ctx, int will_retry) {
adamc@1131 3080 int i;
adamc@1131 3081 cleanup *cl;
adamc@1131 3082
adamc@1131 3083 if (ctx->client)
adamc@1131 3084 release_client(ctx->client);
adamc@1131 3085
adamc@1131 3086 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@1131 3087 cl->func(cl->arg);
adamc@1131 3088
adamc@1131 3089 ctx->cleanup_front = ctx->cleanup;
adamc@1131 3090
adamc@1131 3091 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@1131 3092 if (ctx->transactionals[i].rollback != NULL)
adamc@1131 3093 ctx->transactionals[i].rollback(ctx->transactionals[i].data);
adamc@1131 3094
adamc@1131 3095 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@1131 3096 if (ctx->transactionals[i].free)
adam@1327 3097 ctx->transactionals[i].free(ctx->transactionals[i].data, will_retry);
adamc@1131 3098
adamc@1144 3099 return ctx->app ? ctx->app->db_rollback(ctx) : 0;
adamc@1131 3100 }
adamc@1131 3101
adamc@671 3102 void uw_commit(uw_context ctx) {
adamc@1104 3103 int i;
adamc@1104 3104
adamc@1131 3105 if (uw_has_error(ctx)) {
adam@1327 3106 uw_rollback(ctx, 0);
adamc@1131 3107 return;
adamc@1131 3108 }
adamc@1131 3109
adamc@1104 3110 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@771 3111 if (ctx->transactionals[i].rollback != NULL)
adamc@1131 3112 if (ctx->transactionals[i].commit) {
adamc@1083 3113 ctx->transactionals[i].commit(ctx->transactionals[i].data);
adamc@1131 3114 if (uw_has_error(ctx)) {
adam@1327 3115 uw_rollback(ctx, 0);
adamc@1131 3116 return;
adamc@1131 3117 }
adamc@1131 3118 }
adamc@771 3119
adamc@1104 3120 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@771 3121 if (ctx->transactionals[i].rollback == NULL)
adamc@1131 3122 if (ctx->transactionals[i].commit) {
adamc@1083 3123 ctx->transactionals[i].commit(ctx->transactionals[i].data);
adamc@1131 3124 if (uw_has_error(ctx)) {
adam@1327 3125 uw_rollback(ctx, 0);
adamc@1131 3126 return;
adamc@1131 3127 }
adamc@1131 3128 }
adamc@1131 3129
adamc@1131 3130 if (ctx->app->db_commit(ctx)) {
adamc@1131 3131 uw_set_error_message(ctx, "Error running SQL COMMIT");
adamc@1131 3132 return;
adamc@1131 3133 }
adamc@682 3134
adamc@682 3135 for (i = 0; i < ctx->used_deltas; ++i) {
adamc@682 3136 delta *d = &ctx->deltas[i];
adamc@682 3137 client *c = find_client(d->client);
adamc@682 3138
adamc@682 3139 assert (c != NULL && c->mode == USED);
adamc@682 3140
adamc@685 3141 client_send(c, &d->msgs);
adamc@682 3142 }
adamc@682 3143
adamc@682 3144 if (ctx->client)
adamc@685 3145 release_client(ctx->client);
adamc@771 3146
adamc@1104 3147 for (i = ctx->used_transactionals-1; i >= 0; --i)
adamc@1083 3148 if (ctx->transactionals[i].free)
adam@1327 3149 ctx->transactionals[i].free(ctx->transactionals[i].data, 0);
adamc@916 3150
adam@1413 3151 if (*ctx->page.front)
adam@1413 3152 uw_writec(ctx, 0);
adam@1413 3153
adamc@916 3154 // Splice script data into appropriate part of page
adamc@1065 3155 if (ctx->returning_indirectly || ctx->script_header[0] == 0) {
adamc@995 3156 char *start = strstr(ctx->page.start, "<sc>");
adamc@995 3157 if (start) {
adam@1369 3158 memmove(start, start + 4, uw_buffer_used(&ctx->page) - (start - ctx->page.start) - 4);
adamc@995 3159 ctx->page.front -= 4;
adamc@995 3160 }
adam@1369 3161 } else if (uw_buffer_used(&ctx->script) == 0) {
adamc@916 3162 size_t len = strlen(ctx->script_header);
adamc@916 3163 char *start = strstr(ctx->page.start, "<sc>");
adamc@916 3164 if (start) {
adam@1369 3165 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) - 4 + len);
adamc@917 3166 start = strstr(ctx->page.start, "<sc>");
adam@1369 3167 memmove(start + len, start + 4, uw_buffer_used(&ctx->page) - (start - ctx->page.start) - 3);
adamc@916 3168 ctx->page.front += len - 4;
adamc@916 3169 memcpy(start, ctx->script_header, len);
adamc@916 3170 }
adamc@916 3171 } else {
adam@1369 3172 size_t lenH = strlen(ctx->script_header), len = uw_buffer_used(&ctx->script);
adamc@916 3173 size_t lenP = lenH + 40 + len;
adamc@916 3174 char *start = strstr(ctx->page.start, "<sc>");
adamc@916 3175 if (start) {
adam@1369 3176 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) - 4 + lenP);
adamc@917 3177 start = strstr(ctx->page.start, "<sc>");
adam@1369 3178 memmove(start + lenP, start + 4, uw_buffer_used(&ctx->page) - (start - ctx->page.start) - 3);
adamc@916 3179 ctx->page.front += lenP - 4;
adamc@916 3180 memcpy(start, ctx->script_header, lenH);
adamc@916 3181 memcpy(start + lenH, "<script type=\"text/javascript\">", 31);
adamc@916 3182 memcpy(start + lenH + 31, ctx->script.start, len);
adamc@916 3183 memcpy(start + lenH + 31 + len, "</script>", 9);
adamc@916 3184 }
adamc@916 3185 }
adamc@671 3186 }
adamc@671 3187
adamc@683 3188
adamc@1113 3189 size_t uw_transactionals_max = SIZE_MAX;
adamc@1113 3190
adamc@771 3191 void uw_register_transactional(uw_context ctx, void *data, uw_callback commit, uw_callback rollback,
adam@1327 3192 uw_callback_with_retry free) {
adamc@771 3193 if (ctx->used_transactionals >= ctx->n_transactionals) {
adamc@1113 3194 if (ctx->used_transactionals+1 > uw_transactionals_max)
adamc@1113 3195 uw_error(ctx, FATAL, "Exceeded limit on number of transactionals");
adamc@1084 3196 ctx->transactionals = realloc(ctx->transactionals, sizeof(transactional) * (ctx->used_transactionals+1));
adamc@771 3197 ++ctx->n_transactionals;
adamc@771 3198 }
adamc@771 3199
adamc@771 3200 ctx->transactionals[ctx->used_transactionals].data = data;
adamc@771 3201 ctx->transactionals[ctx->used_transactionals].commit = commit;
adamc@771 3202 ctx->transactionals[ctx->used_transactionals].rollback = rollback;
adamc@771 3203 ctx->transactionals[ctx->used_transactionals++].free = free;
adamc@771 3204 }
adamc@771 3205
adamc@683 3206
adamc@683 3207 // "Garbage collection"
adamc@683 3208
adamc@1121 3209 void uw_do_expunge(uw_context ctx, uw_Basis_client cli, void *data);
adamc@1121 3210 void uw_post_expunge(uw_context ctx, void *data);
adamc@1121 3211
adamc@1121 3212 static failure_kind uw_expunge(uw_context ctx, uw_Basis_client cli, void *data) {
adamc@683 3213 int r = setjmp(ctx->jmp_buf);
adamc@683 3214
adamc@1121 3215 if (r == 0)
adamc@1121 3216 uw_do_expunge(ctx, cli, data);
adamc@1121 3217 else
adamc@1121 3218 ctx->app->db_rollback(ctx);
adamc@1121 3219
adamc@1121 3220 uw_post_expunge(ctx, data);
adamc@683 3221
adamc@683 3222 return r;
adamc@683 3223 }
adamc@683 3224
adamc@683 3225 void uw_prune_clients(uw_context ctx) {
adamc@683 3226 client *c, *next, *prev = NULL;
adamc@683 3227 time_t cutoff;
adamc@683 3228
adamc@1094 3229 cutoff = time(NULL) - ctx->app->timeout;
adamc@683 3230
adamc@683 3231 pthread_mutex_lock(&clients_mutex);
adamc@683 3232
adamc@683 3233 for (c = clients_used; c; c = next) {
adamc@683 3234 next = c->next;
adamc@683 3235 pthread_mutex_lock(&c->lock);
adamc@685 3236 if (c->last_contact < cutoff && c->refcount == 0) {
adamc@683 3237 failure_kind fk = UNLIMITED_RETRY;
adamc@683 3238 if (prev)
adamc@683 3239 prev->next = next;
adamc@683 3240 else
adamc@683 3241 clients_used = next;
adamc@684 3242 uw_reset(ctx);
adamc@683 3243 while (fk == UNLIMITED_RETRY) {
adamc@1121 3244 fk = uw_expunge(ctx, c->id, c->data);
adamc@1121 3245 if (fk == UNLIMITED_RETRY)
adamc@684 3246 printf("Unlimited retry during expunge: %s\n", uw_error_message(ctx));
adamc@683 3247 }
adamc@684 3248 if (fk == SUCCESS)
adamc@684 3249 free_client(c);
adamc@1121 3250 else
adamc@856 3251 fprintf(stderr, "Expunge blocked by error: %s\n", uw_error_message(ctx));
adamc@683 3252 }
adamc@683 3253 else
adamc@683 3254 prev = c;
adamc@683 3255 pthread_mutex_unlock(&c->lock);
adamc@683 3256 }
adamc@683 3257
adamc@683 3258 pthread_mutex_unlock(&clients_mutex);
adamc@683 3259 }
adamc@687 3260
adamc@687 3261 failure_kind uw_initialize(uw_context ctx) {
adamc@687 3262 int r = setjmp(ctx->jmp_buf);
adamc@687 3263
adamc@687 3264 if (r == 0) {
adamc@1094 3265 if (ctx->app->db_begin(ctx))
adamc@687 3266 uw_error(ctx, FATAL, "Error running SQL BEGIN");
adamc@1094 3267 ctx->app->initializer(ctx);
adamc@1094 3268 if (ctx->app->db_commit(ctx))
adamc@687 3269 uw_error(ctx, FATAL, "Error running SQL COMMIT");
adamc@687 3270 }
adamc@687 3271
adamc@687 3272 return r;
adamc@687 3273 }
adamc@717 3274
adamc@1065 3275 static int url_bad(uw_Basis_string s) {
adamc@1065 3276 for (; *s; ++s)
adamc@1153 3277 if (!isgraph((int)*s))
adamc@1065 3278 return 1;
adamc@1065 3279
adamc@1065 3280 return 0;
adamc@1065 3281 }
adamc@1065 3282
adamc@717 3283 uw_Basis_string uw_Basis_bless(uw_context ctx, uw_Basis_string s) {
adamc@1065 3284 if (url_bad(s))
adamc@1065 3285 uw_error(ctx, FATAL, "Invalid URL %s", uw_Basis_htmlifyString(ctx, s));
adamc@1094 3286 if (ctx->app->check_url(s))
adamc@770 3287 return s;
adamc@770 3288 else
adamc@770 3289 uw_error(ctx, FATAL, "Disallowed URL %s", uw_Basis_htmlifyString(ctx, s));
adamc@717 3290 }
adamc@734 3291
adamc@770 3292 uw_Basis_string uw_Basis_checkUrl(uw_context ctx, uw_Basis_string s) {
adamc@1065 3293 if (url_bad(s))
adamc@1065 3294 return NULL;
adamc@1094 3295 if (ctx->app->check_url(s))
adamc@770 3296 return s;
adamc@770 3297 else
adamc@770 3298 return NULL;
adamc@770 3299 }
adamc@770 3300
adamc@770 3301 int mime_format(const char *s) {
adamc@770 3302 for (; *s; ++s)
adamc@1153 3303 if (!isalnum((int)*s) && *s != '/' && *s != '-' && *s != '.')
adamc@770 3304 return 0;
adamc@770 3305
adamc@770 3306 return 1;
adamc@770 3307 }
adamc@770 3308
adamc@741 3309 uw_Basis_string uw_Basis_blessMime(uw_context ctx, uw_Basis_string s) {
adamc@770 3310 if (!mime_format(s))
adamc@770 3311 uw_error(ctx, FATAL, "MIME type \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s));
adamc@770 3312
adamc@1094 3313 if (ctx->app->check_mime(s))
adamc@770 3314 return s;
adamc@770 3315 else
adamc@770 3316 uw_error(ctx, FATAL, "Disallowed MIME type %s", uw_Basis_htmlifyString(ctx, s));
adamc@770 3317 }
adamc@770 3318
adamc@770 3319 uw_Basis_string uw_Basis_checkMime(uw_context ctx, uw_Basis_string s) {
adamc@770 3320 if (!mime_format(s))
adamc@770 3321 return NULL;
adamc@770 3322
adamc@1094 3323 if (ctx->app->check_mime(s))
adamc@770 3324 return s;
adamc@770 3325 else
adamc@770 3326 return NULL;
adamc@741 3327 }
adamc@741 3328
adamc@734 3329 uw_Basis_string uw_unnull(uw_Basis_string s) {
adamc@734 3330 return s ? s : "";
adamc@734 3331 }
adamc@734 3332
adamc@734 3333 extern int uw_hash_blocksize;
adamc@734 3334
adamc@734 3335 uw_Basis_string uw_Basis_makeSigString(uw_context ctx, uw_Basis_string sig) {
adamc@734 3336 uw_Basis_string r = uw_malloc(ctx, 2 * uw_hash_blocksize + 1);
adamc@734 3337 int i;
adamc@734 3338
adamc@734 3339 for (i = 0; i < uw_hash_blocksize; ++i)
adamc@734 3340 sprintf(&r[2*i], "%.02X", ((unsigned char *)sig)[i]);
adamc@734 3341
adamc@734 3342 return r;
adamc@734 3343 }
adamc@734 3344
adamc@734 3345 uw_Basis_string uw_Basis_sigString(uw_context ctx, uw_unit u) {
adamc@1094 3346 return ctx->app->cookie_sig(ctx);
adamc@734 3347 }
adamc@737 3348
adamc@737 3349 uw_Basis_string uw_Basis_fileName(uw_context ctx, uw_Basis_file f) {
adamc@737 3350 return f.name;
adamc@737 3351 }
adamc@737 3352
adamc@740 3353 uw_Basis_string uw_Basis_fileMimeType(uw_context ctx, uw_Basis_file f) {
adamc@740 3354 return f.type;
adamc@740 3355 }
adamc@740 3356
adamc@745 3357 uw_Basis_int uw_Basis_blobSize(uw_context ctx, uw_Basis_blob b) {
adamc@745 3358 return b.size;
adamc@745 3359 }
adamc@745 3360
adamc@1119 3361 uw_Basis_blob uw_Basis_textBlob(uw_context ctx, uw_Basis_string s) {
adamc@1119 3362 uw_Basis_blob b = {strlen(s), s};
adamc@1119 3363
adamc@1119 3364 return b;
adamc@1119 3365 }
adamc@1119 3366
adamc@737 3367 uw_Basis_blob uw_Basis_fileData(uw_context ctx, uw_Basis_file f) {
adamc@737 3368 return f.data;
adamc@737 3369 }
adamc@741 3370
adam@1347 3371 uw_Basis_string uw_Basis_postType(uw_context ctx, uw_Basis_postBody pb) {
adam@1347 3372 return pb.type;
adam@1347 3373 }
adam@1347 3374
adam@1347 3375 uw_Basis_string uw_Basis_postData(uw_context ctx, uw_Basis_postBody pb) {
adam@1347 3376 return pb.data;
adam@1347 3377 }
adam@1347 3378
adamc@741 3379 __attribute__((noreturn)) void uw_return_blob(uw_context ctx, uw_Basis_blob b, uw_Basis_string mimeType) {
adamc@741 3380 cleanup *cl;
adamc@741 3381 int len;
adamc@741 3382
adamc@1065 3383 ctx->returning_indirectly = 1;
adam@1369 3384 uw_buffer_reset(&ctx->outHeaders);
adam@1369 3385 uw_buffer_reset(&ctx->page);
adamc@741 3386
adamc@856 3387 uw_write_header(ctx, on_success);
adamc@856 3388 uw_write_header(ctx, "Content-Type: ");
adamc@741 3389 uw_write_header(ctx, mimeType);
adamc@741 3390 uw_write_header(ctx, "\r\nContent-Length: ");
adam@1369 3391 ctx_uw_buffer_check(ctx, "headers", &ctx->outHeaders, INTS_MAX);
adamc@1134 3392 sprintf(ctx->outHeaders.front, "%lu%n", (unsigned long)b.size, &len);
adamc@741 3393 ctx->outHeaders.front += len;
adamc@1066 3394 uw_write_header(ctx, "\r\n");
adamc@741 3395
adam@1369 3396 ctx_uw_buffer_append(ctx, "page", &ctx->page, b.data, b.size);
adamc@741 3397
adamc@741 3398 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@741 3399 cl->func(cl->arg);
adamc@741 3400
adamc@741 3401 ctx->cleanup_front = ctx->cleanup;
adamc@741 3402
adamc@1065 3403 longjmp(ctx->jmp_buf, RETURN_INDIRECTLY);
adamc@1065 3404 }
adamc@1065 3405
adamc@1065 3406 __attribute__((noreturn)) void uw_redirect(uw_context ctx, uw_Basis_string url) {
adamc@1065 3407 cleanup *cl;
adamc@1066 3408 char *s;
adamc@1065 3409
adamc@1065 3410 ctx->returning_indirectly = 1;
adam@1369 3411 uw_buffer_reset(&ctx->page);
adam@1369 3412 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->outHeaders)+1);
adam@1369 3413 memcpy(ctx->page.start, ctx->outHeaders.start, uw_buffer_used(&ctx->outHeaders));
adam@1369 3414 ctx->page.start[uw_buffer_used(&ctx->outHeaders)] = 0;
adam@1369 3415 uw_buffer_reset(&ctx->outHeaders);
adamc@1065 3416
adam@1409 3417 if (on_success[0])
adam@1409 3418 uw_write_header(ctx, on_redirect);
adam@1409 3419 else
adam@1409 3420 uw_write_header(ctx, "Status: 303 See Other\r\n");
adam@1409 3421 s = on_success[0] ? strchr(ctx->page.start, '\n') : ctx->page.start;
adamc@1066 3422 if (s) {
adamc@1066 3423 char *s2;
adam@1409 3424 if (s[0] == '\n') ++s;
adam@1409 3425 for (; (s2 = strchr(s, '\n')); s = s2+1) {
adamc@1066 3426 *s2 = 0;
adamc@1066 3427 if (!strncmp(s, "Set-Cookie: ", 12)) {
adamc@1066 3428 uw_write_header(ctx, s);
adamc@1066 3429 uw_write_header(ctx, "\n");
adamc@1066 3430 }
adamc@1066 3431 }
adamc@1066 3432 }
adamc@1066 3433
adamc@1065 3434 uw_write_header(ctx, "Location: ");
adamc@1065 3435 uw_write_header(ctx, url);
adamc@1065 3436 uw_write_header(ctx, "\r\n\r\n");
adamc@1065 3437
adamc@1065 3438 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@1065 3439 cl->func(cl->arg);
adamc@1065 3440
adamc@1065 3441 ctx->cleanup_front = ctx->cleanup;
adamc@1065 3442
adamc@1065 3443 longjmp(ctx->jmp_buf, RETURN_INDIRECTLY);
adamc@741 3444 }
adamc@874 3445
adamc@874 3446 uw_Basis_string uw_Basis_unAs(uw_context ctx, uw_Basis_string s) {
adamc@874 3447 uw_Basis_string r = uw_malloc(ctx, strlen(s) + 1);
adamc@874 3448
adamc@874 3449 for (; *s; ++s) {
adamc@874 3450 if (s[0] == '\'') {
adamc@874 3451 *r++ = '\'';
adamc@874 3452 for (++s; *s; ++s) {
adamc@874 3453 if (s[0] == '\'') {
adamc@874 3454 *r++ = '\'';
adamc@874 3455 break;
adamc@874 3456 } else if (s[0] == '\\') {
adamc@874 3457 if (s[1] == '\\') {
adamc@874 3458 *r++ = '\\';
adamc@874 3459 *r++ = '\\';
adamc@874 3460 ++s;
adamc@874 3461 } else if (s[1] == '\'') {
adamc@874 3462 *r++ = '\\';
adamc@874 3463 *r++ = '\'';
adamc@874 3464 ++s;
adamc@874 3465 } else
adamc@874 3466 *r++ = '\'';
adamc@874 3467 } else
adamc@874 3468 *r++ = s[0];
adamc@874 3469 }
adamc@874 3470 if (*s == 0) break;
adamc@874 3471 } else if (s[0] == 'T' && s[1] == '.')
adamc@874 3472 ++s;
adamc@874 3473 else
adamc@874 3474 *r++ = s[0];
adamc@874 3475 }
adamc@874 3476
adamc@874 3477 return r;
adamc@874 3478 }
adamc@874 3479
adamc@922 3480 uw_Basis_string uw_Basis_mstrcat(uw_context ctx, ...) {
adamc@922 3481 va_list ap;
adamc@922 3482 size_t len = 1;
adamc@922 3483 char *s, *r, *s2;
adamc@922 3484
adamc@922 3485 va_start(ap, ctx);
adamc@922 3486 for (s = va_arg(ap, char*); s; s = va_arg(ap, char*))
adamc@922 3487 len += strlen(s);
adamc@922 3488 va_end(ap);
adamc@922 3489
adamc@922 3490 r = uw_malloc(ctx, len);
adamc@922 3491 va_start(ap, ctx);
adamc@922 3492 for (s2 = r, s = va_arg(ap, char*); s; s = va_arg(ap, char*))
adamc@922 3493 while (*s)
adamc@922 3494 *s2++ = *s++;
adamc@922 3495 va_end(ap);
adamc@922 3496 *s2 = 0;
adamc@922 3497
adamc@922 3498 return r;
adamc@922 3499 }
adamc@1006 3500
adam@1365 3501 const uw_Basis_time uw_Basis_minTime = {};
adamc@1050 3502
adamc@1006 3503 uw_Basis_time uw_Basis_now(uw_context ctx) {
adam@1365 3504 uw_Basis_time r = { time(NULL) };
adam@1365 3505 return r;
adamc@1006 3506 }
adamc@1061 3507
adam@1369 3508 uw_Basis_time uw_Basis_addSeconds(uw_context ctx, uw_Basis_time tm, uw_Basis_int n) {
adam@1369 3509 tm.seconds += n;
adam@1365 3510 return tm;
adamc@1138 3511 }
adamc@1138 3512
adamc@1061 3513 void *uw_get_global(uw_context ctx, char *name) {
adamc@1061 3514 int i;
adamc@1061 3515
adamc@1061 3516 for (i = 0; i < ctx->n_globals; ++i)
adamc@1061 3517 if (!strcmp(name, ctx->globals[i].name))
adamc@1061 3518 return ctx->globals[i].data;
adamc@1061 3519
adamc@1061 3520 return NULL;
adamc@1061 3521 }
adamc@1061 3522
adamc@1113 3523 size_t uw_globals_max = SIZE_MAX;
adamc@1113 3524
adamc@1061 3525 void uw_set_global(uw_context ctx, char *name, void *data, void (*free)(void*)) {
adamc@1061 3526 int i;
adamc@1061 3527
adamc@1061 3528 for (i = 0; i < ctx->n_globals; ++i)
adamc@1061 3529 if (!strcmp(name, ctx->globals[i].name)) {
adamc@1089 3530 if (ctx->globals[i].free)
adamc@1061 3531 ctx->globals[i].free(ctx->globals[i].data);
adamc@1061 3532 ctx->globals[i].data = data;
adamc@1061 3533 ctx->globals[i].free = free;
adamc@1061 3534 return;
adamc@1061 3535 }
adamc@1089 3536
adamc@1113 3537 if (ctx->n_globals+1 > uw_globals_max)
adamc@1113 3538 uw_error(ctx, FATAL, "Exceeded limit on number of globals");
adamc@1113 3539
adamc@1061 3540 ++ctx->n_globals;
adamc@1061 3541 ctx->globals = realloc(ctx->globals, ctx->n_globals * sizeof(global));
adamc@1061 3542 ctx->globals[ctx->n_globals-1].name = name;
adamc@1061 3543 ctx->globals[ctx->n_globals-1].data = data;
adamc@1061 3544 ctx->globals[ctx->n_globals-1].free = free;
adamc@1061 3545 }
adamc@1061 3546
adamc@1061 3547 uw_Basis_bool uw_Basis_isalnum(uw_context ctx, uw_Basis_char c) {
adamc@1153 3548 return !!isalnum((int)c);
adamc@1061 3549 }
adamc@1061 3550
adamc@1061 3551 uw_Basis_bool uw_Basis_isalpha(uw_context ctx, uw_Basis_char c) {
adamc@1153 3552 return !!isalpha((int)c);
adamc@1061 3553 }
adamc@1061 3554
adamc@1061 3555 uw_Basis_bool uw_Basis_isblank(uw_context ctx, uw_Basis_char c) {
adamc@1153 3556 return !!isblank((int)c);
adamc@1061 3557 }
adamc@1061 3558
adamc@1061 3559 uw_Basis_bool uw_Basis_iscntrl(uw_context ctx, uw_Basis_char c) {
adamc@1153 3560 return !!iscntrl((int)c);
adamc@1061 3561 }
adamc@1061 3562
adamc@1061 3563 uw_Basis_bool uw_Basis_isdigit(uw_context ctx, uw_Basis_char c) {
adamc@1153 3564 return !!isdigit((int)c);
adamc@1061 3565 }
adamc@1061 3566
adamc@1061 3567 uw_Basis_bool uw_Basis_isgraph(uw_context ctx, uw_Basis_char c) {
adamc@1153 3568 return !!isgraph((int)c);
adamc@1061 3569 }
adamc@1061 3570
adamc@1061 3571 uw_Basis_bool uw_Basis_islower(uw_context ctx, uw_Basis_char c) {
adamc@1153 3572 return !!islower((int)c);
adamc@1061 3573 }
adamc@1061 3574
adamc@1061 3575 uw_Basis_bool uw_Basis_isprint(uw_context ctx, uw_Basis_char c) {
adamc@1153 3576 return !!isprint((int)c);
adamc@1061 3577 }
adamc@1061 3578
adamc@1061 3579 uw_Basis_bool uw_Basis_ispunct(uw_context ctx, uw_Basis_char c) {
adamc@1153 3580 return !!ispunct((int)c);
adamc@1061 3581 }
adamc@1061 3582
adamc@1061 3583 uw_Basis_bool uw_Basis_isspace(uw_context ctx, uw_Basis_char c) {
adamc@1153 3584 return !!isspace((int)c);
adamc@1061 3585 }
adamc@1063 3586
adamc@1061 3587 uw_Basis_bool uw_Basis_isupper(uw_context ctx, uw_Basis_char c) {
adamc@1153 3588 return !!isupper((int)c);
adamc@1061 3589 }
adamc@1061 3590
adamc@1061 3591 uw_Basis_bool uw_Basis_isxdigit(uw_context ctx, uw_Basis_char c) {
adamc@1153 3592 return !!isxdigit((int)c);
adamc@1061 3593 }
adamc@1061 3594
adamc@1061 3595 uw_Basis_char uw_Basis_tolower(uw_context ctx, uw_Basis_char c) {
adamc@1153 3596 return tolower((int)c);
adamc@1061 3597 }
adamc@1061 3598
adamc@1061 3599 uw_Basis_char uw_Basis_toupper(uw_context ctx, uw_Basis_char c) {
adamc@1153 3600 return toupper((int)c);
adamc@1061 3601 }
adamc@1066 3602
adamc@1128 3603 uw_Basis_int uw_Basis_ord(uw_context ctx, uw_Basis_char c) {
adamc@1128 3604 return (unsigned char)c;
adamc@1128 3605 }
adamc@1128 3606
adamc@1128 3607 uw_Basis_char uw_Basis_chr(uw_context ctx, uw_Basis_int n) {
adamc@1128 3608 return n;
adamc@1128 3609 }
adamc@1128 3610
adamc@1066 3611 uw_Basis_string uw_Basis_currentUrl(uw_context ctx) {
adamc@1066 3612 return ctx->current_url;
adamc@1066 3613 }
adamc@1066 3614
adamc@1066 3615 void uw_set_currentUrl(uw_context ctx, char *s) {
adamc@1066 3616 ctx->current_url = s;
adamc@1066 3617 }
adamc@1114 3618
adamc@1114 3619 void uw_set_deadline(uw_context ctx, int n) {
adamc@1114 3620 ctx->deadline = n;
adamc@1114 3621 }
adamc@1114 3622
adamc@1114 3623 void uw_check_deadline(uw_context ctx) {
adamc@1114 3624 if (uw_time > ctx->deadline)
adamc@1114 3625 uw_error(ctx, FATAL, "Maximum running time exceeded");
adamc@1114 3626 }
adamc@1115 3627
adamc@1115 3628 size_t uw_database_max = SIZE_MAX;
adamc@1120 3629
adam@1389 3630 uw_Basis_int uw_Basis_naughtyDebug(uw_context ctx, uw_Basis_string s) {
adam@1389 3631 fprintf(stderr, "%s\n", s);
adam@1389 3632 return 0;
adam@1389 3633 }
adam@1389 3634
adamc@1120 3635 uw_Basis_unit uw_Basis_debug(uw_context ctx, uw_Basis_string s) {
adam@1334 3636 if (ctx->log_debug)
adam@1334 3637 ctx->log_debug(ctx->logger_data, "%s\n", s);
adam@1334 3638 else
adam@1334 3639 fprintf(stderr, "%s\n", s);
adamc@1120 3640 return uw_unit_v;
adamc@1120 3641 }
adamc@1250 3642
adamc@1250 3643 uw_Basis_int uw_Basis_rand(uw_context ctx) {
adamc@1250 3644 uw_Basis_int n = abs(rand());
adamc@1250 3645 return n;
adamc@1250 3646 }
adam@1347 3647
adam@1347 3648 void uw_noPostBody(uw_context ctx) {
adam@1347 3649 ctx->hasPostBody = 0;
adam@1347 3650 }
adam@1347 3651
adam@1347 3652 void uw_postBody(uw_context ctx, uw_Basis_postBody pb) {
adam@1347 3653 ctx->hasPostBody = 1;
adam@1347 3654 ctx->postBody = pb;
adam@1347 3655 }
adam@1347 3656
adam@1347 3657 int uw_hasPostBody(uw_context ctx) {
adam@1347 3658 return ctx->hasPostBody;
adam@1347 3659 }
adam@1347 3660
adam@1386 3661 void uw_isPost(uw_context ctx) {
adam@1386 3662 ctx->isPost = 1;
adam@1386 3663 }
adam@1386 3664
adam@1386 3665 uw_Basis_bool uw_Basis_currentUrlHasPost(uw_context ctx) {
adam@1386 3666 return ctx->isPost;
adam@1386 3667 }
adam@1386 3668
adam@1370 3669 void uw_setQueryString(uw_context ctx, uw_Basis_string s) {
adam@1370 3670 ctx->queryString = s;
adam@1370 3671 }
adam@1370 3672
adam@1370 3673 uw_Basis_string uw_queryString(uw_context ctx) {
adam@1370 3674 return ctx->queryString;
adam@1370 3675 }
adam@1370 3676
adam@1347 3677 uw_Basis_postBody uw_getPostBody(uw_context ctx) {
adam@1347 3678 if (ctx->hasPostBody)
adam@1347 3679 return ctx->postBody;
adam@1347 3680 else
adam@1347 3681 uw_error(ctx, FATAL, "Asked for POST body when none exists");
adam@1347 3682 }
adam@1349 3683
adam@1349 3684 failure_kind uw_runCallback(uw_context ctx, void (*callback)(uw_context)) {
adam@1349 3685 int r = setjmp(ctx->jmp_buf);
adam@1349 3686
adam@1349 3687 if (r == 0) {
adam@1418 3688 if (ctx->app->db_begin(ctx))
adam@1418 3689 uw_error(ctx, BOUNDED_RETRY, "Error running SQL BEGIN");
adam@1418 3690
adam@1349 3691 callback(ctx);
adam@1349 3692 }
adam@1349 3693
adam@1349 3694 return r;
adam@1349 3695 }
adam@1360 3696
adam@1360 3697 uw_Basis_string uw_Basis_crypt(uw_context ctx, uw_Basis_string key, uw_Basis_string salt) {
adam@1397 3698 char buf[14];
adam@1397 3699 return uw_strdup(ctx, DES_fcrypt(key, salt, buf));
adam@1360 3700 }
adam@1365 3701
adam@1365 3702 uw_Basis_bool uw_Basis_eq_time(uw_context ctx, uw_Basis_time t1, uw_Basis_time t2) {
adam@1365 3703 return !!(t1.seconds == t2.seconds && t1.microseconds == t2.microseconds);
adam@1365 3704 }
adam@1365 3705
adam@1365 3706 uw_Basis_bool uw_Basis_lt_time(uw_context ctx, uw_Basis_time t1, uw_Basis_time t2) {
adam@1365 3707 return !!(t1.seconds < t2.seconds || t1.microseconds < t2.microseconds);
adam@1365 3708 }
adam@1365 3709
adam@1365 3710 uw_Basis_bool uw_Basis_le_time(uw_context ctx, uw_Basis_time t1, uw_Basis_time t2) {
adam@1365 3711 return !!(uw_Basis_eq_time(ctx, t1, t2) || uw_Basis_lt_time(ctx, t1, t2));
adam@1365 3712 }
adam@1365 3713
adam@1374 3714 /*uw_Basis_time *uw_Basis_readUtc(uw_context ctx, uw_Basis_string s) {
adam@1374 3715 uw_Basis_time *r = uw_Basis_stringToTime(ctx, s);
adam@1374 3716
adam@1374 3717 printf("timezone = %ld\n", timezone);
adam@1374 3718
adam@1374 3719 if (r)
adam@1374 3720 r->seconds -= timezone;
adam@1374 3721
adam@1374 3722 return r;
adam@1374 3723 }*/
adam@1374 3724
adam@1372 3725 uw_Basis_time *uw_Basis_readUtc(uw_context ctx, uw_Basis_string s) {
adam@1374 3726 struct tm stm = {};
adam@1375 3727 char *end = strchr(s, 0);
adam@1392 3728 stm.tm_isdst = -1;
adam@1375 3729
adam@1375 3730 if (strptime(s, TIME_FMT_PG, &stm) == end || strptime(s, TIME_FMT, &stm) == end) {
adam@1373 3731 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adam@1375 3732
adam@1375 3733 tzset();
adam@1374 3734 stm.tm_hour -= timezone / (60 * 60);
adam@1375 3735
adam@1374 3736 r->seconds = mktime(&stm);
adam@1373 3737 r->microseconds = 0;
adam@1373 3738
adam@1373 3739 return r;
adam@1374 3740 }
adam@1374 3741 else
adam@1373 3742 return NULL;
adam@1372 3743 }
adam@1407 3744
adam@1407 3745 static const char begin_xhtml[] = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">";
adam@1407 3746
adam@1407 3747 failure_kind uw_begin_onError(uw_context ctx, char *msg) {
adam@1407 3748 int r = setjmp(ctx->jmp_buf);
adam@1407 3749
adam@1407 3750 if (ctx->app->on_error) {
adam@1407 3751 if (r == 0) {
adam@1407 3752 if (ctx->app->db_begin(ctx))
adam@1407 3753 uw_error(ctx, BOUNDED_RETRY, "Error running SQL BEGIN");
adam@1407 3754
adam@1409 3755 uw_buffer_reset(&ctx->outHeaders);
adam@1409 3756 if (on_success[0])
adam@1409 3757 uw_write_header(ctx, "HTTP/1.1 ");
adam@1409 3758 else
adam@1409 3759 uw_write_header(ctx, "Status: ");
adam@1409 3760 uw_write_header(ctx, "500 Internal Server Error\r\n");
adam@1407 3761 uw_write_header(ctx, "Content-type: text/html\r\n\r\n");
adam@1407 3762 uw_write(ctx, begin_xhtml);
adam@1407 3763 ctx->app->on_error(ctx, msg);
adam@1407 3764 uw_write(ctx, "</html>");
adam@1407 3765 }
adam@1407 3766
adam@1407 3767 return r;
adam@1407 3768 } else
adam@1407 3769 uw_error(ctx, FATAL, "Tried to run nonexistent onError handler");
adam@1407 3770 }