Adaptation of the HttpServer by user yueee_yt. This version has improved handling of the HTTP headers (**NOTE**: There are limitations with this implementation and it is not fully functional. Use it only as a starting point.)

Dependents:   DMSupport DMSupport DMSupport DMSupport

Fork of DM_HttpServer by EmbeddedArtists AB

HTTPRequestHandler.cpp

Committer:
embeddedartists
Date:
2019-11-04
Revision:
11:9dcff8cf906a
Parent:
10:c1c8276af541

File content as of revision 11:9dcff8cf906a:

/*
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifdef _DEBUG_ALL
#define _DEBUG_REQUEST_HANDLER
#endif

#include "HTTPRequestHandler.h"
#include "DMBoard.h"

#include <string.h>

#define HTTP_REQUEST_TIMEOUT 5000

//HTTPRequestHandler::HTTPRequestHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : NetService(),
//   m_pTCPSocketConnection(pTCPSocketConnection), m_reqHeaders(), m_respHeaders(),
//   m_rootPath(rootPath), m_path(path), m_errc(200),
//   m_watchdog(), m_timeout(0),**/ m_closed(false), m_headersSent(false) //OK
HTTPRequestHandler::HTTPRequestHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocketConnection) :
    m_pTCPSocketConnection(pTCPSocketConnection), m_reqHeaders(), m_respHeaders(),
    m_rootPath(rootPath), m_path(path), m_errc(200), m_closed(false), m_headersSent(false)
{
#ifdef _DEBUG_REQUEST_HANDLER
    DMBoard::instance().logger()->printf("+++(HTTPRequestHandler) init \r\n");
#endif
    //Read & parse headers
    readHeaders();
//*  m_pTCPSocket->setOnEvent(this, &HTTPRequestHandler::onTCPSocketEvent);
//*  setTimeout(HTTP_REQUEST_TIMEOUT);
#ifdef _DEBUG_REQUEST_HANDLER
    DMBoard::instance().logger()->printf("+++(HTTPRequestHandler) init end \r\n");
#endif
}

HTTPRequestHandler::~HTTPRequestHandler()
{
    close();
#ifdef _DEBUG_REQUEST_HANDLER
    DMBoard::instance().logger()->printf("+++(HTTPRequestHandler) Destroy end\r\n");
#endif
}

void HTTPRequestHandler::onTimeout() //Connection has timed out
{
    close();
}

void HTTPRequestHandler::close() //Close socket and destroy data
{
    if(m_closed)
        return;
    m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else
    /**  m_watchdog.detach(); **/
//*  onClose();
//*  m_pTCPSocket->resetOnEvent();
//*  m_pTCPSocket->close();
//*  delete m_pTCPSocket; //Can safely destroy socket
//*  NetService::close();
}

map<string, string>& HTTPRequestHandler::reqHeaders() //const
{
    return m_reqHeaders;
}

string& HTTPRequestHandler::path() //const
{
    return m_path;
}

int HTTPRequestHandler::dataLen() const
{
    map<string,string>::const_iterator it;
    it = m_reqHeaders.find("Content-Length");
    if( it == m_reqHeaders.end() ) {
        return 0;
    }
    return atoi((*it).second.c_str()); //return 0 if parse fails, so that's fine
}

int HTTPRequestHandler::readData(char* buf, int len)
{
    return m_pTCPSocketConnection->recv(buf, len);
}

string& HTTPRequestHandler::rootPath() //const
{
    return m_rootPath;
}

void HTTPRequestHandler::setErrCode(int errc)
{
    m_errc = errc;
}

void HTTPRequestHandler::setContentLen(int len)
{
    char len_str[6] = {0};
    snprintf(len_str, 6, "%d", len);
    respHeaders()["Content-Length"] = len_str;
}

map<string, string>& HTTPRequestHandler::respHeaders()
{
    return m_respHeaders;
}

int HTTPRequestHandler::writeData(const char* buf, int len)
{
    if(!m_headersSent) {
        m_headersSent = true;
        writeHeaders();
    }

    return m_pTCPSocketConnection->send((char *)buf, len);
}
/**
void HTTPRequestHandler::setTimeout(int ms)
{
  m_timeout = 1000*ms;
  resetTimeout();
}
**/
/**
void HTTPRequestHandler::resetTimeout()
{
  m_watchdog.detach();
  m_watchdog.attach_us<HTTPRequestHandler>(this, &HTTPRequestHandler::onTimeout, m_timeout);
}
**/

void HTTPRequestHandler::readHeaders()
{
    static char line[128];
    static char key[128];
    static char value[128];
    
    // Prepare cache
    read_cache_t* cache = (read_cache_t*)malloc(sizeof(read_cache_t));
    cache->size = 1024;
    cache->num = cache->pos = 0;
    
    while( readLineCached(line, 128, cache) > 0) { //if == 0, it is an empty line = end of headers
        int n = sscanf(line, "%[^:]: %[^\n]", key, value);
        if ( n == 2 ) {
#ifdef _DEBUG_REQUEST_HANDLER
            DMBoard::instance().logger()->printf("\r\n+++(HTTPRequestHandler)Read header : %s : %s\r\n", key, value);
#endif
            m_reqHeaders[key] = value;
        }
        //TODO: Impl n==1 case (part 2 of previous header)
    }
    free(cache);
}

void HTTPRequestHandler::writeHeaders() //Called at the first writeData call
{
    static char line[128];

    //Response line
    snprintf(line, 128, "HTTP/1.1 %d MbedInfo\r\n", m_errc); //Not a violation of the standard not to include the descriptive text
    m_pTCPSocketConnection->send(line, strlen(line));

    map<string,string>::iterator it;
    while( !m_respHeaders.empty() ) {
        it = m_respHeaders.begin();
        snprintf(line, 128, "%s: %s\r\n", (*it).first.c_str(), (*it).second.c_str() );
#ifdef _DEBUG_REQUEST_HANDLER
        DMBoard::instance().logger()->printf("\r\n+++(HTTPRequestHandler)%s", line);
#endif
        m_pTCPSocketConnection->send(line, strlen(line));
        m_respHeaders.erase(it);
    }
    m_pTCPSocketConnection->send("\r\n",2); //End of head
}

int HTTPRequestHandler::readLine(char* str, int maxLen)
{
    int ret;
    int len = 0;
    for(int i = 0; i < maxLen - 1; i++) {
        ret = m_pTCPSocketConnection->recv(str, 1);
        if(!ret) {
            break;
        }
        if( (len > 1) && *(str-1)=='\r' && *str=='\n' ) {
            str--;
            len-=2;
            break;
        } else if( *str=='\n' ) {
            len--;
            break;
        }
        str++;
        len++;
    }
    *str = 0;
    return len;
}


int HTTPRequestHandler::readLineCached(char* str, int maxLen, read_cache_t* cache)
{
    maxLen--; // leave room for null termination
    
    int len = 0;
    int i = 0;
    bool found = false;
    while (i < maxLen && !found) {
        if (cache->num <= cache->pos) {
            // get something to process
            int ret = m_pTCPSocketConnection->recv(cache->buff, cache->size);
            if(ret == -1) {
                // error
                break;
            } else if (ret == 0) {
                // didn't get anything to process, try again
                continue;
            }
            cache->num = ret;
            cache->pos = 0;
        }
        
        // have >0 bytes to process
        while ((cache->pos < cache->num) && (i < maxLen)) {
            *str = cache->buff[cache->pos++];
            if( (len > 1) && *(str-1)=='\r' && *str=='\n' ) {
                str--;
                len-=2;
                found = true;
                break;
            } else if( *str=='\n' ) {
                len--;
                found = true;
                break;
            }
            str++;
            len++;
        }
    }
    *str = 0;
    return len;
}

/**
void HTTPRequestHandler::onTCPSocketEvent(TCPSocketEvent e)
{
  //printf("\r\nEvent %d in HTTPRequestHandler\r\n", e);
  printf("\r\n+++(HTTPRequestHandler)Event in HTTPRequestHandler\r\n");

  if(m_closed)
  {
    printf("\r\n+++(HTTPRequestHandler)WARN: Discarded\r\n");
    return;
  }

  switch(e)
  {
        case TCPSOCKET_READABLE:
    resetTimeout();
   onReadable();
    break;
  case TCPSOCKET_WRITEABLE:
    resetTimeout();
    onWriteable();
    break;
  case TCPSOCKET_CONTIMEOUT:
  case TCPSOCKET_CONRST:
  case TCPSOCKET_CONABRT:
  case TCPSOCKET_ERROR:
  case TCPSOCKET_DISCONNECTED:
    DBG("\r\nConnection error in handler\r\n");
    close();
    break;
  }
}
**/