# HG changeset patch # User Adam Chlipala # Date 1225982836 18000 # Node ID 360cbc2027565a750bc0b2d1cc2c3313fd0b83f5 # Parent 1a4fa157fedde18fd48e43105bf75186b4d6897c Request header reading works diff -r 1a4fa157fedd -r 360cbc202756 include/urweb.h --- a/include/urweb.h Thu Nov 06 09:21:34 2008 -0500 +++ b/include/urweb.h Thu Nov 06 09:47:16 2008 -0500 @@ -15,7 +15,7 @@ void uw_reset_keep_error_message(uw_context); failure_kind uw_begin_init(uw_context); -failure_kind uw_begin(uw_context, char *path); +failure_kind uw_begin(uw_context, char *headers, char *path); __attribute__((noreturn)) void uw_error(uw_context, failure_kind, const char *fmt, ...); char *uw_error_message(uw_context); @@ -95,3 +95,5 @@ uw_Basis_float uw_Basis_stringToFloat_error(uw_context, uw_Basis_string); uw_Basis_bool uw_Basis_stringToBool_error(uw_context, uw_Basis_string); uw_Basis_time uw_Basis_stringToTime_error(uw_context, uw_Basis_string); + +uw_Basis_string uw_Basis_requestHeader(uw_context, uw_Basis_string); diff -r 1a4fa157fedd -r 360cbc202756 src/c/driver.c --- a/src/c/driver.c Thu Nov 06 09:21:34 2008 -0500 +++ b/src/c/driver.c Thu Nov 06 09:47:16 2008 -0500 @@ -135,7 +135,7 @@ if (s = strstr(buf, "\r\n\r\n")) { failure_kind fk; - char *cmd, *path, path_copy[uw_bufsize+1], *inputs; + char *cmd, *path, *headers, path_copy[uw_bufsize+1], *inputs; *s = 0; @@ -145,6 +145,7 @@ } *s = 0; + headers = s + 2; cmd = s = buf; printf("Read: %s\n", buf); @@ -208,7 +209,7 @@ uw_write(ctx, ""); strcpy(path_copy, path); - fk = uw_begin(ctx, path_copy); + fk = uw_begin(ctx, headers, path_copy); if (fk == SUCCESS) { uw_write(ctx, ""); diff -r 1a4fa157fedd -r 360cbc202756 src/c/urweb.c --- 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 #include #include +#include #include #include #include @@ -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; + } + } + +} diff -r 1a4fa157fedd -r 360cbc202756 src/compiler.sml --- a/src/compiler.sml Thu Nov 06 09:21:34 2008 -0500 +++ b/src/compiler.sml Thu Nov 06 09:47:16 2008 -0500 @@ -552,7 +552,7 @@ val (cname, oname, cleanup) = if #debug job then - ("/tmp/urweb.c", "/tmp/urweb.o", fn () => ()) + ("/tmp/webapp.c", "/tmp/webapp.o", fn () => ()) else let val dir = OS.FileSys.tmpName () @@ -560,8 +560,8 @@ OS.FileSys.remove dir else () - val cname = OS.Path.joinDirFile {dir = dir, file = "urweb.c"} - val oname = OS.Path.joinDirFile {dir = dir, file = "urweb.o"} + val cname = OS.Path.joinDirFile {dir = dir, file = "webapp.c"} + val oname = OS.Path.joinDirFile {dir = dir, file = "webapp.o"} in OS.FileSys.mkDir dir; (cname, oname, diff -r 1a4fa157fedd -r 360cbc202756 tests/reqheader.ur --- a/tests/reqheader.ur Thu Nov 06 09:21:34 2008 -0500 +++ b/tests/reqheader.ur Thu Nov 06 09:47:16 2008 -0500 @@ -1,5 +1,5 @@ fun main () : transaction page = - ua <- requestHeader "UserAgent"; + ua <- requestHeader "User-Agent"; case ua of None => return Not found - | Some s => return UserAgent: {[s]} + | Some s => return User-Agent: {[s]}