changeset 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 f542bc3133dc
files include/urweb.h src/c/driver.c src/c/urweb.c
diffstat 3 files changed, 29 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/include/urweb.h	Thu Nov 06 09:47:16 2008 -0500
+++ b/include/urweb.h	Thu Nov 06 10:04:03 2008 -0500
@@ -15,7 +15,8 @@
 void uw_reset_keep_error_message(uw_context);
 
 failure_kind uw_begin_init(uw_context);
-failure_kind uw_begin(uw_context, char *headers, char *path);
+void uw_set_headers(uw_context, char *headers);
+failure_kind uw_begin(uw_context, char *path);
 
 __attribute__((noreturn)) void uw_error(uw_context, failure_kind, const char *fmt, ...);
 char *uw_error_message(uw_context);
--- a/src/c/driver.c	Thu Nov 06 09:47:16 2008 -0500
+++ b/src/c/driver.c	Thu Nov 06 10:04:03 2008 -0500
@@ -188,6 +188,8 @@
 
         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 @@
           uw_write(ctx, "<html>");
 
           strcpy(path_copy, path);
-          fk = uw_begin(ctx, headers, path_copy);
+          fk = uw_begin(ctx, path_copy);
           if (fk == SUCCESS) {
             uw_write(ctx, "</html>");
 
--- 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;
     }
   }
-        
+
 }