Single instance HTTP Server using WiFly Interface.

Dependents:   WiFlyHTTPServerSample MultiThreadingHTTPServer

This is my implementation for a HTTP Server using the WiFly Interface. Please note that this is still under development.

It may still contain several bugs. I have tested it using a 1768 on an application board plus RN-XV board.

Currently there is only a FileSystem implemented. Also it is limited to GET request.

I try to extend it further so it will be more useful.

Btw, it does NOT work with RTOS, which seems not to be the Problem of my library.

Do not Forget to Import the WiFly Interface into your Project when using this library.

Change History:

REV5: - added support for basic RPC GET request functionality.

REV4: - added argument parsing from the request uri. - documentation extended and updated.

Files at this revision

API Documentation at this revision

Comitter:
leihen
Date:
Sun Jun 02 00:33:56 2013 +0000
Parent:
7:cb7fec1265b5
Child:
10:cbde7929db7f
Commit message:
Some small simplifications and improvements.

Changed in this revision

HTTPConnection.cpp Show annotated file Show diff for this revision Revisions of this file
HTTPRequestHandler.cpp Show annotated file Show diff for this revision Revisions of this file
HTTPServer.cpp Show annotated file Show diff for this revision Revisions of this file
HTTPServer.h Show annotated file Show diff for this revision Revisions of this file
Handler/FsHandler.cpp Show annotated file Show diff for this revision Revisions of this file
Handler/RpcHandler.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/HTTPConnection.cpp	Sat Jun 01 17:47:45 2013 +0000
+++ b/HTTPConnection.cpp	Sun Jun 02 00:33:56 2013 +0000
@@ -58,7 +58,7 @@
 
     
     int rcvd= 0;
-    INFO("[HTTPConnection]Waiting for new data in connection");
+    INFO("Waiting for new data in connection");
     //  Try receiving request line
     rcvd = receiveLine(buffer, 255, 3000); 
     if (rcvd == -1) {
@@ -73,7 +73,7 @@
     if (rcvd == -1) {
         //  Invalid content received, so close the connection
         INFO("Invalid message received, so sending negative response and closing connection !");
-        sprintf(buffer,"HTTP/1.1 400 NOK\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r\n\r",0);
+        sprintf(buffer,"HTTP/1.0 400 BadRequest\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r\n\r",0);
         m_Tcp.set_blocking(true, 1500);
         m_Tcp.send(buffer,strlen(buffer));
         close();
--- a/HTTPRequestHandler.cpp	Sat Jun 01 17:47:45 2013 +0000
+++ b/HTTPRequestHandler.cpp	Sun Jun 02 00:33:56 2013 +0000
@@ -49,7 +49,7 @@
             
         default:
             INFO("Error in handleRequest, unhandled request type.");
-            err = 404;
+            err = HTTP_NotImplemented;
             break;
     }
     
@@ -65,7 +65,7 @@
 {
     INFO("Handling error !");
     tcp.set_blocking(true, 1500);
-    sprintf(buffer,"HTTP/1.1 %d Error\r\n", errorCode);
+    sprintf(buffer,"HTTP/1.0 %d Error\r\n", errorCode);
     tcp.send(buffer, strlen(buffer));
     sprintf(buffer, "Content-Length: %d\r\n", strlen(szErrorPage));
     tcp.send(buffer, strlen(buffer));
@@ -90,7 +90,7 @@
 {
     INFO("Starting response (%ld bytes in total)!", nLen);
     tcp.set_blocking(true, 1500);
-    sprintf(buffer, "HTTP/1.1 %d OK\r\n", returnCode);
+    sprintf(buffer, "HTTP/1.0 %d OK\r\n", returnCode);
     tcp.send(buffer, strlen(buffer));
     sprintf(buffer, "Content-Length: %ld\r\n", nLen);    //  Add 2 chars for the terminating CR+LF
     tcp.send(buffer, strlen(buffer));
--- a/HTTPServer.cpp	Sat Jun 01 17:47:45 2013 +0000
+++ b/HTTPServer.cpp	Sun Jun 02 00:33:56 2013 +0000
@@ -1,7 +1,7 @@
 #include "mbed.h"
 #include "HTTPServer.h"
 
-#define _DEBUG      0
+#define _DEBUG      1
 
 #ifdef _DEBUG
 DigitalOut led1(LED1);
@@ -11,9 +11,9 @@
 #endif
 
 #if (_DEBUG && !defined(TARGET_LPC11U24))
-#define INFO(x, ...) if (m_pDbg) m_pDbg->printf("[HttpServer : DBG]"x"\r\n", ##__VA_ARGS__); else printf("[HttpServer : DBG]"x"\r\n", ##__VA_ARGS__);
-#define WARN(x, ...) if (m_pDbg) m_pDbg->printf("[HttpServer : WARN]"x"\r\n", ##__VA_ARGS__); else printf("[HttpServer : DBG]"x"\r\n", ##__VA_ARGS__);
-#define ERR(x, ...) if (m_pDbg) m_pDbg->printf("[HttpServer : ERR]"x"\r\n", ##__VA_ARGS__); else printf("[HttpServer : DBG]"x"\r\n", ##__VA_ARGS__);
+#define INFO(x, ...) std::printf("[HttpServer : DBG]"x"\r\n", ##__VA_ARGS__);
+#define WARN(x, ...) std::printf("[HttpServer : DBG]"x"\r\n", ##__VA_ARGS__);
+#define ERR(x, ...) std::printf("[HttpServer : DBG]"x"\r\n", ##__VA_ARGS__);
 #else
 #define INFO(x, ...)
 #define WARN(x, ...)
@@ -23,11 +23,11 @@
 
 /* Constructor */
 /* initialize all members and set the standard error handler. */
-HTTPServer::HTTPServer(Serial *dbg)
+HTTPServer::HTTPServer(PinName tx, PinName rx, PinName reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec)
+    : m_wifly(tx, rx, reset, tcp_status, ssid, phrase, sec)
 {
-    m_pDbg = dbg;
+    m_pErrorHandler = StdErrorHandler;
     m_pSvr = NULL;
-    m_pErrorHandler = StdErrorHandler;
 }
 
 HTTPServer::~HTTPServer()
@@ -58,6 +58,20 @@
         ERR("start function was already called, server is already in listening state.");
         return -1;
     }
+    INFO("Initializing wifly\n");    
+    //  Initialize the wifly wlan device
+    m_wifly.init();
+
+    INFO("Connecting to network...");
+    //  Try join the network
+    while(!m_wifly.connect());
+    INFO("connected\n");
+
+    // check if the start member was called already once
+    if (m_pSvr != NULL) {
+        ERR("start function was already called, server is already in listening state.");
+        return -1;
+    }
     
     //  Create a new server object
     m_pSvr = new TCPSocketServer();
--- a/HTTPServer.h	Sat Jun 01 17:47:45 2013 +0000
+++ b/HTTPServer.h	Sun Jun 02 00:33:56 2013 +0000
@@ -23,6 +23,7 @@
 #ifndef __HTTPSERVER_H__
 #define __HTTPSERVER_H__
 #include "mbed.h"
+#include "WiflyInterface.h"
 #include "HTTPConnection.h"
 #include "HTTPRequestHandler.h"
 
@@ -77,12 +78,12 @@
         
         TCPSocketServer*        m_pSvr;
         bool                    m_bServerListening;
-        Serial*                 m_pDbg;
-
+        WiflyInterface          m_wifly;
+        
     public:
         /** Constructor for HTTPServer objects.
         */
-        HTTPServer(Serial* dbg = NULL);
+        HTTPServer(PinName tx, PinName rx, PinName reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec);
         
         /** Destructor for HTTPServer objects.
         */
@@ -138,6 +139,9 @@
         * ready for processing the next request. Simply call \c poll as long as you want to serve new incoming requests.
         */
         int poll(bool blocking = true);
+ 
+        string getTime()
+        { return m_wifly.getTime(false); }
   
     private:
         /** The standard error handler function.
--- a/Handler/FsHandler.cpp	Sat Jun 01 17:47:45 2013 +0000
+++ b/Handler/FsHandler.cpp	Sun Jun 02 00:33:56 2013 +0000
@@ -3,7 +3,7 @@
 #include "FsHandler.h"
 
 
-#define _DEBUG 1
+#define _DEBUG 0
 
 #if (_DEBUG && !defined(TARGET_LPC11U24))
 #define INFO(x, ...) std::printf("[HTTPFsRequestHandler : DBG]"x"\r\n", ##__VA_ARGS__);
--- a/Handler/RpcHandler.cpp	Sat Jun 01 17:47:45 2013 +0000
+++ b/Handler/RpcHandler.cpp	Sun Jun 02 00:33:56 2013 +0000
@@ -4,7 +4,7 @@
 #include "mbed_rpc.h"
 
 
-#define _DEBUG 1
+#define _DEBUG 0
 
 #if (_DEBUG && !defined(TARGET_LPC11U24))
 #define INFO(x, ...) std::printf("[HTTPRpcHandler : DBG]"x"\r\n", ##__VA_ARGS__);