diff options
author | Adam Chlipala <adamc@hcoop.net> | 2009-04-16 14:35:01 -0400 |
---|---|---|
committer | Adam Chlipala <adamc@hcoop.net> | 2009-04-16 14:35:01 -0400 |
commit | 59809464cf40942ec6d1787eeb5d3080e2b074f9 (patch) | |
tree | aec05da1db8412d9b63202985582272d1de56322 /src | |
parent | 6ada30a32e69cf288e50ee0ca2963e031c588ee0 (diff) |
POST support
Diffstat (limited to 'src')
-rw-r--r-- | src/c/driver.c | 56 | ||||
-rw-r--r-- | src/monoize.sml | 10 |
2 files changed, 52 insertions, 14 deletions
diff --git a/src/c/driver.c b/src/c/driver.c index 712de2de..e6538616 100644 --- a/src/c/driver.c +++ b/src/c/driver.c @@ -107,7 +107,7 @@ static void *worker(void *data) { uw_context ctx = new_context(); while (1) { - char buf[uw_bufsize+1], *back = buf, *s; + char buf[uw_bufsize+1], *back = buf, *s, *post; int sock, dont_close = 0; pthread_mutex_lock(&queue_mutex); @@ -132,16 +132,18 @@ static void *worker(void *data) { break; } - printf("Received %d bytes.\n", r); + //printf("Received %d bytes.\n", r); back += r; *back = 0; - + if (s = strstr(buf, "\r\n\r\n")) { failure_kind fk; - char *cmd, *path, *headers, path_copy[uw_bufsize+1], *inputs; + int is_post = 0; + char *cmd, *path, *headers, path_copy[uw_bufsize+1], *inputs, *after_headers; s[2] = 0; + after_headers = s + 4; if (!(s = strstr(buf, "\r\n"))) { fprintf(stderr, "No newline in buf\n"); @@ -159,8 +161,40 @@ static void *worker(void *data) { break; } - if (strcmp(cmd, "GET")) { - fprintf(stderr, "Not ready for non-get command: %s\n", cmd); + uw_set_headers(ctx, headers); + + if (!strcmp(cmd, "POST")) { + char *clen_s = uw_Basis_requestHeader(ctx, "Content-length"); + if (!clen_s) { + printf("No Content-length with POST\n"); + goto done; + } + int clen = atoi(clen_s); + if (clen < 0) { + printf("Negative Content-length with POST\n"); + goto done; + } + + while (back - after_headers < clen) { + r = recv(sock, back, uw_bufsize - (back - buf), 0); + + if (r < 0) { + fprintf(stderr, "Recv failed\n"); + goto done; + } + + if (r == 0) { + printf("Connection closed.\n"); + goto done; + } + + back += r; + *back = 0; + } + + is_post = 1; + } else if (strcmp(cmd, "GET")) { + fprintf(stderr, "Not ready for non-GET/POST command: %s\n", cmd); break; } @@ -170,8 +204,6 @@ static void *worker(void *data) { break; } - uw_set_headers(ctx, headers); - if (!strcmp(path, "/.msgs")) { char *id = uw_Basis_requestHeader(ctx, "UrWeb-Client"); char *pass = uw_Basis_requestHeader(ctx, "UrWeb-Pass"); @@ -188,9 +220,12 @@ static void *worker(void *data) { break; } - if (inputs = strchr(path, '?')) { - char *name, *value; + if (is_post) + inputs = after_headers; + else if (inputs = strchr(path, '?')) *inputs++ = 0; + if (inputs) { + char *name, *value; while (*inputs) { name = inputs; @@ -280,6 +315,7 @@ static void *worker(void *data) { } } + done: if (!dont_close) close(sock); uw_reset(ctx); diff --git a/src/monoize.sml b/src/monoize.sml index b816b287..0c05cf90 100644 --- a/src/monoize.sml +++ b/src/monoize.sml @@ -2426,7 +2426,7 @@ fun monoExp (env, st, fm) (all as (e, loc)) = | L.EApp ( (L.EApp ( (L.EApp ( - (L.ECApp ( + (L.EApp ( (L.ECApp ( (L.ECApp ( (L.ECApp ( @@ -2434,8 +2434,10 @@ fun monoExp (env, st, fm) (all as (e, loc)) = (L.ECApp ( (L.ECApp ( (L.ECApp ( - (L.EFfi ("Basis", "tag"), - _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), + (L.ECApp ( + (L.EFfi ("Basis", "tag"), + _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), + _), _), attrs), _), _), _), xml) => @@ -2468,7 +2470,7 @@ fun monoExp (env, st, fm) (all as (e, loc)) = val (xml, fm) = monoExp (env, st, fm) xml in - ((L'.EStrcat ((L'.EStrcat ((L'.EPrim (Prim.String "<form"), loc), + ((L'.EStrcat ((L'.EStrcat ((L'.EPrim (Prim.String "<form method=\"post\""), loc), (L'.EStrcat (action, (L'.EPrim (Prim.String ">"), loc)), loc)), loc), (L'.EStrcat (xml, |