adamc@1268: #include "config.h" adamc@1268: adamc@117: #include adamc@102: #include adamc@117: #include adamc@457: #include adamc@106: #include adamc@167: #include adamc@167: #include adamc@682: #include adamc@741: #include adamc@1114: #include adamc@1113: #include adamc@1094: #include adamc@1094: #include adam@1397: #include adam@1486: #include adam@1374: #include adam@1571: #include adamc@102: adamc@667: #include adamc@667: adamc@102: #include "types.h" adamc@102: adam@1431: uw_unit uw_unit_v = 0; adamc@102: adamc@667: adamc@667: // Socket extras adamc@667: adamc@863: int uw_really_send(int sock, const void *buf, ssize_t len) { adamc@667: while (len > 0) { adamc@856: ssize_t n = send(sock, buf, len, 0); adamc@856: adamc@856: if (n < 0) adamc@856: return n; adamc@856: adamc@856: buf += n; adamc@856: len -= n; adamc@856: } adamc@856: adamc@856: return 0; adamc@856: } adamc@856: adamc@856: int uw_really_write(int fd, const void *buf, size_t len) { adamc@856: while (len > 0) { adamc@856: ssize_t n = write(fd, buf, len); adamc@667: adamc@667: if (n < 0) adamc@667: return n; adamc@667: adamc@667: buf += n; adamc@667: len -= n; adamc@667: } adamc@667: adamc@667: return 0; adamc@667: } adamc@667: adamc@667: adamc@667: // Buffers adamc@667: adam@1369: void uw_buffer_init(size_t max, uw_buffer *b, size_t s) { adamc@1113: b->max = max; adamc@667: b->front = b->start = malloc(s); adamc@667: b->back = b->front + s; adamc@667: } adamc@667: adam@1369: void uw_buffer_free(uw_buffer *b) { adamc@667: free(b->start); adamc@667: } adamc@667: adam@1369: void uw_buffer_reset(uw_buffer *b) { adamc@667: b->front = b->start; adamc@667: } adamc@667: adam@1369: int uw_buffer_check(uw_buffer *b, size_t extra) { adamc@667: if (b->back - b->front < extra) { adamc@667: size_t desired = b->front - b->start + extra, next; adamc@667: char *new_heap; adamc@667: adamc@667: next = b->back - b->start; adamc@667: if (next == 0) adamc@667: next = 1; adamc@667: for (; next < desired; next *= 2); adamc@667: adamc@1134: if (next > b->max) { adamc@1113: if (desired <= b->max) adamc@1113: next = desired; adamc@1113: else adamc@1113: return 1; adamc@1134: } adamc@1113: adamc@667: new_heap = realloc(b->start, next); adamc@667: b->front = new_heap + (b->front - b->start); adamc@667: b->back = new_heap + next; adamc@667: b->start = new_heap; adamc@667: } adamc@1113: adamc@1113: return 0; adamc@1113: } adamc@1113: adamc@1113: __attribute__((noreturn)) void uw_error(uw_context, failure_kind, const char *, ...); adamc@1113: adam@1369: static void ctx_uw_buffer_check(uw_context ctx, const char *kind, uw_buffer *b, size_t extra) { adam@1369: if (uw_buffer_check(b, extra)) adamc@1113: uw_error(ctx, FATAL, "Memory limit exceeded (%s)", kind); adamc@667: } adamc@667: adam@1369: size_t uw_buffer_used(uw_buffer *b) { adamc@667: return b->front - b->start; adamc@667: } adamc@667: adam@1369: size_t uw_buffer_avail(uw_buffer *b) { adamc@667: return b->back - b->start; adamc@667: } adamc@667: adam@1369: int uw_buffer_append(uw_buffer *b, const char *s, size_t len) { adam@1369: if (uw_buffer_check(b, len+1)) adamc@1113: return 1; adamc@1113: adamc@1113: memcpy(b->front, s, len); adamc@1113: b->front += len; adamc@1113: *b->front = 0; adamc@1113: adamc@1113: return 0; adamc@1113: } adamc@1113: adam@1369: static void ctx_uw_buffer_append(uw_context ctx, const char *kind, uw_buffer *b, const char *s, size_t len) { adam@1369: ctx_uw_buffer_check(ctx, kind, b, len+1); adamc@1113: adamc@668: memcpy(b->front, s, len); adamc@668: b->front += len; adamc@672: *b->front = 0; adamc@668: } adamc@667: adamc@668: adamc@672: // Persistent state types adamc@667: adamc@667: typedef enum { UNUSED, USED } usage; adamc@667: adamc@667: typedef struct client { adamc@682: unsigned id; adamc@667: usage mode; adamc@682: int pass; adamc@682: struct client *next; adamc@689: pthread_mutex_t lock, pull_lock; adam@1369: uw_buffer msgs; adamc@682: int sock; adamc@863: int (*send)(int sockfd, const void *buf, ssize_t len); adamc@863: int (*close)(int fd); adamc@682: time_t last_contact; adamc@682: unsigned n_channels; adamc@685: unsigned refcount; adamc@1121: void *data; adamc@667: } client; adamc@667: adamc@672: adamc@672: // Persistent client state adamc@672: adamc@682: static client **clients, *clients_free, *clients_used; adamc@682: static unsigned n_clients; adamc@667: adam@1707: static pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER; adamc@1113: size_t uw_messages_max = SIZE_MAX; adamc@1113: size_t uw_clients_max = SIZE_MAX; adamc@1113: adamc@1121: void *uw_init_client_data(); adamc@1121: void uw_free_client_data(void *); adamc@1121: void uw_copy_client_data(void *dst, void *src); adamc@1121: adamc@682: static client *new_client() { adamc@667: client *c; adamc@667: adamc@667: pthread_mutex_lock(&clients_mutex); adamc@667: adamc@667: if (clients_free) { adamc@667: c = clients_free; adamc@682: clients_free = clients_free->next; adamc@667: } adam@1672: else if (n_clients >= uw_clients_max) { adam@1672: pthread_mutex_unlock(&clients_mutex); adamc@1113: return NULL; adam@1672: } else { adamc@667: ++n_clients; adamc@667: clients = realloc(clients, sizeof(client) * n_clients); adamc@667: c = malloc(sizeof(client)); adamc@667: c->id = n_clients-1; adamc@682: pthread_mutex_init(&c->lock, NULL); adamc@689: pthread_mutex_init(&c->pull_lock, NULL); adam@1369: uw_buffer_init(uw_messages_max, &c->msgs, 0); adamc@667: clients[n_clients-1] = c; adamc@667: } adamc@667: adamc@682: pthread_mutex_lock(&c->lock); adamc@667: c->mode = USED; adamc@682: c->pass = rand(); adamc@682: c->sock = -1; adamc@682: c->last_contact = time(NULL); adam@1369: uw_buffer_reset(&c->msgs); adamc@682: c->n_channels = 0; adamc@685: c->refcount = 0; adamc@1121: c->data = uw_init_client_data(); adamc@682: pthread_mutex_unlock(&c->lock); adamc@682: adamc@682: c->next = clients_used; adamc@682: clients_used = c; adamc@667: adamc@667: pthread_mutex_unlock(&clients_mutex); adamc@667: adamc@667: return c; adamc@667: } adamc@667: adamc@685: static void use_client(client *c) { adamc@685: pthread_mutex_lock(&c->lock); adamc@685: ++c->refcount; adamc@685: pthread_mutex_unlock(&c->lock); adamc@689: pthread_mutex_lock(&c->pull_lock); adamc@685: } adamc@685: adamc@685: static void release_client(client *c) { adamc@689: pthread_mutex_unlock(&c->pull_lock); adamc@685: pthread_mutex_lock(&c->lock); adamc@685: --c->refcount; adamc@685: pthread_mutex_unlock(&c->lock); adamc@685: } adamc@685: adamc@856: static const char begin_msgs[] = "Content-type: text/plain\r\n\r\n"; adam@1707: static pthread_t pruning_thread; adam@1707: static int pruning_thread_initialized = 0; adamc@667: adamc@682: static client *find_client(unsigned id) { adamc@667: client *c; adam@1708: int i_am_pruner = pruning_thread_initialized && pthread_equal(pruning_thread, pthread_self()); adam@1708: adam@1708: if (!i_am_pruner) pthread_mutex_lock(&clients_mutex); adamc@667: adamc@667: if (id >= n_clients) { adam@1708: if (!i_am_pruner) pthread_mutex_unlock(&clients_mutex); adamc@668: return NULL; adamc@667: } adamc@667: adamc@667: c = clients[id]; adamc@667: adam@1708: if (!i_am_pruner) pthread_mutex_unlock(&clients_mutex); adamc@668: return c; adamc@668: } adamc@668: adamc@856: static char *on_success = "HTTP/1.1 200 OK\r\n"; adam@1409: static char *on_redirect = "HTTP/1.1 303 See Other\r\n"; adamc@856: adamc@856: void uw_set_on_success(char *s) { adamc@856: on_success = s; adamc@856: } adamc@856: adam@1552: static void chastise(int (*send)(int sockfd, const void *buf, ssize_t len), int sock) { adam@1552: send(sock, on_success, strlen(on_success)); adam@1552: send(sock, begin_msgs, sizeof(begin_msgs) - 1); adam@1552: send(sock, "R", 1); adam@1552: close(sock); adam@1552: } adam@1552: adamc@863: void uw_client_connect(unsigned id, int pass, int sock, adamc@863: int (*send)(int sockfd, const void *buf, ssize_t len), adamc@864: int (*close)(int fd), adamc@864: void *logger_data, uw_logger log_error) { adamc@682: client *c = find_client(id); adamc@668: adamc@668: if (c == NULL) { adam@1552: chastise(send, sock); adamc@864: log_error(logger_data, "Out-of-bounds client request (%u)\n", id); adamc@667: return; adamc@667: } adamc@667: adamc@682: pthread_mutex_lock(&c->lock); adamc@671: adamc@682: if (c->mode != USED) { adamc@682: pthread_mutex_unlock(&c->lock); adam@1552: chastise(send, sock); adamc@864: log_error(logger_data, "Client request for unused slot (%u)\n", id); adamc@667: return; adamc@667: } adamc@667: adamc@682: if (pass != c->pass) { adamc@682: pthread_mutex_unlock(&c->lock); adam@1552: chastise(send, sock); adamc@864: log_error(logger_data, "Wrong client password (%u, %d)\n", id, pass); adamc@682: return; adamc@667: } adamc@667: adamc@682: if (c->sock != -1) { adamc@863: c->close(c->sock); adamc@682: c->sock = -1; adamc@682: } adamc@667: adamc@682: c->last_contact = time(NULL); adamc@682: adam@1369: if (uw_buffer_used(&c->msgs) > 0) { adamc@863: send(sock, on_success, strlen(on_success)); adamc@863: send(sock, begin_msgs, sizeof(begin_msgs) - 1); adam@1369: send(sock, c->msgs.start, uw_buffer_used(&c->msgs)); adam@1369: uw_buffer_reset(&c->msgs); adamc@667: close(sock); adamc@667: } adamc@863: else { adamc@682: c->sock = sock; adamc@863: c->send = send; adamc@863: c->close = close; adamc@863: } adamc@667: adamc@682: pthread_mutex_unlock(&c->lock); adamc@667: } adamc@667: adamc@682: static void free_client(client *c) { adamc@682: c->mode = UNUSED; adamc@682: c->pass = -1; adamc@667: adamc@682: c->next = clients_free; adamc@682: clients_free = c; adamc@667: } adamc@667: adamc@682: static uw_Basis_channel new_channel(client *c) { adamc@682: uw_Basis_channel ch = {c->id, c->n_channels++}; adamc@668: return ch; adamc@668: } adamc@668: adam@1447: static void client_send(client *c, uw_buffer *msg, const char *script, int script_len) { adamc@685: pthread_mutex_lock(&c->lock); adamc@671: adamc@682: if (c->sock != -1) { adamc@863: c->send(c->sock, on_success, strlen(on_success)); adamc@863: c->send(c->sock, begin_msgs, sizeof(begin_msgs) - 1); adam@1447: if (script_len > 0) { adam@1447: c->send(c->sock, "E\n", 2); adam@1447: c->send(c->sock, script, script_len); adam@1447: c->send(c->sock, "\n", 1); adam@1447: } adam@1369: c->send(c->sock, msg->start, uw_buffer_used(msg)); adamc@863: c->close(c->sock); adamc@682: c->sock = -1; adam@1665: } else if (uw_buffer_append(&c->msgs, msg->start, uw_buffer_used(msg))) adamc@1113: fprintf(stderr, "Client message buffer size exceeded"); adamc@671: adamc@685: pthread_mutex_unlock(&c->lock); adamc@668: } adamc@668: adamc@668: adamc@668: // Global entry points adamc@668: adamc@1164: extern void uw_global_custom(); adamc@1104: extern void uw_init_crypto(); adamc@1104: adamc@668: void uw_global_init() { adamc@668: srand(time(NULL) ^ getpid()); adamc@668: adamc@668: clients = malloc(0); adamc@1104: adamc@1164: uw_global_custom(); adamc@1104: uw_init_crypto(); adamc@1094: } adamc@1094: adamc@1094: void uw_app_init(uw_app *app) { adamc@1094: app->client_init(); adamc@668: } adamc@668: adam@1332: int uw_time = 0, uw_time_max = 0, uw_min_heap = 0; adamc@1114: adamc@668: adamc@667: // Single-request state adamc@667: adamc@323: typedef struct regions { adamc@323: struct regions *next; adamc@323: } regions; adamc@323: adamc@425: typedef struct { adamc@425: void (*func)(void*); adamc@425: void *arg; adamc@425: } cleanup; adamc@425: adamc@671: typedef struct { adamc@682: unsigned client; adam@1369: uw_buffer msgs; adamc@682: } delta; adamc@671: adamc@737: typedef enum { adamc@759: UNSET, NORMAL, FIL, SUBFORM, SUBFORMS, ENTRY adamc@737: } input_kind; adamc@737: adamc@756: typedef struct input { adamc@737: input_kind kind; adamc@737: union { adamc@737: char *normal; adamc@739: uw_Basis_file file; adamc@756: struct { adamc@759: struct input *fields, *parent; adamc@756: } subform; adamc@759: struct { adamc@759: struct input *entries, *parent; adamc@759: } subforms; adamc@759: struct { adamc@759: struct input *fields, *next, *parent; adamc@759: } entry; adamc@737: } data; adamc@737: } input; adamc@737: adamc@771: typedef struct { adamc@771: void *data; adam@1327: uw_callback commit, rollback; adam@1327: uw_callback_with_retry free; adamc@771: } transactional; adamc@771: adamc@1061: typedef struct { adamc@1061: char *name; adamc@1061: void *data; adamc@1061: void (*free)(void*); adamc@1061: } global; adamc@1061: adamc@311: struct uw_context { adamc@1094: uw_app *app; adam@1446: int id; adamc@1094: adamc@854: char *(*get_header)(void *, const char *); adamc@854: void *get_header_data; adamc@457: adam@1799: char *(*get_env)(void *, const char *); adam@1799: void *get_env_data; adam@1799: adam@1369: uw_buffer outHeaders, page, heap, script; adam@1451: int allowed_to_return_indirectly, returning_indirectly; adamc@759: input *inputs, *subinputs, *cur_container; adamc@1094: size_t sz_inputs, n_subinputs, used_subinputs; adamc@167: adam@1446: unsigned long long source_count; adamc@562: adamc@272: void *db; adam@1931: int transaction_started; adamc@272: adamc@167: jmp_buf jmp_buf; adamc@167: adamc@323: regions *regions; adamc@323: adamc@425: cleanup *cleanup, *cleanup_front, *cleanup_back; adamc@425: adamc@1094: const char *script_header; adamc@643: adam@1936: int needs_push, needs_sig, could_write_db; adamc@693: adamc@682: size_t n_deltas, used_deltas; adamc@682: delta *deltas; adamc@671: adamc@682: client *client; adamc@682: adamc@771: transactional *transactionals; adamc@771: size_t n_transactionals, used_transactionals; adamc@771: adamc@1061: global *globals; adamc@1061: size_t n_globals; adamc@1061: adamc@1066: char *current_url; adamc@1066: adamc@1114: int deadline; adamc@1114: adamc@1121: void *client_data; adamc@1121: adam@1334: void *logger_data; adam@1334: uw_logger log_debug; adam@1334: adam@1386: int isPost, hasPostBody; adam@1347: uw_Basis_postBody postBody; adam@1370: uw_Basis_string queryString; adam@1347: adam@1556: unsigned nextId; adam@1556: adam@1782: int amInitializing; adam@1782: adamc@167: char error_message[ERROR_BUF_LEN]; adam@1877: adam@1877: int usedSig, needsResig; adam@1949: adam@1949: char *output_buffer; adam@1949: size_t output_buffer_size; adamc@117: }; adamc@117: adamc@1113: size_t uw_headers_max = SIZE_MAX; adamc@1113: size_t uw_page_max = SIZE_MAX; adamc@1113: size_t uw_heap_max = SIZE_MAX; adamc@1113: size_t uw_script_max = SIZE_MAX; adamc@1113: adam@1446: uw_context uw_init(int id, void *logger_data, uw_logger log_debug) { adamc@311: uw_context ctx = malloc(sizeof(struct uw_context)); adamc@136: adamc@1094: ctx->app = NULL; adam@1446: ctx->id = id; adamc@1094: adamc@854: ctx->get_header = NULL; adamc@854: ctx->get_header_data = NULL; adamc@457: adam@1799: ctx->get_env = NULL; adam@1799: ctx->get_env_data = NULL; adam@1799: adam@1413: uw_buffer_init(uw_headers_max, &ctx->outHeaders, 1); adam@1413: ctx->outHeaders.start[0] = 0; adam@1413: uw_buffer_init(uw_page_max, &ctx->page, 1); adam@1413: ctx->page.start[0] = 0; adam@1451: ctx->allowed_to_return_indirectly = ctx->returning_indirectly = 0; adam@1369: uw_buffer_init(uw_heap_max, &ctx->heap, uw_min_heap); adam@1369: uw_buffer_init(uw_script_max, &ctx->script, 1); adamc@667: ctx->script.start[0] = 0; adamc@136: adamc@1094: ctx->inputs = malloc(0); adamc@759: ctx->cur_container = NULL; adamc@756: ctx->subinputs = malloc(0); adamc@1094: ctx->sz_inputs = ctx->n_subinputs = ctx->used_subinputs = 0; adamc@144: adamc@272: ctx->db = NULL; adam@1931: ctx->transaction_started = 0; adamc@272: adamc@323: ctx->regions = NULL; adamc@323: adamc@425: ctx->cleanup_front = ctx->cleanup_back = ctx->cleanup = malloc(0); adamc@425: adamc@643: ctx->script_header = ""; adamc@693: ctx->needs_push = 0; adamc@736: ctx->needs_sig = 0; adam@1936: ctx->could_write_db = 1; adamc@1104: adamc@565: ctx->source_count = 0; adamc@562: adamc@682: ctx->n_deltas = ctx->used_deltas = 0; adamc@671: ctx->deltas = malloc(0); adamc@671: adamc@700: ctx->client = NULL; adamc@700: adamc@700: ctx->error_message[0] = 0; adamc@700: adamc@771: ctx->transactionals = malloc(0); adamc@771: ctx->n_transactionals = ctx->used_transactionals = 0; adamc@771: adamc@1061: ctx->globals = malloc(0); adamc@1061: ctx->n_globals = 0; adamc@1061: adamc@1066: ctx->current_url = ""; adamc@1066: adamc@1114: ctx->deadline = INT_MAX; adamc@1114: adamc@1121: ctx->client_data = uw_init_client_data(); adamc@1121: adam@1334: ctx->logger_data = logger_data; adam@1334: ctx->log_debug = log_debug; adam@1334: adam@1386: ctx->isPost = ctx->hasPostBody = 0; adam@1347: adam@1370: ctx->queryString = NULL; adam@1370: adam@1556: ctx->nextId = 0; adam@1556: adam@1782: ctx->amInitializing = 0; adam@1782: adam@1877: ctx->usedSig = 0; adam@1877: ctx->needsResig = 0; adam@1877: adam@1949: ctx->output_buffer = malloc(1); adam@1949: ctx->output_buffer_size = 1; adam@1949: adamc@117: return ctx; adamc@106: } adamc@106: adamc@1113: size_t uw_inputs_max = SIZE_MAX; adamc@1113: adamc@1121: uw_app *uw_get_app(uw_context ctx) { adamc@1121: return ctx->app; adamc@1121: } adamc@1121: adamc@1113: int uw_set_app(uw_context ctx, uw_app *app) { adamc@1094: ctx->app = app; adamc@1094: adamc@1105: if (app && app->inputs_len > ctx->sz_inputs) { adamc@1113: if (app->inputs_len > uw_inputs_max) adamc@1113: return 1; adamc@1113: adamc@1094: ctx->sz_inputs = app->inputs_len; adamc@1094: ctx->inputs = realloc(ctx->inputs, ctx->sz_inputs * sizeof(input)); adamc@1094: memset(ctx->inputs, 0, ctx->sz_inputs * sizeof(input)); adamc@1094: } adamc@1134: adamc@1134: return 0; adamc@1094: } adamc@1094: adamc@1121: void uw_set_client_data(uw_context ctx, void *data) { adamc@1121: uw_copy_client_data(ctx->client_data, data); adamc@1121: } adamc@1121: adamc@311: void uw_set_db(uw_context ctx, void *db) { adamc@272: ctx->db = db; adamc@272: } adamc@272: adamc@311: void *uw_get_db(uw_context ctx) { adamc@272: return ctx->db; adamc@272: } adamc@272: adamc@311: void uw_free(uw_context ctx) { adamc@671: size_t i; adamc@671: adam@1369: uw_buffer_free(&ctx->outHeaders); adam@1369: uw_buffer_free(&ctx->script); adam@1369: uw_buffer_free(&ctx->page); adam@1369: uw_buffer_free(&ctx->heap); adamc@144: free(ctx->inputs); adamc@756: free(ctx->subinputs); adamc@425: free(ctx->cleanup); adamc@771: free(ctx->transactionals); adamc@1121: uw_free_client_data(ctx->client_data); adamc@671: adamc@682: for (i = 0; i < ctx->n_deltas; ++i) adam@1369: uw_buffer_free(&ctx->deltas[i].msgs); marco-oweber@1330: free(ctx->deltas); adamc@671: adamc@1061: for (i = 0; i < ctx->n_globals; ++i) adamc@1089: if (ctx->globals[i].free) adamc@1089: ctx->globals[i].free(ctx->globals[i].data); marco-oweber@1330: free(ctx->globals); adamc@1061: adam@1949: free(ctx->output_buffer); adam@1949: adamc@136: free(ctx); adamc@136: } adamc@136: adamc@562: void uw_reset_keep_error_message(uw_context ctx) { adam@1369: uw_buffer_reset(&ctx->outHeaders); adam@1369: uw_buffer_reset(&ctx->script); adamc@667: ctx->script.start[0] = 0; adam@1369: uw_buffer_reset(&ctx->page); adam@1451: ctx->allowed_to_return_indirectly = ctx->returning_indirectly = 0; adam@1369: uw_buffer_reset(&ctx->heap); adamc@323: ctx->regions = NULL; adamc@425: ctx->cleanup_front = ctx->cleanup; adamc@682: ctx->used_deltas = 0; adamc@682: ctx->client = NULL; adamc@759: ctx->cur_container = NULL; adamc@771: ctx->used_transactionals = 0; adamc@1117: ctx->script_header = ""; adam@1370: ctx->queryString = NULL; adam@1556: ctx->nextId = 0; adam@1782: ctx->amInitializing = 0; adam@1877: ctx->usedSig = 0; adam@1877: ctx->needsResig = 0; adamc@167: } adamc@167: adamc@562: void uw_reset_keep_request(uw_context ctx) { adamc@562: uw_reset_keep_error_message(ctx); adamc@562: ctx->error_message[0] = 0; adamc@167: } adamc@167: adamc@311: void uw_reset(uw_context ctx) { adamc@311: uw_reset_keep_request(ctx); adamc@1105: if (ctx->app) adamc@1105: memset(ctx->inputs, 0, ctx->app->inputs_len * sizeof(input)); adamc@756: memset(ctx->subinputs, 0, ctx->n_subinputs * sizeof(input)); adam@1461: ctx->used_subinputs = ctx->hasPostBody = ctx->isPost = 0; adam@1943: ctx->transaction_started = 0; adamc@144: } adamc@144: adamc@311: failure_kind uw_begin_init(uw_context ctx) { adamc@272: int r = setjmp(ctx->jmp_buf); adamc@272: adamc@1105: if (r == 0 && ctx->app) adamc@1094: ctx->app->db_init(ctx); adamc@272: adamc@272: return r; adamc@272: } adamc@272: adamc@1118: void uw_close(uw_context ctx) { adamc@1118: ctx->app->db_close(ctx); adamc@1118: } adamc@1118: adam@1446: uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) { adam@1446: return ctx->get_header(ctx->get_header_data, h); adam@1446: } adam@1446: adamc@854: void uw_set_headers(uw_context ctx, char *(*get_header)(void *, const char *), void *get_header_data) { adamc@854: ctx->get_header = get_header; adamc@854: ctx->get_header_data = get_header_data; adamc@742: } adamc@742: adam@1799: void uw_set_env(uw_context ctx, char *(*get_env)(void *, const char *), void *get_env_data) { adam@1799: ctx->get_env = get_env; adam@1799: ctx->get_env_data = get_env_data; adam@1799: } adam@1799: adamc@801: static void uw_set_error(uw_context ctx, const char *fmt, ...) { adamc@801: va_list ap; adamc@801: va_start(ap, fmt); adamc@801: adamc@801: vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap); adamc@801: } adamc@801: adamc@1131: int uw_has_error(uw_context ctx) { adamc@1131: return ctx->error_message[0] != 0; adamc@1131: } adamc@1131: adamc@311: __attribute__((noreturn)) void uw_error(uw_context ctx, failure_kind fk, const char *fmt, ...) { adamc@425: cleanup *cl; adamc@425: adamc@167: va_list ap; adamc@167: va_start(ap, fmt); adamc@167: adamc@167: vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap); adamc@167: adamc@425: for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl) adamc@425: cl->func(cl->arg); adamc@425: adamc@425: ctx->cleanup_front = ctx->cleanup; adamc@425: adamc@190: longjmp(ctx->jmp_buf, fk); adamc@167: } adamc@167: adamc@1113: size_t uw_cleanup_max = SIZE_MAX; adamc@1113: adamc@425: void uw_push_cleanup(uw_context ctx, void (*func)(void *), void *arg) { adamc@425: if (ctx->cleanup_front >= ctx->cleanup_back) { adamc@425: int len = ctx->cleanup_back - ctx->cleanup, newLen; adamc@425: if (len == 0) adamc@425: newLen = 1; adamc@425: else adamc@428: newLen = len * 2; adamc@1113: adamc@1134: if (newLen > uw_cleanup_max) { adamc@1113: if (len+1 <= uw_cleanup_max) adamc@1113: newLen = uw_cleanup_max; adamc@1113: else adamc@1113: uw_error(ctx, FATAL, "Exceeded limit on number of cleanup handlers"); adamc@1134: } adamc@1113: adamc@470: ctx->cleanup = realloc(ctx->cleanup, newLen * sizeof(cleanup)); adamc@425: ctx->cleanup_front = ctx->cleanup + len; adamc@425: ctx->cleanup_back = ctx->cleanup + newLen; adamc@425: } adamc@425: adamc@425: ctx->cleanup_front->func = func; adamc@425: ctx->cleanup_front->arg = arg; adamc@425: ++ctx->cleanup_front; adamc@425: } adamc@425: adam@1444: char *uw_Basis_htmlifyString(uw_context, const char *); adam@1444: adamc@682: void uw_login(uw_context ctx) { adam@1942: char *id_s, *pass_s; adam@1942: adam@1942: if ((id_s = uw_Basis_requestHeader(ctx, "UrWeb-Client")) adam@1942: && (pass_s = uw_Basis_requestHeader(ctx, "UrWeb-Pass"))) { adam@1942: unsigned id = atoi(id_s); adam@1942: int pass = atoi(pass_s); adam@1942: client *c = find_client(id); adam@1942: adam@1942: if (c == NULL) adam@1942: uw_error(ctx, FATAL, "Unknown client ID in HTTP headers (%s, %s)", uw_Basis_htmlifyString(ctx, id_s), uw_Basis_htmlifyString(ctx, pass_s)); adam@1942: else { adamc@685: use_client(c); adamc@682: ctx->client = c; adam@1942: adam@1942: if (c->mode != USED) adam@1942: uw_error(ctx, FATAL, "Stale client ID (%u) in subscription request", id); adam@1942: if (c->pass != pass) adam@1942: uw_error(ctx, FATAL, "Wrong client password (%u, %d) in subscription request", id, pass); adamc@864: } adam@1942: } else if (ctx->needs_push) { adam@1942: client *c = new_client(); adam@1942: adam@1942: if (c == NULL) adam@1942: uw_error(ctx, FATAL, "Limit exceeded on number of message-passing clients"); adam@1942: adam@1942: use_client(c); adam@1942: uw_copy_client_data(c->data, ctx->client_data); adam@1942: ctx->client = c; adamc@682: } adamc@682: } adamc@682: adamc@671: failure_kind uw_begin(uw_context ctx, char *path) { adamc@671: int r = setjmp(ctx->jmp_buf); adamc@671: adam@1931: if (r == 0) adam@1931: ctx->app->handle(ctx, path); adam@1931: adam@1931: return r; adam@1931: } adam@1931: adam@1931: void uw_ensure_transaction(uw_context ctx) { adam@1931: if (!ctx->transaction_started) { adam@1936: if (ctx->app->db_begin(ctx, ctx->could_write_db)) adamc@671: uw_error(ctx, BOUNDED_RETRY, "Error running SQL BEGIN"); adam@1931: ctx->transaction_started = 1; adamc@671: } adamc@671: } adamc@671: adamc@772: uw_Basis_client uw_Basis_self(uw_context ctx) { adamc@682: if (ctx->client == NULL) adamc@682: uw_error(ctx, FATAL, "Call to Basis.self() from page that has only server-side code"); adamc@682: adamc@682: return ctx->client->id; adamc@682: } adamc@682: adamc@425: void uw_pop_cleanup(uw_context ctx) { adamc@425: if (ctx->cleanup_front == ctx->cleanup) adamc@425: uw_error(ctx, FATAL, "Attempt to pop from empty cleanup action stack"); adamc@425: adamc@425: --ctx->cleanup_front; adamc@425: ctx->cleanup_front->func(ctx->cleanup_front->arg); adamc@425: } adamc@425: adamc@311: char *uw_error_message(uw_context ctx) { adamc@167: return ctx->error_message; adamc@167: } adamc@167: adamc@1115: void uw_set_error_message(uw_context ctx, const char *msg) { adamc@1115: strncpy(ctx->error_message, msg, sizeof(ctx->error_message)); adamc@1115: ctx->error_message[sizeof(ctx->error_message)-1] = 0; adamc@1115: } adamc@1115: adamc@759: static input *INP(uw_context ctx) { adamc@759: if (ctx->cur_container == NULL) adamc@759: return ctx->inputs; adamc@759: else if (ctx->cur_container->kind == SUBFORM) adamc@759: return ctx->cur_container->data.subform.fields; adamc@759: else if (ctx->cur_container->kind == ENTRY) adamc@759: return ctx->cur_container->data.entry.fields; adamc@759: else adamc@760: uw_error(ctx, FATAL, "INP: Wrong kind (%d, %p)", ctx->cur_container->kind, ctx->cur_container); adamc@759: } adamc@759: adamc@760: static void adjust_pointer(input **ptr, input *old_start, input *new_start, size_t len) { adamc@760: if (*ptr != NULL && *ptr >= old_start && *ptr < old_start + len) adamc@760: *ptr += new_start - old_start; adamc@760: } adamc@760: adamc@760: static void adjust_input(input *x, input *old_start, input *new_start, size_t len) { adamc@759: switch (x->kind) { adamc@759: case SUBFORM: adamc@760: adjust_pointer(&x->data.subform.fields, old_start, new_start, len); adamc@760: adjust_pointer(&x->data.subform.parent, old_start, new_start, len); adamc@759: break; adamc@759: case SUBFORMS: adamc@760: adjust_pointer(&x->data.subforms.entries, old_start, new_start, len); adamc@760: adjust_pointer(&x->data.subforms.parent, old_start, new_start, len); adamc@759: break; adamc@759: case ENTRY: adamc@760: adjust_pointer(&x->data.entry.fields, old_start, new_start, len); adamc@760: adjust_pointer(&x->data.entry.next, old_start, new_start, len); adamc@760: adjust_pointer(&x->data.entry.parent, old_start, new_start, len); adamc@1134: break; adamc@1134: default: adamc@1134: break; adamc@759: } adamc@759: } adamc@759: adamc@1113: size_t uw_subinputs_max = SIZE_MAX; adamc@1113: adamc@759: static input *check_input_space(uw_context ctx, size_t len) { adamc@759: size_t i; adamc@759: input *r; adamc@759: adamc@759: if (ctx->used_subinputs + len >= ctx->n_subinputs) { adamc@1113: if (ctx->used_subinputs + len > uw_subinputs_max) adamc@1113: uw_error(ctx, FATAL, "Exceeded limit on number of subinputs"); adamc@1113: adamc@759: input *new_subinputs = realloc(ctx->subinputs, sizeof(input) * (ctx->used_subinputs + len)); adamc@759: adamc@760: if (ctx->subinputs != new_subinputs) { adamc@760: for (i = 0; i < ctx->used_subinputs; ++i) adamc@760: adjust_input(&new_subinputs[i], ctx->subinputs, new_subinputs, ctx->used_subinputs); adamc@1094: for (i = 0; i < ctx->app->inputs_len; ++i) adamc@760: adjust_input(&ctx->inputs[i], ctx->subinputs, new_subinputs, ctx->used_subinputs); adamc@760: adamc@760: adjust_pointer(&ctx->cur_container, ctx->subinputs, new_subinputs, ctx->used_subinputs); adamc@760: adamc@760: ctx->n_subinputs = ctx->used_subinputs + len; adamc@760: ctx->subinputs = new_subinputs; adamc@760: } adamc@759: } adamc@759: adamc@759: r = &ctx->subinputs[ctx->used_subinputs]; adamc@759: adamc@759: for (i = 0; i < len; ++i) adam@1474: ctx->subinputs[ctx->used_subinputs++].kind = UNSET; adamc@759: adamc@759: return r; adamc@759: } adamc@756: adamc@801: int uw_set_input(uw_context ctx, const char *name, char *value) { adamc@1050: //printf("Input name %s\n", name); adamc@1038: adamc@756: if (!strcasecmp(name, ".b")) { adamc@1094: int n = ctx->app->input_num(value); adamc@756: input *inps; adamc@756: adamc@801: if (n < 0) { adam@1444: uw_set_error(ctx, "Bad subform name %s", uw_Basis_htmlifyString(ctx, value)); adamc@801: return -1; adamc@801: } adamc@801: adamc@1094: if (n >= ctx->app->inputs_len) { adam@1444: uw_set_error(ctx, "For subform name %s, index %d is out of range", uw_Basis_htmlifyString(ctx, value), n); adamc@801: return -1; adamc@801: } adamc@756: adamc@1094: inps = check_input_space(ctx, ctx->app->inputs_len); adamc@759: adamc@759: INP(ctx)[n].kind = SUBFORM; adamc@759: INP(ctx)[n].data.subform.parent = ctx->cur_container; adamc@759: INP(ctx)[n].data.subform.fields = inps; adamc@759: ctx->cur_container = &INP(ctx)[n]; adamc@756: } else if (!strcasecmp(name, ".e")) { adamc@756: input *tmp; adamc@756: adamc@801: if (ctx->cur_container == NULL) { adamc@801: uw_set_error(ctx, "Unmatched subform closer"); adamc@801: return -1; adamc@801: } adamc@756: adamc@759: tmp = ctx->cur_container; adamc@759: switch (tmp->kind) { adamc@759: case SUBFORM: adamc@759: ctx->cur_container = tmp->data.subform.parent; adamc@759: tmp->data.subform.parent = NULL; adamc@759: break; adamc@759: case SUBFORMS: adamc@759: ctx->cur_container = tmp->data.subforms.parent; adamc@759: tmp->data.subforms.parent = NULL; adamc@759: break; adamc@759: case ENTRY: adamc@759: ctx->cur_container = tmp->data.entry.parent; adamc@759: break; adamc@759: default: adamc@801: uw_set_error(ctx, "uw_set_input: Wrong kind"); adamc@801: return -1; adamc@759: } adamc@759: } else if (!strcasecmp(name, ".s")) { adamc@1094: int n = ctx->app->input_num(value); adamc@759: adamc@801: if (n < 0) { adam@1444: uw_set_error(ctx, "Bad subforms name %s", uw_Basis_htmlifyString(ctx, value)); adamc@801: return -1; adamc@801: } adamc@801: adamc@1094: if (n >= ctx->app->inputs_len) { adam@1444: uw_set_error(ctx, "For subforms name %s, index %d is out of range", uw_Basis_htmlifyString(ctx, value), n); adamc@801: return -1; adamc@801: } adamc@759: adamc@759: INP(ctx)[n].kind = SUBFORMS; adamc@759: INP(ctx)[n].data.subforms.parent = ctx->cur_container; adamc@759: INP(ctx)[n].data.subforms.entries = NULL; adamc@759: ctx->cur_container = &INP(ctx)[n]; adamc@759: } else if (!strcasecmp(name, ".i")) { adamc@759: input *inps; adamc@759: adamc@801: if (!ctx->cur_container) { adamc@801: uw_set_error(ctx, "New entry without container"); adamc@801: return -1; adamc@801: } adamc@801: adamc@801: if (ctx->cur_container->kind != SUBFORMS) { adamc@801: uw_set_error(ctx, "Bad kind for entry parent"); adamc@801: return -1; adamc@801: } adamc@759: adamc@1094: inps = check_input_space(ctx, ctx->app->inputs_len + 1); adamc@759: adamc@759: inps->kind = ENTRY; adamc@759: inps->data.entry.parent = ctx->cur_container; adamc@759: inps->data.entry.next = ctx->cur_container->data.subforms.entries; adamc@759: ctx->cur_container->data.subforms.entries = inps; adamc@759: adamc@759: inps->data.entry.fields = inps+1; adamc@759: ctx->cur_container = inps; adamc@756: } else { adamc@1094: int n = ctx->app->input_num(name); adamc@756: adam@1384: if (n < 0) adam@1384: return 0; adamc@801: adamc@1094: if (n >= ctx->app->inputs_len) { adam@1444: uw_set_error(ctx, "For input name %s, index %d is out of range", uw_Basis_htmlifyString(ctx, name), n); adamc@801: return -1; adamc@801: } adamc@756: adamc@756: INP(ctx)[n].kind = NORMAL; adamc@756: INP(ctx)[n].data.normal = value; adamc@756: } adamc@801: adamc@801: return 0; adamc@144: } adamc@144: adamc@311: char *uw_get_input(uw_context ctx, int n) { adamc@169: if (n < 0) adamc@311: uw_error(ctx, FATAL, "Negative input index %d", n); adamc@1094: if (n >= ctx->app->inputs_len) adamc@311: uw_error(ctx, FATAL, "Out-of-bounds input index %d", n); adamc@737: adamc@756: switch (INP(ctx)[n].kind) { adamc@737: case UNSET: adamc@737: return NULL; adamc@739: case FIL: adamc@739: uw_error(ctx, FATAL, "Tried to read a file form input as normal"); adamc@756: case SUBFORM: adamc@756: uw_error(ctx, FATAL, "Tried to read a subform form input as normal"); adamc@759: case SUBFORMS: adamc@759: uw_error(ctx, FATAL, "Tried to read a subforms form input as normal"); adamc@759: case ENTRY: adamc@759: uw_error(ctx, FATAL, "Tried to read an entry form input as normal"); adamc@737: case NORMAL: adamc@756: return INP(ctx)[n].data.normal; adamc@737: default: adamc@737: uw_error(ctx, FATAL, "Impossible input kind"); adamc@737: } adamc@138: } adamc@138: adamc@311: char *uw_get_optional_input(uw_context ctx, int n) { adamc@190: if (n < 0) adamc@311: uw_error(ctx, FATAL, "Negative input index %d", n); adamc@1094: if (n >= ctx->app->inputs_len) adamc@311: uw_error(ctx, FATAL, "Out-of-bounds input index %d", n); adamc@737: adamc@756: switch (INP(ctx)[n].kind) { adamc@737: case UNSET: adamc@737: return ""; adamc@739: case FIL: adamc@739: uw_error(ctx, FATAL, "Tried to read a file form input as normal"); adamc@756: case SUBFORM: adamc@756: uw_error(ctx, FATAL, "Tried to read a subform form input as normal"); adamc@759: case SUBFORMS: adamc@759: uw_error(ctx, FATAL, "Tried to read a subforms form input as normal"); adamc@759: case ENTRY: adamc@759: uw_error(ctx, FATAL, "Tried to read an entry form input as normal"); adamc@737: case NORMAL: adamc@756: return INP(ctx)[n].data.normal; adamc@737: default: adamc@737: uw_error(ctx, FATAL, "Impossible input kind"); adamc@737: } adamc@737: } adamc@737: adamc@801: int uw_set_file_input(uw_context ctx, const char *name, uw_Basis_file f) { adamc@1094: int n = ctx->app->input_num(name); adamc@737: adamc@801: if (n < 0) { adam@1444: uw_set_error(ctx, "Bad file input name %s", uw_Basis_htmlifyString(ctx, name)); adamc@801: return -1; adamc@801: } adamc@801: adamc@1094: if (n >= ctx->app->inputs_len) { adam@1444: uw_set_error(ctx, "For file input name %s, index %d is out of range", uw_Basis_htmlifyString(ctx, name), n); adamc@801: return -1; adamc@801: } adamc@737: adamc@739: ctx->inputs[n].kind = FIL; adamc@739: ctx->inputs[n].data.file = f; adamc@801: adamc@801: return 0; adamc@737: } adamc@737: adamc@739: void *uw_malloc(uw_context ctx, size_t len); adamc@739: adamc@739: uw_Basis_file uw_get_file_input(uw_context ctx, int n) { adamc@737: if (n < 0) adamc@737: uw_error(ctx, FATAL, "Negative file input index %d", n); adamc@1094: if (n >= ctx->app->inputs_len) adamc@737: uw_error(ctx, FATAL, "Out-of-bounds file input index %d", n); adamc@737: adamc@756: switch (INP(ctx)[n].kind) { adamc@737: case UNSET: adamc@737: { adamc@739: char *data = uw_malloc(ctx, 0); adamc@740: uw_Basis_file f = {NULL, "", {0, data}}; adamc@739: return f; adamc@737: } adamc@739: case FIL: adamc@756: return INP(ctx)[n].data.file; adamc@737: case NORMAL: adamc@737: uw_error(ctx, FATAL, "Tried to read a normal form input as files"); adamc@756: case SUBFORM: adamc@756: uw_error(ctx, FATAL, "Tried to read a subform form input as files"); adamc@759: case SUBFORMS: adamc@759: uw_error(ctx, FATAL, "Tried to read a subforms form input as files"); adamc@759: case ENTRY: adamc@759: uw_error(ctx, FATAL, "Tried to read an entry form input as files"); adamc@737: default: adamc@737: uw_error(ctx, FATAL, "Impossible input kind"); adamc@737: } adamc@190: } adamc@190: adamc@756: void uw_enter_subform(uw_context ctx, int n) { adamc@756: if (n < 0) adamc@756: uw_error(ctx, FATAL, "Negative subform index %d", n); adamc@1094: if (n >= ctx->app->inputs_len) adamc@756: uw_error(ctx, FATAL, "Out-of-bounds subform index %d", n); adamc@756: adamc@756: switch (INP(ctx)[n].kind) { adamc@756: case UNSET: adamc@756: uw_error(ctx, FATAL, "Missing subform"); adamc@756: case FIL: adamc@756: uw_error(ctx, FATAL, "Tried to read a file form input as subform"); adamc@756: case NORMAL: adamc@756: uw_error(ctx, FATAL, "Tried to read a normal form input as subform"); adamc@759: case SUBFORMS: adamc@759: uw_error(ctx, FATAL, "Tried to read a subforms form input as subform"); adamc@759: case ENTRY: adamc@759: uw_error(ctx, FATAL, "Tried to read an entry form input as subform"); adamc@756: case SUBFORM: adamc@759: INP(ctx)[n].data.subform.parent = ctx->cur_container; adamc@760: ctx->cur_container = &INP(ctx)[n]; adamc@756: return; adamc@756: default: adamc@756: uw_error(ctx, FATAL, "Impossible input kind"); adamc@756: } adamc@756: } adamc@756: adamc@756: void uw_leave_subform(uw_context ctx) { adamc@756: input *tmp; adamc@756: adamc@759: if (ctx->cur_container == NULL) adamc@756: uw_error(ctx, FATAL, "Unmatched uw_leave_subform"); adamc@756: adamc@759: tmp = ctx->cur_container; adamc@759: ctx->cur_container = tmp->data.subform.parent; adamc@759: tmp->data.subform.parent = NULL; adamc@759: } adamc@759: adamc@759: int uw_enter_subforms(uw_context ctx, int n) { adamc@759: input *inps; adamc@759: adamc@759: if (n < 0) adamc@759: uw_error(ctx, FATAL, "Negative subforms index %d", n); adamc@1094: if (n >= ctx->app->inputs_len) adamc@759: uw_error(ctx, FATAL, "Out-of-bounds subforms index %d", n); adamc@759: adamc@759: switch (INP(ctx)[n].kind) { adamc@759: case UNSET: adamc@759: uw_error(ctx, FATAL, "Missing subforms"); adamc@759: case FIL: adamc@759: uw_error(ctx, FATAL, "Tried to read a file form input as subforms"); adamc@759: case NORMAL: adamc@759: uw_error(ctx, FATAL, "Tried to read a normal form input %p as subforms", &INP(ctx)[n]); adamc@759: case SUBFORM: adamc@759: uw_error(ctx, FATAL, "Tried to read a subform form input as subforms"); adamc@759: case ENTRY: adamc@759: uw_error(ctx, FATAL, "Tried to read an entry form input as subforms"); adamc@759: case SUBFORMS: adamc@759: inps = INP(ctx)[n].data.subforms.entries; adamc@759: if (inps) { adamc@759: INP(ctx)[n].data.subforms.parent = ctx->cur_container; adamc@759: ctx->cur_container = INP(ctx)[n].data.subforms.entries; adamc@759: return 1; adamc@759: } else adamc@759: return 0; adamc@759: default: adamc@759: uw_error(ctx, FATAL, "Impossible input kind"); adamc@759: } adamc@759: } adamc@759: adamc@759: int uw_next_entry(uw_context ctx) { adamc@759: if (ctx->cur_container == NULL) adamc@759: uw_error(ctx, FATAL, "uw_next_entry(NULL)"); adamc@759: adamc@759: switch (ctx->cur_container->kind) { adamc@759: case UNSET: adamc@759: uw_error(ctx, FATAL, "Missing entry"); adamc@759: case FIL: adamc@759: uw_error(ctx, FATAL, "Tried to read a file form input as entry"); adamc@759: case NORMAL: adamc@759: uw_error(ctx, FATAL, "Tried to read a normal form input as entry"); adamc@759: case SUBFORM: adamc@759: uw_error(ctx, FATAL, "Tried to read a subform form input as entry"); adamc@759: case SUBFORMS: adamc@759: uw_error(ctx, FATAL, "Tried to read a subforms form input as entry"); adamc@759: case ENTRY: adamc@759: if (ctx->cur_container->data.entry.next) { adamc@759: ctx->cur_container = ctx->cur_container->data.entry.next; adamc@759: return 1; adamc@759: } else { adamc@759: ctx->cur_container = ctx->cur_container->data.entry.parent->data.subforms.parent; adamc@759: return 0; adamc@759: } adamc@759: default: adamc@759: uw_error(ctx, FATAL, "Impossible input kind"); adamc@759: } adamc@756: } adamc@756: adamc@643: void uw_set_script_header(uw_context ctx, const char *s) { adamc@643: ctx->script_header = s; adamc@643: } adamc@643: adamc@1094: const char *uw_get_url_prefix(uw_context ctx) { adamc@1094: return ctx->app->url_prefix; adamc@667: } adamc@667: adamc@693: void uw_set_needs_push(uw_context ctx, int n) { adamc@693: ctx->needs_push = n; adamc@693: } adamc@693: adamc@736: void uw_set_needs_sig(uw_context ctx, int n) { adamc@736: ctx->needs_sig = n; adamc@736: } adamc@736: adam@1936: void uw_set_could_write_db(uw_context ctx, int n) { adam@1936: ctx->could_write_db = n; adam@1936: } adam@1936: adamc@667: adam@1369: static void uw_buffer_check_ctx(uw_context ctx, const char *kind, uw_buffer *b, size_t extra, const char *desc) { adamc@666: if (b->back - b->front < extra) { adamc@666: size_t desired = b->front - b->start + extra, next; adamc@136: char *new_heap; adamc@136: adamc@666: next = b->back - b->start; adamc@317: if (next == 0) adamc@317: next = 1; adamc@317: for (; next < desired; next *= 2); adamc@136: adamc@1134: if (next > b->max) { adamc@1113: if (desired <= b->max) adamc@1113: next = desired; adamc@1113: else adamc@1113: uw_error(ctx, FATAL, "Memory limit exceeded (%s)", kind); adamc@1134: } adamc@1113: adamc@666: new_heap = realloc(b->start, next); adamc@666: b->front = new_heap + (b->front - b->start); adamc@666: b->back = new_heap + next; adamc@136: adamc@667: if (new_heap != b->start) { adamc@666: b->start = new_heap; adam@1389: uw_error(ctx, UNLIMITED_RETRY, "Couldn't allocate new %s contiguously; increasing size to %llu", desc, (unsigned long long)next); adamc@136: } adamc@136: adamc@666: b->start = new_heap; adamc@136: } adamc@136: } adamc@136: adamc@832: void uw_check_heap(uw_context ctx, size_t extra) { adam@1369: uw_buffer_check_ctx(ctx, "heap", &ctx->heap, extra, "heap chunk"); adamc@666: } adamc@666: adamc@832: char *uw_heap_front(uw_context ctx) { adamc@832: return ctx->heap.front; adamc@832: } adamc@832: adamc@832: void uw_set_heap_front(uw_context ctx, char *fr) { adamc@832: ctx->heap.front = fr; adamc@832: } adamc@832: adam@1782: void uw_begin_initializing(uw_context ctx) { adam@1782: ctx->amInitializing = 1; adam@1782: } adam@1782: adam@1782: void uw_end_initializing(uw_context ctx) { adam@1782: ctx->amInitializing = 0; adam@1782: } adam@1782: adamc@311: void *uw_malloc(uw_context ctx, size_t len) { adamc@136: void *result; adamc@136: adam@1782: if (ctx->amInitializing) { adam@1782: result = malloc(len); adam@1782: adam@1782: if (result) adam@1782: return result; adam@1782: else adam@1782: uw_error(ctx, FATAL, "uw_malloc: malloc() returns 0"); adam@1782: } else { adam@1782: uw_check_heap(ctx, len); adam@1782: adam@1782: result = ctx->heap.front; adam@1782: ctx->heap.front += len; adam@1782: return result; adam@1782: } adamc@117: } adamc@117: adamc@323: void uw_begin_region(uw_context ctx) { adamc@666: regions *r = (regions *) ctx->heap.front; adamc@323: adamc@323: uw_check_heap(ctx, sizeof(regions)); adamc@323: adamc@666: ctx->heap.front += sizeof(regions); adamc@323: adamc@323: r->next = ctx->regions; adamc@323: ctx->regions = r; adamc@323: } adamc@323: adamc@323: void uw_end_region(uw_context ctx) { adamc@323: regions *r = ctx->regions; adamc@323: adamc@323: if (r == NULL) adamc@323: uw_error(ctx, FATAL, "Region stack underflow"); adamc@323: adamc@666: ctx->heap.front = (char *) r; adamc@323: ctx->regions = r->next; adamc@323: } adamc@323: adamc@324: void uw_memstats(uw_context ctx) { adam@1369: printf("Headers: %lu/%lu\n", (unsigned long)uw_buffer_used(&ctx->outHeaders), (unsigned long)uw_buffer_avail(&ctx->outHeaders)); adam@1369: printf("Script: %lu/%lu\n", (unsigned long)uw_buffer_used(&ctx->script), (unsigned long)uw_buffer_avail(&ctx->script)); adam@1369: printf("Page: %lu/%lu\n", (unsigned long)uw_buffer_used(&ctx->page), (unsigned long)uw_buffer_avail(&ctx->page)); adam@1369: printf("Heap: %lu/%lu\n", (unsigned long)uw_buffer_used(&ctx->heap), (unsigned long)uw_buffer_avail(&ctx->heap)); adamc@324: } adamc@324: adam@1915: int uw_pagelen(uw_context ctx) { adam@1915: return ctx->page.front - ctx->page.start; adam@1915: } adam@1915: adamc@311: int uw_send(uw_context ctx, int sock) { adam@1949: size_t target_length = (ctx->outHeaders.front - ctx->outHeaders.start) + 2 + (ctx->page.front - ctx->page.start); adam@1949: adam@1949: if (ctx->output_buffer_size < target_length) { adam@1949: do { adam@1949: ctx->output_buffer_size *= 2; adam@1949: } while (ctx->output_buffer_size < target_length); adam@1949: ctx->output_buffer = realloc(ctx->output_buffer, ctx->output_buffer_size); adam@1949: } adam@1949: adam@1949: memcpy(ctx->output_buffer, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start); adam@1949: memcpy(ctx->output_buffer + (ctx->outHeaders.front - ctx->outHeaders.start), "\r\n", 2); adam@1949: memcpy(ctx->output_buffer + (ctx->outHeaders.front - ctx->outHeaders.start) + 2, ctx->page.start, ctx->page.front - ctx->page.start); adam@1949: adam@1949: return uw_really_send(sock, ctx->output_buffer, target_length); adamc@462: } adamc@462: adamc@856: int uw_print(uw_context ctx, int fd) { adamc@856: int n = uw_really_write(fd, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start); adamc@856: adamc@856: if (n < 0) adamc@856: return n; adamc@856: adamc@856: n = uw_really_write(fd, "\r\n", 2); adamc@856: adamc@856: if (n < 0) adamc@856: return n; adamc@856: adamc@856: return uw_really_write(fd, ctx->page.start, ctx->page.front - ctx->page.start); adamc@856: } adamc@856: adamc@860: int uw_output(uw_context ctx, int (*output)(void *data, char *buf, size_t len), void *data) { adamc@860: int n = output(data, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start); adamc@860: adamc@860: if (n < 0) adamc@860: return n; adamc@860: adamc@860: n = output(data, "\r\n", 2); adamc@860: adamc@860: if (n < 0) adamc@860: return n; adamc@860: adamc@860: return output(data, ctx->page.start, ctx->page.front - ctx->page.start); adamc@860: } adamc@860: adamc@462: static void uw_check_headers(uw_context ctx, size_t extra) { adam@1369: ctx_uw_buffer_check(ctx, "headers", &ctx->outHeaders, extra); adamc@462: } adamc@462: adamc@462: void uw_write_header(uw_context ctx, uw_Basis_string s) { adamc@462: int len = strlen(s); adamc@462: adamc@462: uw_check_headers(ctx, len + 1); adamc@666: strcpy(ctx->outHeaders.front, s); adamc@666: ctx->outHeaders.front += len; adamc@562: } adamc@562: adam@1934: int uw_has_contentLength(uw_context ctx) { adam@1934: return strstr(ctx->outHeaders.start, "Content-length: ") != NULL; adam@1934: } adam@1934: adamc@1110: void uw_clear_headers(uw_context ctx) { adam@1369: uw_buffer_reset(&ctx->outHeaders); adamc@1110: } adamc@1110: adam@1932: void uw_Basis_clear_page(uw_context ctx) { adam@1932: uw_buffer_reset(&ctx->page); adam@1932: } adam@1932: adamc@562: static void uw_check_script(uw_context ctx, size_t extra) { adam@1369: ctx_uw_buffer_check(ctx, "script", &ctx->script, extra); adamc@562: } adamc@562: adamc@562: void uw_write_script(uw_context ctx, uw_Basis_string s) { adamc@562: int len = strlen(s); adamc@562: adamc@562: uw_check_script(ctx, len + 1); adamc@666: strcpy(ctx->script.front, s); adamc@666: ctx->script.front += len; adamc@562: } adamc@562: adam@1446: const char *uw_get_real_script(uw_context ctx) { adam@1671: if (strstr(ctx->outHeaders.start, "Set-Cookie: ")) { adam@1671: uw_write_script(ctx, "sig=\""); adam@1671: uw_write_script(ctx, ctx->app->cookie_sig(ctx)); adam@1671: uw_write_script(ctx, "\";"); adam@1671: } adam@1671: adam@1446: return ctx->script.start; adam@1446: } adam@1446: adamc@695: uw_Basis_string uw_Basis_maybe_onload(uw_context ctx, uw_Basis_string s) { adamc@695: if (s[0] == 0) adamc@695: return ""; adamc@695: else { adamc@695: char *r = uw_malloc(ctx, 11 + strlen(s)); adamc@695: sprintf(r, " onload='%s'", s); adamc@695: return r; adamc@695: } adamc@695: } adamc@695: adamc@1042: uw_Basis_string uw_Basis_maybe_onunload(uw_context ctx, uw_Basis_string s) { adamc@1042: if (ctx->script_header[0] == 0) adamc@1042: return ""; adamc@1042: else { vshabanoff@1763: char *r = uw_malloc(ctx, 37 + strlen(s)); vshabanoff@1763: sprintf(r, " onunload='unloading=true;%s;unload()'", s); adamc@1042: return r; adamc@1042: } adamc@1042: } adamc@1042: adamc@694: const char *uw_Basis_get_settings(uw_context ctx, uw_unit u) { adamc@736: if (ctx->client == NULL) { adamc@736: if (ctx->needs_sig) { adamc@1094: char *sig = ctx->app->cookie_sig(ctx); adamc@736: char *r = uw_malloc(ctx, strlen(sig) + 8); adamc@736: sprintf(r, "sig=\"%s\";", sig); adamc@736: return r; adamc@736: } adamc@736: else adamc@736: return ""; adamc@736: } else { adamc@1094: char *sig = ctx->needs_sig ? ctx->app->cookie_sig(ctx) : ""; adamc@1094: char *r = uw_malloc(ctx, 59 + 3 * INTS_MAX + strlen(ctx->app->url_prefix) adamc@736: + (ctx->needs_sig ? strlen(sig) + 7 : 0)); adam@1552: sprintf(r, "isPost=%s;client_id=%u;client_pass=%d;url_prefix=\"%s\";timeout=%d;%s%s%slistener();", adam@1552: (ctx->isPost ? "true" : "false"), adamc@682: ctx->client->id, adamc@682: ctx->client->pass, adamc@1094: ctx->app->url_prefix, adamc@1094: ctx->app->timeout, adamc@736: ctx->needs_sig ? "sig=\"" : "", adamc@736: sig, adamc@736: ctx->needs_sig ? "\";" : ""); adamc@679: return r; adamc@668: } adamc@667: } adamc@667: adamc@574: uw_Basis_string uw_Basis_jsifyString(uw_context ctx, uw_Basis_string s) { adamc@574: char *r, *s2; adamc@574: adam@1446: uw_check_heap(ctx, strlen(s) * 4 + 3); adamc@574: adamc@666: r = s2 = ctx->heap.front; adamc@574: *s2++ = '"'; adamc@574: adamc@574: for (; *s; s++) { adam@1286: unsigned char c = *s; adamc@574: adamc@574: switch (c) { adamc@574: case '"': adamc@574: strcpy(s2, "\\\""); adamc@574: s2 += 2; adamc@574: break; adamc@1027: case '\'': adamc@1027: strcpy(s2, "\\047"); adamc@1027: s2 += 4; adamc@1027: break; adamc@574: case '\\': adamc@574: strcpy(s2, "\\\\"); adamc@574: s2 += 2; adamc@574: break; adam@1457: case '<': adam@1457: strcpy(s2, "\\074"); adam@1457: s2 += 4; adam@1457: break; adamc@574: default: adam@1286: if (isprint((int)c) || c >= 128) adamc@574: *s2++ = c; adamc@574: else { adam@1429: sprintf(s2, "\\%03o", c); adamc@574: s2 += 4; adamc@574: } adamc@574: } adamc@574: } adamc@574: adamc@574: strcpy(s2, "\""); adamc@910: ctx->heap.front = s2 + 2; adamc@574: return r; adamc@574: } adamc@574: adam@1286: uw_Basis_string uw_Basis_jsifyChar(uw_context ctx, uw_Basis_char c1) { adam@1286: unsigned char c = c1; adamc@1025: char *r, *s2; adamc@1025: adam@1446: uw_check_heap(ctx, 7); adamc@1025: adamc@1025: r = s2 = ctx->heap.front; adamc@1025: *s2++ = '"'; adamc@1025: adamc@1025: switch (c) { adamc@1025: case '"': adamc@1025: strcpy(s2, "\\\""); adamc@1025: s2 += 2; adamc@1025: break; adamc@1027: case '\'': adamc@1027: strcpy(s2, "\\047"); adamc@1027: s2 += 4; adamc@1027: break; adamc@1025: case '\\': adamc@1025: strcpy(s2, "\\\\"); adamc@1025: s2 += 2; adamc@1025: break; adam@1457: case '<': adam@1457: strcpy(s2, "\\074"); adam@1457: s2 += 4; adam@1457: break; adamc@1025: default: adam@1286: if (isprint((int)c) || c >= 128) adamc@1025: *s2++ = c; adamc@1025: else { adam@1429: sprintf(s2, "\\%03o", (unsigned char)c); adamc@1025: s2 += 4; adamc@1025: } adamc@1025: } adamc@1025: adamc@1025: strcpy(s2, "\""); adamc@1025: ctx->heap.front = s2 + 2; adamc@1025: return r; adamc@1025: } adamc@1025: adamc@574: uw_Basis_string uw_Basis_jsifyString_ws(uw_context ctx, uw_Basis_string s) { adamc@574: char *r, *s2; adamc@574: adam@1446: uw_check_script(ctx, strlen(s) * 4 + 3); adamc@574: adamc@666: r = s2 = ctx->script.front; adamc@574: *s2++ = '"'; adamc@574: adamc@574: for (; *s; s++) { adam@1286: unsigned char c = *s; adamc@574: adamc@574: switch (c) { adamc@577: case '\'': adamc@1027: strcpy(s2, "\\"); adamc@574: s2 += 2; adamc@574: break; adamc@574: case '\\': adamc@574: strcpy(s2, "\\\\"); adamc@574: s2 += 2; adamc@574: break; adam@1457: case '<': adam@1457: strcpy(s2, "\\074"); adam@1457: s2 += 4; adam@1457: break; adamc@574: default: adam@1286: if (isprint((int)c) || c >= 128) adamc@574: *s2++ = c; adamc@574: else { adam@1429: sprintf(s2, "\\%03o", c); adamc@574: s2 += 4; adamc@574: } adamc@574: } adamc@574: } adamc@574: adamc@574: strcpy(s2, "\""); adamc@666: ctx->script.front = s2 + 1; adamc@574: return r; adamc@574: } adamc@574: adamc@682: char *uw_Basis_jsifyChannel(uw_context ctx, uw_Basis_channel chn) { adamc@682: if (ctx->client == NULL || chn.cli != ctx->client->id) adamc@682: return "null"; adamc@682: else { adamc@682: int len; adamc@682: char *r; adamc@682: adamc@682: uw_check_heap(ctx, INTS_MAX + 1); adamc@682: r = ctx->heap.front; adamc@682: sprintf(r, "%u%n", chn.chn, &len); adamc@682: ctx->heap.front += len+1; adamc@682: return r; adamc@682: } adamc@682: } adamc@682: adam@1446: uw_Basis_source uw_Basis_new_client_source(uw_context ctx, uw_Basis_string s) { adamc@577: int len; adamc@577: size_t s_len = strlen(s); adamc@562: adam@1446: uw_check_script(ctx, 15 + 2 * INTS_MAX + s_len); adam@1446: sprintf(ctx->script.front, "s%d_%llu=sc(exec(%n", ctx->id, ctx->source_count, &len); adamc@666: ctx->script.front += len; adamc@666: strcpy(ctx->script.front, s); adamc@666: ctx->script.front += s_len; adamc@970: strcpy(ctx->script.front, "));"); adamc@972: ctx->script.front += 3; adamc@562: adam@1446: uw_Basis_source r = {ctx->id, ctx->source_count++}; adam@1446: return r; adam@1446: } adam@1446: adam@1446: uw_unit uw_Basis_set_client_source(uw_context ctx, uw_Basis_source src, uw_Basis_string s) { adamc@577: int len; adamc@577: size_t s_len = strlen(s); adamc@577: adam@1446: uw_check_script(ctx, 15 + 2 * INTS_MAX + s_len); adam@1446: sprintf(ctx->script.front, "sv(s%d_%llu,exec(%n", src.context, src.source, &len); adamc@666: ctx->script.front += len; adamc@666: strcpy(ctx->script.front, s); adamc@666: ctx->script.front += s_len; adamc@970: strcpy(ctx->script.front, "));"); adamc@972: ctx->script.front += 3; adamc@577: adamc@577: return uw_unit_v; adamc@577: } adamc@577: adamc@311: static void uw_check(uw_context ctx, size_t extra) { adam@1369: ctx_uw_buffer_check(ctx, "page", &ctx->page, extra); adamc@117: } adamc@117: adamc@311: static void uw_writec_unsafe(uw_context ctx, char c) { adamc@666: *(ctx->page.front)++ = c; adam@1413: *ctx->page.front = 0; adamc@117: } adamc@117: adamc@311: void uw_writec(uw_context ctx, char c) { adam@1413: uw_check(ctx, 2); adamc@311: uw_writec_unsafe(ctx, c); adamc@117: } adamc@117: adamc@311: static void uw_write_unsafe(uw_context ctx, const char* s) { adamc@117: int len = strlen(s); adamc@666: memcpy(ctx->page.front, s, len); adamc@666: ctx->page.front += len; adamc@117: } adamc@117: adamc@311: void uw_write(uw_context ctx, const char* s) { adamc@311: uw_check(ctx, strlen(s) + 1); adamc@311: uw_write_unsafe(ctx, s); adamc@666: *ctx->page.front = 0; adamc@102: } adamc@106: adamc@311: char *uw_Basis_attrifyInt(uw_context ctx, uw_Basis_int n) { adamc@136: char *result; adamc@136: int len; adamc@311: uw_check_heap(ctx, INTS_MAX); adamc@666: result = ctx->heap.front; adamc@276: sprintf(result, "%lld%n", n, &len); adamc@666: ctx->heap.front += len+1; adamc@136: return result; adamc@106: } adamc@106: adamc@311: char *uw_Basis_attrifyFloat(uw_context ctx, uw_Basis_float n) { adamc@136: char *result; adamc@136: int len; adamc@311: uw_check_heap(ctx, FLOATS_MAX); adamc@666: result = ctx->heap.front; adam@1920: sprintf(result, "%.16g%n", n, &len); adamc@666: ctx->heap.front += len+1; adamc@136: return result; adamc@106: } adamc@106: adamc@311: char *uw_Basis_attrifyString(uw_context ctx, uw_Basis_string s) { adamc@136: int len = strlen(s); adamc@136: char *result, *p; adamc@311: uw_check_heap(ctx, len * 6 + 1); adamc@136: adamc@666: result = p = ctx->heap.front; adamc@136: adamc@136: for (; *s; s++) { adamc@1054: unsigned char c = *s; adamc@136: adamc@136: if (c == '"') { adamc@136: strcpy(p, """); adamc@136: p += 6; adamc@136: } else if (c == '&') { adamc@136: strcpy(p, "&"); adamc@136: p += 5; adamc@136: } adamc@1059: else adamc@136: *p++ = c; adamc@136: } adamc@136: adamc@137: *p++ = 0; adamc@666: ctx->heap.front = p; adamc@136: return result; adamc@106: } adamc@106: adamc@1011: char *uw_Basis_attrifyChar(uw_context ctx, uw_Basis_char c) { adamc@1011: char *result, *p; adamc@1011: uw_check_heap(ctx, 7); adamc@1011: adamc@1011: result = p = ctx->heap.front; adamc@1011: adamc@1011: if (c == '"') { adamc@1011: strcpy(p, """); adamc@1011: p += 6; adamc@1011: } else if (c == '&') { adamc@1011: strcpy(p, "&"); adamc@1011: p += 5; adamc@1011: } adamc@1059: else adamc@1011: *p++ = c; adamc@1011: adamc@1011: *p++ = 0; adamc@1011: ctx->heap.front = p; adamc@1011: return result; adamc@1011: } adamc@1011: adamc@721: char *uw_Basis_attrifyCss_class(uw_context ctx, uw_Basis_css_class s) { adamc@721: return s; adamc@721: } adamc@721: adamc@311: static void uw_Basis_attrifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) { adamc@117: int len; adamc@117: adamc@666: sprintf(ctx->page.front, "%lld%n", n, &len); adamc@666: ctx->page.front += len; adamc@106: } adamc@106: adamc@428: uw_unit uw_Basis_attrifyInt_w(uw_context ctx, uw_Basis_int n) { adamc@311: uw_check(ctx, INTS_MAX); adamc@311: uw_Basis_attrifyInt_w_unsafe(ctx, n); adamc@428: adamc@428: return uw_unit_v; adamc@106: } adamc@106: adamc@428: uw_unit uw_Basis_attrifyFloat_w(uw_context ctx, uw_Basis_float n) { adamc@117: int len; adamc@117: adamc@311: uw_check(ctx, FLOATS_MAX); adamc@666: sprintf(ctx->page.front, "%g%n", n, &len); adamc@666: ctx->page.front += len; adamc@428: adamc@428: return uw_unit_v; adamc@117: } adamc@117: adamc@428: uw_unit uw_Basis_attrifyString_w(uw_context ctx, uw_Basis_string s) { adamc@311: uw_check(ctx, strlen(s) * 6); adamc@117: adamc@106: for (; *s; s++) { adamc@1054: unsigned char c = *s; adamc@106: adamc@106: if (c == '"') adamc@311: uw_write_unsafe(ctx, """); adamc@136: else if (c == '&') adamc@311: uw_write_unsafe(ctx, "&"); adamc@1059: else adamc@311: uw_writec_unsafe(ctx, c); adamc@106: } adamc@428: adamc@428: return uw_unit_v; adamc@106: } adamc@120: adamc@1011: uw_unit uw_Basis_attrifyChar_w(uw_context ctx, uw_Basis_char c) { adamc@1011: uw_check(ctx, 6); adamc@1011: adamc@1011: if (c == '"') adamc@1011: uw_write_unsafe(ctx, """); adamc@1011: else if (c == '&') adamc@1011: uw_write_unsafe(ctx, "&"); adamc@1059: else adamc@1011: uw_writec_unsafe(ctx, c); adamc@1011: adamc@1011: return uw_unit_v; adamc@1011: } adamc@1011: adamc@120: adamc@311: char *uw_Basis_urlifyInt(uw_context ctx, uw_Basis_int n) { adamc@137: int len; adamc@137: char *r; adamc@137: adamc@311: uw_check_heap(ctx, INTS_MAX); adamc@666: r = ctx->heap.front; adamc@276: sprintf(r, "%lld%n", n, &len); adamc@666: ctx->heap.front += len+1; adamc@137: return r; adamc@120: } adamc@120: adamc@682: char *uw_Basis_urlifyChannel(uw_context ctx, uw_Basis_channel chn) { adamc@682: if (ctx->client == NULL || chn.cli != ctx->client->id) adamc@682: return ""; adamc@682: else { adamc@682: int len; adamc@682: char *r; adamc@679: adamc@682: uw_check_heap(ctx, INTS_MAX + 1); adamc@682: r = ctx->heap.front; adamc@682: sprintf(r, "%u%n", chn.chn, &len); adamc@682: ctx->heap.front += len+1; adamc@682: return r; adamc@682: } adamc@679: } adamc@679: adamc@311: char *uw_Basis_urlifyFloat(uw_context ctx, uw_Basis_float n) { adamc@137: int len; adamc@137: char *r; adamc@137: adamc@311: uw_check_heap(ctx, FLOATS_MAX); adamc@666: r = ctx->heap.front; adamc@137: sprintf(r, "%g%n", n, &len); adamc@666: ctx->heap.front += len+1; adamc@137: return r; adamc@120: } adamc@120: adamc@311: char *uw_Basis_urlifyString(uw_context ctx, uw_Basis_string s) { adamc@137: char *r, *p; adamc@137: adamc@925: if (s[0] == '\0') adamc@925: return "_"; adamc@925: adamc@925: uw_check_heap(ctx, strlen(s) * 3 + 1 + !!(s[0] == '_')); adamc@925: adamc@925: r = p = ctx->heap.front; adamc@925: if (s[0] == '_') adamc@925: *p++ = '_'; adamc@925: adamc@925: for (; *s; s++) { adamc@1087: unsigned char c = *s; adamc@137: adamc@137: if (c == ' ') adamc@137: *p++ = '+'; adamc@137: else if (isalnum(c)) adamc@137: *p++ = c; adamc@137: else { adamc@1259: sprintf(p, ".%02X", c); adamc@137: p += 3; adamc@137: } adamc@137: } adamc@137: adamc@137: *p++ = 0; adamc@666: ctx->heap.front = p; adamc@137: return r; adamc@120: } adamc@120: adamc@311: char *uw_Basis_urlifyBool(uw_context ctx, uw_Basis_bool b) { adamc@311: if (b == uw_Basis_False) adamc@186: return "0"; adamc@186: else adamc@186: return "1"; adamc@186: } adamc@186: adam@1620: char *uw_Basis_urlifySource(uw_context ctx, uw_Basis_source src) { adam@1620: char *r; adam@1620: int len; adam@1620: uw_check_heap(ctx, 2 * INTS_MAX + 2); adam@1620: r = ctx->heap.front; adam@1620: sprintf(r, "%d/%llu%n", src.context, src.source, &len); adam@1620: ctx->heap.front += len+1; adam@1620: return r; adam@1620: } adam@1620: adamc@311: static void uw_Basis_urlifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) { adamc@120: int len; adamc@120: adamc@666: sprintf(ctx->page.front, "%lld%n", n, &len); adamc@666: ctx->page.front += len; adamc@120: } adamc@120: adamc@428: uw_unit uw_Basis_urlifyInt_w(uw_context ctx, uw_Basis_int n) { adamc@311: uw_check(ctx, INTS_MAX); adamc@311: uw_Basis_urlifyInt_w_unsafe(ctx, n); adamc@428: adamc@428: return uw_unit_v; adamc@120: } adamc@120: adamc@682: uw_unit uw_Basis_urlifyChannel_w(uw_context ctx, uw_Basis_channel chn) { adamc@682: if (ctx->client != NULL && chn.cli == ctx->client->id) { adamc@682: int len; adamc@682: adamc@682: uw_check(ctx, INTS_MAX + 1); adamc@682: sprintf(ctx->page.front, "%u%n", chn.chn, &len); adamc@682: ctx->page.front += len; adamc@682: } adamc@682: adamc@679: return uw_unit_v; adamc@679: } adamc@679: adamc@428: uw_unit uw_Basis_urlifyFloat_w(uw_context ctx, uw_Basis_float n) { adamc@120: int len; adamc@120: adamc@311: uw_check(ctx, FLOATS_MAX); adamc@666: sprintf(ctx->page.front, "%g%n", n, &len); adamc@666: ctx->page.front += len; adamc@428: adamc@428: return uw_unit_v; adamc@120: } adamc@120: adamc@488: uw_Basis_string uw_Basis_urlifyTime(uw_context ctx, uw_Basis_time t) { adamc@1376: return uw_Basis_urlifyInt(ctx, (uw_Basis_int)t.seconds * 1000000 + t.microseconds); adamc@488: } adamc@488: adam@1404: uw_unit uw_Basis_urlifyTime_w(uw_context ctx, uw_Basis_time t) { adam@1404: return uw_Basis_urlifyInt_w(ctx, (uw_Basis_int)t.seconds * 1000000 + t.microseconds); adam@1404: } adam@1404: adamc@428: uw_unit uw_Basis_urlifyString_w(uw_context ctx, uw_Basis_string s) { adamc@925: if (s[0] == '\0') { adamc@925: uw_check(ctx, 1); adamc@925: uw_writec_unsafe(ctx, '_'); adamc@925: return uw_unit_v; adamc@925: } adamc@925: adamc@925: uw_check(ctx, strlen(s) * 3 + !!(s[0] == '_')); adamc@925: adamc@925: if (s[0] == '_') adamc@925: uw_writec_unsafe(ctx, '_'); adamc@120: adamc@120: for (; *s; s++) { adamc@1087: unsigned char c = *s; adamc@120: adamc@120: if (c == ' ') adamc@311: uw_writec_unsafe(ctx, '+'); adamc@120: else if (isalnum(c)) adamc@311: uw_writec_unsafe(ctx, c); adamc@120: else { adamc@1259: sprintf(ctx->page.front, ".%02X", c); adamc@666: ctx->page.front += 3; adamc@120: } adamc@120: } adamc@428: adamc@428: return uw_unit_v; adamc@120: } adamc@120: adamc@428: uw_unit uw_Basis_urlifyBool_w(uw_context ctx, uw_Basis_bool b) { adamc@311: if (b == uw_Basis_False) adamc@311: uw_writec(ctx, '0'); adamc@186: else adamc@311: uw_writec(ctx, '1'); adamc@428: adamc@428: return uw_unit_v; adamc@186: } adamc@186: adam@1620: uw_unit uw_Basis_urlifySource_w(uw_context ctx, uw_Basis_source src) { adam@1620: int len; adam@1620: adam@1620: uw_check(ctx, 2 * INTS_MAX + 2); adam@1620: sprintf(ctx->page.front, "%d/%llu%n", src.context, src.source, &len); adam@1620: ctx->page.front += len; adam@1620: adam@1620: return uw_unit_v; adam@1620: } adam@1620: adamc@120: adamc@311: static char *uw_unurlify_advance(char *s) { adamc@144: char *new_s = strchr(s, '/'); adamc@120: adamc@120: if (new_s) adamc@120: *new_s++ = 0; adamc@120: else adamc@144: new_s = strchr(s, 0); adamc@144: adamc@144: return new_s; adamc@144: } adamc@144: adamc@311: uw_Basis_int uw_Basis_unurlifyInt(uw_context ctx, char **s) { adamc@311: char *new_s = uw_unurlify_advance(*s); adamc@311: uw_Basis_int r; adamc@120: adamc@276: r = atoll(*s); adamc@120: *s = new_s; adamc@120: return r; adamc@120: } adamc@120: adamc@311: uw_Basis_float uw_Basis_unurlifyFloat(uw_context ctx, char **s) { adamc@311: char *new_s = uw_unurlify_advance(*s); adamc@311: uw_Basis_float r; adamc@120: adamc@120: r = atof(*s); adamc@120: *s = new_s; adamc@120: return r; adamc@120: } adamc@120: adamc@488: uw_Basis_time uw_Basis_unurlifyTime(uw_context ctx, char **s) { adam@1365: uw_Basis_int n = uw_Basis_unurlifyInt(ctx, s); adam@1365: uw_Basis_time r = {n / 1000000, n % 1000000}; adam@1365: return r; adamc@488: } adamc@488: adamc@1023: static uw_Basis_string uw_unurlifyString_to(int fromClient, uw_context ctx, char *r, char *s) { adamc@927: char *s1, *s2 = s; adamc@144: int n; adamc@136: adamc@1023: if (!fromClient) { adamc@1023: if (*s2 == '_') adamc@1023: ++s2; adamc@1259: else if ((s2[0] == '%' || s2[0] == '.') && s2[1] == '5' && (s2[2] == 'f' || s2[2] == 'F')) adamc@1023: s2 += 3; adamc@1023: } adamc@925: adamc@927: for (s1 = r; *s2; ++s1, ++s2) { adamc@1087: unsigned char c = *s2; adamc@136: adamc@136: switch (c) { adamc@136: case '+': adamc@136: *s1 = ' '; adamc@136: break; adamc@136: case '%': adamc@169: if (s2[1] == 0) adamc@311: uw_error(ctx, FATAL, "Missing first character of escaped URL byte"); adamc@169: if (s2[2] == 0) adamc@311: uw_error(ctx, FATAL, "Missing second character of escaped URL byte"); adamc@169: if (sscanf(s2+1, "%02X", &n) != 1) adam@1444: uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", uw_Basis_htmlifyString(ctx, s2)); adamc@136: *s1 = n; adamc@136: s2 += 2; adamc@136: break; adamc@1259: case '.': adamc@1259: if (!fromClient) { adamc@1259: if (s2[1] == 0) adamc@1259: uw_error(ctx, FATAL, "Missing first character of escaped URL byte"); adamc@1259: if (s2[2] == 0) adamc@1259: uw_error(ctx, FATAL, "Missing second character of escaped URL byte"); adamc@1259: if (sscanf(s2+1, "%02X", &n) != 1) adam@1444: uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", uw_Basis_htmlifyString(ctx, s2)); adamc@1259: *s1 = n; adamc@1259: s2 += 2; adamc@1259: break; adamc@1259: } adamc@136: default: adamc@136: *s1 = c; adamc@136: } adamc@136: } adamc@136: *s1++ = 0; adamc@144: return s1; adamc@144: } adamc@144: adamc@311: uw_Basis_bool uw_Basis_unurlifyBool(uw_context ctx, char **s) { adamc@311: char *new_s = uw_unurlify_advance(*s); adamc@311: uw_Basis_bool r; adamc@186: adamc@186: if (*s[0] == 0 || !strcmp(*s, "0") || !strcmp(*s, "off")) adamc@311: r = uw_Basis_False; adamc@186: else adamc@311: r = uw_Basis_True; adamc@186: adamc@186: *s = new_s; adamc@186: return r; adamc@186: } adamc@186: adamc@311: uw_Basis_string uw_Basis_unurlifyString(uw_context ctx, char **s) { adamc@311: char *new_s = uw_unurlify_advance(*s); adamc@1134: char *r; adamc@1134: int len; adamc@144: adamc@200: len = strlen(*s); adamc@311: uw_check_heap(ctx, len + 1); adamc@144: adamc@666: r = ctx->heap.front; adamc@1023: ctx->heap.front = uw_unurlifyString_to(0, ctx, ctx->heap.front, *s); adamc@1023: *s = new_s; adamc@1023: return r; adamc@1023: } adamc@1023: adamc@1109: uw_Basis_unit uw_Basis_unurlifyUnit(uw_context ctx, char **s) { adamc@1109: *s = uw_unurlify_advance(*s); adamc@1109: return uw_unit_v; adamc@1109: } adamc@1109: adamc@1023: uw_Basis_string uw_Basis_unurlifyString_fromClient(uw_context ctx, char **s) { adamc@1023: char *new_s = uw_unurlify_advance(*s); adamc@1134: char *r; adamc@1134: int len; adamc@1023: adamc@1023: len = strlen(*s); adamc@1023: uw_check_heap(ctx, len + 1); adamc@1023: adamc@1023: r = ctx->heap.front; adamc@1023: ctx->heap.front = uw_unurlifyString_to(1, ctx, ctx->heap.front, *s); adamc@136: *s = new_s; adamc@136: return r; adamc@120: } adamc@135: adamc@135: adamc@311: char *uw_Basis_htmlifyInt(uw_context ctx, uw_Basis_int n) { adamc@286: int len; adamc@286: char *r; adamc@286: adamc@311: uw_check_heap(ctx, INTS_MAX); adamc@666: r = ctx->heap.front; adamc@286: sprintf(r, "%lld%n", n, &len); adamc@666: ctx->heap.front += len+1; adamc@286: return r; adamc@286: } adamc@286: adamc@428: uw_unit uw_Basis_htmlifyInt_w(uw_context ctx, uw_Basis_int n) { adamc@286: int len; adamc@286: adamc@311: uw_check(ctx, INTS_MAX); adamc@666: sprintf(ctx->page.front, "%lld%n", n, &len); adamc@666: ctx->page.front += len; adamc@428: adamc@428: return uw_unit_v; adamc@286: } adamc@286: adam@1358: char *uw_Basis_htmlifySpecialChar(uw_context ctx, unsigned char ch) { adam@1358: unsigned int n = ch; adam@1358: int len; adam@1358: char *r; adam@1358: adam@1358: uw_check_heap(ctx, INTS_MAX+3); adam@1358: r = ctx->heap.front; adam@1358: sprintf(r, "&#%u;%n", n, &len); adam@1358: ctx->heap.front += len+1; adam@1358: return r; adam@1358: } adam@1358: adam@1358: uw_unit uw_Basis_htmlifySpecialChar_w(uw_context ctx, unsigned char ch) { adam@1358: unsigned int n = ch; adam@1358: int len; adam@1358: adam@1358: uw_check(ctx, INTS_MAX+3); adam@1358: sprintf(ctx->page.front, "&#%u;%n", n, &len); adam@1358: ctx->page.front += len; adam@1358: return uw_unit_v; adam@1358: } adam@1358: adamc@311: char *uw_Basis_htmlifyFloat(uw_context ctx, uw_Basis_float n) { adamc@286: int len; adamc@286: char *r; adamc@286: adamc@311: uw_check_heap(ctx, FLOATS_MAX); adamc@666: r = ctx->heap.front; adamc@286: sprintf(r, "%g%n", n, &len); adamc@666: ctx->heap.front += len+1; adamc@286: return r; adamc@286: } adamc@286: adamc@428: uw_unit uw_Basis_htmlifyFloat_w(uw_context ctx, uw_Basis_float n) { adamc@286: int len; adamc@286: adamc@311: uw_check(ctx, FLOATS_MAX); adamc@666: sprintf(ctx->page.front, "%g%n", n, &len); adamc@666: ctx->page.front += len; adamc@428: adamc@428: return uw_unit_v; adamc@286: } adamc@286: adam@1365: char *uw_Basis_jsifyTime(uw_context ctx, uw_Basis_time t) { adam@1360: int len; adam@1360: char *r; adam@1360: adam@1360: uw_check_heap(ctx, INTS_MAX); adam@1360: r = ctx->heap.front; adamc@1377: sprintf(r, "%lld%n", (uw_Basis_int)t.seconds * 1000000 + t.microseconds, &len); adam@1360: ctx->heap.front += len+1; adam@1360: return r; adam@1360: } adam@1360: adam@1365: uw_unit uw_Basis_jsifyInt_w(uw_context ctx, uw_Basis_int n) { adam@1360: int len; adam@1360: adam@1360: uw_check(ctx, INTS_MAX); adam@1360: sprintf(ctx->page.front, "%lld%n", (uw_Basis_int)n, &len); adam@1360: ctx->page.front += len; adam@1360: adam@1360: return uw_unit_v; adam@1360: } adam@1360: adam@1444: char *uw_Basis_htmlifyString(uw_context ctx, const char *s) { adamc@137: char *r, *s2; adamc@137: adamc@311: uw_check_heap(ctx, strlen(s) * 5 + 1); adamc@137: adamc@666: for (r = s2 = ctx->heap.front; *s; s++) { adamc@1054: unsigned char c = *s; adamc@137: adamc@137: switch (c) { adamc@137: case '<': adamc@137: strcpy(s2, "<"); adamc@137: s2 += 4; adamc@137: break; adamc@137: case '&': adamc@137: strcpy(s2, "&"); adamc@137: s2 += 5; adamc@137: break; adamc@137: default: adamc@1059: *s2++ = c; adamc@137: } adamc@137: } adamc@137: adamc@137: *s2++ = 0; adamc@666: ctx->heap.front = s2; adamc@137: return r; adamc@135: } adamc@135: adamc@428: uw_unit uw_Basis_htmlifyString_w(uw_context ctx, uw_Basis_string s) { adamc@321: uw_check(ctx, strlen(s) * 6); adamc@135: adamc@135: for (; *s; s++) { adamc@1054: unsigned char c = *s; adamc@135: adamc@135: switch (c) { adamc@135: case '<': adamc@311: uw_write_unsafe(ctx, "<"); adamc@135: break; adamc@135: case '&': adamc@311: uw_write_unsafe(ctx, "&"); adamc@135: break; adamc@135: default: adamc@1059: uw_writec_unsafe(ctx, c); adamc@135: } adamc@135: } adamc@428: adamc@428: return uw_unit_v; adamc@135: } adamc@180: adamc@311: uw_Basis_string uw_Basis_htmlifyBool(uw_context ctx, uw_Basis_bool b) { adamc@311: if (b == uw_Basis_False) adamc@286: return "False"; adamc@286: else adamc@286: return "True"; adamc@286: } adamc@286: adamc@428: uw_unit uw_Basis_htmlifyBool_w(uw_context ctx, uw_Basis_bool b) { adamc@311: if (b == uw_Basis_False) { adamc@311: uw_check(ctx, 6); adamc@666: strcpy(ctx->page.front, "False"); adamc@666: ctx->page.front += 5; adamc@286: } else { adamc@311: uw_check(ctx, 5); adamc@666: strcpy(ctx->page.front, "True"); adamc@666: ctx->page.front += 4; adamc@286: } adamc@428: adamc@428: return uw_unit_v; adamc@286: } adamc@286: adamc@436: #define TIME_FMT "%x %X" adamc@438: #define TIME_FMT_PG "%Y-%m-%d %T" adam@1632: #define TIME_FMT_JS "%Y/%m/%d %T" adamc@436: adam@1629: uw_Basis_string uw_Basis_timeToString(uw_context, uw_Basis_time); adam@1629: adamc@436: uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) { adam@1629: return uw_Basis_htmlifyString(ctx, uw_Basis_timeToString(ctx, t)); adamc@436: } adamc@436: adamc@436: uw_unit uw_Basis_htmlifyTime_w(uw_context ctx, uw_Basis_time t) { adam@1629: return uw_Basis_htmlifyString_w(ctx, uw_Basis_timeToString(ctx, t)); adamc@436: } adamc@436: adam@1446: char *uw_Basis_htmlifySource(uw_context ctx, uw_Basis_source src) { adam@1446: int len; adam@1446: char *r; adam@1446: adam@1446: uw_check_heap(ctx, 2 * INTS_MAX + 2); adam@1446: r = ctx->heap.front; adam@1446: sprintf(r, "s%d_%llu%n", src.context, src.source, &len); adam@1446: ctx->heap.front += len+1; adam@1446: return r; adam@1446: } adam@1446: adam@1446: uw_unit uw_Basis_htmlifySource_w(uw_context ctx, uw_Basis_source src) { adam@1446: int len; adam@1446: adam@1446: uw_check(ctx, 2 * INTS_MAX + 1); adam@1446: sprintf(ctx->page.front, "s%d_%llu%n", src.context, src.source, &len); adam@1446: ctx->page.front += len; adam@1446: adam@1446: return uw_unit_v; adam@1446: } adam@1446: adamc@821: uw_Basis_char uw_Basis_strsub(uw_context ctx, uw_Basis_string s, uw_Basis_int n) { adam@1388: while (n >= 0) { adam@1388: if (*s == 0) adam@1388: uw_error(ctx, FATAL, "Out-of-bounds strsub"); adam@1388: adam@1388: if (n == 0) adam@1388: return *s; adam@1388: adam@1388: --n; adam@1388: ++s; adam@1388: } adam@1388: adam@1388: uw_error(ctx, FATAL, "Negative strsub bound"); adamc@821: } adamc@821: adamc@821: uw_Basis_string uw_Basis_strsuffix(uw_context ctx, uw_Basis_string s, uw_Basis_int n) { adam@1388: while (n >= 0) { adam@1388: if (*s == 0 || n == 0) adam@1388: return s; adam@1388: adam@1388: --n; adam@1388: ++s; adam@1388: } adam@1388: adam@1388: uw_error(ctx, FATAL, "Negative strsuffix bound"); adamc@821: } adamc@821: adamc@828: uw_Basis_int uw_Basis_strlen(uw_context ctx, uw_Basis_string s) { adamc@828: return strlen(s); adamc@828: } adamc@828: adam@1388: uw_Basis_bool uw_Basis_strlenGe(uw_context ctx, uw_Basis_string s, uw_Basis_int n) { adam@1388: while (n > 0) { adam@1388: if (*s == 0) adam@1388: return uw_Basis_False; adam@1388: adam@1388: --n; adam@1388: ++s; adam@1388: } adam@1388: adam@1388: return uw_Basis_True; adam@1388: } adam@1388: adamc@829: uw_Basis_string uw_Basis_strchr(uw_context ctx, uw_Basis_string s, uw_Basis_char ch) { adamc@829: return strchr(s, ch); adamc@829: } adamc@829: adamc@831: uw_Basis_int uw_Basis_strcspn(uw_context ctx, uw_Basis_string s, uw_Basis_string chs) { adamc@831: return strcspn(s, chs); adamc@831: } adamc@831: adamc@829: uw_Basis_int *uw_Basis_strindex(uw_context ctx, uw_Basis_string s, uw_Basis_char ch) { adamc@829: uw_Basis_string r = strchr(s, ch); adamc@829: if (r == NULL) adamc@829: return NULL; adamc@829: else { adamc@829: uw_Basis_int *nr = uw_malloc(ctx, sizeof(uw_Basis_int)); adamc@829: *nr = r - s; adamc@829: return nr; adamc@829: } adamc@829: } adamc@829: adam@1390: uw_Basis_int *uw_Basis_strsindex(uw_context ctx, const char *haystack, const char *needle) { adam@1390: uw_Basis_string r = strstr(haystack, needle); adam@1390: if (r == NULL) adam@1390: return NULL; adam@1390: else { adam@1390: uw_Basis_int *nr = uw_malloc(ctx, sizeof(uw_Basis_int)); adam@1390: *nr = r - haystack; adam@1390: return nr; adam@1390: } adam@1390: } adam@1390: adamc@311: uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) { adamc@828: int len = uw_Basis_strlen(ctx, s1) + uw_Basis_strlen(ctx, s2) + 1; adamc@180: char *s; adamc@180: adamc@311: uw_check_heap(ctx, len); adamc@180: adamc@666: s = ctx->heap.front; adamc@180: adamc@180: strcpy(s, s1); adamc@180: strcat(s, s2); adamc@666: ctx->heap.front += len; adamc@180: adamc@180: return s; adamc@180: } adamc@278: adamc@829: uw_Basis_string uw_Basis_substring(uw_context ctx, uw_Basis_string s, uw_Basis_int start, uw_Basis_int len) { adamc@829: size_t full_len = uw_Basis_strlen(ctx, s); adamc@829: adamc@829: if (start < 0) adamc@829: uw_error(ctx, FATAL, "substring: Negative start index"); adamc@829: if (len < 0) adamc@829: uw_error(ctx, FATAL, "substring: Negative length"); adamc@829: if (start + len > full_len) adamc@829: uw_error(ctx, FATAL, "substring: Start index plus length is too large"); adamc@829: adamc@829: if (start + len == full_len) adamc@829: return &s[start]; adamc@829: else { adamc@829: uw_Basis_string r = uw_malloc(ctx, len+1); adamc@959: memcpy(r, s+start, len); adamc@829: r[len] = 0; adamc@829: return r; adamc@829: } adamc@829: adamc@829: } adamc@829: adamc@1023: uw_Basis_string uw_Basis_str1(uw_context ctx, uw_Basis_char ch) { adamc@1023: char *r; adamc@1023: adamc@1023: uw_check_heap(ctx, 2); adamc@1023: r = ctx->heap.front; adamc@1023: r[0] = ch; adamc@1023: r[1] = 0; adamc@1023: adamc@1023: ctx->heap.front += 2; adamc@1023: adamc@1023: return r; adamc@1023: } adamc@1023: adamc@737: uw_Basis_string uw_strdup(uw_context ctx, uw_Basis_string s1) { adamc@828: int len = uw_Basis_strlen(ctx, s1) + 1; adamc@278: char *s; adamc@278: adamc@311: uw_check_heap(ctx, len); adamc@278: adamc@666: s = ctx->heap.front; adamc@278: adamc@278: strcpy(s, s1); adamc@666: ctx->heap.front += len; adamc@278: adamc@278: return s; adamc@278: } adamc@280: adam@1295: uw_Basis_string uw_dup_and_clear_error_message(uw_context ctx) { adam@1295: if (ctx->error_message[0]) { adam@1295: char *s = uw_strdup(ctx, ctx->error_message); adam@1295: ctx->error_message[0] = 0; adam@1295: return s; adam@1295: } else adam@1295: return NULL; adam@1295: } adam@1295: adamc@737: uw_Basis_string uw_maybe_strdup(uw_context ctx, uw_Basis_string s1) { adamc@493: if (s1) adamc@737: return uw_strdup(ctx, s1); adamc@493: else adamc@493: return NULL; adamc@493: } adamc@493: adamc@742: char *uw_memdup(uw_context ctx, const char *p, size_t len) { adamc@742: char *r = uw_malloc(ctx, len); adamc@742: memcpy(r, p, len); adamc@742: return r; adamc@742: } adamc@280: adamc@879: char *uw_sqlfmtInt = "%lld::int8%n"; adamc@879: adamc@311: char *uw_Basis_sqlifyInt(uw_context ctx, uw_Basis_int n) { adamc@281: int len; adamc@281: char *r; adamc@281: adamc@311: uw_check_heap(ctx, INTS_MAX + 6); adamc@666: r = ctx->heap.front; adamc@879: sprintf(r, uw_sqlfmtInt, n, &len); adamc@666: ctx->heap.front += len+1; adamc@281: return r; adamc@281: } adamc@281: adamc@467: char *uw_Basis_sqlifyIntN(uw_context ctx, uw_Basis_int *n) { adamc@467: if (n == NULL) adamc@467: return "NULL"; adamc@467: else adamc@467: return uw_Basis_sqlifyInt(ctx, *n); adamc@467: } adamc@467: adam@1920: char *uw_sqlfmtFloat = "%.16g::float8%n"; adamc@879: adamc@311: char *uw_Basis_sqlifyFloat(uw_context ctx, uw_Basis_float n) { adamc@281: int len; adamc@281: char *r; adamc@281: adamc@311: uw_check_heap(ctx, FLOATS_MAX + 8); adamc@666: r = ctx->heap.front; adamc@879: sprintf(r, uw_sqlfmtFloat, n, &len); adamc@666: ctx->heap.front += len+1; adamc@281: return r; adamc@281: } adamc@281: adamc@467: char *uw_Basis_sqlifyFloatN(uw_context ctx, uw_Basis_float *n) { adamc@467: if (n == NULL) adamc@467: return "NULL"; adamc@467: else adamc@467: return uw_Basis_sqlifyFloat(ctx, *n); adamc@467: } adamc@467: adam@1834: int uw_Estrings = 1, uw_sql_type_annotations = 1; adamc@879: char *uw_sqlsuffixString = "::text"; adamc@1011: char *uw_sqlsuffixChar = "::char"; adamc@281: adamc@311: uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) { adamc@280: char *r, *s2; adamc@280: adamc@879: uw_check_heap(ctx, strlen(s) * 2 + 3 + uw_Estrings + strlen(uw_sqlsuffixString)); adamc@280: adamc@666: r = s2 = ctx->heap.front; adamc@879: if (uw_Estrings) adamc@879: *s2++ = 'E'; adamc@280: *s2++ = '\''; adamc@280: adamc@280: for (; *s; s++) { adamc@280: char c = *s; adamc@280: adamc@280: switch (c) { adamc@280: case '\'': adamc@885: if (uw_Estrings) adamc@885: strcpy(s2, "\\'"); adamc@885: else adamc@885: strcpy(s2, "''"); adamc@280: s2 += 2; adamc@280: break; adamc@280: case '\\': adamc@885: if (uw_Estrings) { adamc@885: strcpy(s2, "\\\\"); adamc@885: s2 += 2; adamc@885: } else adamc@885: *s2++ = '\\'; adamc@280: break; adamc@280: default: adamc@1150: if (isprint((int)c)) adamc@280: *s2++ = c; adamc@879: else if (uw_Estrings) { adam@1857: sprintf(s2, "\\%03o", (unsigned char)c); adamc@280: s2 += 4; adamc@280: } adamc@879: else adam@1689: *s2++ = c; // I hope this is safe to do... don't know how to support UTF-8 outside Postgres otherwise! adamc@280: } adamc@280: } adamc@280: adamc@879: *s2++ = '\''; adamc@879: strcpy(s2, uw_sqlsuffixString); adamc@879: ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixString); adamc@280: return r; adamc@280: } adamc@281: adamc@1011: uw_Basis_string uw_Basis_sqlifyChar(uw_context ctx, uw_Basis_char c) { adamc@1011: char *r, *s2; adamc@1011: adamc@1011: uw_check_heap(ctx, 5 + uw_Estrings + strlen(uw_sqlsuffixChar)); adamc@1011: adamc@1011: r = s2 = ctx->heap.front; adamc@1011: if (uw_Estrings) adamc@1011: *s2++ = 'E'; adamc@1011: *s2++ = '\''; adamc@1011: adamc@1011: switch (c) { adamc@1011: case '\'': adamc@1011: if (uw_Estrings) adamc@1011: strcpy(s2, "\\'"); adamc@1011: else adamc@1011: strcpy(s2, "''"); adamc@1011: s2 += 2; adamc@1011: break; adamc@1011: case '\\': adamc@1011: if (uw_Estrings) { adamc@1011: strcpy(s2, "\\\\"); adamc@1011: s2 += 2; adamc@1011: } else adamc@1011: *s2++ = '\\'; adamc@1011: break; adamc@1011: default: adamc@1150: if (isprint((int)c)) adamc@1011: *s2++ = c; adamc@1011: else if (uw_Estrings) { adam@1857: sprintf(s2, "\\%03o", (unsigned char)c); adamc@1011: s2 += 4; adamc@1011: } adamc@1011: else adamc@1011: uw_error(ctx, FATAL, "Non-printable character %u in char to SQLify", c); adamc@1011: } adamc@1011: adamc@1011: *s2++ = '\''; adamc@1011: strcpy(s2, uw_sqlsuffixChar); adamc@1011: ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixChar); adamc@1011: return r; adamc@1011: } adamc@1011: adamc@879: char *uw_sqlsuffixBlob = "::bytea"; adamc@879: adam@1867: uw_Basis_string uw_Basis_sqlifyBlob(uw_context ctx, uw_Basis_blob b) { adamc@737: char *r, *s2; adamc@737: size_t i; adamc@737: adam@1867: uw_check_heap(ctx, b.size * 5 + 4 + strlen(uw_sqlsuffixBlob)); adamc@737: adamc@737: r = s2 = ctx->heap.front; adamc@879: if (uw_Estrings) adamc@879: *s2++ = 'E'; adam@1867: else adam@1867: *s2++ = 'X'; adamc@737: *s2++ = '\''; adamc@737: adamc@737: for (i = 0; i < b.size; ++i) { adam@1867: unsigned char c = b.data[i]; adam@1867: adam@1867: if (uw_Estrings) { adam@1867: switch (c) { adam@1867: case '\'': adamc@885: strcpy(s2, "\\'"); adam@1867: s2 += 2; adam@1867: break; adam@1867: case '\\': adamc@885: strcpy(s2, "\\\\\\\\"); adamc@885: s2 += 4; adam@1867: break; adam@1867: default: adam@1867: if (isprint((int)c)) adam@1867: *s2++ = c; adam@1867: else { adam@1867: sprintf(s2, "\\\\%03o", c); adam@1867: s2 += 5; adam@1867: } adamc@737: } adam@1867: } else { adam@1867: sprintf(s2, "%02X", c); adam@1867: s2 += 2; adamc@737: } adam@1867: } adamc@737: adamc@879: *s2++ = '\''; adamc@879: strcpy(s2, uw_sqlsuffixBlob); adamc@879: ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixBlob); adamc@737: return r; adamc@737: } adamc@737: adamc@682: char *uw_Basis_sqlifyChannel(uw_context ctx, uw_Basis_channel chn) { adamc@682: int len; adamc@682: char *r; adamc@682: unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn; adamc@682: adamc@682: uw_check_heap(ctx, INTS_MAX + 7); adamc@682: r = ctx->heap.front; adamc@879: sprintf(r, uw_sqlfmtInt, combo, &len); adamc@682: ctx->heap.front += len+1; adamc@682: return r; adamc@682: } adamc@682: adamc@682: char *uw_Basis_attrifyChannel(uw_context ctx, uw_Basis_channel chn) { adamc@682: int len; adamc@682: char *r; adamc@682: unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn; adamc@682: adamc@682: uw_check_heap(ctx, INTS_MAX + 1); adamc@682: r = ctx->heap.front; adamc@682: sprintf(r, "%lld%n", combo, &len); adamc@682: ctx->heap.front += len+1; adamc@682: return r; adamc@682: } adamc@682: adamc@879: char *uw_sqlfmtUint4 = "%u::int4%n"; adamc@879: adamc@682: char *uw_Basis_sqlifyClient(uw_context ctx, uw_Basis_client cli) { adamc@682: int len; adamc@682: char *r; adamc@682: adamc@682: uw_check_heap(ctx, INTS_MAX + 7); adamc@682: r = ctx->heap.front; adamc@879: sprintf(r, uw_sqlfmtUint4, cli, &len); adamc@682: ctx->heap.front += len+1; adamc@682: return r; adamc@682: } adamc@682: adamc@682: char *uw_Basis_attrifyClient(uw_context ctx, uw_Basis_client cli) { adamc@682: int len; adamc@682: char *r; adamc@682: adamc@682: uw_check_heap(ctx, INTS_MAX + 1); adamc@682: r = ctx->heap.front; adamc@682: sprintf(r, "%u%n", cli, &len); adamc@682: ctx->heap.front += len+1; adamc@682: return r; adamc@682: } adamc@682: adamc@467: uw_Basis_string uw_Basis_sqlifyStringN(uw_context ctx, uw_Basis_string s) { adamc@467: if (s == NULL) adamc@467: return "NULL"; adamc@467: else adamc@467: return uw_Basis_sqlifyString(ctx, s); adamc@467: } adamc@467: adamc@311: char *uw_Basis_sqlifyBool(uw_context ctx, uw_Basis_bool b) { adamc@311: if (b == uw_Basis_False) adamc@281: return "FALSE"; adamc@281: else adamc@281: return "TRUE"; adamc@281: } adamc@282: adamc@467: char *uw_Basis_sqlifyBoolN(uw_context ctx, uw_Basis_bool *b) { adamc@467: if (b == NULL) adamc@467: return "NULL"; adamc@467: else adamc@467: return uw_Basis_sqlifyBool(ctx, *b); adamc@467: } adamc@467: adamc@439: char *uw_Basis_sqlifyTime(uw_context ctx, uw_Basis_time t) { adamc@439: size_t len; adamc@682: char *r, *s; adam@1392: struct tm stm = {}; adam@1392: stm.tm_isdst = -1; adamc@682: adam@1365: if (localtime_r(&t.seconds, &stm)) { adamc@682: s = uw_malloc(ctx, TIMES_MAX); adam@1364: len = strftime(s, TIMES_MAX, TIME_FMT_PG, &stm); adam@1834: if (uw_sql_type_annotations) { adam@1834: if (t.microseconds) { adam@1834: r = uw_malloc(ctx, len + 21); adam@1834: sprintf(r, "'%s.%06u'::timestamp", s, t.microseconds); adam@1834: } else { adam@1834: r = uw_malloc(ctx, len + 14); adam@1834: sprintf(r, "'%s'::timestamp", s); adam@1834: } adam@1367: } else { adam@1834: r = uw_malloc(ctx, len + 3); adam@1834: sprintf(r, "'%s'", s); adam@1367: } adamc@682: return r; adamc@682: } else adamc@682: return ""; adamc@682: } adamc@682: adamc@682: char *uw_Basis_attrifyTime(uw_context ctx, uw_Basis_time t) { adamc@682: size_t len; adamc@439: char *r; adam@1392: struct tm stm = {}; adam@1392: stm.tm_isdst = -1; adamc@439: adam@1365: if (localtime_r(&t.seconds, &stm)) { adamc@439: uw_check_heap(ctx, TIMES_MAX); adamc@666: r = ctx->heap.front; adamc@439: len = strftime(r, TIMES_MAX, TIME_FMT, &stm); adamc@666: ctx->heap.front += len+1; adamc@439: return r; adamc@439: } else adamc@439: return ""; adamc@439: } adamc@439: adam@1364: char *uw_Basis_ensqlTime(uw_context ctx, uw_Basis_time t) { adam@1364: size_t len; adam@1364: char *r; adam@1392: struct tm stm = {}; adam@1392: stm.tm_isdst = -1; adam@1364: adam@1365: if (localtime_r(&t.seconds, &stm)) { adam@1364: uw_check_heap(ctx, TIMES_MAX); adam@1364: r = ctx->heap.front; adam@1365: len = strftime(r, TIMES_MAX-7, TIME_FMT_PG, &stm); adam@1365: ctx->heap.front += len; adam@1365: sprintf(ctx->heap.front, ".%06u", t.microseconds); adam@1365: ctx->heap.front += 8; adam@1364: return r; adam@1364: } else adam@1364: return ""; adam@1364: } adam@1364: adamc@467: char *uw_Basis_sqlifyTimeN(uw_context ctx, uw_Basis_time *t) { adamc@467: if (t == NULL) adamc@467: return "NULL"; adamc@467: else adamc@467: return uw_Basis_sqlifyTime(ctx, *t); adamc@467: } adamc@467: adamc@311: char *uw_Basis_ensqlBool(uw_Basis_bool b) { adamc@311: static uw_Basis_int true = 1; adamc@311: static uw_Basis_int false = 0; adamc@282: adamc@311: if (b == uw_Basis_False) adamc@282: return (char *)&false; adamc@282: else adamc@282: return (char *)&true; adamc@282: } adamc@284: adamc@311: uw_Basis_string uw_Basis_intToString(uw_context ctx, uw_Basis_int n) { adamc@284: int len; adamc@284: char *r; adamc@284: adamc@311: uw_check_heap(ctx, INTS_MAX); adamc@666: r = ctx->heap.front; adamc@284: sprintf(r, "%lld%n", n, &len); adamc@666: ctx->heap.front += len+1; adamc@284: return r; adamc@284: } adamc@285: adamc@311: uw_Basis_string uw_Basis_floatToString(uw_context ctx, uw_Basis_float n) { adamc@285: int len; adamc@285: char *r; adamc@285: adamc@311: uw_check_heap(ctx, FLOATS_MAX); adamc@666: r = ctx->heap.front; adamc@285: sprintf(r, "%g%n", n, &len); adamc@666: ctx->heap.front += len+1; adamc@285: return r; adamc@285: } adamc@285: adamc@821: uw_Basis_string uw_Basis_charToString(uw_context ctx, uw_Basis_char ch) { adamc@821: char *r = uw_malloc(ctx, 2); adamc@821: r[0] = ch; adamc@821: r[1] = 0; adamc@821: return r; adamc@821: } adamc@821: adamc@311: uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) { adamc@311: if (b == uw_Basis_False) adamc@285: return "False"; adamc@285: else adamc@285: return "True"; adamc@285: } adamc@288: adam@1359: uw_Basis_string uw_Basis_timef(uw_context ctx, const char *fmt, uw_Basis_time t) { adam@1352: size_t len; adam@1352: char *r; adam@1392: struct tm stm = {}; adam@1392: stm.tm_isdst = -1; adam@1352: adam@1365: if (localtime_r(&t.seconds, &stm)) { adam@1352: uw_check_heap(ctx, TIMES_MAX); adam@1352: r = ctx->heap.front; adam@1352: len = strftime(r, TIMES_MAX, fmt, &stm); adam@1352: ctx->heap.front += len+1; adam@1352: return r; adam@1352: } else adam@1352: return ""; adam@1352: } adam@1352: adam@1629: uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) { adam@1629: return uw_Basis_timef(ctx, ctx->app->time_format, t); adam@1629: } adam@1629: adamc@311: uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) { adamc@288: char *endptr; adamc@311: uw_Basis_int n = strtoll(s, &endptr, 10); adamc@288: adamc@288: if (*s != '\0' && *endptr == '\0') { adamc@311: uw_Basis_int *r = uw_malloc(ctx, sizeof(uw_Basis_int)); adamc@288: *r = n; adamc@288: return r; adamc@288: } else adamc@288: return NULL; adamc@288: } adamc@289: adamc@311: uw_Basis_float *uw_Basis_stringToFloat(uw_context ctx, uw_Basis_string s) { adamc@289: char *endptr; adamc@311: uw_Basis_float n = strtod(s, &endptr); adamc@289: adamc@289: if (*s != '\0' && *endptr == '\0') { adamc@311: uw_Basis_float *r = uw_malloc(ctx, sizeof(uw_Basis_float)); adamc@289: *r = n; adamc@289: return r; adamc@289: } else adamc@289: return NULL; adamc@289: } adamc@289: adamc@821: uw_Basis_char *uw_Basis_stringToChar(uw_context ctx, uw_Basis_string s) { adamc@821: if (s[0] == 0) { adamc@821: uw_Basis_char *r = uw_malloc(ctx, 1); adamc@821: r[0] = 0; adamc@821: return r; adamc@821: } else if (s[1] != 0) adamc@821: return NULL; adamc@821: else { adamc@821: uw_Basis_char *r = uw_malloc(ctx, 1); adamc@821: r[0] = s[0]; adamc@821: return r; adamc@821: } adamc@821: } adamc@821: adamc@311: uw_Basis_bool *uw_Basis_stringToBool(uw_context ctx, uw_Basis_string s) { adamc@311: static uw_Basis_bool true = uw_Basis_True; adamc@311: static uw_Basis_bool false = uw_Basis_False; adamc@289: adamc@289: if (!strcasecmp (s, "True")) adamc@289: return &true; adamc@289: else if (!strcasecmp (s, "False")) adamc@289: return &false; adamc@289: else adamc@289: return NULL; adamc@289: } adamc@292: adamc@436: uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) { adamc@438: char *dot = strchr(s, '.'), *end = strchr(s, 0); adam@1374: struct tm stm = {}; adam@1392: stm.tm_isdst = -1; adamc@436: adamc@439: if (dot) { adamc@439: *dot = 0; adamc@439: if (strptime(s, TIME_FMT_PG, &stm) == end) { adamc@439: *dot = '.'; adamc@439: uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time)); adam@1365: r->seconds = mktime(&stm); adam@1365: r->microseconds = 0; adamc@439: return r; adamc@439: } adamc@439: else { adamc@439: *dot = '.'; adamc@439: return NULL; adamc@439: } adamc@436: } adamc@439: else { adamc@439: if (strptime(s, TIME_FMT_PG, &stm) == end) { adamc@439: uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time)); adam@1365: r->seconds = mktime(&stm); adam@1365: r->microseconds = 0; adamc@439: return r; adamc@439: } adamc@439: else if (strptime(s, TIME_FMT, &stm) == end) { adamc@439: uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time)); adam@1365: r->seconds = mktime(&stm); adam@1365: r->microseconds = 0; adamc@439: return r; adam@1632: } else if (strptime(s, TIME_FMT_JS, &stm) == end) { adam@1632: uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time)); adam@1632: r->seconds = mktime(&stm); adam@1632: r->microseconds = 0; adam@1632: return r; adamc@439: } adamc@439: else adamc@439: return NULL; adamc@439: } adamc@436: } adamc@436: adam@1352: uw_Basis_time *uw_Basis_stringToTimef(uw_context ctx, const char *fmt, uw_Basis_string s) { adam@1352: char *end = strchr(s, 0); adam@1374: struct tm stm = {}; adam@1392: stm.tm_isdst = -1; adam@1352: adam@1352: if (strptime(s, fmt, &stm) == end) { adam@1352: uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time)); adam@1365: r->seconds = mktime(&stm); adam@1365: r->microseconds = 0; adam@1352: return r; adam@1352: } adam@1352: else adam@1352: return NULL; adam@1352: } adam@1352: adamc@311: uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) { adamc@292: char *endptr; adamc@311: uw_Basis_int n = strtoll(s, &endptr, 10); adamc@292: adamc@292: if (*s != '\0' && *endptr == '\0') adamc@292: return n; adamc@292: else adam@1444: uw_error(ctx, FATAL, "Can't parse int: %s", uw_Basis_htmlifyString(ctx, s)); adamc@292: } adamc@293: adamc@682: #include adamc@682: adamc@678: uw_Basis_channel uw_Basis_stringToChannel_error(uw_context ctx, uw_Basis_string s) { adamc@682: unsigned long long n; adamc@682: adamc@682: if (sscanf(s, "%llu", &n) < 1) adam@1444: uw_error(ctx, FATAL, "Can't parse channel: %s", uw_Basis_htmlifyString(ctx, s)); adamc@682: else { adamc@682: uw_Basis_channel ch = {n >> 32, n & ((1ull << 32) - 1)}; adamc@682: return ch; adamc@682: } adamc@682: } adamc@682: adamc@682: uw_Basis_client uw_Basis_stringToClient_error(uw_context ctx, uw_Basis_string s) { adamc@678: char *endptr; adamc@682: unsigned long n = strtoul(s, &endptr, 10); adamc@678: adamc@678: if (*s != '\0' && *endptr == '\0') adamc@678: return n; adamc@678: else adam@1444: uw_error(ctx, FATAL, "Can't parse client: %s", uw_Basis_htmlifyString(ctx, s)); adamc@678: } adamc@678: adamc@311: uw_Basis_float uw_Basis_stringToFloat_error(uw_context ctx, uw_Basis_string s) { adamc@293: char *endptr; adamc@311: uw_Basis_float n = strtod(s, &endptr); adamc@293: adamc@293: if (*s != '\0' && *endptr == '\0') adamc@293: return n; adamc@293: else adam@1444: uw_error(ctx, FATAL, "Can't parse float: %s", uw_Basis_htmlifyString(ctx, s)); adamc@293: } adamc@293: adamc@821: uw_Basis_char uw_Basis_stringToChar_error(uw_context ctx, uw_Basis_string s) { adamc@821: if (s[0] == 0) adamc@821: return 0; adamc@821: else if (s[1] != 0) adam@1444: uw_error(ctx, FATAL, "Can't parse char: %s", uw_Basis_htmlifyString(ctx, s)); adamc@821: else adamc@821: return s[0]; adamc@821: } adamc@821: adamc@311: uw_Basis_bool uw_Basis_stringToBool_error(uw_context ctx, uw_Basis_string s) { adamc@296: if (!strcasecmp(s, "T") || !strcasecmp (s, "True")) adamc@311: return uw_Basis_True; adamc@296: else if (!strcasecmp(s, "F") || !strcasecmp (s, "False")) adamc@311: return uw_Basis_False; adamc@293: else adam@1444: uw_error(ctx, FATAL, "Can't parse bool: %s", uw_Basis_htmlifyString(ctx, s)); adamc@293: } adamc@436: adam@1352: uw_Basis_time uw_Basis_unsqlTime(uw_context ctx, uw_Basis_string s) { adam@1352: char *dot = strchr(s, '.'), *end = strchr(s, 0); adam@1352: struct tm stm = {}; adam@1392: stm.tm_isdst = -1; adam@1352: adam@1352: if (dot) { adam@1352: *dot = 0; adam@1352: if (strptime(s, TIME_FMT_PG, &stm)) { adam@1352: *dot = '.'; adam@1410: char usec[] = "000000"; adam@1410: int len = strlen(dot+1); adam@1410: memcpy(usec, dot+1, len < 6 ? len : 6); adam@1410: uw_Basis_time r = { mktime(&stm), atoi(usec) }; adam@1365: return r; adam@1352: } adam@1352: else { adam@1352: *dot = '.'; adam@1444: uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s)); adam@1352: } adam@1352: } adam@1352: else { adam@1352: if (strptime(s, TIME_FMT_PG, &stm) == end) { adam@1365: uw_Basis_time r = { mktime(&stm) }; adam@1365: return r; adam@1352: } else if (strptime(s, TIME_FMT, &stm) == end) { adam@1365: uw_Basis_time r = { mktime(&stm) }; adam@1365: return r; adam@1352: } else adam@1444: uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s)); adam@1352: } adam@1352: } adam@1352: adamc@436: uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) { adamc@438: char *dot = strchr(s, '.'), *end = strchr(s, 0); adamc@436: struct tm stm = {}; adam@1392: stm.tm_isdst = -1; adamc@436: adamc@438: if (dot) { adamc@438: *dot = 0; adamc@438: if (strptime(s, TIME_FMT_PG, &stm)) { adamc@438: *dot = '.'; adam@1365: { adam@1365: uw_Basis_time r = { mktime(&stm) }; adam@1365: return r; adam@1365: } adamc@438: } adamc@438: else { adamc@438: *dot = '.'; adam@1444: uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s)); adamc@438: } adamc@438: } adamc@438: else { adam@1365: if (strptime(s, TIME_FMT_PG, &stm) == end) { adam@1365: uw_Basis_time r = { mktime(&stm) }; adam@1365: return r; adam@1365: } else if (strptime(s, TIME_FMT, &stm) == end) { adam@1365: uw_Basis_time r = { mktime(&stm) }; adam@1365: return r; adam@1632: } else if (strptime(s, TIME_FMT_JS, &stm) == end) { adam@1632: uw_Basis_time r = { mktime(&stm) }; adam@1632: return r; adam@1365: } else adam@1444: uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s)); adamc@438: } adamc@436: } adamc@457: adam@1352: uw_Basis_time uw_Basis_stringToTimef_error(uw_context ctx, const char *fmt, uw_Basis_string s) { adam@1352: char *end = strchr(s, 0); adamc@939: struct tm stm = {}; adam@1392: stm.tm_isdst = -1; adamc@939: adam@1365: if (strptime(s, fmt, &stm) == end) { adam@1365: uw_Basis_time r = { mktime(&stm) }; adam@1365: return r; adam@1365: } else adam@1444: uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s)); adamc@939: } adamc@939: adamc@742: uw_Basis_blob uw_Basis_stringToBlob_error(uw_context ctx, uw_Basis_string s, size_t len) { adamc@742: char *r = ctx->heap.front; adamc@742: uw_Basis_blob b = {len, r}; adamc@742: adamc@742: uw_check_heap(ctx, len); adamc@742: adamc@742: while (*s) { adamc@742: if (s[0] == '\\') { adamc@742: if (s[1] == '\\') { adamc@742: *r++ = '\\'; adamc@742: s += 2; adamc@1153: } else if (isdigit((int)s[1]) && isdigit((int)s[2]) && isdigit((int)s[3])) { adamc@742: *r++ = (s[1] - '0') * 8 * 8 + ((s[2] - '0') * 8) + (s[3] - '0'); adamc@742: s += 4; adamc@742: } adamc@742: else { adamc@742: *r++ = '\\'; adamc@742: ++s; adamc@742: } adamc@742: } else { adamc@742: *r++ = s[0]; adamc@742: ++s; adamc@742: } adamc@742: } adamc@742: adamc@742: b.size = r - ctx->heap.front; adamc@742: ctx->heap.front = r; adamc@742: return b; adamc@742: } adamc@742: adam@1402: #define THE_PAST "expires=Sat, 01-Jan-2011 00:00:00 GMT" adamc@1052: adamc@463: uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) { adamc@463: int len = strlen(c); adamc@854: char *p = ctx->outHeaders.start; adamc@474: adamc@1134: while ((p = strstr(p, "\nSet-Cookie: "))) { adamc@474: char *p2; adamc@474: p += 13; adamc@474: p2 = strchr(p, '='); adamc@474: adamc@474: if (p2) { adamc@474: size_t sz = strcspn(p2+1, ";\r\n"); adamc@474: adamc@474: if (!strncasecmp(p, c, p2 - p)) { adamc@1052: if (sz == 0 && strstr(p2+2, THE_PAST)) adamc@1052: return NULL; adamc@1052: else { adamc@1052: char *ret = uw_malloc(ctx, sz + 1); adamc@1052: memcpy(ret, p2+1, sz); adamc@1052: ret[sz] = 0; adamc@1052: return ret; adamc@1052: } adamc@474: } adamc@474: } adamc@474: } adamc@463: adamc@1134: if ((p = uw_Basis_requestHeader(ctx, "Cookie"))) { adamc@1051: char *p2; adamc@1051: adamc@854: while (1) { adamc@1051: if (!strncmp(p, c, len) && p[len] == '=') { adamc@1134: if ((p2 = strchr(p, ';'))) { adamc@1051: size_t n = p2 - (p + len); adamc@1051: char *r = uw_malloc(ctx, n); adamc@1051: memcpy(r, p + 1 + len, n-1); adamc@1051: r[n-1] = 0; adamc@1051: return r; adamc@1051: } else adamc@1051: return p + 1 + len; adamc@1134: } else if ((p = strchr(p, ';'))) adamc@854: p += 2; adamc@463: else adamc@463: return NULL; adamc@463: } adamc@463: } adamc@792: adamc@792: return NULL; adamc@457: } adamc@462: adam@1877: static void set_cookie(uw_context ctx) { adam@1877: if (ctx->usedSig) adam@1877: ctx->needsResig = 1; adam@1877: } adam@1877: adamc@1050: 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: uw_write_header(ctx, "Set-Cookie: "); adamc@462: uw_write_header(ctx, c); adamc@462: uw_write_header(ctx, "="); adamc@462: uw_write_header(ctx, v); adamc@466: uw_write_header(ctx, "; path="); adamc@466: uw_write_header(ctx, prefix); adamc@1050: if (expires) { adamc@1050: char formatted[30]; adam@1392: struct tm tm = {}; adam@1392: tm.tm_isdst = -1; adamc@1050: adam@1365: gmtime_r(&expires->seconds, &tm); adamc@1050: adamc@1050: strftime(formatted, sizeof formatted, "%a, %d-%b-%Y %T GMT", &tm); adamc@1050: adamc@1050: uw_write_header(ctx, "; expires="); adamc@1050: uw_write_header(ctx, formatted); adamc@1050: } adamc@1050: if (secure) adamc@1050: uw_write_header(ctx, "; secure"); adamc@462: uw_write_header(ctx, "\r\n"); adam@1877: set_cookie(ctx); adamc@462: adamc@462: return uw_unit_v; adamc@462: } adamc@667: adamc@1050: uw_unit uw_Basis_clear_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c) { adamc@1050: uw_write_header(ctx, "Set-Cookie: "); adamc@1050: uw_write_header(ctx, c); adamc@1050: uw_write_header(ctx, "=; path="); adamc@1050: uw_write_header(ctx, prefix); adamc@1052: uw_write_header(ctx, "; " THE_PAST "\r\n"); adam@1877: set_cookie(ctx); adamc@1050: adamc@1050: return uw_unit_v; adamc@1050: } adamc@1050: adamc@1113: size_t uw_deltas_max = SIZE_MAX; adamc@1113: adamc@682: static delta *allocate_delta(uw_context ctx, unsigned client) { adamc@682: unsigned i; adamc@682: delta *d; adamc@671: adamc@682: for (i = 0; i < ctx->used_deltas; ++i) adamc@682: if (ctx->deltas[i].client == client) adamc@682: return &ctx->deltas[i]; adamc@671: adamc@682: if (ctx->used_deltas >= ctx->n_deltas) { adamc@1113: if (ctx->n_deltas + 1 > uw_deltas_max) adamc@1113: uw_error(ctx, FATAL, "Exceeded limit on number of deltas"); adamc@1113: adamc@682: ctx->deltas = realloc(ctx->deltas, sizeof(delta) * ++ctx->n_deltas); adam@1369: uw_buffer_init(uw_messages_max, &ctx->deltas[ctx->n_deltas-1].msgs, 0); adamc@682: } adamc@671: adamc@682: d = &ctx->deltas[ctx->used_deltas++]; adamc@682: d->client = client; adam@1369: uw_buffer_reset(&d->msgs); adamc@682: return d; adamc@671: } adamc@671: adamc@668: uw_Basis_channel uw_Basis_new_channel(uw_context ctx, uw_unit u) { adamc@682: if (ctx->client == NULL) adamc@682: uw_error(ctx, FATAL, "Attempt to create channel on request not associated with a persistent connection"); adamc@671: adamc@682: return new_channel(ctx->client); adamc@668: } adamc@668: adamc@668: uw_unit uw_Basis_send(uw_context ctx, uw_Basis_channel chn, uw_Basis_string msg) { adamc@682: delta *d = allocate_delta(ctx, chn.cli); adamc@682: size_t len; adamc@682: int preLen; adamc@682: char pre[INTS_MAX + 2]; adamc@668: adamc@682: len = strlen(msg); adamc@682: adamc@682: sprintf(pre, "%u\n%n", chn.chn, &preLen); adamc@682: adam@1369: ctx_uw_buffer_append(ctx, "messages", &d->msgs, pre, preLen); adam@1369: ctx_uw_buffer_append(ctx, "messages", &d->msgs, msg, len); adam@1369: ctx_uw_buffer_append(ctx, "messages", &d->msgs, "\n", 1); adamc@671: adamc@671: return uw_unit_v; adamc@668: } adamc@671: adam@1327: int uw_rollback(uw_context ctx, int will_retry) { adamc@1131: int i; adamc@1131: cleanup *cl; adamc@1131: adamc@1131: if (ctx->client) adamc@1131: release_client(ctx->client); adamc@1131: adamc@1131: for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl) adamc@1131: cl->func(cl->arg); adamc@1131: adamc@1131: ctx->cleanup_front = ctx->cleanup; adamc@1131: adamc@1131: for (i = ctx->used_transactionals-1; i >= 0; --i) adamc@1131: if (ctx->transactionals[i].rollback != NULL) adamc@1131: ctx->transactionals[i].rollback(ctx->transactionals[i].data); adamc@1131: adamc@1131: for (i = ctx->used_transactionals-1; i >= 0; --i) adamc@1131: if (ctx->transactionals[i].free) adam@1327: ctx->transactionals[i].free(ctx->transactionals[i].data, will_retry); adamc@1131: adam@1941: if (ctx->app && ctx->transaction_started) { adam@1941: ctx->transaction_started = 0; adam@1941: return ctx->app->db_rollback(ctx); adam@1941: } else adam@1941: return 0; adamc@1131: } adamc@1131: adam@1956: const char uw_begin_xhtml[] = "\n\n"; adam@1956: const char uw_begin_html5[] = ""; adam@1728: adam@1877: extern int uw_hash_blocksize; adam@1877: adam@1877: static const char sig_intro[] = "used_transactionals-1; i >= 0; --i) adamc@771: if (ctx->transactionals[i].rollback != NULL) adamc@1131: if (ctx->transactionals[i].commit) { adamc@1083: ctx->transactionals[i].commit(ctx->transactionals[i].data); adamc@1131: if (uw_has_error(ctx)) { adam@1327: uw_rollback(ctx, 0); adam@1938: return 0; adamc@1131: } adamc@1131: } adamc@771: adamc@1104: for (i = ctx->used_transactionals-1; i >= 0; --i) adamc@771: if (ctx->transactionals[i].rollback == NULL) adamc@1131: if (ctx->transactionals[i].commit) { adamc@1083: ctx->transactionals[i].commit(ctx->transactionals[i].data); adamc@1131: if (uw_has_error(ctx)) { adam@1327: uw_rollback(ctx, 0); adam@1938: return 0; adamc@1131: } adamc@1131: } adamc@1131: adam@1938: if (ctx->transaction_started) { adam@1939: int code = ctx->app->db_commit(ctx); adam@1938: adam@1938: if (code) { adam@1939: if (code == -1) adam@1938: return 1; adam@1938: adam@1938: for (i = ctx->used_transactionals-1; i >= 0; --i) adam@1938: if (ctx->transactionals[i].free) adam@1938: ctx->transactionals[i].free(ctx->transactionals[i].data, 0); adam@1938: adam@1938: uw_set_error_message(ctx, "Error running SQL COMMIT"); adam@1938: return 0; adam@1938: } adamc@1131: } adamc@682: adamc@682: for (i = 0; i < ctx->used_deltas; ++i) { adamc@682: delta *d = &ctx->deltas[i]; adamc@682: client *c = find_client(d->client); adamc@682: adam@1551: assert (c != NULL); adam@1551: assert(c->mode == USED); adamc@682: adam@1447: client_send(c, &d->msgs, ctx->script.start, uw_buffer_used(&ctx->script)); adamc@682: } adamc@682: adamc@682: if (ctx->client) adamc@685: release_client(ctx->client); adamc@771: adamc@1104: for (i = ctx->used_transactionals-1; i >= 0; --i) adamc@1083: if (ctx->transactionals[i].free) adam@1327: ctx->transactionals[i].free(ctx->transactionals[i].data, 0); adamc@916: vshabanoff@1726: uw_check(ctx, 1); vshabanoff@1726: *ctx->page.front = 0; adam@1413: adam@1956: if (!ctx->returning_indirectly adam@1956: && (ctx->app->is_html5 adam@1956: ? !strncmp(ctx->page.start, uw_begin_html5, sizeof uw_begin_html5 - 1) adam@1956: : !strncmp(ctx->page.start, uw_begin_xhtml, sizeof uw_begin_xhtml - 1))) { adam@1728: char *s; adam@1728: adam@1728: // Splice script data into appropriate part of page, also adding if needed. adam@1956: s = ctx->page.start + (ctx->app->is_html5 ? sizeof uw_begin_html5 - 1 : sizeof uw_begin_xhtml - 1); adam@1728: s = strchr(s, '<'); adam@1728: if (s == NULL) { adam@1728: // Weird. Document has no tags! adam@1728: adam@1728: uw_write(ctx, ""); adam@1728: uw_check(ctx, 1); adam@1728: *ctx->page.front = 0; adam@1728: } else if (!strncmp(s, "", 6)) { adam@1728: // is present. Let's add the ", 9); adam@1729: } else { adam@1729: size_t lenH = strlen(ctx->script_header); adam@1729: char *start = s + 6, *oldPage = ctx->page.start; adam@1729: adam@1729: ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenH); adam@1729: start += ctx->page.start - oldPage; adam@1729: memmove(start + lenH, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1); adam@1729: ctx->page.front += lenH; adam@1729: memcpy(start, ctx->script_header, lenH); adam@1729: } adam@1728: } else { adam@1728: // No . At this point, add it, with ", 16); adam@1729: } else { adam@1729: size_t lenH = strlen(ctx->script_header); adam@1729: size_t lenP = lenH + 13; adam@1729: char *start = s, *oldPage = ctx->page.start; adam@1729: adam@1729: ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenP); adam@1729: start += ctx->page.start - oldPage; adam@1729: memmove(start + lenP, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1); adam@1729: ctx->page.front += lenP; adam@1729: memcpy(start, "", 6); adam@1729: memcpy(start + 6, ctx->script_header, lenH); adam@1729: memcpy(start + 6 + lenH, "", 7); adam@1729: } adamc@916: } adamc@916: } adam@1877: adam@1877: if (ctx->needsResig) { adam@1877: sig = find_sig(ctx->page.start); adam@1877: if (sig) { adam@1877: char *realsig = ctx->app->cookie_sig(ctx); adam@1877: adam@1877: do { adam@1877: memcpy(sig, realsig, 2*uw_hash_blocksize); adam@1877: sig = find_sig(sig); adam@1877: } while (sig); adam@1877: } adam@1877: } adam@1938: adam@1938: return 0; adamc@671: } adamc@671: adamc@683: adamc@1113: size_t uw_transactionals_max = SIZE_MAX; adamc@1113: adamc@771: void uw_register_transactional(uw_context ctx, void *data, uw_callback commit, uw_callback rollback, adam@1327: uw_callback_with_retry free) { adamc@771: if (ctx->used_transactionals >= ctx->n_transactionals) { adamc@1113: if (ctx->used_transactionals+1 > uw_transactionals_max) adamc@1113: uw_error(ctx, FATAL, "Exceeded limit on number of transactionals"); adamc@1084: ctx->transactionals = realloc(ctx->transactionals, sizeof(transactional) * (ctx->used_transactionals+1)); adamc@771: ++ctx->n_transactionals; adamc@771: } adamc@771: adamc@771: ctx->transactionals[ctx->used_transactionals].data = data; adamc@771: ctx->transactionals[ctx->used_transactionals].commit = commit; adamc@771: ctx->transactionals[ctx->used_transactionals].rollback = rollback; adamc@771: ctx->transactionals[ctx->used_transactionals++].free = free; adamc@771: } adamc@771: adamc@683: adamc@683: // "Garbage collection" adamc@683: adamc@1121: void uw_do_expunge(uw_context ctx, uw_Basis_client cli, void *data); adamc@1121: void uw_post_expunge(uw_context ctx, void *data); adamc@1121: adamc@1121: static failure_kind uw_expunge(uw_context ctx, uw_Basis_client cli, void *data) { adamc@683: int r = setjmp(ctx->jmp_buf); adamc@683: adamc@1121: if (r == 0) adamc@1121: uw_do_expunge(ctx, cli, data); adamc@1121: else adamc@1121: ctx->app->db_rollback(ctx); adamc@1121: adamc@1121: uw_post_expunge(ctx, data); adamc@683: adamc@683: return r; adamc@683: } adamc@683: adamc@683: void uw_prune_clients(uw_context ctx) { adamc@683: client *c, *next, *prev = NULL; adamc@683: time_t cutoff; adamc@683: adamc@1094: cutoff = time(NULL) - ctx->app->timeout; adamc@683: adamc@683: pthread_mutex_lock(&clients_mutex); adam@1707: pruning_thread = pthread_self(); adam@1707: pruning_thread_initialized = 1; adamc@683: adamc@683: for (c = clients_used; c; c = next) { adamc@683: next = c->next; adamc@683: pthread_mutex_lock(&c->lock); adamc@685: if (c->last_contact < cutoff && c->refcount == 0) { adamc@683: failure_kind fk = UNLIMITED_RETRY; adamc@683: if (prev) adamc@683: prev->next = next; adamc@683: else adamc@683: clients_used = next; adamc@683: while (fk == UNLIMITED_RETRY) { adam@1941: uw_reset(ctx); adamc@1121: fk = uw_expunge(ctx, c->id, c->data); adamc@1121: if (fk == UNLIMITED_RETRY) adamc@684: printf("Unlimited retry during expunge: %s\n", uw_error_message(ctx)); adamc@683: } adamc@684: if (fk == SUCCESS) adamc@684: free_client(c); adamc@1121: else adamc@856: fprintf(stderr, "Expunge blocked by error: %s\n", uw_error_message(ctx)); adamc@683: } adamc@683: else adamc@683: prev = c; adamc@683: pthread_mutex_unlock(&c->lock); adamc@683: } adamc@683: adamc@683: pthread_mutex_unlock(&clients_mutex); adamc@683: } adamc@687: adamc@687: failure_kind uw_initialize(uw_context ctx) { adamc@687: int r = setjmp(ctx->jmp_buf); adamc@687: adamc@687: if (r == 0) { adam@1936: uw_ensure_transaction(ctx); adamc@1094: ctx->app->initializer(ctx); adamc@1094: if (ctx->app->db_commit(ctx)) adamc@687: uw_error(ctx, FATAL, "Error running SQL COMMIT"); adamc@687: } adamc@687: adamc@687: return r; adamc@687: } adamc@717: adamc@1065: static int url_bad(uw_Basis_string s) { adamc@1065: for (; *s; ++s) adamc@1153: if (!isgraph((int)*s)) adamc@1065: return 1; adamc@1065: adamc@1065: return 0; adamc@1065: } adamc@1065: adamc@717: uw_Basis_string uw_Basis_bless(uw_context ctx, uw_Basis_string s) { adamc@1065: if (url_bad(s)) adamc@1065: uw_error(ctx, FATAL, "Invalid URL %s", uw_Basis_htmlifyString(ctx, s)); adamc@1094: if (ctx->app->check_url(s)) adamc@770: return s; adamc@770: else adamc@770: uw_error(ctx, FATAL, "Disallowed URL %s", uw_Basis_htmlifyString(ctx, s)); adamc@717: } adamc@734: adamc@770: uw_Basis_string uw_Basis_checkUrl(uw_context ctx, uw_Basis_string s) { adamc@1065: if (url_bad(s)) adamc@1065: return NULL; adamc@1094: if (ctx->app->check_url(s)) adamc@770: return s; adamc@770: else adamc@770: return NULL; adamc@770: } adamc@770: adam@1465: static int mime_format(const char *s) { adamc@770: for (; *s; ++s) adamc@1153: if (!isalnum((int)*s) && *s != '/' && *s != '-' && *s != '.') adamc@770: return 0; adamc@770: adamc@770: return 1; adamc@770: } adamc@770: adamc@741: uw_Basis_string uw_Basis_blessMime(uw_context ctx, uw_Basis_string s) { adamc@770: if (!mime_format(s)) adamc@770: uw_error(ctx, FATAL, "MIME type \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s)); adamc@770: adamc@1094: if (ctx->app->check_mime(s)) adamc@770: return s; adamc@770: else adamc@770: uw_error(ctx, FATAL, "Disallowed MIME type %s", uw_Basis_htmlifyString(ctx, s)); adamc@770: } adamc@770: adamc@770: uw_Basis_string uw_Basis_checkMime(uw_context ctx, uw_Basis_string s) { adamc@770: if (!mime_format(s)) adamc@770: return NULL; adamc@770: adamc@1094: if (ctx->app->check_mime(s)) adamc@770: return s; adamc@770: else adamc@770: return NULL; adamc@741: } adamc@741: adam@1465: uw_Basis_string uw_Basis_blessRequestHeader(uw_context ctx, uw_Basis_string s) { adam@1465: if (!mime_format(s)) adam@1465: uw_error(ctx, FATAL, "Request header \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s)); adam@1465: adam@1465: if (ctx->app->check_requestHeader(s)) adam@1465: return s; adam@1465: else adam@1465: uw_error(ctx, FATAL, "Disallowed request header %s", uw_Basis_htmlifyString(ctx, s)); adam@1465: } adam@1465: adam@1465: uw_Basis_string uw_Basis_checkRequestHeader(uw_context ctx, uw_Basis_string s) { adam@1465: if (!mime_format(s)) adam@1465: return NULL; adam@1465: adam@1465: if (ctx->app->check_requestHeader(s)) adam@1465: return s; adam@1465: else adam@1465: return NULL; adam@1465: } adam@1465: adam@1465: uw_Basis_string uw_Basis_blessResponseHeader(uw_context ctx, uw_Basis_string s) { adam@1465: if (!mime_format(s)) adam@1465: uw_error(ctx, FATAL, "Response header \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s)); adam@1465: adam@1465: if (ctx->app->check_responseHeader(s)) adam@1465: return s; adam@1465: else adam@1465: uw_error(ctx, FATAL, "Disallowed response header %s", uw_Basis_htmlifyString(ctx, s)); adam@1465: } adam@1465: adam@1799: static int envVar_format(const char *s) { adam@1799: for (; *s; ++s) adam@1799: if (!isalnum((int)*s) && *s != '_' && *s != '.') adam@1799: return 0; adam@1799: adam@1799: return 1; adam@1799: } adam@1799: adam@1465: uw_Basis_string uw_Basis_checkResponseHeader(uw_context ctx, uw_Basis_string s) { adam@1799: if (!envVar_format(s)) adam@1465: return NULL; adam@1465: adam@1465: if (ctx->app->check_responseHeader(s)) adam@1465: return s; adam@1465: else adam@1465: return NULL; adam@1465: } adam@1465: adam@1799: uw_Basis_string uw_Basis_blessEnvVar(uw_context ctx, uw_Basis_string s) { adam@1799: if (!envVar_format(s)) adam@1799: uw_error(ctx, FATAL, "Environment variable \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s)); adam@1799: adam@1799: if (ctx->app->check_envVar(s)) adam@1799: return s; adam@1799: else adam@1799: uw_error(ctx, FATAL, "Disallowed environment variable %s", uw_Basis_htmlifyString(ctx, s)); adam@1799: } adam@1799: adam@1799: uw_Basis_string uw_Basis_checkEnvVar(uw_context ctx, uw_Basis_string s) { adam@1799: if (!mime_format(s)) adam@1799: return NULL; adam@1799: adam@1799: if (ctx->app->check_envVar(s)) adam@1799: return s; adam@1799: else adam@1799: return NULL; adam@1799: } adam@1799: adam@1465: uw_Basis_string uw_Basis_getHeader(uw_context ctx, uw_Basis_string name) { adam@1465: return uw_Basis_requestHeader(ctx, name); adam@1465: } adam@1465: adam@1465: static int mime_value_format(const char *s) { adam@1465: for (; *s; ++s) adam@1465: if (*s == '\r' || *s == '\n') adam@1465: return 0; adam@1465: adam@1465: return 1; adam@1465: } adam@1465: adam@1465: uw_unit uw_Basis_setHeader(uw_context ctx, uw_Basis_string name, uw_Basis_string value) { adam@1465: if (!mime_value_format(value)) adam@1465: uw_error(ctx, FATAL, "Invalid value for HTTP response header"); adam@1465: adam@1465: uw_write_header(ctx, name); adam@1465: uw_write_header(ctx, ": "); adam@1465: uw_write_header(ctx, value); adam@1465: uw_write_header(ctx, "\r\n"); adam@1465: adam@1465: return uw_unit_v; adam@1465: } adam@1465: adam@1799: uw_Basis_string uw_Basis_getenv(uw_context ctx, uw_Basis_string name) { adam@1799: return ctx->get_env(ctx->get_env_data, name); adam@1799: } adam@1799: adamc@734: uw_Basis_string uw_unnull(uw_Basis_string s) { adamc@734: return s ? s : ""; adamc@734: } adamc@734: adamc@734: uw_Basis_string uw_Basis_makeSigString(uw_context ctx, uw_Basis_string sig) { adamc@734: uw_Basis_string r = uw_malloc(ctx, 2 * uw_hash_blocksize + 1); adamc@734: int i; adamc@734: adamc@734: for (i = 0; i < uw_hash_blocksize; ++i) adamc@734: sprintf(&r[2*i], "%.02X", ((unsigned char *)sig)[i]); adamc@734: adamc@734: return r; adamc@734: } adamc@734: adam@1512: /* This bit of crafty code is intended to prevent GCC from performing adam@1512: * optimizations that would enable timing attacks. See: adam@1512: * http://www.impredicative.com/pipermail/ur/2011-July/000659.html adam@1512: */ adam@1512: int uw_streq(uw_Basis_string s1, uw_Basis_string s2) { adam@1512: int i, x = 0, len1 = strlen(s1); adam@1512: adam@1512: if (len1 != strlen(s2)) return 0; adam@1512: adam@1512: for (i = 0; i < len1; ++i) { adam@1512: __asm__ __volatile__ (""); adam@1512: x |= s1[i] ^ s2[i]; adam@1512: } adam@1512: adam@1512: return x == 0; adam@1512: } adam@1512: adamc@734: uw_Basis_string uw_Basis_sigString(uw_context ctx, uw_unit u) { adam@1877: ctx->usedSig = 1; adamc@1094: return ctx->app->cookie_sig(ctx); adamc@734: } adamc@737: adamc@737: uw_Basis_string uw_Basis_fileName(uw_context ctx, uw_Basis_file f) { adamc@737: return f.name; adamc@737: } adamc@737: adamc@740: uw_Basis_string uw_Basis_fileMimeType(uw_context ctx, uw_Basis_file f) { adamc@740: return f.type; adamc@740: } adamc@740: adamc@745: uw_Basis_int uw_Basis_blobSize(uw_context ctx, uw_Basis_blob b) { adamc@745: return b.size; adamc@745: } adamc@745: adamc@1119: uw_Basis_blob uw_Basis_textBlob(uw_context ctx, uw_Basis_string s) { adamc@1119: uw_Basis_blob b = {strlen(s), s}; adamc@1119: adamc@1119: return b; adamc@1119: } adamc@1119: adamc@737: uw_Basis_blob uw_Basis_fileData(uw_context ctx, uw_Basis_file f) { adamc@737: return f.data; adamc@737: } adamc@741: adam@1347: uw_Basis_string uw_Basis_postType(uw_context ctx, uw_Basis_postBody pb) { adam@1347: return pb.type; adam@1347: } adam@1347: adam@1347: uw_Basis_string uw_Basis_postData(uw_context ctx, uw_Basis_postBody pb) { adam@1347: return pb.data; adam@1347: } adam@1347: adam@1471: static char *old_headers(uw_context ctx) { adam@1471: if (uw_buffer_used(&ctx->outHeaders) == 0) adam@1471: return NULL; adam@1471: else { adam@1471: char *s = strchr(ctx->outHeaders.start, '\n'); adam@1471: adam@1471: if (s == NULL || strncasecmp(s+1, "Content-type: ", 14)) adam@1471: return NULL; adam@1471: else { adam@1471: s = strchr(s+15, '\n'); adam@1471: if (s == NULL) adam@1471: return NULL; adam@1471: else adam@1471: return uw_strdup(ctx, s+1); adam@1471: } adam@1471: } adam@1471: } adam@1471: adamc@741: __attribute__((noreturn)) void uw_return_blob(uw_context ctx, uw_Basis_blob b, uw_Basis_string mimeType) { adamc@741: cleanup *cl; adamc@741: int len; adam@1471: char *oldh; adamc@741: adam@1451: if (!ctx->allowed_to_return_indirectly) adam@1451: uw_error(ctx, FATAL, "Tried to return a blob from an RPC"); adam@1451: adamc@1065: ctx->returning_indirectly = 1; adam@1471: oldh = old_headers(ctx); adam@1369: uw_buffer_reset(&ctx->outHeaders); adam@1369: uw_buffer_reset(&ctx->page); adamc@741: adamc@856: uw_write_header(ctx, on_success); adamc@856: uw_write_header(ctx, "Content-Type: "); adamc@741: uw_write_header(ctx, mimeType); adam@1934: uw_write_header(ctx, "\r\nContent-length: "); adam@1369: ctx_uw_buffer_check(ctx, "headers", &ctx->outHeaders, INTS_MAX); adamc@1134: sprintf(ctx->outHeaders.front, "%lu%n", (unsigned long)b.size, &len); adamc@741: ctx->outHeaders.front += len; adamc@1066: uw_write_header(ctx, "\r\n"); adam@1471: if (oldh) uw_write_header(ctx, oldh); adamc@741: adam@1369: ctx_uw_buffer_append(ctx, "page", &ctx->page, b.data, b.size); adamc@741: adamc@741: for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl) adamc@741: cl->func(cl->arg); adamc@741: adamc@741: ctx->cleanup_front = ctx->cleanup; adamc@741: adamc@1065: longjmp(ctx->jmp_buf, RETURN_INDIRECTLY); adamc@1065: } adamc@1065: adam@1932: __attribute__((noreturn)) void uw_return_blob_from_page(uw_context ctx, uw_Basis_string mimeType) { adam@1932: cleanup *cl; adam@1932: int len; adam@1932: char *oldh; adam@1932: adam@1932: if (!ctx->allowed_to_return_indirectly) adam@1932: uw_error(ctx, FATAL, "Tried to return a blob from an RPC"); adam@1932: adam@1932: ctx->returning_indirectly = 1; adam@1932: oldh = old_headers(ctx); adam@1932: uw_buffer_reset(&ctx->outHeaders); adam@1932: adam@1932: uw_write_header(ctx, on_success); adam@1932: uw_write_header(ctx, "Content-Type: "); adam@1932: uw_write_header(ctx, mimeType); adam@1934: uw_write_header(ctx, "\r\nContent-length: "); adam@1932: ctx_uw_buffer_check(ctx, "headers", &ctx->outHeaders, INTS_MAX); adam@1932: sprintf(ctx->outHeaders.front, "%lu%n", (unsigned long)uw_buffer_used(&ctx->page), &len); adam@1932: ctx->outHeaders.front += len; adam@1932: uw_write_header(ctx, "\r\n"); adam@1932: if (oldh) uw_write_header(ctx, oldh); adam@1932: adam@1932: for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl) adam@1932: cl->func(cl->arg); adam@1932: adam@1932: ctx->cleanup_front = ctx->cleanup; adam@1932: adam@1932: longjmp(ctx->jmp_buf, RETURN_INDIRECTLY); adam@1932: } adam@1932: adamc@1065: __attribute__((noreturn)) void uw_redirect(uw_context ctx, uw_Basis_string url) { adamc@1065: cleanup *cl; adamc@1066: char *s; adam@1471: char *oldh; adamc@1065: adam@1451: if (!ctx->allowed_to_return_indirectly) adam@1451: uw_error(ctx, FATAL, "Tried to redirect from an RPC"); adam@1451: adamc@1065: ctx->returning_indirectly = 1; adam@1471: oldh = old_headers(ctx); adam@1369: uw_buffer_reset(&ctx->page); adam@1369: ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->outHeaders)+1); adam@1369: memcpy(ctx->page.start, ctx->outHeaders.start, uw_buffer_used(&ctx->outHeaders)); adam@1369: ctx->page.start[uw_buffer_used(&ctx->outHeaders)] = 0; adam@1369: uw_buffer_reset(&ctx->outHeaders); adamc@1065: adam@1409: if (on_success[0]) adam@1409: uw_write_header(ctx, on_redirect); adam@1409: else adam@1409: uw_write_header(ctx, "Status: 303 See Other\r\n"); adam@1409: s = on_success[0] ? strchr(ctx->page.start, '\n') : ctx->page.start; adamc@1066: if (s) { adamc@1066: char *s2; adam@1409: if (s[0] == '\n') ++s; adam@1409: for (; (s2 = strchr(s, '\n')); s = s2+1) { adamc@1066: *s2 = 0; adamc@1066: if (!strncmp(s, "Set-Cookie: ", 12)) { adamc@1066: uw_write_header(ctx, s); adamc@1066: uw_write_header(ctx, "\n"); adamc@1066: } adamc@1066: } adamc@1066: } adamc@1066: adamc@1065: uw_write_header(ctx, "Location: "); adamc@1065: uw_write_header(ctx, url); adamc@1065: uw_write_header(ctx, "\r\n\r\n"); adam@1471: if (oldh) uw_write_header(ctx, oldh); adamc@1065: adamc@1065: for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl) adamc@1065: cl->func(cl->arg); adamc@1065: adamc@1065: ctx->cleanup_front = ctx->cleanup; adamc@1065: adamc@1065: longjmp(ctx->jmp_buf, RETURN_INDIRECTLY); adamc@741: } adamc@874: adamc@874: uw_Basis_string uw_Basis_unAs(uw_context ctx, uw_Basis_string s) { adam@1467: uw_Basis_string ret = uw_malloc(ctx, strlen(s) + 1), r = ret; adamc@874: adamc@874: for (; *s; ++s) { adamc@874: if (s[0] == '\'') { adamc@874: *r++ = '\''; adamc@874: for (++s; *s; ++s) { adamc@874: if (s[0] == '\'') { adamc@874: *r++ = '\''; adamc@874: break; adamc@874: } else if (s[0] == '\\') { adam@1467: *r++ = '\\'; adam@1467: *r++ = s[1]; adam@1467: ++s; adamc@874: } else adamc@874: *r++ = s[0]; adamc@874: } adamc@874: if (*s == 0) break; adam@1467: } else if (s[0] == 'T' && s[1] == '_' && s[2] == 'T' && s[3] == '.') adam@1467: s += 3; adamc@874: else adamc@874: *r++ = s[0]; adamc@874: } adamc@874: adam@1467: *r = 0; adam@1467: return ret; adamc@874: } adamc@874: adamc@922: uw_Basis_string uw_Basis_mstrcat(uw_context ctx, ...) { adamc@922: va_list ap; adamc@922: size_t len = 1; adamc@922: char *s, *r, *s2; adamc@922: adamc@922: va_start(ap, ctx); adamc@922: for (s = va_arg(ap, char*); s; s = va_arg(ap, char*)) adamc@922: len += strlen(s); adamc@922: va_end(ap); adamc@922: adamc@922: r = uw_malloc(ctx, len); adamc@922: va_start(ap, ctx); adamc@922: for (s2 = r, s = va_arg(ap, char*); s; s = va_arg(ap, char*)) adamc@922: while (*s) adamc@922: *s2++ = *s++; adamc@922: va_end(ap); adamc@922: *s2 = 0; adamc@922: adamc@922: return r; adamc@922: } adamc@1006: adam@1365: const uw_Basis_time uw_Basis_minTime = {}; adamc@1050: adamc@1006: uw_Basis_time uw_Basis_now(uw_context ctx) { adam@1365: uw_Basis_time r = { time(NULL) }; adam@1365: return r; adamc@1006: } adamc@1061: adam@1369: uw_Basis_time uw_Basis_addSeconds(uw_context ctx, uw_Basis_time tm, uw_Basis_int n) { adam@1369: tm.seconds += n; adam@1365: return tm; adamc@1138: } adamc@1138: adam@1514: uw_Basis_int uw_Basis_diffInSeconds(uw_context ctx, uw_Basis_time tm1, uw_Basis_time tm2) { adam@1514: return difftime(tm2.seconds, tm1.seconds); adam@1514: } adam@1514: adam@1685: uw_Basis_int uw_Basis_toMilliseconds(uw_context ctx, uw_Basis_time tm) { adam@1685: return tm.seconds * 1000 + tm.microseconds / 1000; adam@1685: } adam@1685: adam@1685: uw_Basis_int uw_Basis_diffInMilliseconds(uw_context ctx, uw_Basis_time tm1, uw_Basis_time tm2) { adam@1685: return uw_Basis_toMilliseconds(ctx, tm2) - uw_Basis_toMilliseconds(ctx, tm1); adam@1685: } adam@1685: adam@1514: uw_Basis_int uw_Basis_toSeconds(uw_context ctx, uw_Basis_time tm) { adam@1514: return tm.seconds; adam@1514: } adam@1514: adamc@1061: void *uw_get_global(uw_context ctx, char *name) { adamc@1061: int i; adamc@1061: adamc@1061: for (i = 0; i < ctx->n_globals; ++i) adamc@1061: if (!strcmp(name, ctx->globals[i].name)) adamc@1061: return ctx->globals[i].data; adamc@1061: adamc@1061: return NULL; adamc@1061: } adamc@1061: adamc@1113: size_t uw_globals_max = SIZE_MAX; adamc@1113: adamc@1061: void uw_set_global(uw_context ctx, char *name, void *data, void (*free)(void*)) { adamc@1061: int i; adamc@1061: adamc@1061: for (i = 0; i < ctx->n_globals; ++i) adamc@1061: if (!strcmp(name, ctx->globals[i].name)) { adamc@1089: if (ctx->globals[i].free) adamc@1061: ctx->globals[i].free(ctx->globals[i].data); adamc@1061: ctx->globals[i].data = data; adamc@1061: ctx->globals[i].free = free; adamc@1061: return; adamc@1061: } adamc@1089: adamc@1113: if (ctx->n_globals+1 > uw_globals_max) adamc@1113: uw_error(ctx, FATAL, "Exceeded limit on number of globals"); adamc@1113: adamc@1061: ++ctx->n_globals; adamc@1061: ctx->globals = realloc(ctx->globals, ctx->n_globals * sizeof(global)); adamc@1061: ctx->globals[ctx->n_globals-1].name = name; adamc@1061: ctx->globals[ctx->n_globals-1].data = data; adamc@1061: ctx->globals[ctx->n_globals-1].free = free; adamc@1061: } adamc@1061: adamc@1061: uw_Basis_bool uw_Basis_isalnum(uw_context ctx, uw_Basis_char c) { adamc@1153: return !!isalnum((int)c); adamc@1061: } adamc@1061: adamc@1061: uw_Basis_bool uw_Basis_isalpha(uw_context ctx, uw_Basis_char c) { adamc@1153: return !!isalpha((int)c); adamc@1061: } adamc@1061: adamc@1061: uw_Basis_bool uw_Basis_isblank(uw_context ctx, uw_Basis_char c) { adamc@1153: return !!isblank((int)c); adamc@1061: } adamc@1061: adamc@1061: uw_Basis_bool uw_Basis_iscntrl(uw_context ctx, uw_Basis_char c) { adamc@1153: return !!iscntrl((int)c); adamc@1061: } adamc@1061: adamc@1061: uw_Basis_bool uw_Basis_isdigit(uw_context ctx, uw_Basis_char c) { adamc@1153: return !!isdigit((int)c); adamc@1061: } adamc@1061: adamc@1061: uw_Basis_bool uw_Basis_isgraph(uw_context ctx, uw_Basis_char c) { adamc@1153: return !!isgraph((int)c); adamc@1061: } adamc@1061: adamc@1061: uw_Basis_bool uw_Basis_islower(uw_context ctx, uw_Basis_char c) { adamc@1153: return !!islower((int)c); adamc@1061: } adamc@1061: adamc@1061: uw_Basis_bool uw_Basis_isprint(uw_context ctx, uw_Basis_char c) { adamc@1153: return !!isprint((int)c); adamc@1061: } adamc@1061: adamc@1061: uw_Basis_bool uw_Basis_ispunct(uw_context ctx, uw_Basis_char c) { adamc@1153: return !!ispunct((int)c); adamc@1061: } adamc@1061: adamc@1061: uw_Basis_bool uw_Basis_isspace(uw_context ctx, uw_Basis_char c) { adamc@1153: return !!isspace((int)c); adamc@1061: } adamc@1063: adamc@1061: uw_Basis_bool uw_Basis_isupper(uw_context ctx, uw_Basis_char c) { adamc@1153: return !!isupper((int)c); adamc@1061: } adamc@1061: adamc@1061: uw_Basis_bool uw_Basis_isxdigit(uw_context ctx, uw_Basis_char c) { adamc@1153: return !!isxdigit((int)c); adamc@1061: } adamc@1061: adamc@1061: uw_Basis_char uw_Basis_tolower(uw_context ctx, uw_Basis_char c) { adamc@1153: return tolower((int)c); adamc@1061: } adamc@1061: adamc@1061: uw_Basis_char uw_Basis_toupper(uw_context ctx, uw_Basis_char c) { adamc@1153: return toupper((int)c); adamc@1061: } adamc@1066: adamc@1128: uw_Basis_int uw_Basis_ord(uw_context ctx, uw_Basis_char c) { adamc@1128: return (unsigned char)c; adamc@1128: } adamc@1128: adamc@1128: uw_Basis_char uw_Basis_chr(uw_context ctx, uw_Basis_int n) { adamc@1128: return n; adamc@1128: } adamc@1128: adamc@1066: uw_Basis_string uw_Basis_currentUrl(uw_context ctx) { adamc@1066: return ctx->current_url; adamc@1066: } adamc@1066: adamc@1066: void uw_set_currentUrl(uw_context ctx, char *s) { adamc@1066: ctx->current_url = s; adamc@1066: } adamc@1114: adamc@1114: void uw_set_deadline(uw_context ctx, int n) { adamc@1114: ctx->deadline = n; adamc@1114: } adamc@1114: adamc@1114: void uw_check_deadline(uw_context ctx) { adamc@1114: if (uw_time > ctx->deadline) adamc@1114: uw_error(ctx, FATAL, "Maximum running time exceeded"); adamc@1114: } adamc@1115: adamc@1115: size_t uw_database_max = SIZE_MAX; adamc@1120: adam@1389: uw_Basis_int uw_Basis_naughtyDebug(uw_context ctx, uw_Basis_string s) { adam@1389: fprintf(stderr, "%s\n", s); adam@1389: return 0; adam@1389: } adam@1389: adamc@1120: uw_Basis_unit uw_Basis_debug(uw_context ctx, uw_Basis_string s) { adam@1334: if (ctx->log_debug) adam@1334: ctx->log_debug(ctx->logger_data, "%s\n", s); adam@1334: else adam@1334: fprintf(stderr, "%s\n", s); adamc@1120: return uw_unit_v; adamc@1120: } adamc@1250: adam@1933: static pthread_mutex_t rand_mutex = PTHREAD_MUTEX_INITIALIZER; adam@1933: adamc@1250: uw_Basis_int uw_Basis_rand(uw_context ctx) { adam@1486: uw_Basis_int ret; adam@1933: pthread_mutex_lock(&rand_mutex); adam@1486: int r = RAND_bytes((unsigned char *)&ret, sizeof ret); adam@1933: pthread_mutex_unlock(&rand_mutex); adam@1486: adam@1486: if (r) adam@1486: return abs(ret); adam@1486: else adam@1486: uw_error(ctx, FATAL, "Random number generation failed"); adamc@1250: } adam@1347: adam@1347: void uw_noPostBody(uw_context ctx) { adam@1347: ctx->hasPostBody = 0; adam@1347: } adam@1347: adam@1347: void uw_postBody(uw_context ctx, uw_Basis_postBody pb) { adam@1347: ctx->hasPostBody = 1; adam@1347: ctx->postBody = pb; adam@1347: } adam@1347: adam@1347: int uw_hasPostBody(uw_context ctx) { adam@1347: return ctx->hasPostBody; adam@1347: } adam@1347: adam@1386: void uw_isPost(uw_context ctx) { adam@1386: ctx->isPost = 1; adam@1386: } adam@1386: adam@1386: uw_Basis_bool uw_Basis_currentUrlHasPost(uw_context ctx) { adam@1386: return ctx->isPost; adam@1386: } adam@1386: adam@1485: uw_Basis_bool uw_Basis_currentUrlHasQueryString(uw_context ctx) { adam@1485: return ctx->queryString != NULL && ctx->queryString[0] != 0; adam@1485: } adam@1485: adam@1370: void uw_setQueryString(uw_context ctx, uw_Basis_string s) { adam@1370: ctx->queryString = s; adam@1370: } adam@1370: adam@1370: uw_Basis_string uw_queryString(uw_context ctx) { adam@1370: return ctx->queryString; adam@1370: } adam@1370: adam@1347: uw_Basis_postBody uw_getPostBody(uw_context ctx) { adam@1347: if (ctx->hasPostBody) adam@1347: return ctx->postBody; adam@1347: else adam@1347: uw_error(ctx, FATAL, "Asked for POST body when none exists"); adam@1347: } adam@1349: adam@1349: failure_kind uw_runCallback(uw_context ctx, void (*callback)(uw_context)) { adam@1349: int r = setjmp(ctx->jmp_buf); adam@1349: adam@1349: if (r == 0) { adam@1936: uw_ensure_transaction(ctx); adam@1418: adam@1349: callback(ctx); adam@1349: } adam@1349: adam@1349: return r; adam@1349: } adam@1360: adam@1360: uw_Basis_string uw_Basis_crypt(uw_context ctx, uw_Basis_string key, uw_Basis_string salt) { adam@1397: char buf[14]; adam@1397: return uw_strdup(ctx, DES_fcrypt(key, salt, buf)); adam@1360: } adam@1365: adam@1365: uw_Basis_bool uw_Basis_eq_time(uw_context ctx, uw_Basis_time t1, uw_Basis_time t2) { adam@1365: return !!(t1.seconds == t2.seconds && t1.microseconds == t2.microseconds); adam@1365: } adam@1365: adam@1365: uw_Basis_bool uw_Basis_lt_time(uw_context ctx, uw_Basis_time t1, uw_Basis_time t2) { adam@1365: return !!(t1.seconds < t2.seconds || t1.microseconds < t2.microseconds); adam@1365: } adam@1365: adam@1365: uw_Basis_bool uw_Basis_le_time(uw_context ctx, uw_Basis_time t1, uw_Basis_time t2) { adam@1365: return !!(uw_Basis_eq_time(ctx, t1, t2) || uw_Basis_lt_time(ctx, t1, t2)); adam@1365: } adam@1365: adam@1372: uw_Basis_time *uw_Basis_readUtc(uw_context ctx, uw_Basis_string s) { adam@1374: struct tm stm = {}; adam@1375: char *end = strchr(s, 0); adam@1449: stm.tm_isdst = -1; adam@1375: adam@1632: if (strptime(s, TIME_FMT_PG, &stm) == end || strptime(s, TIME_FMT, &stm) == end || strptime(s, TIME_FMT_JS, &stm) == end) { adam@1373: uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time)); adam@1375: kkallio@1448: r->seconds = timegm(&stm); adam@1373: r->microseconds = 0; adam@1373: adam@1373: return r; adam@1374: } adam@1374: else adam@1373: return NULL; adam@1372: } adam@1407: adam@1407: failure_kind uw_begin_onError(uw_context ctx, char *msg) { adam@1407: int r = setjmp(ctx->jmp_buf); adam@1407: adam@1407: if (ctx->app->on_error) { adam@1407: if (r == 0) { adam@1936: uw_ensure_transaction(ctx); adam@1407: adam@1409: uw_buffer_reset(&ctx->outHeaders); adam@1409: if (on_success[0]) adam@1409: uw_write_header(ctx, "HTTP/1.1 "); adam@1409: else adam@1409: uw_write_header(ctx, "Status: "); adam@1409: uw_write_header(ctx, "500 Internal Server Error\r\n"); adam@1919: uw_write_header(ctx, "Content-type: text/html\r\n"); adam@1956: uw_write(ctx, ctx->app->is_html5 ? uw_begin_html5 : uw_begin_xhtml); adam@1407: ctx->app->on_error(ctx, msg); adam@1407: uw_write(ctx, ""); adam@1407: } adam@1407: adam@1407: return r; adam@1407: } else adam@1407: uw_error(ctx, FATAL, "Tried to run nonexistent onError handler"); adam@1407: } adam@1451: adam@1451: void uw_mayReturnIndirectly(uw_context ctx) { adam@1451: ctx->allowed_to_return_indirectly = 1; adam@1451: } adam@1483: adam@1556: uw_Basis_string uw_Basis_fresh(uw_context ctx) { adam@1703: int len; adam@1703: char *r; adam@1703: adam@1703: uw_check_heap(ctx, 2+INTS_MAX); adam@1703: r = ctx->heap.front; adam@1703: sprintf(r, "uw%u%n", ctx->nextId++, &len); adam@1703: ctx->heap.front += len+1; adam@1703: return r; adam@1556: } adam@1571: adam@1571: uw_Basis_float uw_Basis_floatFromInt(uw_context ctx, uw_Basis_int n) { adam@1571: return n; adam@1571: } adam@1571: adam@1571: uw_Basis_int uw_Basis_ceil(uw_context ctx, uw_Basis_float n) { adam@1571: return ceil(n); adam@1571: } adam@1571: adam@1571: uw_Basis_int uw_Basis_trunc(uw_context ctx, uw_Basis_float n) { adam@1571: return trunc(n); adam@1571: } adam@1571: adam@1571: uw_Basis_int uw_Basis_round(uw_context ctx, uw_Basis_float n) { adam@1571: return round(n); adam@1571: } adam@1755: adam@1755: uw_Basis_string uw_Basis_atom(uw_context ctx, uw_Basis_string s) { adam@1755: char *p; adam@1755: adam@1755: for (p = s; *p; ++p) { adam@1755: char c = *p; adam@1801: if (!isalnum((int)c) && c != '+' && c != '-' && c != '.' && c != '%' && c != '#') adam@1755: uw_error(ctx, FATAL, "Disallowed character in CSS atom"); adam@1755: } adam@1755: adam@1755: return s; adam@1755: } adam@1755: adam@1755: uw_Basis_string uw_Basis_css_url(uw_context ctx, uw_Basis_string s) { adam@1755: char *p; adam@1755: adam@1755: for (p = s; *p; ++p) { adam@1755: char c = *p; adam@1801: if (!isalnum((int)c) && c != ':' && c != '/' && c != '.' && c != '_' && c != '+' adam@1755: && c != '-' && c != '%' && c != '?' && c != '&' && c != '=' && c != '#') adam@1755: uw_error(ctx, FATAL, "Disallowed character in CSS URL"); adam@1755: } adam@1755: adam@1755: return s; adam@1755: } adam@1755: adam@1755: uw_Basis_string uw_Basis_property(uw_context ctx, uw_Basis_string s) { adam@1755: char *p; adam@1755: adam@1755: if (!*s) adam@1755: uw_error(ctx, FATAL, "Empty CSS property"); adam@1755: adam@1801: if (!islower((int)s[0]) && s[0] != '_') adam@1755: uw_error(ctx, FATAL, "Bad initial character in CSS property"); adam@1755: adam@1755: for (p = s; *p; ++p) { adam@1755: char c = *p; adam@1801: if (!islower((int)c) && !isdigit((int)c) && c != '_' && c != '-') adam@1755: uw_error(ctx, FATAL, "Disallowed character in CSS property"); adam@1755: } adam@1755: adam@1755: return s; adam@1755: } adam@1787: adam@1787: uw_Basis_string uw_Basis_fieldName(uw_context ctx, uw_Basis_postField f) { adam@1787: return f.name; adam@1787: } adam@1787: adam@1787: uw_Basis_string uw_Basis_fieldValue(uw_context ctx, uw_Basis_postField f) { adam@1787: return f.value; adam@1787: } adam@1787: adam@1787: uw_Basis_string uw_Basis_remainingFields(uw_context ctx, uw_Basis_postField f) { adam@1787: return f.remaining; adam@1787: } adam@1787: adam@1787: uw_Basis_postField *uw_Basis_firstFormField(uw_context ctx, uw_Basis_string s) { adam@1788: char *unurl; adam@1787: uw_Basis_postField *f; adam@1787: adam@1788: if (!ctx->hasPostBody) adam@1788: uw_error(ctx, FATAL, "firstFormField called when there is no POST body"); adam@1788: adam@1788: if (s < ctx->postBody.data || s >= ctx->postBody.data + ctx->postBody.len) adam@1787: return NULL; adam@1787: adam@1787: f = uw_malloc(ctx, sizeof(uw_Basis_postField)); adam@1788: unurl = s; adam@1787: f->name = uw_Basis_unurlifyString(ctx, &unurl); adam@1788: s = strchr(s, 0); adam@1788: if (!s) adam@1788: uw_error(ctx, FATAL, "firstFormField: Missing null terminator"); adam@1788: ++s; adam@1788: unurl = s; adam@1788: f->value = uw_Basis_unurlifyString(ctx, &unurl); adam@1788: s = strchr(s, 0); adam@1788: if (!s) adam@1788: uw_error(ctx, FATAL, "firstFormField: Missing null terminator"); adam@1788: f->remaining = s+1; adam@1787: adam@1787: return f; adam@1787: }