changeset 730:1b1047992ecf

POST support
author Adam Chlipala <adamc@hcoop.net>
date Thu, 16 Apr 2009 14:35:01 -0400
parents 7c6b6c3c7b79
children e0dd85ea58e1
files src/c/driver.c src/monoize.sml
diffstat 2 files changed, 52 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/c/driver.c	Thu Apr 16 13:47:20 2009 -0400
+++ b/src/c/driver.c	Thu Apr 16 14:35:01 2009 -0400
@@ -107,7 +107,7 @@
   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 @@
         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 @@
           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 @@
           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 @@
           break;
         }
 
-        if (inputs = strchr(path, '?')) {
+        if (is_post)
+          inputs = after_headers;
+        else if (inputs = strchr(path, '?'))
+          *inputs++ = 0;
+        if (inputs) {
           char *name, *value;
-          *inputs++ = 0;
 
           while (*inputs) {
             name = inputs;
@@ -280,6 +315,7 @@
       }
     }
 
+  done:
     if (!dont_close)
       close(sock);
     uw_reset(ctx);
--- a/src/monoize.sml	Thu Apr 16 13:47:20 2009 -0400
+++ b/src/monoize.sml	Thu Apr 16 14:35:01 2009 -0400
@@ -2426,7 +2426,7 @@
                       | L.EApp (
                         (L.EApp (
                          (L.EApp (
-                          (L.ECApp (
+                          (L.EApp (
                            (L.ECApp (
                             (L.ECApp (
                              (L.ECApp (
@@ -2434,8 +2434,10 @@
                                (L.ECApp (
                                 (L.ECApp (
                                  (L.ECApp (
-                                  (L.EFfi ("Basis", "tag"),
-                                   _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
+                                  (L.ECApp (
+                                   (L.EFfi ("Basis", "tag"),
+                                    _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
+                           _), _),
                           attrs), _),
                          _), _),
                         xml) =>
@@ -2468,7 +2470,7 @@
                 
                 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,