Mercurial > urweb
changeset 457:360cbc202756
Request header reading works
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Thu, 06 Nov 2008 09:47:16 -0500 |
parents | 1a4fa157fedd |
children | 8f65b0fa3b29 |
files | include/urweb.h src/c/driver.c src/c/urweb.c src/compiler.sml tests/reqheader.ur |
diffstat | 5 files changed, 44 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- 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);
--- 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, "<html>"); strcpy(path_copy, path); - fk = uw_begin(ctx, path_copy); + fk = uw_begin(ctx, headers, path_copy); if (fk == SUCCESS) { uw_write(ctx, "</html>");
--- 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; + } + } + +}
--- 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,
--- 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 <xml>Not found</xml> - | Some s => return <xml>UserAgent: {[s]}</xml> + | Some s => return <xml>User-Agent: {[s]}</xml>