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:
12:ba81cc117fb6
Parent:
11:3943841e1798
Child:
13:aa5338a5e452
diff -r 3943841e1798 -r ba81cc117fb6 HTTPRequestHandler.cpp
--- a/HTTPRequestHandler.cpp	Sun Jun 02 22:59:51 2013 +0000
+++ b/HTTPRequestHandler.cpp	Wed Jun 05 23:39:24 2013 +0000
@@ -1,6 +1,7 @@
 /* HTTPRequestHandler.cpp */
 #include "mbed.h"
 #include "HTTPRequestHandler.h"
+#include <ctype.h>
 
 #define _DEBUG 0
 
@@ -20,21 +21,72 @@
 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";
 
+
+static int _stricmp(const char* a, const char* b)
+{
+    int la = strlen(a);
+    int lb = strlen(b);
+    for (int i = 0 ; i < min(la, lb) ; i++) {
+        if (tolower((int)a[i]) != tolower((int)b[i]))
+            return i;
+    }
+    return 0;
+}
+
+
+static const struct mapping_t {
+    const char* key;
+    const char* value;
+} fileTypeMapping[]  = {
+        {".gif", "Content-Type: image/gif\r\n"   },
+    {".jpg", "Content-Type: image/jpeg\r\n"  },
+    {".jpeg","Content-Type: image/jpeg\r\n"  },
+    {".ico", "Content-Type: image/x-icon\r\n"},
+    {".png", "Content-Type: image/png\r\n"   },
+    {".zip", "Content-Type: image/zip\r\n"   },
+    {".gz",  "Content-Type: image/gz\r\n"    },
+    {".tar", "Content-Type: image/tar\r\n"   },
+    {".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"   }};
+    
 HTTPRequestHandler::HTTPRequestHandler(HTTPConnection::HTTPMessage& Msg, TCPSocketConnection& Tcp)
     : msg(Msg), tcp(Tcp)
 {
     msg = Msg;
     tcp = Tcp;
+
 }
 
 HTTPRequestHandler::~HTTPRequestHandler()
 {
 }
 
+void HTTPRequestHandler::getStandardHeaders(HTTPHeaders& header, const char* fext)
+{
+    header.clear();
+    header["DNT"] = "1";
+    header["MaxAge"] = "0";
+    header["Connection"] = "Keep-Alive";
+    header["Server"] = "mbed Embedded";
+    if (fext == NULL)
+        header["Content-Type"] = "text/html";
+    else {
+        for (int i = 0 ; i < sizeof(fileTypeMapping)/sizeof(struct mapping_t) ;i++) {
+            if (_stricmp(fileTypeMapping[i].key, fext) == 0) {
+                header["Content-Type"] = fileTypeMapping[i].value;
+                break;
+            }
+        }
+    }
+}
+
 void HTTPRequestHandler::handleRequest()
 {
     int err = 0;
@@ -107,6 +159,7 @@
         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));
     }