annotate src/c/urweb.c @ 1416:a8606c1cfe87

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