Single instance HTTP Server using new Ethernet Interface. Blocking mode only; this improved stability, but the HTTP server must be started from a separate thread.

Dependents:   SmartLight

Fork of HTTPServer by Henry Leinen

HTTPServer.cpp

Committer:
leihen
Date:
2013-06-01
Revision:
5:dc88012caef1
Parent:
4:d065642c32cc
Child:
6:fe661fa9d18a

File content as of revision 5:dc88012caef1:

#include "mbed.h"
#include "HTTPServer.h"

#define _DEBUG      1

#ifdef _DEBUG
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
#endif

#if (_DEBUG && !defined(TARGET_LPC11U24))
#define INFO(x, ...) if (m_pDbg) m_pDbg->printf("[HttpServer : DBG]"x"\r\n", ##__VA_ARGS__); else printf("[HttpServer : DBG]"x"\r\n", ##__VA_ARGS__);
#define WARN(x, ...) if (m_pDbg) m_pDbg->printf("[HttpServer : WARN]"x"\r\n", ##__VA_ARGS__); else printf("[HttpServer : DBG]"x"\r\n", ##__VA_ARGS__);
#define ERR(x, ...) if (m_pDbg) m_pDbg->printf("[HttpServer : ERR]"x"\r\n", ##__VA_ARGS__); else printf("[HttpServer : DBG]"x"\r\n", ##__VA_ARGS__);
#else
#define INFO(x, ...)
#define WARN(x, ...)
#define ERR(x, ...)
#endif


/* Constructor */
/* initialize all members and set the standard error handler. */
HTTPServer::HTTPServer(Serial *dbg)
{
    m_pDbg = dbg;
    m_pSvr = NULL;
    m_pErrorHandler = StdErrorHandler;
}

HTTPServer::~HTTPServer()
{
    if (m_pSvr) {
        delete m_pSvr;
        m_pSvr = NULL;
    }
}


static const char* szStdErrorPage = "<HTML><HEAD><META content=\"text/html\" http-equiv=Content-Type></HEAD><BODY><h1>Error 404</h1><P>This resource is not available<P></BODY></HTML>\r\n\r\n";

void HTTPServer::StdErrorHandler(HTTPConnection::HTTPMessage& msg, TCPSocketConnection& tcp)
{
    char echoHeader[256];
    tcp.set_blocking(true, 1500);
    sprintf(echoHeader,"HTTP/1.1 404 Fail\r\nContent-Length: %d\r\nContent-Type: text/html\r\nServer: mbed embedded\r\n\n\r",strlen(szStdErrorPage));
    tcp.send(echoHeader, strlen(echoHeader));
    tcp.send((char*)szStdErrorPage, strlen(szStdErrorPage));
}


int HTTPServer::start(int port)
{
    // check if the start member was called already once
    if (m_pSvr != NULL) {
        ERR("start function was already called, server is already in listening state.");
        return -1;
    }
    
    //  Create a new server object
    m_pSvr = new TCPSocketServer();
    
    //  Bind the local server to the given port
    if (m_pSvr->bind(port) < 0) {
        ERR("Failed to bind to port %d\n", port);
        return -1;
    }
    else {
        INFO("Binding succeeded !\n");
    }

    //  Listen to a maximum of 10 concurrent connections
    if (m_pSvr->listen(1) < 0) {
        ERR("Faild to listen !\n");
        delete m_pSvr;
        m_pSvr = NULL;
        return -1;
    }
    else {
        INFO("Listening\n");
    }

    //  set into non blocking operation
    m_pSvr->set_blocking(false, 100);
    
    return 0;
}


int HTTPServer::poll()
{    
    int retval = -1;
    INFO("Listening for new connection requests.");

    //  This thread basically checks if there is a new incoming connection.
    //  If so , a new HTTPConnection is created and the connection thread is started.
    TCPSocketConnection Clnt;

#ifdef _DEBUG
    led4 = 1;   //  Indicate we are waiting for a new connection 
#endif
    m_pSvr->set_blocking(true); 
    retval = m_pSvr->accept(Clnt);
    if (retval > 0) {
        // no connection availale yet, so just return
#ifdef _DEBUG
led4 = 0;
led3 = 0;
led2 = 0;
#endif
        return retval;
    }
    if ( retval < 0) {
        //  an error occured 
        ERR("There was an error, Accept returned with an error. Probably the connection to the router was lost. Shutting down server");
#ifdef _DEBUG
        led2 = 0;
#endif 
        m_pSvr->close();
        delete m_pSvr;
        m_pSvr = NULL;
#ifdef _DEBUG
        led4 = 0;
        led3 = 1;   //  ERROR
        led2 = 0;
        led1 = 0;
#endif
        return -1;
    }
    else {
#ifdef _DEBUG
        led4 = 0;
#endif 
        //   a new connection was received
        INFO("Client (IP=%s) is connected !\n", Clnt.get_address());
        //  Start the main connection thread
#ifdef _DEBUG
        led3 = 1;
        led2 = 1;
#endif
        HTTPConnection con;
        int c = con.poll();
        if (c == 0) {
            //  Handle the request
            HandleRequest(con.m_Msg, Clnt);
        }
#ifdef _DEBUG
        led2 = 0;
        led3 = 0;
#endif
    }
    
    INFO("Leaving polling thread");
    return 0;
}

void HTTPServer::HandleRequest(HTTPConnection::HTTPMessage& msg, TCPSocketConnection& tcp)
{
    std::string localPath;
    std::map<std::string, HTTPRequestHandler*(*)(const char*, const char*, HTTPConnection::HTTPMessage&, TCPSocketConnection&), handlersComp>::const_iterator it;

    //  Iterate through registered handlers and check if the handler's path is a subset of the requested uri.
    for (it = m_lpHandlers.begin() ; it != m_lpHandlers.end() ; it++) {
        //  check if this entries' path is fully contained at the beginning of the requested path
        std::string curpth = it->first;

        if (msg.uri.find(curpth) == 0) {
            // firts matching handler found, we just take it and we'll be happy
            localPath = msg.uri.substr(curpth.length());
            break;
        }
    }
    
    if (it == m_lpHandlers.end()) {
        //  There is no such handler, so return invalid

        m_pErrorHandler(msg, tcp);        
        INFO("Webrequest left unhandled.");
    }
    else {
        //  Valid handler was found
        INFO("Routing webrequest !");
        //  Instantiate the handler object (handling will be done from withing the object's constructor
        HTTPRequestHandler *phdl = (*it->second)(it->first.c_str(), localPath.c_str(), msg, tcp);
        //  now we can delete the object, because handling is completed.
        if (phdl != NULL)
            delete phdl;
    }
}