HTTPServer

Committer:
jphuc96
Date:
Sat Sep 16 02:39:55 2017 +0000
Revision:
0:caf5feddac47
v1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jphuc96 0:caf5feddac47 1 /* FsHandler.cpp */
jphuc96 0:caf5feddac47 2 #include "mbed.h"
jphuc96 0:caf5feddac47 3 #include "FsHandler.h"
jphuc96 0:caf5feddac47 4 #define DEBUG
jphuc96 0:caf5feddac47 5 #include "hl_debug.h"
jphuc96 0:caf5feddac47 6
jphuc96 0:caf5feddac47 7
jphuc96 0:caf5feddac47 8
jphuc96 0:caf5feddac47 9 static int matchstrings(const char* one, const char* two)
jphuc96 0:caf5feddac47 10 {
jphuc96 0:caf5feddac47 11 int m = 0;
jphuc96 0:caf5feddac47 12
jphuc96 0:caf5feddac47 13 for (m = 0; m < min(strlen(one), strlen(two)) ; m++) {
jphuc96 0:caf5feddac47 14 if (one[m] != two[m])
jphuc96 0:caf5feddac47 15 return m;
jphuc96 0:caf5feddac47 16 }
jphuc96 0:caf5feddac47 17 return m;
jphuc96 0:caf5feddac47 18 }
jphuc96 0:caf5feddac47 19
jphuc96 0:caf5feddac47 20 std::map<const char*, const char*> HTTPFsRequestHandler::m_fsMap;
jphuc96 0:caf5feddac47 21
jphuc96 0:caf5feddac47 22 HTTPFsRequestHandler::HTTPFsRequestHandler(const char* rootPath, const char* localPath, HTTPConnection::HTTPMessage& Msg, TCPSocketConnection& Tcp)
jphuc96 0:caf5feddac47 23 : HTTPRequestHandler(Msg, Tcp)
jphuc96 0:caf5feddac47 24 {
jphuc96 0:caf5feddac47 25 m_rootPath = rootPath;
jphuc96 0:caf5feddac47 26 m_localPath = localPath;
jphuc96 0:caf5feddac47 27
jphuc96 0:caf5feddac47 28 string myPath = m_rootPath + m_localPath;
jphuc96 0:caf5feddac47 29
jphuc96 0:caf5feddac47 30 // Now replace the virtual root path with a mounted device path
jphuc96 0:caf5feddac47 31 std::map<const char*, const char*>::iterator it;
jphuc96 0:caf5feddac47 32 const char *bestMatch = NULL;
jphuc96 0:caf5feddac47 33 const char *bestMatchExchange = NULL;
jphuc96 0:caf5feddac47 34 int match_ind = -1;
jphuc96 0:caf5feddac47 35 for (it = m_fsMap.begin() ; it != m_fsMap.end() ; it++) {
jphuc96 0:caf5feddac47 36 // find best match (if the given logical path is containted in the root
jphuc96 0:caf5feddac47 37 int s = matchstrings(myPath.c_str(), it->second);
jphuc96 0:caf5feddac47 38 INFO("Matching Root %s with handler %s results in %d identical characters\n", myPath.c_str(), it->second, s);
jphuc96 0:caf5feddac47 39 if ((s == strlen(it->second)) && (s > match_ind)) {
jphuc96 0:caf5feddac47 40 match_ind = s;
jphuc96 0:caf5feddac47 41 bestMatch = it->first;
jphuc96 0:caf5feddac47 42 bestMatchExchange = it->second;
jphuc96 0:caf5feddac47 43 }
jphuc96 0:caf5feddac47 44 }
jphuc96 0:caf5feddac47 45
jphuc96 0:caf5feddac47 46 if (bestMatch != NULL) {
jphuc96 0:caf5feddac47 47 m_rootPath = bestMatch;
jphuc96 0:caf5feddac47 48 m_localPath = string(myPath).substr(strlen(bestMatchExchange));
jphuc96 0:caf5feddac47 49 }
jphuc96 0:caf5feddac47 50
jphuc96 0:caf5feddac47 51 handleRequest();
jphuc96 0:caf5feddac47 52 }
jphuc96 0:caf5feddac47 53
jphuc96 0:caf5feddac47 54 HTTPFsRequestHandler::~HTTPFsRequestHandler()
jphuc96 0:caf5feddac47 55 {
jphuc96 0:caf5feddac47 56 }
jphuc96 0:caf5feddac47 57
jphuc96 0:caf5feddac47 58 int HTTPFsRequestHandler::handleGetRequest()
jphuc96 0:caf5feddac47 59 {
jphuc96 0:caf5feddac47 60 HTTPHeaders headers;
jphuc96 0:caf5feddac47 61
jphuc96 0:caf5feddac47 62 if (m_localPath.length() > 4)
jphuc96 0:caf5feddac47 63 getStandardHeaders(headers, m_localPath.substr(m_localPath.length()-4).c_str());
jphuc96 0:caf5feddac47 64 else
jphuc96 0:caf5feddac47 65 getStandardHeaders(headers);
jphuc96 0:caf5feddac47 66
jphuc96 0:caf5feddac47 67 INFO("Handling Get Request (root = %s, local = %s).", m_rootPath.c_str(), m_localPath.c_str());
jphuc96 0:caf5feddac47 68 int retval = 0; //success
jphuc96 0:caf5feddac47 69 std::string reqPath;
jphuc96 0:caf5feddac47 70
jphuc96 0:caf5feddac47 71 // Check if we received a directory with the local bath
jphuc96 0:caf5feddac47 72 if ((m_localPath.length() == 0) || (m_localPath.substr( m_localPath.length()-1, 1) == "/")) {
jphuc96 0:caf5feddac47 73 // yes, we shall append the default page name
jphuc96 0:caf5feddac47 74 m_localPath += "index.html";
jphuc96 0:caf5feddac47 75 }
jphuc96 0:caf5feddac47 76
jphuc96 0:caf5feddac47 77 reqPath = m_rootPath + m_localPath;
jphuc96 0:caf5feddac47 78
jphuc96 0:caf5feddac47 79 INFO("Mapping \"%s\" to \"%s\"", msg.uri.c_str(), reqPath.c_str());
jphuc96 0:caf5feddac47 80
jphuc96 0:caf5feddac47 81 FILE *fp = fopen(reqPath.c_str(), "r");
jphuc96 0:caf5feddac47 82 if (fp != NULL) {
jphuc96 0:caf5feddac47 83 char * pBuffer = NULL;
jphuc96 0:caf5feddac47 84 int sz = 8192;
jphuc96 0:caf5feddac47 85 while( pBuffer == NULL) {
jphuc96 0:caf5feddac47 86 sz /= 2;
jphuc96 0:caf5feddac47 87 pBuffer = (char*)malloc(sz);
jphuc96 0:caf5feddac47 88 if (sz < 128)
jphuc96 0:caf5feddac47 89 error ("OutOfMemory");
jphuc96 0:caf5feddac47 90 }
jphuc96 0:caf5feddac47 91
jphuc96 0:caf5feddac47 92 // File was found and can be returned
jphuc96 0:caf5feddac47 93
jphuc96 0:caf5feddac47 94 // first determine the size
jphuc96 0:caf5feddac47 95 fseek(fp, 0, SEEK_END);
jphuc96 0:caf5feddac47 96 long size = ftell(fp);
jphuc96 0:caf5feddac47 97 fseek(fp, 0, SEEK_SET);
jphuc96 0:caf5feddac47 98
jphuc96 0:caf5feddac47 99 startResponse(200, size);
jphuc96 0:caf5feddac47 100 while(!feof(fp) && !ferror(fp)) {
jphuc96 0:caf5feddac47 101 int cnt = fread(pBuffer, 1, sz , fp);
jphuc96 0:caf5feddac47 102 if (cnt < 0)
jphuc96 0:caf5feddac47 103 cnt = 0;
jphuc96 0:caf5feddac47 104 processResponse(cnt, pBuffer);
jphuc96 0:caf5feddac47 105 }
jphuc96 0:caf5feddac47 106 delete pBuffer;
jphuc96 0:caf5feddac47 107 endResponse();
jphuc96 0:caf5feddac47 108 fclose(fp);
jphuc96 0:caf5feddac47 109 }
jphuc96 0:caf5feddac47 110 else {
jphuc96 0:caf5feddac47 111 retval = 404;
jphuc96 0:caf5feddac47 112 ERR("Requested file was not found !");
jphuc96 0:caf5feddac47 113 }
jphuc96 0:caf5feddac47 114
jphuc96 0:caf5feddac47 115 return retval;
jphuc96 0:caf5feddac47 116 }
jphuc96 0:caf5feddac47 117
jphuc96 0:caf5feddac47 118 int HTTPFsRequestHandler::handlePostRequest()
jphuc96 0:caf5feddac47 119 {
jphuc96 0:caf5feddac47 120 return 404;
jphuc96 0:caf5feddac47 121 }
jphuc96 0:caf5feddac47 122
jphuc96 0:caf5feddac47 123 int HTTPFsRequestHandler::handlePutRequest()
jphuc96 0:caf5feddac47 124 {
jphuc96 0:caf5feddac47 125 return 404;
jphuc96 0:caf5feddac47 126 }