Team E1

Fork of HTTPServer by Sam Kirsch

Revision:
3:d6224049b3bf
Parent:
1:6b7472d5e9ee
Child:
4:d065642c32cc
--- a/HTTPRequestHandler.cpp	Sun May 26 23:22:36 2013 +0000
+++ b/HTTPRequestHandler.cpp	Tue May 28 01:56:14 2013 +0000
@@ -2,11 +2,119 @@
 #include "mbed.h"
 #include "HTTPRequestHandler.h"
 
+#if (1 && !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
 
-HTTPRequestHandler::HTTPRequestHandler()
+static char buffer[128];
+
+HTTPRequestHandler::HTTPRequestHandler(HTTPConnection::HTTPMessage& Msg, TCPSocketConnection& Tcp)
+    : msg(Msg), tcp(Tcp)
 {
+    msg = Msg;
+    tcp = Tcp;
 }
 
 HTTPRequestHandler::~HTTPRequestHandler()
 {
-}
\ No newline at end of file
+}
+
+void HTTPRequestHandler::handleRequest()
+{
+    int err = 0;
+    
+    switch (msg.request) {
+        case HTTP_RT_GET:
+            INFO("Dispatching GET Request.");
+            err = handleGetRequest();
+            break;
+            
+//        case HTTP_RT_POST:
+//            err = handlePostRequest();
+//            break;
+            
+//        case HTTP_RT_PUT:
+//            err = handlePutRequest();
+//            break;
+            
+        default:
+            INFO("Error in handleRequest, unhandled request type.");
+            err = 404;
+            break;
+    }
+    
+    //  if any of these functions returns a negative number, call the error handler
+    if (err > 0) {
+        handleError(err);
+    }        
+}
+
+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";
+
+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));
+    sprintf(buffer, "Content-Length: %d\r\n", strlen(szErrorPage));
+    tcp.send(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));
+    }
+    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);
+        }
+        tcp.send("\r\n",2);
+    }
+    tcp.send((char*)szErrorPage, strlen(szErrorPage));
+}
+
+
+void HTTPRequestHandler::startResponse(int returnCode, int nLen, HTTPHeaders* header)
+{
+    INFO("Starting response !");
+    tcp.set_blocking(true, 1500);
+    sprintf(buffer, "HTTP/1.1 %d OK\r\n", returnCode);
+    tcp.send(buffer, strlen(buffer));
+    sprintf(buffer, "Content-Length: %d\r\n", nLen + 2);    //  Add 2 chars for the terminating CR+LF
+    tcp.send(buffer, strlen(buffer));
+    if (header == NULL) {
+        sprintf(buffer, "Content-Type: text/html\r\nServer: mbed embedded\r\n\r\n");
+        tcp.send(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\r\n",2);
+        }
+        tcp.send("\r\n", 2);
+    }
+    //  other content must be sent using the 'processResponse' function
+}
+
+void HTTPRequestHandler::processResponse(int nLen, char* body)
+{
+    INFO("Processing Response !");
+    tcp.send(body, nLen);
+}
+
+void HTTPRequestHandler::endResponse()
+{
+    INFO("Ending Response !");
+    tcp.send("\r\n", 2);
+}
+