From 45dee9afc8f0b8030115943af95df499ba8ee13e Mon Sep 17 00:00:00 2001 From: Adam Chlipala Date: Thu, 6 Nov 2008 10:04:03 -0500 Subject: Avoid allocating strings for requestHeader --- src/c/driver.c | 4 +++- src/c/urweb.c | 41 ++++++++++++++++++++++++----------------- 2 files changed, 27 insertions(+), 18 deletions(-) (limited to 'src/c') diff --git a/src/c/driver.c b/src/c/driver.c index 4c54d3ba..ac968dc9 100644 --- a/src/c/driver.c +++ b/src/c/driver.c @@ -188,6 +188,8 @@ static void *worker(void *data) { printf("Serving URI %s....\n", path); + uw_set_headers(ctx, headers); + while (1) { if (uw_db_begin(ctx)) { printf("Error running SQL BEGIN\n"); @@ -209,7 +211,7 @@ static void *worker(void *data) { uw_write(ctx, ""); strcpy(path_copy, path); - fk = uw_begin(ctx, headers, path_copy); + fk = uw_begin(ctx, path_copy); if (fk == SUCCESS) { uw_write(ctx, ""); diff --git a/src/c/urweb.c b/src/c/urweb.c index 9e83191e..5f718db6 100644 --- a/src/c/urweb.c +++ b/src/c/urweb.c @@ -24,7 +24,7 @@ typedef struct { } cleanup; struct uw_context { - char *headers; + char *headers, *headers_end; char *page, *page_front, *page_back; char *heap, *heap_front, *heap_back; @@ -46,7 +46,7 @@ extern int uw_inputs_len; uw_context uw_init(size_t page_len, size_t heap_len) { uw_context ctx = malloc(sizeof(struct uw_context)); - ctx->headers = NULL; + ctx->headers = ctx->headers_end = NULL; ctx->page_front = ctx->page = malloc(page_len); ctx->page_back = ctx->page_front + page_len; @@ -116,11 +116,26 @@ failure_kind uw_begin_init(uw_context ctx) { return r; } -failure_kind uw_begin(uw_context ctx, char *headers, char *path) { - int r = setjmp(ctx->jmp_buf); - +void uw_set_headers(uw_context ctx, char *headers) { + char *s = headers, *s2; ctx->headers = headers; + while (s2 = strchr(s, '\r')) { + s = s2; + + if (s[1] == 0) + break; + + *s = 0; + s += 2; + } + + ctx->headers_end = s; +} + +failure_kind uw_begin(uw_context ctx, char *path) { + int r = setjmp(ctx->jmp_buf); + if (r == 0) uw_handle(ctx, path); @@ -1065,21 +1080,13 @@ uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) { while (p = strchr(s, ':')) { if (p - s == len && !strncasecmp(s, h, len)) { - s = p + 2; - if (p = strchr(s, '\r')) { - uw_Basis_string ret = uw_malloc(ctx, p - s + 1); - memcpy(ret, s, p - s); - ret[p - s] = 0; - return ret; - } - else - return NULL; + return p + 2; } else { - if (s = strchr(s, '\n')) - ++s; + if ((s = strchr(p, 0)) && s < ctx->headers_end) + s += 2; else return NULL; } } - + } -- cgit v1.2.3