Single instance HTTP Server using new Ethernet Interface.

Dependents:   EthHTTPServer if201410_section5 _PE2E_12-04_EthernetInterfaceServer MGAS_GR_Peach ... more

Fork of WiFlyHTTPServer by Henry Leinen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FsHandler.cpp Source File

FsHandler.cpp

00001 /* FsHandler.cpp */
00002 #include "mbed.h"
00003 #include "FsHandler.h"
00004 #define DEBUG
00005 #include "hl_debug.h"
00006 
00007 
00008 
00009 static int matchstrings(const char* one, const char* two)
00010 {
00011     int m = 0;
00012     
00013     for (m = 0; m < min(strlen(one), strlen(two)) ; m++) {
00014         if (one[m] != two[m])
00015             return m;
00016     }
00017     return m;
00018 }
00019 
00020 std::map<const char*, const char*> HTTPFsRequestHandler::m_fsMap;
00021  
00022 HTTPFsRequestHandler::HTTPFsRequestHandler(const char* rootPath, const char* localPath, HTTPConnection::HTTPMessage& Msg, TCPSocketConnection& Tcp)
00023     : HTTPRequestHandler(Msg, Tcp)
00024 {
00025     m_rootPath = rootPath;
00026     m_localPath = localPath;
00027     
00028     string myPath = m_rootPath + m_localPath;
00029     
00030     //  Now replace the virtual root path with a mounted device path
00031     std::map<const char*, const char*>::iterator it;
00032     const char *bestMatch = NULL;
00033     const char *bestMatchExchange = NULL;
00034     int match_ind = -1;
00035     for (it = m_fsMap.begin() ; it != m_fsMap.end() ; it++) {
00036         //  find best match (if the given logical path is containted in the root
00037         int s = matchstrings(myPath.c_str(), it->second);
00038         INFO("Matching Root %s with handler %s results in %d identical characters\n", myPath.c_str(), it->second, s);
00039         if ((s == strlen(it->second)) && (s > match_ind)) {
00040             match_ind = s;
00041             bestMatch = it->first;
00042             bestMatchExchange = it->second;
00043         }
00044     }
00045 
00046     if (bestMatch != NULL) {
00047         m_rootPath = bestMatch;
00048         m_localPath = string(myPath).substr(strlen(bestMatchExchange));
00049     }
00050             
00051     handleRequest();
00052 }
00053 
00054 HTTPFsRequestHandler::~HTTPFsRequestHandler()
00055 {
00056 }
00057 
00058 int HTTPFsRequestHandler::handleGetRequest()
00059 {
00060     HTTPHeaders headers;
00061     
00062     if (m_localPath.length() > 4) 
00063         getStandardHeaders(headers, m_localPath.substr(m_localPath.length()-4).c_str());
00064     else
00065         getStandardHeaders(headers);
00066     
00067     INFO("Handling Get Request (root = %s, local = %s).", m_rootPath.c_str(), m_localPath.c_str());
00068     int retval = 0;   //success
00069     std::string reqPath;
00070 
00071     //  Check if we received a directory with the local bath
00072     if ((m_localPath.length() == 0) || (m_localPath.substr( m_localPath.length()-1, 1) == "/")) {
00073         //  yes, we shall append the default page name
00074         m_localPath += "index.html";
00075     }
00076     
00077     reqPath = m_rootPath + m_localPath;
00078     
00079     INFO("Mapping \"%s\" to \"%s\"", msg.uri.c_str(), reqPath.c_str());
00080         
00081     FILE *fp = fopen(reqPath.c_str(), "r");
00082     if (fp != NULL) {
00083         char * pBuffer = NULL;
00084         int sz = 8192;
00085         while( pBuffer == NULL) {
00086             sz /= 2;
00087             pBuffer = (char*)malloc(sz);
00088             if (sz < 128)
00089                 error ("OutOfMemory");
00090         }
00091         
00092         //  File was found and can be returned
00093     
00094         //  first determine the size
00095         fseek(fp, 0, SEEK_END);
00096         long size = ftell(fp);
00097         fseek(fp, 0, SEEK_SET);
00098     
00099         startResponse(200, size);
00100         while(!feof(fp) && !ferror(fp)) {
00101             int cnt = fread(pBuffer, 1, sz , fp);
00102             if (cnt < 0)
00103                 cnt = 0;
00104             processResponse(cnt, pBuffer);
00105         }
00106         delete pBuffer;
00107         endResponse();
00108         fclose(fp);
00109     }
00110     else {
00111         retval = 404;
00112         ERR("Requested file was not found !");
00113     }
00114     
00115     return retval;
00116 }
00117 
00118 int HTTPFsRequestHandler::handlePostRequest()
00119 {
00120     return 404;
00121 }
00122 
00123 int HTTPFsRequestHandler::handlePutRequest()
00124 {
00125     return 404;
00126 }