adamc@1094: #define _XOPEN_SOURCE 600 adamc@436: 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 adamc@102: adamc@667: #include adamc@667: adamc@102: #include "types.h" adamc@102: adamc@311: uw_unit uw_unit_v = {}; 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: adamc@667: typedef struct { adamc@1113: size_t max; adamc@667: char *start, *front, *back; adamc@667: } buf; adamc@667: adamc@1113: static void buf_init(size_t max, buf *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: adamc@667: static void buf_free(buf *b) { adamc@667: free(b->start); adamc@667: } adamc@667: adamc@667: static void buf_reset(buf *b) { adamc@667: b->front = b->start; adamc@667: } adamc@667: adamc@1113: static int buf_check(buf *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: adamc@1113: static void ctx_buf_check(uw_context ctx, const char *kind, buf *b, size_t extra) { adamc@1113: if (buf_check(b, extra)) adamc@1113: uw_error(ctx, FATAL, "Memory limit exceeded (%s)", kind); adamc@667: } adamc@667: adamc@667: static size_t buf_used(buf *b) { adamc@667: return b->front - b->start; adamc@667: } adamc@667: adamc@667: static size_t buf_avail(buf *b) { adamc@667: return b->back - b->start; adamc@667: } adamc@667: adamc@1113: static int buf_append(buf *b, const char *s, size_t len) { adamc@1113: if (buf_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: adamc@1113: static void ctx_buf_append(uw_context ctx, const char *kind, buf *b, const char *s, size_t len) { adamc@1113: ctx_buf_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; adamc@682: buf 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: adamc@667: static pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER; adamc@667: 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: } adamc@1113: else if (n_clients >= uw_clients_max) adamc@1113: return NULL; adamc@667: 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); adamc@1113: buf_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); adamc@682: buf_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@689: adamc@685: } adamc@685: adamc@856: static const char begin_msgs[] = "Content-type: text/plain\r\n\r\n"; adamc@667: adamc@682: static client *find_client(unsigned id) { adamc@667: client *c; adamc@667: adamc@667: pthread_mutex_lock(&clients_mutex); adamc@667: adamc@667: if (id >= n_clients) { adamc@667: pthread_mutex_unlock(&clients_mutex); adamc@668: return NULL; adamc@667: } adamc@667: adamc@667: c = clients[id]; adamc@667: adamc@668: 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"; adamc@1065: 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: 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) { adamc@667: close(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); adamc@667: close(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); adamc@682: close(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: adamc@682: if (buf_used(&c->msgs) > 0) { adamc@863: send(sock, on_success, strlen(on_success)); adamc@863: send(sock, begin_msgs, sizeof(begin_msgs) - 1); adamc@863: send(sock, c->msgs.start, buf_used(&c->msgs)); adamc@682: buf_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: adamc@685: static void client_send(client *c, buf *msg) { 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); adamc@863: c->send(c->sock, msg->start, buf_used(msg)); adamc@863: c->close(c->sock); adamc@682: c->sock = -1; adamc@1113: } else if (buf_append(&c->msgs, msg->start, buf_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@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@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: adamc@1114: int uw_time = 0; adamc@1114: adamc@668: adamc@667: // Single-request state adamc@667: adamc@167: #define ERROR_BUF_LEN 1024 adamc@167: 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; adamc@671: buf 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; adamc@771: uw_callback commit, rollback, 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; adamc@1094: adamc@854: char *(*get_header)(void *, const char *); adamc@854: void *get_header_data; adamc@457: adamc@666: buf outHeaders, page, heap, script; adamc@1065: int returning_indirectly; adamc@759: input *inputs, *subinputs, *cur_container; adamc@1094: size_t sz_inputs, n_subinputs, used_subinputs; adamc@167: adamc@565: int source_count; adamc@562: adamc@272: void *db; 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: adamc@736: int needs_push, needs_sig; 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: adamc@167: char error_message[ERROR_BUF_LEN]; 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: adamc@687: uw_context uw_init() { adamc@311: uw_context ctx = malloc(sizeof(struct uw_context)); adamc@136: adamc@1094: ctx->app = NULL; adamc@1094: adamc@854: ctx->get_header = NULL; adamc@854: ctx->get_header_data = NULL; adamc@457: adamc@1113: buf_init(uw_headers_max, &ctx->outHeaders, 0); adamc@1113: buf_init(uw_page_max, &ctx->page, 0); adamc@1065: ctx->returning_indirectly = 0; adamc@1113: buf_init(uw_heap_max, &ctx->heap, 0); adamc@1113: buf_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; 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; 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: 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: adamc@666: buf_free(&ctx->outHeaders); adamc@666: buf_free(&ctx->script); adamc@666: buf_free(&ctx->page); adamc@666: buf_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) adamc@671: buf_free(&ctx->deltas[i].msgs); 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); adamc@1061: adamc@136: free(ctx); adamc@136: } adamc@136: adamc@562: void uw_reset_keep_error_message(uw_context ctx) { adamc@666: buf_reset(&ctx->outHeaders); adamc@666: buf_reset(&ctx->script); adamc@667: ctx->script.start[0] = 0; adamc@666: buf_reset(&ctx->page); adamc@1065: ctx->returning_indirectly = 0; adamc@666: buf_reset(&ctx->heap); adamc@323: ctx->regions = NULL; adamc@425: ctx->cleanup_front = ctx->cleanup; adamc@565: ctx->source_count = 0; 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 = ""; 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)); adamc@756: ctx->used_subinputs = 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: 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: 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: adamc@682: uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) { adamc@854: return ctx->get_header(ctx->get_header_data, h); adamc@682: } adamc@682: adamc@682: void uw_login(uw_context ctx) { adamc@693: if (ctx->needs_push) { adamc@682: char *id_s, *pass_s; adamc@682: adamc@682: if ((id_s = uw_Basis_requestHeader(ctx, "UrWeb-Client")) adamc@682: && (pass_s = uw_Basis_requestHeader(ctx, "UrWeb-Pass"))) { adamc@682: unsigned id = atoi(id_s); adamc@682: int pass = atoi(pass_s); adamc@682: client *c = find_client(id); adamc@682: adamc@682: if (c == NULL) adamc@682: uw_error(ctx, FATAL, "Unknown client ID in HTTP headers (%s, %s)", id_s, pass_s); adamc@682: else { adamc@685: use_client(c); adamc@682: ctx->client = c; adamc@682: adamc@682: if (c->mode != USED) adamc@682: uw_error(ctx, FATAL, "Stale client ID (%u) in subscription request", id); adamc@682: if (c->pass != pass) adamc@682: uw_error(ctx, FATAL, "Wrong client password (%u, %d) in subscription request", id, pass); adamc@682: } adamc@682: } else { adamc@682: client *c = new_client(); adamc@1113: adamc@1113: if (c == NULL) adamc@1113: uw_error(ctx, FATAL, "Limit exceeded on number of message-passing clients"); adamc@1113: adamc@685: use_client(c); adamc@1121: uw_copy_client_data(c->data, ctx->client_data); adamc@682: ctx->client = c; adamc@864: } 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: adamc@671: if (r == 0) { adamc@1094: if (ctx->app->db_begin(ctx)) adamc@671: uw_error(ctx, BOUNDED_RETRY, "Error running SQL BEGIN"); adamc@682: adamc@1094: ctx->app->handle(ctx, path); adamc@671: } adamc@671: adamc@671: return r; 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) adamc@759: ctx->subinputs[ctx->used_subinputs++].kind = UNUSED; 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) { adamc@801: uw_set_error(ctx, "Bad subform name %s", value); adamc@801: return -1; adamc@801: } adamc@801: adamc@1094: if (n >= ctx->app->inputs_len) { adamc@801: uw_set_error(ctx, "For subform name %s, index %d is out of range", 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) { adamc@801: uw_set_error(ctx, "Bad subforms name %s", value); adamc@801: return -1; adamc@801: } adamc@801: adamc@1094: if (n >= ctx->app->inputs_len) { adamc@801: uw_set_error(ctx, "For subforms name %s, index %d is out of range", 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: adamc@801: if (n < 0) { adamc@1038: if (!strcmp(name, "null")) adamc@1038: return 0; adamc@801: uw_set_error(ctx, "Bad input name %s", name); adamc@801: return -1; adamc@801: } adamc@801: adamc@1094: if (n >= ctx->app->inputs_len) { adamc@801: uw_set_error(ctx, "For input name %s, index %d is out of range", 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) { adamc@801: uw_set_error(ctx, "Bad file input name %s", name); adamc@801: return -1; adamc@801: } adamc@801: adamc@1094: if (n >= ctx->app->inputs_len) { adamc@801: uw_set_error(ctx, "For file input name %s, index %d is out of range", 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: adamc@667: adamc@1113: static void buf_check_ctx(uw_context ctx, const char *kind, buf *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; adamc@666: uw_error(ctx, UNLIMITED_RETRY, "Couldn't allocate new %s contiguously", desc); 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) { adamc@1113: buf_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: adamc@311: void *uw_malloc(uw_context ctx, size_t len) { adamc@136: void *result; adamc@136: adamc@311: uw_check_heap(ctx, len); adamc@136: adamc@666: result = ctx->heap.front; adamc@666: ctx->heap.front += len; adamc@136: return result; 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) { adamc@1134: printf("Headers: %lu/%lu\n", (unsigned long)buf_used(&ctx->outHeaders), (unsigned long)buf_avail(&ctx->outHeaders)); adamc@1134: printf("Script: %lu/%lu\n", (unsigned long)buf_used(&ctx->script), (unsigned long)buf_avail(&ctx->script)); adamc@1134: printf("Page: %lu/%lu\n", (unsigned long)buf_used(&ctx->page), (unsigned long)buf_avail(&ctx->page)); adamc@1134: printf("Heap: %lu/%lu\n", (unsigned long)buf_used(&ctx->heap), (unsigned long)buf_avail(&ctx->heap)); adamc@324: } adamc@324: adamc@311: int uw_send(uw_context ctx, int sock) { adamc@666: int n = uw_really_send(sock, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start); adamc@462: adamc@462: if (n < 0) adamc@462: return n; adamc@462: adamc@462: n = uw_really_send(sock, "\r\n", 2); adamc@462: adamc@462: if (n < 0) adamc@462: return n; adamc@462: adamc@666: return uw_really_send(sock, ctx->page.start, ctx->page.front - ctx->page.start); 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) { adamc@1113: ctx_buf_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: adamc@1110: void uw_clear_headers(uw_context ctx) { adamc@1110: buf_reset(&ctx->outHeaders); adamc@1110: } adamc@1110: adamc@562: static void uw_check_script(uw_context ctx, size_t extra) { adamc@1113: ctx_buf_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: adamc@645: const char *uw_Basis_get_script(uw_context ctx, uw_unit u) { adamc@916: return ""; adamc@565: } adamc@565: 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 { adamc@1042: char *r = uw_malloc(ctx, 22 + strlen(s)); adamc@1042: sprintf(r, " onunload='unload();%s'", 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)); adamc@736: sprintf(r, "client_id=%u;client_pass=%d;url_prefix=\"%s\";timeout=%d;%s%s%slistener();", 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: adamc@574: uw_check_heap(ctx, strlen(s) * 4 + 2); adamc@574: adamc@666: r = s2 = ctx->heap.front; adamc@574: *s2++ = '"'; adamc@574: adamc@574: for (; *s; s++) { adamc@574: 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; adamc@574: default: adamc@574: if (isprint(c)) adamc@574: *s2++ = c; adamc@574: else { adamc@574: sprintf(s2, "\\%3o", 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: adamc@1025: uw_Basis_string uw_Basis_jsifyChar(uw_context ctx, uw_Basis_char c) { adamc@1025: char *r, *s2; adamc@1025: adamc@1025: uw_check_heap(ctx, 6); 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; adamc@1025: default: adamc@1025: if (isprint(c)) adamc@1025: *s2++ = c; adamc@1025: else { adamc@1025: sprintf(s2, "\\%3o", 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: adamc@574: uw_check_script(ctx, strlen(s) * 4 + 2); adamc@574: adamc@666: r = s2 = ctx->script.front; adamc@574: *s2++ = '"'; adamc@574: adamc@574: for (; *s; s++) { adamc@574: 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; adamc@574: default: adamc@574: if (isprint(c)) adamc@574: *s2++ = c; adamc@574: else { adamc@574: sprintf(s2, "\\%3o", 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: adamc@577: uw_Basis_int 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: adamc@970: uw_check_script(ctx, 18 + INTS_MAX + s_len); adamc@970: sprintf(ctx->script.front, "var s%d=sc(exec(%n", 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: adamc@565: return ctx->source_count++; adamc@117: } adamc@117: adamc@577: uw_unit uw_Basis_set_client_source(uw_context ctx, uw_Basis_int n, uw_Basis_string s) { adamc@577: int len; adamc@577: size_t s_len = strlen(s); adamc@577: adamc@970: uw_check_script(ctx, 12 + INTS_MAX + s_len); adamc@970: sprintf(ctx->script.front, "sv(s%d,exec(%n", (int)n, &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) { adamc@1113: ctx_buf_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; adamc@117: } adamc@117: adamc@311: void uw_writec(uw_context ctx, char c) { adamc@311: uw_check(ctx, 1); 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; adamc@136: sprintf(result, "%g%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@137: 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: 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@488: return uw_Basis_urlifyInt(ctx, t); adamc@488: } adamc@488: 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@666: 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: 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) { adamc@488: return uw_Basis_unurlifyInt(ctx, s); 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@1023: else if (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) adamc@311: uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", s2); adamc@136: *s1 = n; adamc@136: s2 += 2; adamc@136: break; 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: 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: adamc@311: char *uw_Basis_htmlifyString(uw_context ctx, uw_Basis_string 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" adamc@436: adamc@436: uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) { adamc@436: size_t len; adamc@436: char *r; adamc@436: struct tm stm; adamc@436: adamc@436: if (localtime_r(&t, &stm)) { adamc@436: uw_check_heap(ctx, TIMES_MAX); adamc@666: r = ctx->heap.front; adamc@436: len = strftime(r, TIMES_MAX, TIME_FMT, &stm); adamc@666: ctx->heap.front += len+1; adamc@436: return r; adamc@436: } else adamc@436: return "Invalid time"; adamc@436: } adamc@436: adamc@436: uw_unit uw_Basis_htmlifyTime_w(uw_context ctx, uw_Basis_time t) { adamc@436: size_t len; adamc@436: char *r; adamc@436: struct tm stm; adamc@436: adamc@436: if (localtime_r(&t, &stm)) { adamc@436: uw_check(ctx, TIMES_MAX); adamc@666: r = ctx->page.front; adamc@436: len = strftime(r, TIMES_MAX, TIME_FMT, &stm); adamc@666: ctx->page.front += len; adamc@436: } else { adamc@436: uw_check(ctx, 20); adamc@666: strcpy(ctx->page.front, "Invalid time"); adamc@666: ctx->page.front += 19; adamc@436: } adamc@436: adamc@436: return uw_unit_v; adamc@436: } adamc@436: adamc@821: uw_Basis_char uw_Basis_strsub(uw_context ctx, uw_Basis_string s, uw_Basis_int n) { adamc@821: if (n >= 0 && n < strlen(s)) adamc@821: return s[n]; adamc@821: else adamc@821: uw_error(ctx, FATAL, "Out-of-bounds strsub"); adamc@821: } adamc@821: adamc@821: uw_Basis_string uw_Basis_strsuffix(uw_context ctx, uw_Basis_string s, uw_Basis_int n) { adamc@821: if (n >= 0 && n < strlen(s)) adamc@821: return &s[n]; adamc@821: else adamc@821: uw_error(ctx, FATAL, "Out-of-bounds strsuffix"); 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: 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: 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: 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: adamc@879: char *uw_sqlfmtFloat = "%g::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: adamc@879: int uw_Estrings = 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@280: if (isprint(c)) adamc@280: *s2++ = c; adamc@879: else if (uw_Estrings) { adamc@737: sprintf(s2, "\\%03o", c); adamc@280: s2 += 4; adamc@280: } adamc@879: else adamc@879: uw_error(ctx, FATAL, "Non-printable character %u in string to SQLify", c); 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@1011: if (isprint(c)) adamc@1011: *s2++ = c; adamc@1011: else if (uw_Estrings) { adamc@1011: sprintf(s2, "\\%03o", 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: adamc@737: 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: adamc@879: uw_check_heap(ctx, b.size * 5 + 3 + uw_Estrings + strlen(uw_sqlsuffixBlob)); adamc@737: adamc@737: r = s2 = ctx->heap.front; adamc@879: if (uw_Estrings) adamc@879: *s2++ = 'E'; adamc@737: *s2++ = '\''; adamc@737: adamc@737: for (i = 0; i < b.size; ++i) { adamc@737: char c = b.data[i]; adamc@737: adamc@737: switch (c) { adamc@737: case '\'': adamc@885: if (uw_Estrings) adamc@885: strcpy(s2, "\\'"); adamc@885: else adamc@885: strcpy(s2, "''"); adamc@737: s2 += 2; adamc@737: break; adamc@737: case '\\': adamc@885: if (uw_Estrings) { adamc@885: strcpy(s2, "\\\\\\\\"); adamc@885: s2 += 4; adamc@885: } else adamc@885: *s2++ = '\\'; adamc@737: break; adamc@737: default: adamc@737: if (isprint(c)) adamc@737: *s2++ = c; adamc@879: else if (uw_Estrings) { adamc@737: sprintf(s2, "\\\\%03o", c); adamc@737: s2 += 5; adamc@737: } adamc@879: else adamc@879: uw_error(ctx, FATAL, "Non-printable character %u in blob to SQLify", c); adamc@737: } adamc@737: } 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; adamc@682: struct tm stm; adamc@682: adamc@682: if (localtime_r(&t, &stm)) { adamc@682: s = uw_malloc(ctx, TIMES_MAX); adamc@682: len = strftime(s, TIMES_MAX, TIME_FMT, &stm); adamc@682: r = uw_malloc(ctx, len + 14); adamc@682: sprintf(r, "'%s'::timestamp", s); 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; adamc@439: struct tm stm; adamc@439: adamc@439: if (localtime_r(&t, &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: 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: adamc@436: uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) { adamc@436: size_t len; adamc@436: char *r; adamc@436: struct tm stm; adamc@436: adamc@436: if (localtime_r(&t, &stm)) { adamc@436: uw_check_heap(ctx, TIMES_MAX); adamc@666: r = ctx->heap.front; adamc@436: len = strftime(r, TIMES_MAX, TIME_FMT, &stm); adamc@666: ctx->heap.front += len+1; adamc@436: return r; adamc@436: } else adamc@436: return ""; adamc@436: } adamc@288: 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); adamc@436: struct tm stm; 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)); adamc@439: *r = mktime(&stm); 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)); adamc@439: *r = mktime(&stm); 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)); adamc@439: *r = mktime(&stm); adamc@439: return r; adamc@439: } adamc@439: else adamc@439: return NULL; adamc@439: } adamc@436: } adamc@436: 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 adamc@311: uw_error(ctx, FATAL, "Can't parse int: %s", 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) adamc@682: uw_error(ctx, FATAL, "Can't parse channel: %s", 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 adamc@682: uw_error(ctx, FATAL, "Can't parse client: %s", 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 adamc@311: uw_error(ctx, FATAL, "Can't parse float: %s", 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) adamc@821: uw_error(ctx, FATAL, "Can't parse char: %s", 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 adamc@311: uw_error(ctx, FATAL, "Can't parse bool: %s", s); adamc@293: } adamc@436: 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 = {}; adamc@436: adamc@438: if (dot) { adamc@438: *dot = 0; adamc@438: if (strptime(s, TIME_FMT_PG, &stm)) { adamc@438: *dot = '.'; adamc@438: return mktime(&stm); adamc@438: } adamc@438: else { adamc@438: *dot = '.'; adamc@438: uw_error(ctx, FATAL, "Can't parse time: %s", s); adamc@438: } adamc@438: } adamc@438: else { adamc@439: if (strptime(s, TIME_FMT_PG, &stm) == end) adamc@439: return mktime(&stm); adamc@439: else if (strptime(s, TIME_FMT, &stm) == end) adamc@438: return mktime(&stm); adamc@438: else adamc@438: uw_error(ctx, FATAL, "Can't parse time: %s", s); adamc@438: } adamc@436: } adamc@457: adamc@939: uw_Basis_time uw_Basis_unsqlTime(uw_context ctx, uw_Basis_string s) { adamc@939: char *dot = strchr(s, '.'), *end = strchr(s, 0); adamc@939: struct tm stm = {}; adamc@939: adamc@939: if (dot) { adamc@939: *dot = 0; adamc@939: if (strptime(s, TIME_FMT_PG, &stm)) { adamc@939: *dot = '.'; adamc@939: return mktime(&stm); adamc@939: } adamc@939: else { adamc@939: *dot = '.'; adamc@939: uw_error(ctx, FATAL, "Can't parse time: %s", s); adamc@939: } adamc@939: } adamc@939: else { adamc@939: if (strptime(s, TIME_FMT_PG, &stm) == end) { adamc@939: return mktime(&stm); adamc@939: } else if (strptime(s, TIME_FMT, &stm) == end) { adamc@939: return mktime(&stm); adamc@939: } else adamc@939: uw_error(ctx, FATAL, "Can't parse time: %s", s); adamc@939: } 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@742: } else if (isdigit(s[1]) && isdigit(s[2]) && isdigit(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: adamc@1052: #define THE_PAST "expires=Mon, 01-01-1970 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: 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]; adamc@1050: struct tm tm; adamc@1050: adamc@1050: gmtime_r(expires, &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"); 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"); 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); adamc@1113: buf_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; adamc@682: buf_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: adamc@1113: ctx_buf_append(ctx, "messages", &d->msgs, pre, preLen); adamc@1113: ctx_buf_append(ctx, "messages", &d->msgs, msg, len); adamc@1113: ctx_buf_append(ctx, "messages", &d->msgs, "\n", 1); adamc@671: adamc@671: return uw_unit_v; adamc@668: } adamc@671: adamc@1131: int uw_rollback(uw_context ctx) { 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) adamc@1131: ctx->transactionals[i].free(ctx->transactionals[i].data); adamc@1131: adamc@1131: return ctx->app->db_rollback(ctx); adamc@1131: } adamc@1131: adamc@671: void uw_commit(uw_context ctx) { adamc@1104: int i; adamc@1104: adamc@1131: if (uw_has_error(ctx)) { adamc@1131: uw_rollback(ctx); adamc@1131: return; adamc@1131: } adamc@1131: 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)) { adamc@1131: uw_rollback(ctx); adamc@1131: return; 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)) { adamc@1131: uw_rollback(ctx); adamc@1131: return; adamc@1131: } adamc@1131: } adamc@1131: adamc@1131: if (ctx->app->db_commit(ctx)) { adamc@1131: uw_set_error_message(ctx, "Error running SQL COMMIT"); adamc@1131: return; 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: adamc@682: assert (c != NULL && c->mode == USED); adamc@682: adamc@685: client_send(c, &d->msgs); 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) adamc@1083: ctx->transactionals[i].free(ctx->transactionals[i].data); adamc@916: adamc@916: // Splice script data into appropriate part of page adamc@1065: if (ctx->returning_indirectly || ctx->script_header[0] == 0) { adamc@995: char *start = strstr(ctx->page.start, ""); adamc@995: if (start) { adamc@995: memmove(start, start + 4, buf_used(&ctx->page) - (start - ctx->page.start) - 4); adamc@995: ctx->page.front -= 4; adamc@995: } adamc@995: } else if (buf_used(&ctx->script) == 0) { adamc@916: size_t len = strlen(ctx->script_header); adamc@916: char *start = strstr(ctx->page.start, ""); adamc@916: if (start) { adamc@1113: ctx_buf_check(ctx, "page", &ctx->page, buf_used(&ctx->page) - 4 + len); adamc@917: start = strstr(ctx->page.start, ""); adamc@916: memmove(start + len, start + 4, buf_used(&ctx->page) - (start - ctx->page.start) - 3); adamc@916: ctx->page.front += len - 4; adamc@916: memcpy(start, ctx->script_header, len); adamc@916: } adamc@916: } else { adamc@916: size_t lenH = strlen(ctx->script_header), len = buf_used(&ctx->script); adamc@916: size_t lenP = lenH + 40 + len; adamc@916: char *start = strstr(ctx->page.start, ""); adamc@916: if (start) { adamc@1113: ctx_buf_check(ctx, "page", &ctx->page, buf_used(&ctx->page) - 4 + lenP); adamc@917: start = strstr(ctx->page.start, ""); adamc@916: memmove(start + lenP, start + 4, buf_used(&ctx->page) - (start - ctx->page.start) - 3); adamc@916: ctx->page.front += lenP - 4; adamc@916: memcpy(start, ctx->script_header, lenH); adamc@916: memcpy(start + lenH, "", 9); adamc@916: } adamc@916: } 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, adamc@771: uw_callback 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); 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@684: uw_reset(ctx); adamc@683: while (fk == UNLIMITED_RETRY) { 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) { adamc@1094: if (ctx->app->db_begin(ctx)) adamc@687: uw_error(ctx, FATAL, "Error running SQL BEGIN"); 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@1065: if (!isgraph(*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: adamc@770: int mime_format(const char *s) { adamc@770: for (; *s; ++s) adamc@770: if (!isalnum(*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: adamc@734: uw_Basis_string uw_unnull(uw_Basis_string s) { adamc@734: return s ? s : ""; adamc@734: } adamc@734: adamc@734: extern int uw_hash_blocksize; 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: adamc@734: uw_Basis_string uw_Basis_sigString(uw_context ctx, uw_unit u) { 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: 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; adamc@741: adamc@1065: ctx->returning_indirectly = 1; adamc@741: buf_reset(&ctx->outHeaders); adamc@741: buf_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); adamc@741: uw_write_header(ctx, "\r\nContent-Length: "); adamc@1113: ctx_buf_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"); adamc@741: adamc@1113: ctx_buf_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: adamc@1065: __attribute__((noreturn)) void uw_redirect(uw_context ctx, uw_Basis_string url) { adamc@1065: cleanup *cl; adamc@1066: char *s; adamc@1065: adamc@1065: ctx->returning_indirectly = 1; adamc@1066: buf_reset(&ctx->page); adamc@1113: ctx_buf_check(ctx, "page", &ctx->page, buf_used(&ctx->outHeaders)+1); adamc@1066: memcpy(ctx->page.start, ctx->outHeaders.start, buf_used(&ctx->outHeaders)); adamc@1066: ctx->page.start[buf_used(&ctx->outHeaders)] = 0; adamc@1065: buf_reset(&ctx->outHeaders); adamc@1065: adamc@1065: uw_write_header(ctx, on_redirect); adamc@1066: adamc@1066: s = strchr(ctx->page.start, '\n'); adamc@1066: if (s) { adamc@1066: char *s2; adamc@1134: for (++s; (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"); 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) { adamc@874: uw_Basis_string r = uw_malloc(ctx, strlen(s) + 1); 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] == '\\') { adamc@874: if (s[1] == '\\') { adamc@874: *r++ = '\\'; adamc@874: *r++ = '\\'; adamc@874: ++s; adamc@874: } else if (s[1] == '\'') { adamc@874: *r++ = '\\'; adamc@874: *r++ = '\''; adamc@874: ++s; adamc@874: } else adamc@874: *r++ = '\''; adamc@874: } else adamc@874: *r++ = s[0]; adamc@874: } adamc@874: if (*s == 0) break; adamc@874: } else if (s[0] == 'T' && s[1] == '.') adamc@874: ++s; adamc@874: else adamc@874: *r++ = s[0]; adamc@874: } adamc@874: adamc@874: return r; 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: adamc@1050: const uw_Basis_time minTime = 0; adamc@1050: adamc@1006: uw_Basis_time uw_Basis_now(uw_context ctx) { adamc@1006: return time(NULL); adamc@1006: } adamc@1061: adamc@1138: uw_Basis_time uw_Basis_minusSeconds(uw_context ctx, uw_Basis_time tm, uw_Basis_int n) { adamc@1138: return tm - n; adamc@1138: } adamc@1138: 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@1063: return !!isalnum(c); adamc@1061: } adamc@1061: adamc@1061: uw_Basis_bool uw_Basis_isalpha(uw_context ctx, uw_Basis_char c) { adamc@1063: return !!isalpha(c); adamc@1061: } adamc@1061: adamc@1061: uw_Basis_bool uw_Basis_isblank(uw_context ctx, uw_Basis_char c) { adamc@1063: return !!isblank(c); adamc@1061: } adamc@1061: adamc@1061: uw_Basis_bool uw_Basis_iscntrl(uw_context ctx, uw_Basis_char c) { adamc@1063: return !!iscntrl(c); adamc@1061: } adamc@1061: adamc@1061: uw_Basis_bool uw_Basis_isdigit(uw_context ctx, uw_Basis_char c) { adamc@1063: return !!isdigit(c); adamc@1061: } adamc@1061: adamc@1061: uw_Basis_bool uw_Basis_isgraph(uw_context ctx, uw_Basis_char c) { adamc@1063: return !!isgraph(c); adamc@1061: } adamc@1061: adamc@1061: uw_Basis_bool uw_Basis_islower(uw_context ctx, uw_Basis_char c) { adamc@1063: return !!islower(c); adamc@1061: } adamc@1061: adamc@1061: uw_Basis_bool uw_Basis_isprint(uw_context ctx, uw_Basis_char c) { adamc@1063: return !!isprint(c); adamc@1061: } adamc@1061: adamc@1061: uw_Basis_bool uw_Basis_ispunct(uw_context ctx, uw_Basis_char c) { adamc@1063: return !!ispunct(c); adamc@1061: } adamc@1061: adamc@1061: uw_Basis_bool uw_Basis_isspace(uw_context ctx, uw_Basis_char c) { adamc@1063: return !!isspace(c); adamc@1061: } adamc@1063: adamc@1061: uw_Basis_bool uw_Basis_isupper(uw_context ctx, uw_Basis_char c) { adamc@1063: return !!isupper(c); adamc@1061: } adamc@1061: adamc@1061: uw_Basis_bool uw_Basis_isxdigit(uw_context ctx, uw_Basis_char c) { adamc@1063: return !!isxdigit(c); adamc@1061: } adamc@1061: adamc@1061: uw_Basis_char uw_Basis_tolower(uw_context ctx, uw_Basis_char c) { adamc@1061: return tolower(c); adamc@1061: } adamc@1061: adamc@1061: uw_Basis_char uw_Basis_toupper(uw_context ctx, uw_Basis_char c) { adamc@1061: return toupper(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: adamc@1120: uw_Basis_unit uw_Basis_debug(uw_context ctx, uw_Basis_string s) { adamc@1120: fprintf(stderr, "%s\n", s); adamc@1120: adamc@1120: return uw_unit_v; adamc@1120: }