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.

Revision:
13:93ff322420b0
Parent:
12:ba81cc117fb6
--- a/HTTPRequestHandler.cpp	Wed Jun 05 23:39:24 2013 +0000
+++ b/HTTPRequestHandler.cpp	Wed Jun 26 21:12:42 2013 +0000
@@ -2,29 +2,22 @@
 #include "mbed.h"
 #include "HTTPRequestHandler.h"
 #include <ctype.h>
-
-#define _DEBUG 0
+#include "Wifly.h"
 
-#if (_DEBUG && !defined(TARGET_LPC11U24))
-#define INFO(x, ...) std::printf("[HTTPRequestHandler : DBG]"x"\r\n", ##__VA_ARGS__);
-#define WARN(x, ...) std::printf("[HTTPRequestHandler : DBG]"x"\r\n", ##__VA_ARGS__);
-#define ERR(x, ...) std::printf("[HTTPRequestHandler : DBG]"x"\r\n", ##__VA_ARGS__);
-#else
-#define INFO(x, ...)
-#define WARN(x, ...)
-#define ERR(x, ...)
-#endif
+//#define DEBUG
+#include "debug.h"
 
 static char buffer[128];
 
+//const char hdrConClose[] = "Connection: Keep-Alive\r\n";
 
-const char hdrDNT[] = "DNT: 1\r\n";
-const char hdrMaxAge[] = "MaxAge: 0\r\n";
-const char hdrConClose[] = "Connection: Keep-Alive\r\n";
-//const char hdrTrsfrEnc[] = "Transfer-Encoding: Chunked\r\n";
-const char hdrContent[] = "Content-Type: text/html\r\n";
-const char hdrServer[] = "Server: mbed embedded\r\n";
-const char hdrEndl[] = "\r\n";
+const char hdrStandard[] = "DNT: 1\r\n"
+                            "MaxAge: 0\r\n"
+                            "Connection: Keep-Alive\r\n"
+                            "Content-Type: text/html\r\n"
+                            "Server: mbed embedded\r\n"
+                            "Accessible: 1\r\n"
+                            "\r\n";
 
 
 static int _stricmp(const char* a, const char* b)
@@ -54,13 +47,13 @@
     {".txt", "Content-Type: plain/text\r\n"  },
     {".pdf", "Content-Type: application/pdf\r\n" },
     {".htm", "Content-Type: text/html\r\n"   },
-    {".html","Content-Type: text/html\r\n"   }};
+    {".html","Content-Type: text/html\r\n"   },
+    {".js", "Content-Type: text/javascript\r\n"}};
     
-HTTPRequestHandler::HTTPRequestHandler(HTTPConnection::HTTPMessage& Msg, TCPSocketConnection& Tcp)
-    : msg(Msg), tcp(Tcp)
+HTTPRequestHandler::HTTPRequestHandler(HTTPConnection::HTTPMessage& Msg)
+    : msg(Msg)
 {
     msg = Msg;
-    tcp = Tcp;
 
 }
 
@@ -75,6 +68,7 @@
     header["MaxAge"] = "0";
     header["Connection"] = "Keep-Alive";
     header["Server"] = "mbed Embedded";
+    header["Accessible"] = "1";
     if (fext == NULL)
         header["Content-Type"] = "text/html";
     else {
@@ -124,53 +118,45 @@
 void HTTPRequestHandler::handleError(int errorCode, HTTPHeaders* header)
 {
     INFO("Handling error !");
-    tcp.set_blocking(true, 1500);
     sprintf(buffer,"HTTP/1.1 %d Error\r\n", errorCode);
-    tcp.send(buffer, strlen(buffer));
+    Wifly::getInstance()->sendData(buffer, strlen(buffer));
     sprintf(buffer, "Content-Length: %d\r\n", strlen(szErrorPage));
-    tcp.send(buffer, strlen(buffer));
+    Wifly::getInstance()->sendData(buffer, strlen(buffer));
     if (header == NULL) {
         sprintf(buffer, "Content-Type: text/html\r\nServer: mbed embedded\r\n\n\r");
-        tcp.send(buffer, strlen(buffer));
+        Wifly::getInstance()->sendData(buffer, strlen(buffer));
     }
     else {
         for ( map<const char*, const char*>::iterator cIter = header->begin() ; cIter != header->end() ; cIter ++) {
-            tcp.send((char*)cIter->first, strlen(cIter->first));
-            tcp.send(": ", 2);
-            tcp.send((char*)cIter->second, strlen(cIter->second));
-            tcp.send("\r\n",2);
+            Wifly::getInstance()->sendData((char*)cIter->first, strlen(cIter->first));
+            Wifly::getInstance()->sendData(": ", 2);
+            Wifly::getInstance()->sendData((char*)cIter->second, strlen(cIter->second));
+            Wifly::getInstance()->sendData("\r\n",2);
         }
-        tcp.send("\r\n",2);
+        Wifly::getInstance()->sendData("\r\n",2);
     }
-    tcp.send((char*)szErrorPage, strlen(szErrorPage));
+    Wifly::getInstance()->sendData((char*)szErrorPage, strlen(szErrorPage));
 }
 
 
 void HTTPRequestHandler::startResponse(int returnCode, long nLen, HTTPHeaders* header)
 {
     INFO("Starting response (%ld bytes in total)!", nLen);
-    tcp.set_blocking(true, 1500);
     sprintf(buffer, "HTTP/1.1 %d OK\r\n", returnCode);
-    tcp.send_all(buffer, strlen(buffer));
-    tcp.send_all((char*)hdrConClose, strlen(hdrConClose));
+    Wifly::getInstance()->sendData(buffer, strlen(buffer));
     sprintf(buffer, "Content-Length: %ld\r\n", nLen);    //  Add 2 chars for the terminating CR+LF
-    tcp.send_all(buffer, strlen(buffer));
+    Wifly::getInstance()->sendData(buffer, strlen(buffer));
     if (header == NULL) {
-        tcp.send_all((char*)hdrDNT, strlen(hdrDNT));
-        tcp.send_all((char*)hdrMaxAge, strlen(hdrMaxAge));
-        tcp.send_all((char*)hdrContent, strlen(hdrContent));
-//        tcp.send_all((char*)hdrTrsfrEnc, strlen(hdrTrsfrEnc));
-        tcp.send_all((char*)hdrServer, strlen(hdrServer));
-        tcp.send_all((char*)hdrEndl, strlen(hdrEndl));
+            Wifly::getInstance()->sendData((char*)hdrStandard, strlen(hdrStandard));
     }
     else {
         for ( map<const char*, const char*>::iterator cIter = header->begin() ; cIter != header->end() ; cIter ++) {
-            tcp.send((char*)cIter->first, strlen(cIter->first));
-            tcp.send(": ", 2);
-            tcp.send((char*)cIter->second, strlen(cIter->second));
-            tcp.send("\r\n\r\n",2);
+            Wifly::getInstance()->sendData((char*)cIter->first, strlen(cIter->first));
+            Wifly::getInstance()->sendData(": ", 2);
+            Wifly::getInstance()->sendData((char*)cIter->second, strlen(cIter->second));
+            Wifly::getInstance()->sendData("\r\n\r\n",2);
         }
-        tcp.send_all("\r\n", 2);
+        Wifly::getInstance()->sendData("\r\n", 2);
     }
     //  other content must be sent using the 'processResponse' function
 }
@@ -178,11 +164,15 @@
 void HTTPRequestHandler::processResponse(int nLen, char* body)
 {
     INFO("Processing Response (%d bytes)!\n",nLen);
-    tcp.send_all(body, nLen);
+    Wifly::getInstance()->sendData(body, nLen);
 }
 
 void HTTPRequestHandler::endResponse()
 {
-    INFO("Ending Response !");
-//    tcp.send("\r\n\r\n", 4);
+//    INFO("Ending Response !");
+//    Wifly::getInstance()->sendData("\r\n\r\n", 4);
+//    Wifly::getInstance()->cmdMode();
+//    Wifly::getInstance()->sendData("close\r", 6);
+//    Thread::wait(250);
+//    Wifly::getInstance()->exit();
 }