Mbed asked me to commit the library. Probably because it's an older version

Dependents:   ece4180_project

Fork of HTTPServer by Sarah Marsh

Revision:
12:ba81cc117fb6
Parent:
9:c2a1462b9b71
Child:
13:aa5338a5e452
--- a/Handler/FsHandler.cpp	Sun Jun 02 22:59:51 2013 +0000
+++ b/Handler/FsHandler.cpp	Wed Jun 05 23:39:24 2013 +0000
@@ -16,9 +16,17 @@
 #endif
 
 
-#define MAX_BUFFERSIZE  512
-static char buffer[MAX_BUFFERSIZE];
 
+static int matchstrings(const char* one, const char* two)
+{
+    int m = 0;
+    
+    for (m = 0; m < min(strlen(one), strlen(two)) ; m++) {
+        if (one[m] != two[m])
+            return m;
+    }
+    return m;
+}
 
 std::map<const char*, const char*> HTTPFsRequestHandler::m_fsMap;
  
@@ -28,16 +36,29 @@
     m_rootPath = rootPath;
     m_localPath = localPath;
     
+    string myPath = m_rootPath + m_localPath;
+    
     //  Now replace the virtual root path with a mounted device path
     std::map<const char*, const char*>::iterator it;
+    const char *bestMatch = NULL;
+    const char *bestMatchExchange = NULL;
+    int match_ind = -1;
     for (it = m_fsMap.begin() ; it != m_fsMap.end() ; it++) {
         //  find best match (if the given logical path is containted in the root
-        if (m_rootPath.find( it->second ) == 0) {
-            m_rootPath = it->first;
-            break;
+        int s = matchstrings(myPath.c_str(), it->second);
+        INFO("Matching Root %s with handler %s results in %d identical characters\n", myPath.c_str(), it->second, s);
+        if ((s == strlen(it->second)) && (s > match_ind)) {
+            match_ind = s;
+            bestMatch = it->first;
+            bestMatchExchange = it->second;
         }
     }
-    
+
+    if (bestMatch != NULL) {
+        m_rootPath = bestMatch;
+        m_localPath = string(myPath).substr(strlen(bestMatchExchange));
+    }
+            
     handleRequest();
 }
 
@@ -47,7 +68,14 @@
 
 int HTTPFsRequestHandler::handleGetRequest()
 {
-    INFO("Handling Get Request.");
+    HTTPHeaders headers;
+    
+    if (m_localPath.length() > 4) 
+        getStandardHeaders(headers, m_localPath.substr(m_localPath.length()-4).c_str());
+    else
+        getStandardHeaders(headers);
+    
+    INFO("Handling Get Request (root = %s, local = %s).", m_rootPath.c_str(), m_localPath.c_str());
     int retval = 0;   //success
     std::string reqPath;
 
@@ -63,6 +91,15 @@
         
     FILE *fp = fopen(reqPath.c_str(), "r");
     if (fp != NULL) {
+        char * pBuffer = NULL;
+        int sz = 8192;
+        while( pBuffer == NULL) {
+            sz /= 2;
+            pBuffer = (char*)malloc(sz);
+            if (sz < 128)
+                error ("OutOfMemory");
+        }
+        
         //  File was found and can be returned
     
         //  first determine the size
@@ -72,11 +109,12 @@
     
         startResponse(200, size);
         while(!feof(fp) && !ferror(fp)) {
-            int cnt = fread(buffer, 1, MAX_BUFFERSIZE , fp);
+            int cnt = fread(pBuffer, 1, sz , fp);
             if (cnt < 0)
                 cnt = 0;
-            processResponse(cnt, buffer);
+            processResponse(cnt, pBuffer);
         }
+        delete pBuffer;
         endResponse();
         fclose(fp);
     }