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

Dependents:   Smart-WiFly-WebServer WattEye X10Svr SSDP_Server

Files at this revision

API Documentation at this revision

Comitter:
WiredHome
Date:
Thu Sep 05 22:59:27 2013 +0000
Parent:
16:6ebacf2946d8
Child:
18:6199558632c0
Commit message:
Some additional performance metrics implemented which helped tune and speed it up a bit.

Changed in this revision

SW_HTTPServer.cpp Show annotated file Show diff for this revision Revisions of this file
SW_HTTPServer.h Show annotated file Show diff for this revision Revisions of this file
--- a/SW_HTTPServer.cpp	Sun Sep 01 19:19:08 2013 +0000
+++ b/SW_HTTPServer.cpp	Thu Sep 05 22:59:27 2013 +0000
@@ -14,16 +14,15 @@
 #include "SW_HTTPServer.h"
 #include "Utility.h"
 
-#define DEBUG
+//#define DEBUG
 
 const char * DEFAULT_FILENAME = "index.htm";
 
 // Header information to always send (must be \r\n terminated)
-const char hdr_httpver[] = "HTTP/1.0";                  // typically HTTP/1.0 or HTTP/1.1
+const char hdr_httpver[] = "HTTP/1.0";                  // Wifly may not be able to support HTTP/1.1 protocol
 const char hdr_age[]     = "Max-age: 0\r\n";            // expires right away
 const char hdr_server[]  = "Server: Smart_Server v0.1\r\n"; // Server
-const char hdr_dnt[]     = "DNT: 1\r\n";                // Do Not Track
-const char hdr_close[]   = "Connection: close\r\n";     // tell the client to close the connection
+const char hdr_close[]   = "Connection: close\r\n";     // tell the client the server closes the connection immediately
 const char nl[]          = "\r\n";                      // final \r\n for the termination of the header
 
 
@@ -165,7 +164,7 @@
     int n;
     static unsigned int t_ref;       // reference point for the PerformanceTimer
 
-#ifdef DEBUG
+#if 1 || defined(DEBUG)
     static state lastOp = Reset;
     if (lastOp != op) {
         const char *states[] = {"Idle", "Receiving", "Sending", "WaitingToClose", "Reset"};
@@ -180,12 +179,13 @@
 
         case Idle:
             PerformanceTimer.reset();
+            t_ref = (unsigned int)PerformanceTimer.read_us();
             bPtr = headerbuffer;
             if (0 == server->accept(client)) {
                 op = Receiving;
-                t_ref = (unsigned int)PerformanceTimer.read_us();
+                t_ref = RecordPerformanceData(&perfData.ConnectionAccepted, t_ref);
 #ifdef DEBUG
-                pc->printf("Accept at %u\r\n", t_ref);
+                pc->printf("Accepted at %u\r\n", (unsigned int)PerformanceTimer.read_us());
 #endif
             }
             break;
@@ -200,7 +200,10 @@
                 bPtr[n] = '\0';
                 if (ParseHeader(headerbuffer)) {
                     op = Sending;
-                    t_ref = RecordPerformanceData(&perfData.Header, t_ref);
+                    t_ref = RecordPerformanceData(&perfData.HeaderParsed, t_ref);
+#ifdef DEBUG
+                    pc->printf("Header Parsed at %u\r\n", (unsigned int)PerformanceTimer.read_us());
+#endif
                 }
                 bPtr += n;
             }
@@ -209,12 +212,22 @@
         case Sending:
             SendResponse();
             op = WaitingToClose;
-            RecordPerformanceData(&perfData.SendData, t_ref);
+            t_ref = RecordPerformanceData(&perfData.ResponseSent, t_ref);
+#ifdef DEBUG
+            pc->printf("Response Sent at %u\r\n", (unsigned int)PerformanceTimer.read_us());
+#endif
             break;
 
         case WaitingToClose:
+#ifdef DEBUG
+            pc->printf("Connection closed entry %u\r\n", (unsigned int)PerformanceTimer.read_us());
+#endif
             close_connection();
             op = Idle;
+            RecordPerformanceData(&perfData.ConnectionClosed, t_ref);
+#ifdef DEBUG
+            pc->printf("Connection closed exit  %u\r\n", (unsigned int)PerformanceTimer.read_us());
+#endif
             break;
     }
 }
@@ -398,7 +411,6 @@
     if (optional_text) {
         send(optional_text);
     }
-    send(hdr_dnt);
     send(hdr_close);
     send(nl);
 }
@@ -504,15 +516,12 @@
 void HTTPServer::SendResponse()
 {
 #ifdef DEBUG
-    pc->printf("SendResponse(%s) [%d]\r\n", queryType, __LINE__);
+    pc->printf("SendResponse(%s) at %u\r\n", queryType, (unsigned int)PerformanceTimer.read_us());
 #endif
     if (strcmp(queryType, "GET") == 0 ||  strcmp(queryType, "POST") == 0) {
         if (!(queryString[0] == '.' && queryString[1] == '.')) {
             const char * fType;
 
-#ifdef DEBUG
-            pc->printf("  SendResponse() [%d]\r\n", __LINE__);
-#endif
             if (!CheckDynamicHandlers()) {
                 // Otherwise, this queryString must be trying to reference a static file
                 if (queryString[strlen(queryString)-1] == '/') {
@@ -536,6 +545,10 @@
         //pc->printf("Unsupported query type %s\r\n", queryType);
         header(400, "Bad Request", "Pragma: err - Unsupported query type\r\n");
     }
+#ifdef DEBUG
+    pc->printf("  SendResponse complete at %u\r\n", (unsigned int)PerformanceTimer.read_us());
+#endif
+
     if (queryType) {
         myfree(queryType);
         queryType = NULL;
@@ -548,6 +561,9 @@
         myfree(postQueryString);
         postQueryString = NULL;
     }
+#ifdef DEBUG
+    pc->printf("  SendResponse free at %u\r\n", (unsigned int)PerformanceTimer.read_us());
+#endif
 }
 
 
@@ -720,6 +736,10 @@
     memcpy(p, &perfData, sizeof(perfData));
 }
 
+unsigned int HTTPServer::GetPerformanceClock()
+{
+    return (unsigned int)PerformanceTimer.read_us();
+}
 
 unsigned int HTTPServer::RecordPerformanceData(SW_PerformanceParam * param, unsigned int refTime)
 {
--- a/SW_HTTPServer.h	Sun Sep 01 19:19:08 2013 +0000
+++ b/SW_HTTPServer.h	Thu Sep 05 22:59:27 2013 +0000
@@ -453,8 +453,10 @@
     * Performance metrics
     */
     typedef struct SW_PERFDATA {
-        SW_PerformanceParam Header;
-        SW_PerformanceParam SendData;
+        SW_PerformanceParam ConnectionAccepted;
+        SW_PerformanceParam HeaderParsed;
+        SW_PerformanceParam ResponseSent;
+        SW_PerformanceParam ConnectionClosed;
         //SW_PerformanceParam SendFile;
     } SW_PerformanceData;
 
@@ -472,6 +474,11 @@
     * Reset performance metrics.
     */
     void ResetPerformanceData();
+    
+    /**
+    * Get performance clock
+    */
+    unsigned int GetPerformanceClock();
 
     /**
     * Get the underlying wifly object.