A simple web server that can be bound to either the EthernetInterface or the WiflyInterface.

Dependents:   Smart-WiFly-WebServer WattEye X10Svr SSDP_Server

Revision:
32:7ded9bacb546
Parent:
31:8f72be717a3c
Child:
33:ef165a67ab22
--- a/SW_HTTPServer.cpp	Thu Nov 28 18:19:06 2013 +0000
+++ b/SW_HTTPServer.cpp	Mon Dec 30 23:03:37 2013 +0000
@@ -12,23 +12,12 @@
 //
 #include "mbed.h"
 
-#include "Utility.h"
-
-//#define DEBUG "HTTP"
 #include "SW_HTTPServer.h"      // define DEBUG before this
 
-#if (defined(DEBUG) && !defined(TARGET_LPC11U24))
-#define DBG(x, ...)  pc->printf("[DBG %s%4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
-#define WARN(x, ...) pc->printf("[WRN %s%4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
-#define ERR(x, ...)  pc->printf("[ERR %s%4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
-#define INFO(x, ...) pc->printf("[INF %s%4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
-#else
-#define DBG(x, ...)
-#define WARN(x, ...)
-#define ERR(x, ...)
-#define INFO(x, ...)
-#endif
+#define DEBUG "httpd"
+#include "Utility.h"
 
+#define CHUNKSIZE 1600      // When receiving and handing partial to the app, this is the chunk size
 
 const char * DEFAULT_FILENAME = "index.htm";
 
@@ -661,40 +650,33 @@
                     //            END:   signals that all chunks were delivered.
                     //
                     // If so, we'll make space for it
-                    postQueryString = (char *)mymalloc(postBytes + 1);
+                    postQueryString = (char *)mymalloc(CHUNKSIZE);
                     INFO("Free space %d", Free());
                     if (postQueryString) {
-                        char * offset;
-                        int len;
+                        int ttlReceived = 0;
 
                         INFO("Processing");
                         dblCR += 4;                         // If we slurped up any of the POST,
                         while (*dblCR && *dblCR <= ' ')
                             dblCR++;
                         strcpy(postQueryString, dblCR);     // copy that in and then get the rest
-                        while ((len = strlen(postQueryString)) < postBytes) {
-                            int n;
-                            offset = postQueryString + len;
-                            n = client.receive(offset, postBytes - len);
-                            if (n >=0) {
-                                offset[n] = '\0';
-                                INFO("HTTPd: %d of %d: [%s]", len, postBytes, offset);
-                            } else if (n < 0) {
-                                INFO("*** receive returned %d ***", n);
-                                break;      // no more data, before the plan
+                        while (ttlReceived < postBytes) {
+                            int len;
+                            wait_ms(4);
+                            len = client.receive(postQueryString, CHUNKSIZE);
+                            if (len >=0) {
+                                INFO("Passing %d bytes (%d of %d) in [%s]", len, ttlReceived, postBytes, postQueryString);
+                                ttlReceived += len;
+                                acceptIt = (*handlers[ndxHandler].callback)(this, DATA_TRANSFER, postQueryString, NULL, len);
+                            } else if (len < 0) {
+                                INFO("*** receive returned %d ***", len);
+                                break;      // no more data
                             }
                         }
-                        if (len >= 0) {
-//                            UnescapeString(postQueryString);
-//                            ParseParameters(postQueryString);
-                            // use the same handler as for the length check
-                            INFO("calling user code");
-                            acceptIt = (*handlers[ndxHandler].callback)(this, DATA_TRANSFER, postQueryString, NULL, 0);
-                        } else {
-                            ERR("HTTPd: len error.");
-                        }
+                        myfree(postQueryString);
+                        INFO("done.");
                     } else {
-                        ERR("HTTPd: attempt to allocate %d failed.", postBytes+1);
+                        ERR("HTTPd: attempt to allocate %d bytes failed.", CHUNKSIZE);
                     }
                 } else {
                     // Simply copy it to the bitbucket