Single instance HTTP Server using WiFly Interface.

Dependents:   WiFlyHTTPServerSample MultiThreadingHTTPServer

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