annotate src/c/urweb.c @ 1369:1a78ca089bd0

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