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

Dependents:   Smart-WiFly-WebServer WattEye X10Svr SSDP_Server

Revision:
14:19c5f6151319
Parent:
13:8975d7928678
Child:
16:6ebacf2946d8
--- a/SW_HTTPServer.cpp	Mon Aug 12 11:26:59 2013 +0000
+++ b/SW_HTTPServer.cpp	Mon Aug 12 23:03:25 2013 +0000
@@ -26,6 +26,7 @@
 const char hdr_close[]   = "Connection: close\r\n";     // tell the client to close the connection
 const char nl[]          = "\r\n";                      // final \r\n for the termination of the header
 
+
 static const struct {
     char *ext;
     char *filetype;
@@ -97,10 +98,6 @@
     pc = _pc;
     queryType = NULL;
     queryString = NULL;
-//    hostString = NULL;
-//    contentLength = NULL;
-//    contentType = NULL;
-//    authorization = NULL;
     postQueryString = NULL;
     queryParamCount = 0;
     handlercount = 0;
@@ -344,8 +341,6 @@
     if (pVal)
         *pVal = '\0';
     do {
-        //pc->printf("Parse(%s)\r\n", pName);
-        //*pName++ = '\0';   // already '\0' on the first entry
         queryParams[queryParamCount].name = pName;
         pVal = strchr(pName, '=');
         pNextName = strchr(pName,'&');
@@ -356,7 +351,6 @@
                 pName = pVal;
             }
         }
-        //pc->printf("  [%s]=[%s]\r\n", queryParams[queryParamCount].name, queryParams[queryParamCount].value);
         queryParamCount++;
         if (pNextName) {
             pName = pNextName;
@@ -411,17 +405,19 @@
     send(nl);
 }
 
+
 bool HTTPServer::close_connection()
 {
     bool res;
 
-    res = server->close();  //wifly->close();
+    res = server->close();
 #ifdef DEBUG
     pc->printf("close connection returned %d\r\n", res);
 #endif
     return res;
 }
 
+
 bool HTTPServer::Extract(char * haystack, char * needle, char ** string)
 {
     bool ret = false;       // assume failure until proven otherwise
@@ -456,6 +452,7 @@
     return ret;
 }
 
+
 char * HTTPServer::rewriteWithDefaultFile(char * queryString)
 {
     char * temp = (char *)mymalloc(strlen(queryString) + strlen(DEFAULT_FILENAME) + 1);
@@ -471,6 +468,7 @@
     }
 }
 
+
 char * HTTPServer::rewritePrependWebroot(char * queryString)
 {
     char * temp = (char *)mymalloc(strlen(webroot) + strlen(queryString) + 1);
@@ -488,6 +486,7 @@
     }
 }
 
+
 bool HTTPServer::CheckDynamicHandlers()
 {
     bool regHandled = false;
@@ -503,6 +502,7 @@
     return regHandled;
 }
 
+
 void HTTPServer::SendResponse()
 {
 #ifdef DEBUG
@@ -546,28 +546,13 @@
         myfree(queryString);
         queryString = NULL;
     }
-//    if (hostString) {
-//        myfree(hostString);
-//        hostString = NULL;
-//    }
-//    if (contentLength) {
-//        myfree(contentLength);
-//        contentLength = NULL;
-//    }
-//    if (contentType) {
-//        myfree(contentType);
-//        contentType = NULL;
-//    }
-//    if (authorization) {
-//        myfree(authorization);
-//        authorization = NULL;
-//    }
     if (postQueryString) {
         myfree(postQueryString);
         postQueryString = NULL;
     }
 }
 
+
 bool HTTPServer::ParseHeader(char * buffer)
 {
     char * dblCR;
@@ -594,9 +579,6 @@
             *eoRec = '\0';
             if (*(eoRec-1) == '\r')
                 *(eoRec-1) = '\0';
-#ifdef DEBUG
-            pc->printf("rec {%s}\r\n", soRec);
-#endif
             // Inspect the supported query types (GET, POST) and ignore (HEAD, PUT, OPTION, DELETE, TRACE, CONNECT]
             // This is very clumsy
             if (strstr(soRec, "GET ") == soRec) {
@@ -605,14 +587,12 @@
                     queryType = (char *)mymalloc(strlen("GET")+1);
                     strcpy(queryType, "GET");
                 }
-                //printf("GET: %s\r\n", queryString);
             } else if (strstr(soRec, "POST ") == soRec) {
                 Extract(soRec, "POST", &queryString);
                 if (queryString) {
                     queryType = (char *)mymalloc(strlen("POST")+1);
                     strcpy(queryType, "POST");
                 }
-                //printf("POST: %s\r\n", queryString);
             }
 
             // if there is a ": " delimiter, we have a header item to parse into name,value pair
@@ -622,7 +602,7 @@
             if (delim 
             && (!chkSpace || (chkSpace && delim < chkSpace))
             && headerParamCount < maxheaderParams) {
-                *delim++ = '\0';
+                *delim++ = '\0';        // replace ": " with null
                 *delim++ = '\0';
                 headerParams[headerParamCount].name = soRec;
                 headerParams[headerParamCount].value = delim;
@@ -632,10 +612,6 @@
 #endif
                 headerParamCount++;
             }
-//            Extract(soRec, "Host: ", &hostString);
-//            Extract(soRec, "Content-Length: ", &contentLength);
-//            Extract(soRec, "Content-Type: ", &contentType);
-//            Extract(soRec, "Authorization: ", &authorization);
             soRec = eoRec + 1;
             eoRec = strchr(soRec, '\n');
         }
@@ -654,8 +630,6 @@
                 UnescapeString(paramDelim);   // everything after the '?'
                 ParseParameters(paramDelim);    // pointing at the NULL, but there are queryParams beyond
             }
-            //for (int i=0; i<queryParamCount; i++)
-            //    pc->printf("param %d '%s'='%s'\r\n", i, queryParams[i].name, queryParams[i].value);
         } else {
             pc->printf("ERROR: queryString not found in (%s)\r\n", soRec);
         }
@@ -671,7 +645,6 @@
         //   'application/x-www-form-urlencoded'?
         int postBytes = atoi(GetHeaderValue("Content-Length"));
         bool acceptIt = false;
-        //pc->printf("Content-Length = %d\r\n", postBytes);
         if (strcmp(queryType, "POST") == 0 && postBytes > 0 ) {
             if (postBytes) {
                 bool regHandled = false;
@@ -730,6 +703,7 @@
     return advanceState;
 }
 
+
 const char * HTTPServer::GetHeaderValue(const char * hdr)
 {
     int i;
@@ -748,6 +722,7 @@
     memcpy(p, &perfData, sizeof(perfData));
 }
 
+
 int HTTPServer::RecordPerformanceData(SW_PerformanceParam * param, int refTime)
 {
     int t_now = PerformanceTimer.read_us();
@@ -758,6 +733,7 @@
     return t_now;
 }
 
+
 void HTTPServer::ResetPerformanceData()
 {
     memset(&perfData, 0, sizeof(perfData));