summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Adam Chlipala <adamc@hcoop.net>2008-11-06 09:47:16 -0500
committerGravatar Adam Chlipala <adamc@hcoop.net>2008-11-06 09:47:16 -0500
commit4f82d607ed4549ecb4bea8ebfc93cfa1b1cec8d2 (patch)
treea2eeed536be5e7cb6b43bf4bc8c8b48af01784a0
parent1841f3b75ec441d16d289cb768526c374f16960a (diff)
Request header reading works
-rw-r--r--include/urweb.h4
-rw-r--r--src/c/driver.c5
-rw-r--r--src/c/urweb.c34
-rw-r--r--src/compiler.sml6
-rw-r--r--tests/reqheader.ur4
5 files changed, 44 insertions, 9 deletions
diff --git a/include/urweb.h b/include/urweb.h
index 43a63324..c7d15a1c 100644
--- a/include/urweb.h
+++ b/include/urweb.h
@@ -15,7 +15,7 @@ void uw_reset_keep_request(uw_context);
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_int uw_Basis_stringToInt_error(uw_context, uw_Basis_string);
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 --git a/src/c/driver.c b/src/c/driver.c
index db982d96..4c54d3ba 100644
--- a/src/c/driver.c
+++ b/src/c/driver.c
@@ -135,7 +135,7 @@ static void *worker(void *data) {
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 @@ static void *worker(void *data) {
}
*s = 0;
+ headers = s + 2;
cmd = s = buf;
printf("Read: %s\n", buf);
@@ -208,7 +209,7 @@ static void *worker(void *data) {
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>");
diff --git a/src/c/urweb.c b/src/c/urweb.c
index f05b0b9d..9e83191e 100644
--- a/src/c/urweb.c
+++ b/src/c/urweb.c
@@ -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 @@ typedef struct {
} cleanup;
struct uw_context {
+ char *headers;
+
char *page, *page_front, *page_back;
char *heap, *heap_front, *heap_back;
char **inputs;
@@ -43,6 +46,8 @@ extern int uw_inputs_len;
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 @@ failure_kind uw_begin_init(uw_context ctx) {
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_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) {
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 --git a/src/compiler.sml b/src/compiler.sml
index 1124bfda..de0490c3 100644
--- a/src/compiler.sml
+++ b/src/compiler.sml
@@ -552,7 +552,7 @@ fun compile job =
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 @@ fun compile job =
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 --git a/tests/reqheader.ur b/tests/reqheader.ur
index 8b69cc3a..d659935c 100644
--- a/tests/reqheader.ur
+++ b/tests/reqheader.ur
@@ -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>