Team E1

Fork of HTTPServer by Sam Kirsch

Committer:
VervoortRaf
Date:
Mon Feb 01 10:25:06 2016 +0000
Revision:
19:e80980c149eb
Parent:
16:cc3f5c53d0d5
Smart remote school project Team E1

Who changed what in which revision?

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