fixed with new libs

Dependents:   SmartRemote WifiFlexManager SmartRemote E7

Fork of HTTPServer by Ollie Milton

Handler/FsHandler.cpp

Committer:
leihen
Date:
2013-06-01
Revision:
7:cb7fec1265b5
Parent:
5:dc88012caef1
Child:
9:c2a1462b9b71

File content as of revision 7:cb7fec1265b5:

/* 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  512
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);
            if (cnt < 0)
                cnt = 0;
            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;
}