Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of HTTPServer by
HTTPFsRequestHandler.cpp
- Committer:
- leihen
- Date:
- 2013-05-28
- Revision:
- 3:d6224049b3bf
File content as of revision 3:d6224049b3bf:
/* HTTPFsRequestHandler.cpp */
#include "mbed.h"
#include "HTTPFsRequestHandler.h"
#if (1 && !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 = 200; //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;
}
