diff src/c/urweb.c @ 854:158d980889ac

Further refactoring of request.c to work with CGI
author Adam Chlipala <adamc@hcoop.net>
date Tue, 23 Jun 2009 15:40:35 -0400
parents 74a1e3bdf430
children 86ec89baee01
line wrap: on
line diff
--- a/src/c/urweb.c	Tue Jun 23 14:05:12 2009 -0400
+++ b/src/c/urweb.c	Tue Jun 23 15:40:35 2009 -0400
@@ -310,7 +310,8 @@
 } transactional;
 
 struct uw_context {
-  char *headers, *headers_end;
+  char *(*get_header)(void *, const char *);
+  void *get_header_data;
 
   buf outHeaders, page, heap, script;
   input *inputs, *subinputs, *cur_container;
@@ -348,7 +349,8 @@
 uw_context uw_init() {
   uw_context ctx = malloc(sizeof(struct uw_context));
 
-  ctx->headers = ctx->headers_end = NULL;
+  ctx->get_header = NULL;
+  ctx->get_header_data = NULL;
 
   buf_init(&ctx->outHeaders, 0);
   buf_init(&ctx->page, 0);
@@ -458,26 +460,9 @@
   return r;
 }
 
-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;
-}
-
-void uw_headers_moved(uw_context ctx, char *headers) {
-  ctx->headers_end = headers + (ctx->headers_end - ctx->headers);
-  ctx->headers = headers;
+void uw_set_headers(uw_context ctx, char *(*get_header)(void *, const char *), void *get_header_data) {
+  ctx->get_header = get_header;
+  ctx->get_header_data = get_header_data;
 }
 
 int uw_db_begin(uw_context);
@@ -523,21 +508,7 @@
 }
 
 uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) {
-  int len = strlen(h);
-  char *s = ctx->headers, *p;
-
-  while (p = strchr(s, ':')) {
-    if (p - s == len && !strncasecmp(s, h, len)) {
-      return p + 2;
-    } else {
-      if ((s = strchr(p, 0)) && s < ctx->headers_end)
-        s += 2;
-      else
-        return NULL;
-    }
-  }
-
-  return NULL;
+  return ctx->get_header(ctx->get_header_data, h);
 }
 
 void uw_login(uw_context ctx) {
@@ -2377,7 +2348,7 @@
 
 uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) {
   int len = strlen(c);
-  char *s = ctx->headers, *p = ctx->outHeaders.start;
+  char *p = ctx->outHeaders.start;
 
   while (p = strstr(p, "\nSet-Cookie: ")) {
     char *p2;
@@ -2396,25 +2367,12 @@
     }
   }
 
-  while (p = strchr(s, ':')) {
-    if (!strncasecmp(s, "Cookie: ", 8)) {
-      p += 2;
-      while (1) {
-        if (!strncmp(p, c, len)
-            && p + len < ctx->headers_end && p[len] == '=')
-          return p + 1 + len;
-        else if (p = strchr(p, ';'))
-          p += 2;
-        else if ((s = strchr(s, 0)) && s < ctx->headers_end) {
-          s += 2;
-          break;
-        }
-        else
-          return NULL;
-      }
-    } else {
-      if ((s = strchr(p, 0)) && s < ctx->headers_end)
-        s += 2;
+  if (p = uw_Basis_requestHeader(ctx, "Cookie")) {
+    while (1) {
+      if (!strncmp(p, c, len) && p[len] == '=')
+        return p + 1 + len;
+      else if (p = strchr(p, ';'))
+        p += 2;
       else
         return NULL;
     }