Mercurial > urweb
diff src/c/urweb.c @ 458:8f65b0fa3b29
Avoid allocating strings for requestHeader
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Thu, 06 Nov 2008 10:04:03 -0500 |
parents | 360cbc202756 |
children | 21bb5bbba2e9 |
line wrap: on
line diff
--- a/src/c/urweb.c Thu Nov 06 09:47:16 2008 -0500 +++ b/src/c/urweb.c Thu Nov 06 10:04:03 2008 -0500 @@ -24,7 +24,7 @@ } 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 @@ 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 @@ return r; } -failure_kind uw_begin(uw_context ctx, char *headers, char *path) { +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); - ctx->headers = headers; - if (r == 0) uw_handle(ctx, path); @@ -1065,21 +1080,13 @@ 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; } } - + }