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 Jun 27 17:08:25 2013 +0000
Parent:
6:fdce4464d92b
Child:
8:262583f054f6
Commit message:
close_connection revised to return the results from close(); Small changes for development

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	Tue Jun 25 18:11:10 2013 +0000
+++ b/SW_HTTPServer.cpp	Thu Jun 27 17:08:25 2013 +0000
@@ -14,7 +14,7 @@
 #include "SW_HTTPServer.h"
 #include "Utility.h"
 
-//#define DEBUG
+#define DEBUG
 
 const char * DEFAULT_FILENAME = "index.htm";
 
@@ -46,13 +46,13 @@
 };
 
 HTTPServer::HTTPServer(
-    Wifly * _wf, 
-    int port, 
-    const char * _webroot, 
-    int _maxparams, 
-    int _maxdynamicpages, 
-    PC * _pc, 
-    int _allocforheader, 
+    Wifly * _wf,
+    int port,
+    const char * _webroot,
+    int _maxparams,
+    int _maxdynamicpages,
+    PC * _pc,
+    int _allocforheader,
     int _allocforfile)
 {
     wifly = _wf;
@@ -124,13 +124,21 @@
         Idle,               // waiting for a connection
         Receiving,          // receiving data
         Sending,            // send the response
-        WaitingToClose      // small timeout to close
+        WaitingToClose,     // small timeout to close
+        Reset
     } state;
     static state op = Idle;
+    static state lastOp = Reset;
     static char * bPtr = headerbuffer;
     int n;
     static int t_ref;       // reference point for the timer
 
+    #ifdef DEBUG
+    if (lastOp != op) {
+        pc->printf("%d", op);
+        lastOp = op;
+    }
+    #endif
     switch(op) {
         default:                    // not expected to arrive here
             op = Idle;
@@ -355,14 +363,15 @@
     send(nl);
 }
 
-void HTTPServer::close_connection( )
+bool HTTPServer::close_connection()
 {
-#ifndef DEBUG
-    wifly->close();
-#else // DEBUG
-    if (!wifly->close())
-        pc->printf("Couldn't close connection\r\n");
+    bool res;
+
+    res = wifly->close();
+#ifdef DEBUG
+    pc->printf("close connection returned %d\r\n", res);
 #endif
+    return res;
 }
 
 bool HTTPServer::Extract(char * haystack, char * needle, char ** string)
@@ -502,7 +511,7 @@
     char * dblCR;
     bool advanceState = false;
     int bytecount;
-    
+
     // Bad hack to have to do this here, but it isn't being set in the
     // underlying layer, and this is what allows it to properly "close"
     // when it is done.
@@ -520,7 +529,7 @@
 #endif
         char * soRec = buffer;                  // start of the next record of text
         char * eoRec = strchr(soRec, '\n');     // search for end of a record
-        
+
         bytecount = strlen(buffer);
         if (bytecount > maxheaderbytes)
             maxheaderbytes = bytecount;
@@ -558,7 +567,6 @@
         }
         advanceState = true;
         buffer[0] = 0;
-//       bPtr = buffer;
 
         // This part parses the extra data on a POST method.
         // Since there has to be a dynamic handler registered for this
@@ -628,11 +636,13 @@
     return advanceState;
 }
 
-void HTTPServer::GetPerformanceData(SW_PerformanceData * p) {
+void HTTPServer::GetPerformanceData(SW_PerformanceData * p)
+{
     memcpy(p, &perfData, sizeof(perfData));
 }
 
-int HTTPServer::RecordPerformanceData(SW_PerformanceParam * param, int refTime) {
+int HTTPServer::RecordPerformanceData(SW_PerformanceParam * param, int refTime)
+{
     int t_now = timer.read_us();
     param->TotalTime_us += (t_now - refTime);
     param->Samples++;
@@ -641,7 +651,8 @@
     return t_now;
 }
 
-void HTTPServer::ResetPerformanceData() {
+void HTTPServer::ResetPerformanceData()
+{
     memset(&perfData, 0, sizeof(perfData));
 }
 
--- a/SW_HTTPServer.h	Tue Jun 25 18:11:10 2013 +0000
+++ b/SW_HTTPServer.h	Thu Jun 27 17:08:25 2013 +0000
@@ -71,7 +71,7 @@
 /// @li Provides a registration interface for dynamically generated pages that
 ///     can then interact with other hardware.
 /// @li Revised to be Non-blocking, however the execution time is variable
-///     depending on the actions being performed.
+///     depending on the actions being performed and can span hundreds of msec.
 ///
 /// Limitations:
 /// @li Supports only a single connection at a time.
@@ -406,8 +406,10 @@
     * This switches the module into command mode, performs the close,
     * then switches it back to data mode. So, this is a time-expensive
     * command.
+    *
+    * @returns true if successful
     */
-    void close_connection();
+    bool close_connection();
     
     /**
     * Get the size of the largest header. 
@@ -542,3 +544,4 @@
 };
 #endif //SW_HTTPSERVER_H
 
+