Single instance HTTP Server using new Ethernet Interface. Blocking mode only; this improved stability, but the HTTP server must be started from a separate thread.

Dependents:   SmartLight

Fork of HTTPServer by Henry Leinen

Revision:
13:aa5338a5e452
Parent:
12:ba81cc117fb6
Child:
16:cc3f5c53d0d5
--- a/HTTPRequestHandler.cpp	Wed Jun 05 23:39:24 2013 +0000
+++ b/HTTPRequestHandler.cpp	Sat Jun 22 15:41:34 2013 +0000
@@ -1,30 +1,21 @@
 /* HTTPRequestHandler.cpp */
 #include "mbed.h"
 #include "HTTPRequestHandler.h"
+#define DEBUG
+#include "debug.h"
 #include <ctype.h>
 
-#define _DEBUG 0
-
-#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
 
 static char buffer[128];
 
 
-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,7 +45,9 @@
     {".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"   },
+    {".css", "Content-Type: text/css\r\n"    },
+    {".js",  "Content-Type: text/javascript\r\n"}};
     
 HTTPRequestHandler::HTTPRequestHandler(HTTPConnection::HTTPMessage& Msg, TCPSocketConnection& Tcp)
     : msg(Msg), tcp(Tcp)
@@ -119,12 +112,12 @@
     }        
 }
 
-static const char* szErrorPage = "<HTML><HEAD><META content=\"text/html\" http-equiv=Content-Type></HEAD><BODY><h1>Error %d</h1><P>HTTPServer Error<P></BODY></HTML>\r\n\r\n";
+static const char* szErrorPage = "<HTML><HEAD><META content=\"text/html\" http-equiv=Content-Type></HEAD><BODY><h1>Error</h1><P>HTTPServer Error<P></BODY></HTML>\r\n\r\n";
 
 void HTTPRequestHandler::handleError(int errorCode, HTTPHeaders* header)
 {
     INFO("Handling error !");
-    tcp.set_blocking(true, 1500);
+    tcp.set_blocking(false, 1500);
     sprintf(buffer,"HTTP/1.1 %d Error\r\n", errorCode);
     tcp.send(buffer, strlen(buffer));
     sprintf(buffer, "Content-Length: %d\r\n", strlen(szErrorPage));
@@ -149,29 +142,25 @@
 void HTTPRequestHandler::startResponse(int returnCode, long nLen, HTTPHeaders* header)
 {
     INFO("Starting response (%ld bytes in total)!", nLen);
-    tcp.set_blocking(true, 1500);
+    tcp.set_blocking(false, 1500);
     sprintf(buffer, "HTTP/1.1 %d OK\r\n", returnCode);
-    tcp.send_all(buffer, strlen(buffer));
-    tcp.send_all((char*)hdrConClose, strlen(hdrConClose));
+    tcp.send(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));
+    tcp.send(buffer, strlen(buffer));
+    INFO("Sending standard headers !");
     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));
+        tcp.send_all((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);
+            tcp.send_all((char*)cIter->first, strlen(cIter->first));
+            tcp.send_all(": ", 2);
+            tcp.send_all((char*)cIter->second, strlen(cIter->second));
+            tcp.send_all("\r\n\r\n",2);
         }
         tcp.send_all("\r\n", 2);
     }
+    INFO("Proceeding !");
     //  other content must be sent using the 'processResponse' function
 }
 
@@ -184,5 +173,4 @@
 void HTTPRequestHandler::endResponse()
 {
     INFO("Ending Response !");
-//    tcp.send("\r\n\r\n", 4);
 }