Single instance HTTP Server using new Ethernet Interface with bug fix for URL arguments

Fork of HTTPServer by Henry Leinen

Revision:
2:8653bbcf7e58
Parent:
1:6b7472d5e9ee
Child:
3:d6224049b3bf
--- a/HTTPServer.cpp	Sun May 26 22:49:42 2013 +0000
+++ b/HTTPServer.cpp	Sun May 26 23:22:36 2013 +0000
@@ -16,13 +16,12 @@
 #define ERR(x, ...)
 #endif
 
-static const char* szMsgs = "No such file or folder.";
-
 HTTPServer::HTTPServer(Serial* pDbg)
 {
     m_pDbg = pDbg;
     m_pSvr = NULL;
     m_bServerListening = false;
+    m_pErrorHandler = StdErrorHandler;
 }
 
 HTTPServer::~HTTPServer()
@@ -34,6 +33,20 @@
     }
 }
 
+
+const char* szStdErrorPage = "<HTML><HEAD><META content=\"text/html\" http-equiv=Content-Type></HEAD><BODY><h1>Error 404</h1><P>This resource is not available<P></BODY></HTML>\r\n\r\n";
+
+void HTTPServer::StdErrorHandler(HTTPMessage& msg, TCPSocketConnection& tcp)
+{
+    char echoHeader[512];
+    
+    tcp.set_blocking(true, 1500);
+    sprintf(echoHeader,"HTTP/1.1 404 Fail\r\nContent-Length: %d\r\nContent-Type: text/html\r\nServer: mbed embedded\r\n\n\r",strlen(szStdErrorPage));
+    tcp.send(echoHeader, strlen(echoHeader));
+    tcp.send((char*)szStdErrorPage, strlen(szStdErrorPage));
+}
+
+
 int HTTPServer::start(int port)
 {
     // check if the start member was called already once
@@ -121,29 +134,14 @@
 
 void HTTPServer::HandleRequest(HTTPMessage& msg, TCPSocketConnection& tcp)
 {
-    static char echoHeader[256] = {};
-    static const char* szPage = {
-        "<HTML>\r\n"
-        "<HEAD>\r\n"
-        "<META content=\"text/html\" http-equiv=Content-Type>\r\n"
-        "</HEAD>\r\n"
-        "<BODY>\r\n"
-        "<h1>ERROR 404</h1>\r\n"
-        "<P>File not found<P>\r\n"
-        "</BODY>\r\n"
-        "</HTML>\r\n\r\n"};
-
     map<string, HTTPRequestHandlerFunction>::iterator it;
     
     it = m_pHandlers.find(msg.uri);
     
     if (it == m_pHandlers.end()) {
         //  There is no such handler, so return invalid
-        
-        tcp.set_blocking(true, 1500);
-        sprintf(echoHeader,"HTTP/1.1 404 Fail\r\nContent-Length: %d\r\nContent-Type: text/html\r\nServer: mbed embedded\r\nConnection: Close\r\n\r\n",strlen(szPage));
-        tcp.send(echoHeader,strlen(echoHeader));
-        tcp.send((char*)szPage,strlen(szMsgs));
+
+        m_pErrorHandler(msg, tcp);        
         INFO("Webrequest left unhandled.");
     }
     else {