adamc@436: #define _XOPEN_SOURCE adamc@436: adamc@117: #include adamc@102: #include adamc@117: #include adamc@457: #include adamc@106: #include adamc@167: #include adamc@167: #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@667: int uw_really_send(int sock, const void *buf, size_t len) { adamc@667: while (len > 0) { adamc@667: size_t n = send(sock, buf, len, 0); 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@667: char *start, *front, *back; adamc@667: } buf; adamc@667: adamc@667: static void buf_init(buf *b, size_t s) { 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@667: static void 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@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@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@668: static void buf_append(buf *b, const char *s, size_t len) { adamc@672: buf_check(b, len+1); 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@672: typedef struct channel_list { adamc@672: struct channel *data; adamc@672: struct channel_list *next; adamc@672: } channel_list; adamc@672: adamc@667: typedef struct client { adamc@667: size_t id; adamc@667: usage mode; adamc@667: union { adamc@667: struct client *next; adamc@667: struct { adamc@667: pthread_mutex_t lock; adamc@667: int pass; adamc@667: buf msgs; adamc@667: int sock; adamc@667: time_t last_contact; adamc@671: unsigned refcount; adamc@672: channel_list *channels; adamc@667: } used; adamc@667: } data; adamc@667: } client; adamc@667: adamc@672: typedef struct client_list { adamc@672: client *data; adamc@672: struct client_list *next; adamc@672: } client_list; adamc@672: adamc@672: typedef struct channel { adamc@672: size_t id; adamc@672: usage mode; adamc@672: union { adamc@672: struct channel *next; adamc@672: struct { adamc@672: pthread_mutex_t lock; adamc@672: client_list *clients; adamc@672: unsigned refcount; adamc@672: } used; adamc@672: } data; adamc@672: } channel; adamc@672: adamc@672: adamc@672: // Persistent client state adamc@672: adamc@667: static client **clients, *clients_free; adamc@667: static size_t n_clients; adamc@667: adamc@667: static pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER; adamc@667: adamc@667: static client *uw_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@667: clients_free = clients_free->data.next; adamc@667: } 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@667: clients[n_clients-1] = c; adamc@667: } adamc@667: adamc@667: c->mode = USED; adamc@667: pthread_mutex_init(&c->data.used.lock, NULL); adamc@667: c->data.used.pass = rand(); adamc@667: c->data.used.sock = -1; adamc@667: c->data.used.last_contact = time(NULL); adamc@672: buf_init(&c->data.used.msgs, 0); adamc@671: c->data.used.refcount = 0; adamc@672: c->data.used.channels = NULL; adamc@667: adamc@667: pthread_mutex_unlock(&clients_mutex); adamc@667: adamc@667: return c; adamc@667: } adamc@667: adamc@667: static const char begin_msgs[] = "HTTP/1.1 200 OK\r\nContent-type: text/plain\r\n\r\n"; adamc@667: adamc@668: static client *uw_find_client(size_t 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@667: if (c->mode != USED) { adamc@667: pthread_mutex_unlock(&clients_mutex); adamc@668: return NULL; adamc@668: } adamc@671: adamc@671: pthread_mutex_lock(&c->data.used.lock); adamc@671: ++c->data.used.refcount; adamc@671: pthread_mutex_unlock(&c->data.used.lock); adamc@668: pthread_mutex_unlock(&clients_mutex); adamc@668: return c; adamc@668: } adamc@668: adamc@671: static void uw_release_client(client *c) { adamc@671: pthread_mutex_lock(&c->data.used.lock); adamc@671: --c->data.used.refcount; adamc@671: pthread_mutex_unlock(&c->data.used.lock); adamc@671: } adamc@671: adamc@668: void uw_client_connect(size_t id, int pass, int sock) { adamc@668: client *c = uw_find_client(id); adamc@668: adamc@668: if (c == NULL) { adamc@667: close(sock); adamc@668: fprintf(stderr, "Out-of-bounds client request (%d)\n", (int)id); adamc@667: return; adamc@667: } adamc@667: adamc@671: uw_release_client(c); adamc@671: adamc@667: pthread_mutex_lock(&c->data.used.lock); adamc@667: adamc@667: if (pass != c->data.used.pass) { adamc@667: pthread_mutex_unlock(&c->data.used.lock); adamc@667: close(sock); adamc@667: fprintf(stderr, "Wrong client password (%d)\n", (int)id); adamc@667: return; adamc@667: } adamc@667: adamc@667: if (c->data.used.sock != -1) { adamc@673: close(c->data.used.sock); adamc@673: c->data.used.sock = -1; adamc@667: } adamc@667: adamc@667: c->data.used.last_contact = time(NULL); adamc@667: adamc@667: if (buf_used(&c->data.used.msgs) > 0) { adamc@667: uw_really_send(sock, begin_msgs, sizeof(begin_msgs) - 1); adamc@667: uw_really_send(sock, c->data.used.msgs.start, buf_used(&c->data.used.msgs)); adamc@667: close(sock); adamc@667: } adamc@668: else adamc@668: c->data.used.sock = sock; adamc@667: adamc@667: pthread_mutex_unlock(&c->data.used.lock); adamc@667: } adamc@667: adamc@672: adamc@667: static void uw_free_client(client *c) { adamc@672: channel_list *chs; adamc@672: adamc@667: printf("Freeing client %d\n", c->id); adamc@667: adamc@672: if (c->mode == USED) { adamc@672: pthread_mutex_lock(&c->data.used.lock); adamc@667: adamc@672: for (chs = c->data.used.channels; chs; ) { adamc@672: client_list *prev, *cs; adamc@672: adamc@672: channel *ch = chs->data; adamc@672: channel_list *tmp = chs->next; adamc@672: free(chs); adamc@672: chs = tmp; adamc@672: adamc@672: pthread_mutex_lock(&ch->data.used.lock); adamc@672: for (prev = NULL, cs = ch->data.used.clients; cs; ) { adamc@672: if (cs->data == c) { adamc@672: client_list *tmp = cs->next; adamc@672: free(cs); adamc@672: cs = tmp; adamc@672: if (prev) adamc@672: prev->next = cs; adamc@672: else adamc@672: ch->data.used.clients = cs; adamc@672: } adamc@672: else { adamc@672: prev = cs; adamc@672: cs = cs->next; adamc@672: } adamc@672: } adamc@672: pthread_mutex_unlock(&ch->data.used.lock); adamc@672: } adamc@672: adamc@672: if (c->data.used.sock != -1) adamc@672: close(c->data.used.sock); adamc@672: adamc@672: pthread_mutex_unlock(&c->data.used.lock); adamc@672: pthread_mutex_destroy(&c->data.used.lock); adamc@672: buf_free(&c->data.used.msgs); adamc@672: c->mode = UNUSED; adamc@672: adamc@672: c->data.next = clients_free; adamc@672: clients_free = c; adamc@672: } adamc@667: } adamc@667: adamc@673: extern int uw_timeout; adamc@673: adamc@673: void uw_prune_clients() { adamc@667: size_t i; adamc@667: time_t cutoff; adamc@667: adamc@673: cutoff = time(NULL) - uw_timeout; adamc@667: adamc@667: pthread_mutex_lock(&clients_mutex); adamc@667: adamc@667: for (i = 0; i < n_clients; ++i) { adamc@671: if (clients[i]->mode == USED && clients[i]->data.used.last_contact < cutoff adamc@671: && clients[i]->data.used.refcount == 0) adamc@667: uw_free_client(clients[i]); adamc@667: } adamc@667: adamc@667: pthread_mutex_unlock(&clients_mutex); adamc@667: } adamc@667: adamc@667: adamc@668: // Persistent channel state adamc@668: adamc@668: adamc@668: static channel **channels, *channels_free; adamc@668: static size_t n_channels; adamc@668: adamc@668: static pthread_mutex_t channels_mutex = PTHREAD_MUTEX_INITIALIZER; adamc@668: adamc@668: static channel *uw_new_channel() { adamc@668: channel *ch; adamc@668: adamc@668: pthread_mutex_lock(&channels_mutex); adamc@668: adamc@668: if (channels_free) { adamc@668: ch = channels_free; adamc@668: channels_free = channels_free->data.next; adamc@668: } adamc@668: else { adamc@668: ++n_channels; adamc@668: channels = realloc(channels, sizeof(channels) * n_channels); adamc@668: ch = malloc(sizeof(channel)); adamc@668: ch->id = n_channels-1; adamc@668: channels[n_channels-1] = ch; adamc@668: } adamc@668: adamc@668: ch->mode = USED; adamc@668: pthread_mutex_init(&ch->data.used.lock, NULL); adamc@668: ch->data.used.clients = NULL; adamc@671: ch->data.used.refcount = 0; adamc@668: adamc@668: pthread_mutex_unlock(&channels_mutex); adamc@668: adamc@668: return ch; adamc@668: } adamc@668: adamc@671: static void uw_free_channel(channel *ch) { adamc@671: if (ch->mode == USED) { adamc@671: client_list *cs; adamc@671: adamc@671: for (cs = ch->data.used.clients; cs; ) { adamc@671: client_list *tmp = cs->next; adamc@671: free(cs); adamc@671: cs = tmp; adamc@671: } adamc@671: pthread_mutex_destroy(&ch->data.used.lock); adamc@671: ch->mode = UNUSED; adamc@671: ch->data.next = channels_free; adamc@671: channels_free = ch; adamc@671: } adamc@671: } adamc@671: adamc@668: static channel *uw_find_channel(size_t id) { adamc@668: channel *ch = NULL; adamc@668: adamc@668: pthread_mutex_lock(&channels_mutex); adamc@668: adamc@671: if (id < n_channels && channels[id]->mode == USED) { adamc@668: ch = channels[id]; adamc@668: adamc@671: pthread_mutex_lock(&ch->data.used.lock); adamc@671: ++ch->data.used.refcount; adamc@671: pthread_mutex_unlock(&ch->data.used.lock); adamc@671: } adamc@671: adamc@668: pthread_mutex_unlock(&channels_mutex); adamc@668: adamc@668: return ch; adamc@668: } adamc@668: adamc@671: static void uw_release_channel(channel *ch) { adamc@671: pthread_mutex_lock(&ch->data.used.lock); adamc@671: ++ch->data.used.refcount; adamc@671: pthread_mutex_unlock(&ch->data.used.lock); adamc@671: } adamc@671: adamc@668: static void uw_subscribe(channel *ch, client *c) { adamc@668: client_list *cs = malloc(sizeof(client_list)); adamc@668: adamc@668: pthread_mutex_lock(&ch->data.used.lock); adamc@668: adamc@668: cs->data = c; adamc@668: cs->next = ch->data.used.clients; adamc@668: ch->data.used.clients = cs; adamc@668: adamc@668: pthread_mutex_unlock(&ch->data.used.lock); adamc@668: } adamc@668: adamc@668: static void uw_unsubscribe(channel *ch, client *c) { adamc@668: client_list *prev, *cur, *tmp; adamc@668: adamc@668: pthread_mutex_lock(&ch->data.used.lock); adamc@668: adamc@668: for (prev = NULL, cur = ch->data.used.clients; cur; ) { adamc@668: if (cur->data == c) { adamc@668: if (prev) adamc@668: prev->next = cur->next; adamc@668: else adamc@668: ch->data.used.clients = cur->next; adamc@668: tmp = cur; adamc@668: cur = cur->next; adamc@668: free(tmp); adamc@668: } adamc@668: else { adamc@668: prev = cur; adamc@668: cur = cur->next; adamc@668: } adamc@668: } adamc@668: adamc@668: pthread_mutex_unlock(&ch->data.used.lock); adamc@668: } adamc@668: adamc@668: static void uw_channel_send(channel *ch, const char *msg) { adamc@668: size_t len = strlen(msg), preLen; adamc@668: char pre[INTS_MAX + 2]; adamc@668: client_list *cs; adamc@668: adamc@668: sprintf(pre, "%d\n", (int)ch->id); adamc@668: preLen = strlen(pre); adamc@668: adamc@668: pthread_mutex_lock(&ch->data.used.lock); adamc@668: adamc@668: for (cs = ch->data.used.clients; cs; cs = cs->next) { adamc@668: client *c = cs->data; adamc@668: adamc@668: pthread_mutex_lock(&c->data.used.lock); adamc@668: adamc@668: if (c->data.used.sock != -1) { adamc@668: uw_really_send(c->data.used.sock, begin_msgs, sizeof(begin_msgs) - 1); adamc@668: uw_really_send(c->data.used.sock, pre, preLen); adamc@668: uw_really_send(c->data.used.sock, msg, len); adamc@668: uw_really_send(c->data.used.sock, "\n", 1); adamc@668: close(c->data.used.sock); adamc@668: c->data.used.sock = -1; adamc@668: } else { adamc@668: buf_append(&c->data.used.msgs, pre, preLen); adamc@668: buf_append(&c->data.used.msgs, msg, len); adamc@668: buf_append(&c->data.used.msgs, "\n", 1); adamc@668: } adamc@668: adamc@668: pthread_mutex_unlock(&c->data.used.lock); adamc@668: } adamc@668: adamc@668: pthread_mutex_unlock(&ch->data.used.lock); adamc@668: } adamc@668: adamc@668: adamc@668: // Global entry points adamc@668: adamc@668: void uw_global_init() { adamc@668: srand(time(NULL) ^ getpid()); adamc@668: adamc@668: clients = malloc(0); adamc@668: channels = malloc(0); adamc@668: } adamc@668: 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@671: usage mode; adamc@671: channel *ch; adamc@671: enum { OLD, NEW } newness; adamc@671: adamc@671: size_t n_subscribed; adamc@671: client **subscribed; adamc@671: adamc@671: buf msgs; adamc@671: } channel_delta; adamc@671: adamc@311: struct uw_context { adamc@458: char *headers, *headers_end; adamc@457: adamc@666: buf outHeaders, page, heap, script; adamc@144: char **inputs; 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@667: const char *script_header, *url_prefix; adamc@643: adamc@671: size_t n_deltas; adamc@671: channel_delta *deltas; adamc@671: adamc@673: int timeout; adamc@673: adamc@167: char error_message[ERROR_BUF_LEN]; adamc@117: }; adamc@117: adamc@311: extern int uw_inputs_len; adamc@144: adamc@562: uw_context uw_init(size_t outHeaders_len, size_t script_len, size_t page_len, size_t heap_len) { adamc@311: uw_context ctx = malloc(sizeof(struct uw_context)); adamc@136: adamc@458: ctx->headers = ctx->headers_end = NULL; adamc@457: adamc@666: buf_init(&ctx->outHeaders, outHeaders_len); adamc@666: buf_init(&ctx->page, page_len); adamc@666: buf_init(&ctx->heap, heap_len); adamc@666: buf_init(&ctx->script, script_len); adamc@667: ctx->script.start[0] = 0; adamc@136: adamc@311: ctx->inputs = calloc(uw_inputs_len, sizeof(char *)); 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@667: ctx->url_prefix = "/"; adamc@643: adamc@167: ctx->error_message[0] = 0; adamc@167: adamc@565: ctx->source_count = 0; adamc@562: adamc@671: ctx->n_deltas = 0; adamc@671: ctx->deltas = malloc(0); adamc@671: adamc@673: ctx->timeout = uw_timeout; adamc@673: adamc@117: return ctx; adamc@106: } adamc@106: 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@425: free(ctx->cleanup); adamc@671: adamc@671: for (i = 0; i < ctx->n_deltas && ctx->deltas[i].mode == USED; ++i) { adamc@671: free(ctx->deltas[i].subscribed); adamc@671: buf_free(&ctx->deltas[i].msgs); adamc@671: } adamc@671: 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@666: buf_reset(&ctx->heap); adamc@323: ctx->regions = NULL; adamc@425: ctx->cleanup_front = ctx->cleanup; adamc@565: ctx->source_count = 0; adamc@671: if (ctx->n_deltas > 0) adamc@671: ctx->deltas[0].mode = UNUSED; 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@311: memset(ctx->inputs, 0, uw_inputs_len * sizeof(char *)); adamc@144: } adamc@144: adamc@311: void uw_db_init(uw_context); adamc@311: void uw_handle(uw_context, char *); adamc@167: adamc@311: failure_kind uw_begin_init(uw_context ctx) { adamc@272: int r = setjmp(ctx->jmp_buf); adamc@272: adamc@272: if (r == 0) adamc@311: uw_db_init(ctx); adamc@272: adamc@272: return r; adamc@272: } adamc@272: adamc@458: void uw_set_headers(uw_context ctx, char *headers) { adamc@458: char *s = headers, *s2; adamc@458: ctx->headers = headers; adamc@458: adamc@458: while (s2 = strchr(s, '\r')) { adamc@458: s = s2; adamc@458: adamc@458: if (s[1] == 0) adamc@458: break; adamc@458: adamc@458: *s = 0; adamc@458: s += 2; adamc@458: } adamc@458: adamc@458: ctx->headers_end = s; adamc@458: } adamc@458: adamc@671: int uw_db_begin(uw_context); adamc@167: 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@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@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@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@671: if (uw_db_begin(ctx)) adamc@671: uw_error(ctx, BOUNDED_RETRY, "Error running SQL BEGIN"); adamc@671: uw_handle(ctx, path); adamc@671: } adamc@671: adamc@671: return r; adamc@671: } adamc@671: 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@311: int uw_input_num(char*); adamc@144: adamc@311: void uw_set_input(uw_context ctx, char *name, char *value) { adamc@311: int n = uw_input_num(name); adamc@144: adamc@169: if (n < 0) adamc@311: uw_error(ctx, FATAL, "Bad input name %s", name); adamc@144: adamc@311: if (n >= uw_inputs_len) adamc@311: uw_error(ctx, FATAL, "For input name %s, index %d is out of range", name, n); adamc@169: adamc@144: ctx->inputs[n] = value; adamc@144: adamc@282: //printf("[%d] %s = %s\n", n, name, value); 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@311: if (n >= uw_inputs_len) adamc@311: uw_error(ctx, FATAL, "Out-of-bounds input index %d", n); adamc@282: //printf("[%d] = %s\n", n, ctx->inputs[n]); adamc@144: return ctx->inputs[n]; 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@311: if (n >= uw_inputs_len) adamc@311: uw_error(ctx, FATAL, "Out-of-bounds input index %d", n); adamc@426: //printf("[%d] = %s\n", n, ctx->inputs[n]); adamc@190: return (ctx->inputs[n] == NULL ? "" : ctx->inputs[n]); adamc@190: } adamc@190: adamc@643: void uw_set_script_header(uw_context ctx, const char *s) { adamc@643: ctx->script_header = s; adamc@643: } adamc@643: adamc@667: void uw_set_url_prefix(uw_context ctx, const char *s) { adamc@667: ctx->url_prefix = s; adamc@667: } adamc@667: adamc@667: adamc@667: static void buf_check_ctx(uw_context ctx, 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@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@666: static void uw_check_heap(uw_context ctx, size_t extra) { adamc@667: buf_check_ctx(ctx, &ctx->heap, extra, "heap chunk"); adamc@666: } adamc@666: 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@666: printf("Headers: %d/%d\n", buf_used(&ctx->outHeaders), buf_avail(&ctx->outHeaders)); adamc@666: printf("Script: %d/%d\n", buf_used(&ctx->script), buf_avail(&ctx->script)); adamc@666: printf("Page: %d/%d\n", buf_used(&ctx->page), buf_avail(&ctx->page)); adamc@666: printf("Heap: %d/%d\n", buf_used(&ctx->heap), 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@462: static void uw_check_headers(uw_context ctx, size_t extra) { adamc@667: buf_check(&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@562: static void uw_check_script(uw_context ctx, size_t extra) { adamc@667: buf_check(&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@667: if (ctx->script_header[0] == 0) adamc@667: return ""; adamc@667: else { adamc@667: int pass; adamc@667: client *c = uw_new_client(&pass); adamc@565: adamc@673: char *r = uw_malloc(ctx, strlen(ctx->script_header) + 65 + 3 * INTS_MAX + buf_used(&ctx->script) adamc@667: + strlen(ctx->url_prefix)); adamc@673: sprintf(r, "%s", adamc@673: ctx->script_header, adamc@673: (int)c->id, adamc@673: c->data.used.pass, adamc@673: ctx->url_prefix, adamc@673: ctx->timeout, adamc@673: ctx->script.start); adamc@565: return r; adamc@565: } adamc@565: } adamc@565: adamc@668: const char *uw_Basis_get_listener(uw_context ctx, uw_Basis_string onload) { adamc@667: if (ctx->script_header[0] == 0) adamc@667: return ""; adamc@668: else if (onload[0] == 0) adamc@667: return " onload='listener()'"; adamc@668: else { adamc@668: uw_Basis_string s = uw_malloc(ctx, strlen(onload) + 22); adamc@668: adamc@668: sprintf(s, " onload='listener();%s'", onload); adamc@668: return s; 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@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->heap.front = s2 + 1; adamc@574: return r; adamc@574: } adamc@574: 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@574: 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@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@577: uw_check_script(ctx, 12 + INTS_MAX + s_len); adamc@666: sprintf(ctx->script.front, "var s%d=sc(%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@666: strcpy(ctx->script.front, ");"); adamc@666: ctx->script.front += 2; 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@577: uw_check_script(ctx, 6 + INTS_MAX + s_len); adamc@666: sprintf(ctx->script.front, "s%d.v=%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@666: strcpy(ctx->script.front, ";"); adamc@666: ctx->script.front++; 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@667: buf_check(&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@136: 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@136: else if (isprint(c)) adamc@136: *p++ = c; adamc@136: else { adamc@136: int len2; adamc@136: sprintf(p, "&#%d;%n", c, &len2); adamc@136: p += len2; adamc@136: } adamc@136: } adamc@136: adamc@137: *p++ = 0; adamc@666: ctx->heap.front = p; adamc@136: return result; adamc@106: } adamc@106: 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@106: 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@106: else if (isprint(c)) adamc@311: uw_writec_unsafe(ctx, c); adamc@106: else { adamc@311: uw_write_unsafe(ctx, "&#"); adamc@311: uw_Basis_attrifyInt_w_unsafe(ctx, c); adamc@311: uw_writec_unsafe(ctx, ';'); adamc@106: } adamc@106: } adamc@428: adamc@428: return uw_unit_v; adamc@106: } adamc@120: 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@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@311: uw_check_heap(ctx, strlen(s) * 3 + 1); adamc@137: adamc@666: for (r = p = ctx->heap.front; *s; s++) { adamc@137: 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@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@311: uw_check(ctx, strlen(s) * 3); adamc@120: adamc@120: for (; *s; s++) { adamc@120: 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@668: uw_Basis_channel uw_Basis_unurlifyChannel(uw_context ctx, char **s) { adamc@668: return uw_Basis_unurlifyInt(ctx, s); adamc@668: } adamc@668: 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@311: static uw_Basis_string uw_unurlifyString_to(uw_context ctx, char *r, char *s) { adamc@144: char *s1, *s2; adamc@144: int n; adamc@136: adamc@144: for (s1 = r, s2 = s; *s2; ++s1, ++s2) { adamc@136: 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@144: char *r, *s1, *s2; adamc@144: int len, n; adamc@144: adamc@200: len = strlen(*s); adamc@311: uw_check_heap(ctx, len + 1); adamc@144: adamc@666: r = ctx->heap.front; adamc@666: ctx->heap.front = uw_unurlifyString_to(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@668: char *uw_Basis_htmlifyChannel(uw_context ctx, uw_Basis_channel ch) { adamc@668: return uw_Basis_htmlifyInt(ctx, ch); adamc@668: } adamc@668: 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@137: 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@137: if (isprint(c)) adamc@137: *s2++ = c; adamc@137: else { adamc@137: int len2; adamc@137: sprintf(s2, "&#%d;%n", c, &len2); adamc@137: s2 += len2; adamc@137: } 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@135: 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@135: if (isprint(c)) adamc@311: uw_writec_unsafe(ctx, c); adamc@135: else { adamc@311: uw_write_unsafe(ctx, "&#"); adamc@311: uw_Basis_attrifyInt_w_unsafe(ctx, c); adamc@311: uw_writec_unsafe(ctx, ';'); adamc@135: } 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@311: uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) { adamc@180: int len = strlen(s1) + strlen(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@311: uw_Basis_string uw_Basis_strdup(uw_context ctx, uw_Basis_string s1) { adamc@278: int len = strlen(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@493: uw_Basis_string uw_Basis_maybe_strdup(uw_context ctx, uw_Basis_string s1) { adamc@493: if (s1) adamc@493: return uw_Basis_strdup(ctx, s1); adamc@493: else adamc@493: return NULL; adamc@493: } adamc@493: adamc@280: 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@281: sprintf(r, "%lld::int8%n", 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@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@281: sprintf(r, "%g::float8%n", 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@281: adamc@311: uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) { adamc@280: char *r, *s2; adamc@280: adamc@311: uw_check_heap(ctx, strlen(s) * 2 + 10); adamc@280: adamc@666: r = s2 = ctx->heap.front; adamc@280: *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@280: strcpy(s2, "\\'"); adamc@280: s2 += 2; adamc@280: break; adamc@280: case '\\': adamc@280: strcpy(s2, "\\\\"); adamc@280: s2 += 2; adamc@280: break; adamc@280: default: adamc@280: if (isprint(c)) adamc@280: *s2++ = c; adamc@280: else { adamc@280: sprintf(s2, "\\%3o", c); adamc@280: s2 += 4; adamc@280: } adamc@280: } adamc@280: } adamc@280: adamc@281: strcpy(s2, "'::text"); adamc@666: ctx->heap.front = s2 + 8; adamc@280: return r; adamc@280: } adamc@281: 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@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@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@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@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@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@457: uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) { adamc@457: int len = strlen(h); adamc@457: char *s = ctx->headers, *p; adamc@457: adamc@457: while (p = strchr(s, ':')) { adamc@457: if (p - s == len && !strncasecmp(s, h, len)) { adamc@458: return p + 2; adamc@457: } else { adamc@458: if ((s = strchr(p, 0)) && s < ctx->headers_end) adamc@458: s += 2; adamc@457: else adamc@457: return NULL; adamc@457: } adamc@457: } adamc@463: } adamc@458: adamc@463: uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) { adamc@463: int len = strlen(c); adamc@666: char *s = ctx->headers, *p = ctx->outHeaders.start; adamc@474: adamc@474: 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@474: char *ret = uw_malloc(ctx, sz + 1); adamc@474: memcpy(ret, p2+1, sz); adamc@474: ret[sz] = 0; adamc@474: return ret; adamc@474: } adamc@474: } adamc@474: } adamc@463: adamc@463: while (p = strchr(s, ':')) { adamc@476: if (!strncasecmp(s, "Cookie: ", 8)) { adamc@476: p += 2; adamc@476: while (1) { adamc@476: if (!strncmp(p, c, len) adamc@476: && p + len < ctx->headers_end && p[len] == '=') adamc@476: return p + 1 + len; adamc@476: else if (p = strchr(p, ';')) adamc@476: p += 2; adamc@476: else if ((s = strchr(s, 0)) && s < ctx->headers_end) { adamc@476: s += 2; adamc@476: break; adamc@476: } adamc@476: else adamc@476: return NULL; adamc@476: } adamc@463: } else { adamc@463: if ((s = strchr(p, 0)) && s < ctx->headers_end) adamc@463: s += 2; adamc@463: else adamc@463: return NULL; adamc@463: } adamc@463: } adamc@457: } adamc@462: adamc@466: uw_unit uw_Basis_set_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c, uw_Basis_string v) { 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@462: uw_write_header(ctx, "\r\n"); adamc@462: adamc@462: return uw_unit_v; adamc@462: } adamc@667: adamc@671: static channel_delta *allocate_delta(uw_context ctx, channel *ch) { adamc@671: size_t i; adamc@671: channel_delta *cd; adamc@671: adamc@671: for (i = 0; i < ctx->n_deltas && ctx->deltas[i].mode == USED && ctx->deltas[i].ch != ch; ++i); adamc@671: adamc@671: if (i < ctx->n_deltas && ctx->deltas[i].mode == USED && ctx->deltas[i].ch == ch) adamc@671: return &ctx->deltas[i]; adamc@671: adamc@671: if (i < ctx->n_deltas) adamc@671: cd = &ctx->deltas[i]; adamc@671: else { adamc@671: ++ctx->n_deltas; adamc@671: ctx->deltas = realloc(ctx->deltas, sizeof(channel_delta) * ctx->n_deltas); adamc@671: cd = &ctx->deltas[ctx->n_deltas-1]; adamc@671: } adamc@671: adamc@671: cd->mode = USED; adamc@671: cd->newness = OLD; adamc@671: cd->ch = ch; adamc@671: if (cd->n_subscribed > 0) adamc@671: cd->subscribed[0] = NULL; adamc@671: buf_reset(&cd->msgs); adamc@671: return cd; adamc@671: } adamc@671: adamc@668: uw_Basis_channel uw_Basis_new_channel(uw_context ctx, uw_unit u) { adamc@671: size_t i; adamc@671: channel *ch = uw_new_channel(); adamc@671: ++ch->data.used.refcount; adamc@671: channel_delta *cd = allocate_delta(ctx, ch); adamc@671: adamc@671: cd->newness = NEW; adamc@671: adamc@671: return ch->id; adamc@671: } adamc@671: adamc@671: static int delta_used(channel_delta *cd) { adamc@671: return cd->newness == NEW || buf_used(&cd->msgs) > 0 || (cd->n_subscribed > 0 && cd->subscribed[0]); adamc@668: } adamc@667: adamc@668: uw_unit uw_Basis_subscribe(uw_context ctx, uw_Basis_channel chn) { adamc@668: channel *ch = uw_find_channel(chn); adamc@668: adamc@668: if (ch == NULL) adamc@668: uw_error(ctx, FATAL, "Bad channel ID %d", (int)chn); adamc@668: else { adamc@668: size_t id = atoi(uw_Basis_requestHeader(ctx, "UrWeb-Client")); adamc@668: int pass = atoi(uw_Basis_requestHeader(ctx, "UrWeb-Pass")); adamc@668: client *c = uw_find_client(id); adamc@668: adamc@671: if (c == NULL) { adamc@671: uw_release_channel(ch); adamc@668: uw_error(ctx, FATAL, "Unknown client ID in subscription request"); adamc@671: } else if (c->data.used.pass != pass) { adamc@671: uw_release_channel(ch); adamc@671: uw_release_client(c); adamc@668: uw_error(ctx, FATAL, "Wrong client password in subscription request"); adamc@671: } else { adamc@671: size_t i; adamc@671: channel_delta *cd = allocate_delta(ctx, ch); adamc@671: adamc@671: if (delta_used(cd)) adamc@671: uw_release_channel(ch); adamc@671: adamc@671: for (i = 0; i < cd->n_subscribed && cd->subscribed[i]; ++i); adamc@671: adamc@671: if (i < cd->n_subscribed) adamc@671: cd->subscribed[i] = c; adamc@671: else { adamc@671: ++cd->n_subscribed; adamc@671: cd->subscribed = realloc(cd->subscribed, sizeof(int) * cd->n_subscribed); adamc@671: cd->subscribed[cd->n_subscribed-1] = c; adamc@671: } adamc@671: } adamc@668: } adamc@671: adamc@671: return uw_unit_v; adamc@668: } adamc@668: adamc@668: uw_unit uw_Basis_send(uw_context ctx, uw_Basis_channel chn, uw_Basis_string msg) { adamc@668: channel *ch = uw_find_channel(chn); adamc@668: adamc@668: if (ch == NULL) adamc@668: uw_error(ctx, FATAL, "Bad channel ID %d", (int)chn); adamc@671: else { adamc@671: channel_delta *cd = allocate_delta(ctx, ch); adamc@671: if (delta_used(cd)) adamc@671: uw_release_channel(ch); adamc@671: buf_append(&cd->msgs, msg, strlen(msg)); adamc@671: } adamc@671: adamc@671: return uw_unit_v; adamc@668: } adamc@671: adamc@671: int uw_db_commit(uw_context); adamc@671: int uw_db_rollback(uw_context); adamc@671: adamc@671: void uw_commit(uw_context ctx) { adamc@671: size_t i, j; adamc@671: adamc@671: for (i = 0; i < ctx->n_deltas && ctx->deltas[i].mode == USED; ++i) { adamc@671: channel *ch = ctx->deltas[i].ch; adamc@671: adamc@671: for (j = 0; j < ctx->deltas[i].n_subscribed && ctx->deltas[i].subscribed[j]; ++j) { adamc@671: client *c = ctx->deltas[i].subscribed[j]; adamc@671: adamc@671: uw_subscribe(ch, c); adamc@671: uw_release_client(c); adamc@671: } adamc@671: adamc@671: if (buf_used(&ctx->deltas[i].msgs) > 0) { adamc@671: uw_channel_send(ch, ctx->deltas[i].msgs.start); adamc@671: } adamc@671: adamc@671: uw_release_channel(ch); adamc@671: } adamc@671: adamc@671: if (uw_db_commit(ctx)) adamc@671: uw_error(ctx, FATAL, "Error running SQL COMMIT"); adamc@671: } adamc@671: adamc@671: int uw_rollback(uw_context ctx) { adamc@671: size_t i, j; adamc@671: adamc@671: for (i = 0; i < ctx->n_deltas && ctx->deltas[i].mode == USED; ++i) { adamc@671: channel *ch = ctx->deltas[i].ch; adamc@671: adamc@671: for (j = 0; j < ctx->deltas[i].n_subscribed && ctx->deltas[i].subscribed[j]; ++j) { adamc@671: client *c = ctx->deltas[i].subscribed[j]; adamc@671: adamc@671: uw_release_client(c); adamc@671: } adamc@671: adamc@671: if (ctx->deltas[i].newness == NEW) adamc@671: uw_free_channel(ch); adamc@671: else adamc@671: uw_release_channel(ch); adamc@671: } adamc@671: adamc@671: return uw_db_rollback(ctx); adamc@671: }