diff src/c/urweb.c @ 457:360cbc202756

Request header reading works
author Adam Chlipala <adamc@hcoop.net>
date Thu, 06 Nov 2008 09:47:16 -0500
parents 322c8620bbdf
children 8f65b0fa3b29
line wrap: on
line diff
--- a/src/c/urweb.c	Thu Nov 06 09:21:34 2008 -0500
+++ b/src/c/urweb.c	Thu Nov 06 09:47:16 2008 -0500
@@ -3,6 +3,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <strings.h>
 #include <ctype.h>
 #include <setjmp.h>
 #include <stdarg.h>
@@ -23,6 +24,8 @@
 } cleanup;
 
 struct uw_context {
+  char *headers;
+
   char *page, *page_front, *page_back;
   char *heap, *heap_front, *heap_back;
   char **inputs;
@@ -43,6 +46,8 @@
 uw_context uw_init(size_t page_len, size_t heap_len) {
   uw_context ctx = malloc(sizeof(struct uw_context));
 
+  ctx->headers = NULL;
+
   ctx->page_front = ctx->page = malloc(page_len);
   ctx->page_back = ctx->page_front + page_len;
 
@@ -111,9 +116,11 @@
   return r;
 }
 
-failure_kind uw_begin(uw_context ctx, char *path) {
+failure_kind uw_begin(uw_context ctx, char *headers, char *path) {
   int r = setjmp(ctx->jmp_buf);
 
+  ctx->headers = headers;
+
   if (r == 0)
     uw_handle(ctx, path);
 
@@ -1051,3 +1058,28 @@
       uw_error(ctx, FATAL, "Can't parse time: %s", s);
   }
 }
+
+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)) {
+      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;
+    } else {
+      if (s = strchr(s, '\n'))
+        ++s;
+      else
+        return NULL;
+    }
+  }
+        
+}