Mbed asked me to commit the library. Probably because it's an older version
Fork of HTTPServer by
Diff: Handler/FsHandler.cpp
- Revision:
- 5:dc88012caef1
- Child:
- 7:cb7fec1265b5
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Handler/FsHandler.cpp Sat Jun 01 06:24:43 2013 +0000 @@ -0,0 +1,97 @@ +/* FsHandler.cpp */ +#include "mbed.h" +#include "FsHandler.h" + + +#define _DEBUG 1 + +#if (_DEBUG && !defined(TARGET_LPC11U24)) +#define INFO(x, ...) std::printf("[HTTPFsRequestHandler : DBG]"x"\r\n", ##__VA_ARGS__); +#define WARN(x, ...) std::printf("[HTTPFsRequestHandler : DBG]"x"\r\n", ##__VA_ARGS__); +#define ERR(x, ...) std::printf("[HTTPFsRequestHandler : DBG]"x"\r\n", ##__VA_ARGS__); +#else +#define INFO(x, ...) +#define WARN(x, ...) +#define ERR(x, ...) +#endif + + +#define MAX_BUFFERSIZE 128 +static char buffer[MAX_BUFFERSIZE]; + + +std::map<const char*, const char*> HTTPFsRequestHandler::m_fsMap; + +HTTPFsRequestHandler::HTTPFsRequestHandler(const char* rootPath, const char* localPath, HTTPConnection::HTTPMessage& Msg, TCPSocketConnection& Tcp) + : HTTPRequestHandler(Msg, Tcp) +{ + m_rootPath = rootPath; + m_localPath = localPath; + + // Now replace the virtual root path with a mounted device path + std::map<const char*, const char*>::iterator it; + 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; + } + } + + handleRequest(); +} + +HTTPFsRequestHandler::~HTTPFsRequestHandler() +{ +} + +int HTTPFsRequestHandler::handleGetRequest() +{ + INFO("Handling Get Request."); + int retval = 0; //success + std::string reqPath; + + // Check if we received a directory with the local bath + if ((m_localPath.length() == 0) || (m_localPath.substr( m_localPath.length()-1, 1) == "/")) { + // yes, we shall append the default page name + m_localPath += "index.html"; + } + + reqPath = m_rootPath + m_localPath; + + INFO("Mapping \"%s\" to \"%s\"", msg.uri.c_str(), reqPath.c_str()); + + FILE *fp = fopen(reqPath.c_str(), "r"); + if (fp != NULL) { + // File was found and can be returned + + // first determine the size + fseek(fp, 0, SEEK_END); + long size = ftell(fp); + fseek(fp, 0, SEEK_SET); + + startResponse(200, size); + while(!feof(fp) && !ferror(fp)) { + int cnt = fread(buffer, 1, MAX_BUFFERSIZE , fp); + processResponse(cnt, buffer); + } + endResponse(); + fclose(fp); + } + else { + retval = 404; + ERR("Requested file was not found !"); + } + + return retval; +} + +int HTTPFsRequestHandler::handlePostRequest() +{ + return 404; +} + +int HTTPFsRequestHandler::handlePutRequest() +{ + return 404; +} \ No newline at end of file