Team E1

Fork of HTTPServer by Sam Kirsch

Revision:
1:6b7472d5e9ee
Parent:
0:7a2421e63e74
Child:
2:8653bbcf7e58
--- a/HTTPServer.cpp	Sun May 26 20:13:28 2013 +0000
+++ b/HTTPServer.cpp	Sun May 26 22:49:42 2013 +0000
@@ -6,7 +6,7 @@
 DigitalOut led3(LED3);
 DigitalOut led4(LED4);
 
-#if (1 && !defined(TARGET_LPC11U24))
+#if (0 && !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__);
@@ -16,6 +16,7 @@
 #define ERR(x, ...)
 #endif
 
+static const char* szMsgs = "No such file or folder.";
 
 HTTPServer::HTTPServer(Serial* pDbg)
 {
@@ -107,12 +108,47 @@
         HTTPConnection con;
         int c = con.poll();
         if (c == 0) {
+            //  Handle the request
+            HandleRequest(con.m_Msg, Clnt);
         }
         led2 = 0;
         led3 = 0;
     }
     
-    
     INFO("Leaving polling thread");
     return 0;
+}
+
+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));
+        INFO("Webrequest left unhandled.");
+    }
+    else {
+        //  Handler was found so pass action to handler
+        INFO("Routing webrequest !");
+        (it->second)(msg, tcp);
+    }
 }
\ No newline at end of file