A simple web server mainly based on ideas from Jasper Schuurmans Netduino web server

Dependents:   RdBlindsServer SpideyWallWeb RdGasUseMonitor

A fast and reliable web server for MBED! http://robdobson.com/2015/08/a-reliable-mbed-webserver/

It has a very neat way to implement REST commands and can serve files from local storage (on LPC1768 for instance) and from SD cards. It also has a caching facility which is particularly useful for serving files from local storage.

The server can be run in the main() thread (and has a sub-2ms response time if this is done) or in a mbed-rtos thread which increases the response time to (a still respectable) 30ms or so.

The latest project that uses this is here - https://developer.mbed.org/users/Bobty/code/SpideyWallWeb/

int main (void)
{
    // Ethernet interface
    EthernetInterface::init();

    // Connect ethernet
    EthernetInterface::connect();

    // Init the web server
    pc.printf("Starting web server\r\n");
    char* baseWebFolder = "/sd/";  // should be /sd/ for SDcard files - not used for local file system
    RdWebServer webServer;
    
    // Add commands to handle the home page and favicon
    webServer.addCommand("", RdWebServerCmdDef::CMD_LOCALFILE, NULL, "index.htm", true);
    webServer.addCommand("favicon.ico", RdWebServerCmdDef::CMD_LOCALFILE, NULL, NULL, true);
    
    // Add the lightwall control commands
    webServer.addCommand("name", RdWebServerCmdDef::CMD_CALLBACK, &lightwallGetSystemName);
    webServer.addCommand("clear", RdWebServerCmdDef::CMD_CALLBACK, &lightwallClear);
    webServer.addCommand("rawfill", RdWebServerCmdDef::CMD_CALLBACK, &lightwallRawFill);
    webServer.addCommand("fill", RdWebServerCmdDef::CMD_CALLBACK, &lightwallFill);
    webServer.addCommand("showleds", RdWebServerCmdDef::CMD_CALLBACK, &lightwallShowLeds);
    
    // Start the server
    webServer.init(WEBPORT, &led4, baseWebFolder);
    webServer.run();

}

// Get system name - No arguments required
char* lightwallGetSystemName(int method, char*cmdStr, char* argStr, char* msgBuffer, int msgLen, 
                int contentLen, unsigned char* pPayload, int payloadLen, int splitPayloadPos)
{
    // Perform any required actions here ....

    // ...

    // Return the system name
    return systemName;
}

This server was originally based on a Netduino web server from Jasper Schuurmans but has been optimised for speed.

RdWebServer.cpp

Committer:
Bobty
Date:
2015-05-05
Revision:
16:0248bbfdb6c1
Parent:
15:0865fa4b046a
Child:
17:080f2bed8b36

File content as of revision 16:0248bbfdb6c1:

/* RdWebServer.cpp
   Rob Dobson 2013-2015
   Inspired by Jasper Schuurmans multi-threaded web server for Netduino http://www.schuurmans.cc/multi-threaded-web-server-for-netduino-plus
   More details at http://robdobson.com/2013/10/moving-my-window-shades-control-to-mbed/
*/

#define RDWEB_DEBUG 5

#include "RdWebServer.h"

const int MAX_CMDSTR_LEN = 100;
const int MAX_ARGSTR_LEN = 100;
const int MAX_FILENAME_LEN = (MAX_ARGSTR_LEN + 20);
const int MAX_MIMETYPE_LEN = 50;

RdWebServer::RdWebServer()
{
    _initOk = false;
    _pStatusLed = NULL;
}

RdWebServer::~RdWebServer()
{
    // Clean-up - probably never called as we're on a microcontroller!
    for (std::vector<RdWebServerCmdDef*>::iterator it = _commands.begin() ; it != _commands.end(); ++it)
        delete (*it);
}

bool RdWebServer::init(int port, DigitalOut* pStatusLed, char* pBaseWebFolder)
{
    // Settings
    _port = port;
    _pStatusLed = pStatusLed;
    _pBaseWebFolder = pBaseWebFolder;

    // Setup tcp socket
    _serverSocket.set_blocking(true);
    if(_serverSocket.bind(port)< 0) 
    {
        RD_WARN("TCP server bind fail\n\r");
        return false;
    }
    if(_serverSocket.listen(1) < 0)
    {
        RD_WARN("TCP server listen fail\n\r");
        return false;
    }
    RD_INFO("TCP server is listening...\r\n");
    _initOk = true;
    return true;
}

void RdWebServer::run()
{
    // Check initialised ok
    if (!_initOk)
        return;
    
    // The sample web server on MBED site turns off blocking after the accept has happened
    // I've tried this but it doesn't seem to yield a reliable server
    bool blockingOnAccept = true;
    bool blockingOnReceive = true;
    
    // This is the same as the default in the socket.cpp file
    int timeoutOnBlocking = 1500;
    
    // Currently tested using close connection after send with a single file which works fine
    // If closeConnAfterSend is set false then the socket connection remains open and only
    // one client can access the server at a time
    // Need to test with the closeConnAfterSend seetting true when trying to download multiple files
    // for a website as previous experience would indicate that requests might be missed in this scenario
    // although all other settings may not have been the same
    bool closeConnAfterSend = true;
    bool closeConnOnReceiveFail = true;
    const char* closeConnStr = "Connection: Close\r\n";
    
    // Start accepting connections
    while (true)
    {
        TCPSocketConnection clientSocketConn;
        // Accept connection if available
        RD_INFO("Waiting for TCP connection\r\n");
        clientSocketConn.set_blocking(blockingOnAccept, timeoutOnBlocking);
        if(_serverSocket.accept(clientSocketConn)<0) 
        {
            RD_WARN("TCP Socket failed to accept connection\n\r");
            continue;
        }
        
        // Connection
        RD_INFO("Connection from IP: %s\n\r", clientSocketConn.get_address());
        if (_pStatusLed != NULL)
            *_pStatusLed = true;
        
        // While connected
        bool forcedClosed = false;
        while(clientSocketConn.is_connected() && !forcedClosed)
        {
            // Receive data
            clientSocketConn.set_blocking(blockingOnReceive, timeoutOnBlocking);
            int rxLen = clientSocketConn.receive(_buffer, HTTPD_MAX_REQ_LENGTH);
            if (rxLen == -1)
            {
                RD_DBG("clientSocketConn.receive() returned %d\r\n", rxLen);
                if (closeConnOnReceiveFail)
                {
                    int closeRet = clientSocketConn.close();
                    RD_DBG("Failed receive connection close() ret %d is connected %d\r\n", closeRet, clientSocketConn.is_connected());
                    forcedClosed = true;
                }
                continue;
            }
            if (rxLen == 0)
            {
                RD_DBG("clientSocketConn.receive() returned %d - ignoring -  is connected %d\r\n", rxLen, clientSocketConn.is_connected());
                continue;
            }
            if (rxLen > HTTPD_MAX_REQ_LENGTH)
            {
                RD_DBG("clientSocketConn.receive() returned %d - too long\r\n", rxLen);
                continue;
            }
        
            RD_DBG("Received len %d\r\n", rxLen);
            _buffer[rxLen] = '\0';
            RD_DBG("%s\r\n", _buffer);
            
            // Send a response
            char* content = "HELLO\r\n";
            int sz = strlen(content);
            sprintf(_httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %d\r\n%s\r\n", sz, closeConnAfterSend ? closeConnStr : "");
            int sentRet = clientSocketConn.send(_httpHeader,strlen(_httpHeader));
            int sentRet2 = clientSocketConn.send(content, strlen(content));
            
            RD_DBG("Sent %s header ret %d content ret %d now connected %d\r\n", content, sentRet, sentRet2, clientSocketConn.is_connected());
            
            if (closeConnAfterSend)
            {
                int closeRet = clientSocketConn.close();
                RD_DBG("After send connection close() ret %d is connected %d\r\n", closeRet, clientSocketConn.is_connected());
                forcedClosed = true;
            }
        }
    }
}
            
//            connectLimitTimer.reset();
//            connectLimitTimer.start();
//            while(true)
//            {
//                // Check connection timer - 10 seconds timeout on HTTP operation
//                if (connectLimitTimer.read() >= 10)
//                {
//                    RD_WARN("Connection timed out\n\r");
//                    break;
//                }
//                // Get received data
//                int rxLen = clientSocketConn.receive(_buffer, HTTPD_MAX_REQ_LENGTH);
//                if (rxLen == -1)
//                {
//                    RD_WARN("Nothing received\n\r");
//                    continue;
//                }
//                else if (rxLen == 0)
//                {
//                    RD_WARN("received buffer is empty.\n\r");
//                    break;                
//                }
//                else if (rxLen > HTTPD_MAX_REQ_LENGTH)
//                {
//                    sprintf(_httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
//                    clientSocketConn.send_all(_httpHeader,strlen(_httpHeader));
//                    clientSocketConn.send_all(_buffer, rxLen);
//                    break;
//                }
//                _buffer[rxLen] = '\0';
//    
//                // Handle buffer
//                if (handleReceivedHttp(clientSocketConn))
//                {
//                    break;
//                }
//                else
//                {
//                    connectLimitTimer.reset();
//                    connectLimitTimer.start();
//                }
//            }
//            
//            // Connection now closed
//            RD_INFO("Connection closed ...\r\n");
//            clientSocketConn.close();
//            if (_pStatusLed != NULL)
//                *_pStatusLed = false;
//        }
//        
//    }
//}
//
bool RdWebServer::handleReceivedHttp(TCPSocketConnection &clientSocketConn)
{
    bool closeConn = true;
    RD_DBG("Received Data: %d\n\r\n\r%.*s\n\r",strlen(_buffer),strlen(_buffer),_buffer);
    int method = METHOD_OTHER;
    if (strncmp(_buffer, "GET ", 4) == 0)
        method = METHOD_GET;
    if (strncmp(_buffer, "POST", 4) == 0)
        method = METHOD_POST;

    char cmdStr[MAX_CMDSTR_LEN];
    char argStr[MAX_ARGSTR_LEN];
    if (extractCmdArgs(_buffer+3, cmdStr, MAX_CMDSTR_LEN, argStr, MAX_ARGSTR_LEN))
    {
        RD_DBG("CmdStr %s\n\r", cmdStr);
        RD_DBG("ArgStr %s\n\r", argStr);
        bool cmdFound = false;
        for (std::vector<RdWebServerCmdDef*>::iterator it = _commands.begin() ; it != _commands.end(); ++it)
        {
            RD_DBG("Testing <<%s>> with <<%s>>\r\n", (*it)->_pCmdStr, cmdStr);
            if (strcasecmp((*it)->_pCmdStr, cmdStr) == 0)
            {                                    
                RD_DBG("FoundCmd <%s> Type %d\n\r", cmdStr, (*it)->_cmdType);
                cmdFound = true;
                if ((*it)->_cmdType == RdWebServerCmdDef::CMD_CALLBACK)
                {
                    char* respStr = ((*it)->_callback)(method, cmdStr, argStr);
                    clientSocketConn.send_all(respStr, strlen(respStr));
                }
                else if ((*it)->_cmdType == RdWebServerCmdDef::CMD_LOCALFILE)
                {
#ifdef SUPPORT_LOCAL_FILE_CACHE
                    if ((*it)->_substFileName[0] != '\0')
                    {
                        char combinedFileName[MAX_FILENAME_LEN];
                        strcpy(combinedFileName, (*it)->_substFileName);
                        if (combinedFileName[strlen(combinedFileName)-1] == '*')
                            strcpy(combinedFileName+strlen(combinedFileName)-1, argStr);
                        handleLocalFileRequest(combinedFileName, argStr, clientSocketConn, _httpHeader, (*it)->_bCacheIfPossible);
                    }
                    else
#endif
                    {
                        handleLocalFileRequest(cmdStr, argStr, clientSocketConn, _httpHeader, (*it)->_bCacheIfPossible);
                    }
                }
                else if ((*it)->_cmdType == RdWebServerCmdDef::CMD_SDORUSBFILE)
                {
#ifdef SUPPORT_LOCAL_FILE_CACHE
                    if ((*it)->_substFileName[0] != '\0')
                    {
                        char combinedFileName[MAX_FILENAME_LEN];
                        strcpy(combinedFileName, (*it)->_substFileName);
                        if (combinedFileName[strlen(combinedFileName)-1] == '*')
                            strcpy(combinedFileName+strlen(combinedFileName)-1, argStr);
                        closeConn = handleSDFileRequest(combinedFileName, argStr, clientSocketConn, _httpHeader);
                    }
                    else
#endif
                    {
                        closeConn = handleSDFileRequest(cmdStr, argStr, clientSocketConn, _httpHeader);
                    }
                }
                break;
            }
        }
        // If command not found see if it is a local file
        if (!cmdFound)
            closeConn = handleSDFileRequest(cmdStr, argStr, clientSocketConn, _httpHeader);
    }
    return closeConn;
}

void RdWebServer::addCommand(char* pCmdStr, int cmdType, CmdCallbackType callback, char* substFileName, bool cacheIfPossible)
{
    _commands.push_back(new RdWebServerCmdDef(pCmdStr, cmdType, callback, substFileName, cacheIfPossible));
}

bool RdWebServer::handleSDFileRequest(char* inFileName, char* argStr, TCPSocketConnection &clientSocketConn, char* httpHeader)
{
    bool closeConn = true;
    const int HTTPD_MAX_FNAME_LENGTH = 127;
    char filename[HTTPD_MAX_FNAME_LENGTH+1];
    
    RD_INFO("Requesting file %s\n\r", inFileName);
    
#ifdef SUPPORT_FOLDER_VIEW
    if ((strlen(inFileName) > 0) && (inFileName[strlen(inFileName)-1] == '/'))
    {
        RD_INFO("Request directory %s%s\r\n", _pBaseWebFolder, inFileName);
        sprintf(filename, "%s%s", _pBaseWebFolder, inFileName);
        DIR *d = opendir(filename);
        if (d != NULL) 
        {
            sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
            clientSocketConn.send_all(httpHeader,strlen(httpHeader));
            sprintf(httpHeader,"<html><head><title>Directory Listing</title></head><body><h1>%s</h1><ul>", inFileName);
            clientSocketConn.send_all(httpHeader,strlen(httpHeader));
            struct dirent *p;
            while((p = readdir(d)) != NULL) 
            {
                RD_INFO("%s\r\n", p->d_name);
                sprintf(httpHeader,"<li>%s</li>", p->d_name);
                clientSocketConn.send_all(httpHeader,strlen(httpHeader));
            }
        }
        closedir(d);
        RD_DBG("Directory closed\n");
        sprintf(httpHeader,"</ul></body></html>");
        clientSocketConn.send_all(httpHeader,strlen(httpHeader));
    }
    else
#endif
    {
        sprintf(filename, "%s%s", _pBaseWebFolder, inFileName);
        RD_INFO("Filename %s\r\n", filename);
            
        FILE* fp = fopen(filename, "r");
        if (fp == NULL)
        {
            RD_WARN("Filename %s not found\r\n", filename);
            sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nContent-Length: 0\r\n\r\n");
            clientSocketConn.send_all(httpHeader,strlen(httpHeader));
            closeConn = false;
        }
        else
        {
            RD_INFO("Sending file %s\r\n", filename);
            fseek(fp, 0L, SEEK_END);
            int sz = ftell(fp);
            fseek(fp, 0L, SEEK_SET);
            sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n", sz);
            clientSocketConn.send_all(httpHeader,strlen(httpHeader));
            // MIME type
            char *pDot = strrchr(filename, '.');
            char mimeType[MAX_MIMETYPE_LEN+1];
            if (pDot && (!strcmp(pDot, ".html") || !strcmp(pDot, ".htm")))
                strcpy(mimeType, "Content-Type: text/html\r\n");
            else if (pDot && !strcmp(pDot, ".css"))
                strcpy(mimeType, "Content-Type: text/css\r\n");
            if (pDot && !strcmp(pDot, ".js"))
                strcpy(mimeType, "Content-Type: application/javascript\r\n");
//            clientSocketConn.send_all(mimeType,strlen(mimeType));
            RD_INFO("MIME TYPE %s", mimeType);
            // Read file in blocks and send
            int rdCnt = 0;
            char fileBuf[1024];
            while ((rdCnt = fread(fileBuf, sizeof( char ), 1024, fp)) == 1024) 
            {
                clientSocketConn.send_all(fileBuf, rdCnt);
            }
            clientSocketConn.send_all(fileBuf, rdCnt);
            fclose(fp);
            closeConn = false;
        }
    }
    return closeConn;
}

#ifdef SUPPORT_LOCAL_FILE_CACHE

void RdWebServer::sendFromCache(RdFileCacheEntry* pCacheEntry, TCPSocketConnection &clientSocketConn, char* httpHeader)
{
    RD_INFO("Sending file %s from cache %d bytes\r\n", pCacheEntry->_fileName, pCacheEntry->_nFileLen);
    sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
    clientSocketConn.send_all(httpHeader,strlen(httpHeader));
    char* pMem = pCacheEntry->_pFileContent;
    int nLenLeft = pCacheEntry->_nFileLen;
    int blkSize = 2048;
    while(nLenLeft > 0)
    {
        if (blkSize > nLenLeft)
            blkSize = nLenLeft;
        clientSocketConn.send_all(pMem, blkSize);
        pMem += blkSize;
        nLenLeft -= blkSize;
    }
}
#endif

void RdWebServer::handleLocalFileRequest(char* inFileName, char* argStr, TCPSocketConnection &clientSocketConn, char* httpHeader, bool bCacheIfPossible)
{

#ifdef SUPPORT_LOCAL_FILESYSTEM

    const int HTTPD_MAX_FNAME_LENGTH = 127;
    char localFilename[HTTPD_MAX_FNAME_LENGTH+1];
    char reqFileNameStr[HTTPD_MAX_FNAME_LENGTH+1];

    RD_INFO("Requesting local file %s\n\r", inFileName);
    sprintf(reqFileNameStr, "/%s", inFileName);
    sprintf(localFilename, "/local/%s", inFileName);
        
    if (bCacheIfPossible)
    {
        // Check if file already cached
        bool bTryToCache = true;
        for (std::vector<RdFileCacheEntry*>::iterator it = _cachedFiles.begin() ; it != _cachedFiles.end(); ++it)
        {
            if (strcmp(inFileName, (*it)->_fileName) == 0)
            {
                if ((*it)->_bCacheValid)
                {
                    sendFromCache(*it, clientSocketConn, httpHeader);
                    return;
                }
                bTryToCache = false; // don't try to cache as cacheing must have already failed
            }
        }
        
        // See if we can cache the file
        if (bTryToCache)
        {
            RdFileCacheEntry* pCacheEntry = new RdFileCacheEntry(inFileName);
            if (pCacheEntry)
            {
                bool bCacheSuccess = pCacheEntry->readLocalFileIntoCache(localFilename);
                // Store the cache entry even if reading failed (mem alloc or file not found, etc) so we don't try to cache again
                _cachedFiles.push_back(pCacheEntry);
                if (bCacheSuccess)
                {
                    sendFromCache(pCacheEntry, clientSocketConn, httpHeader);
                    return;
                }
            }
        }        
    }
    
    LocalFileSystem local("local");
    
    FILE* fp = fopen(localFilename, "r");
    if (fp == NULL)
    {
        RD_WARN("Local file %s not found\r\n", localFilename);
        sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
        clientSocketConn.send_all(httpHeader,strlen(httpHeader));
        clientSocketConn.send_all(reqFileNameStr,strlen(reqFileNameStr));
    }
    else
    {
        RD_INFO("Sending file %s from disk\r\n", localFilename);
        sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
        clientSocketConn.send_all(httpHeader,strlen(httpHeader));
        int rdCnt = 0;
        char fileBuf[2000];
        while ((rdCnt = fread(fileBuf, sizeof( char ), 2000, fp)) == 2000) 
        {
            clientSocketConn.send_all(fileBuf, rdCnt);
        }
        clientSocketConn.send_all(fileBuf, rdCnt);
        fclose(fp);
    }
    
#else

    RD_WARN("Local file system not supported\r\n");
    sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
    clientSocketConn.send_all(httpHeader,strlen(httpHeader));
    clientSocketConn.send_all(inFileName,strlen(inFileName));

#endif
}

#ifdef SUPPORT_LOCAL_FILE_CACHE

bool RdFileCacheEntry::readLocalFileIntoCache(char* fileName)
{
#ifdef SUPPORT_LOCAL_FILESYSTEM
    
    RD_INFO("Reading into cache %s\n\r", fileName);
    LocalFileSystem local("local");
    FILE* fp = fopen(fileName, "r");
    if (fp == NULL)
    {
        RD_WARN("Failed to open file\n\r");
        return false;
    }
    RD_DBG("Seeking\n\r");
    fseek(fp, 0, SEEK_END);
    _nFileLen = (int)ftell(fp);
    _pFileContent = new char[_nFileLen];
    RD_DBG("Len %d Buf %08x\n\r", _nFileLen, _pFileContent);
    if (!_pFileContent)
    {
        RD_WARN("Failed to allocate %lu\n\r", _nFileLen);
        fclose(fp);
        return false;
    }
    RD_DBG("Allocated\n\r");
    memset(_pFileContent, 0, _nFileLen);
    fseek(fp, 0, SEEK_SET);
    char* pMem = _pFileContent;
    char fileBuf[100];
    int totCnt = 0;
    int rdCnt = 0;
    RD_DBG("Reading\n\r");
    while (true)
    {
        int toRead = _nFileLen - totCnt;
        if (toRead > sizeof(fileBuf))
            toRead = sizeof(fileBuf);
        if (toRead <= 0)
            break;
        rdCnt = fread(fileBuf, sizeof(char), toRead, fp);
        if (rdCnt <= 0)
            break;
        RD_DBG("Read %d tot %d of %d\n\r", rdCnt, totCnt, _nFileLen);
        memcpy(pMem, fileBuf, rdCnt);
        pMem += rdCnt;
        totCnt += rdCnt;
    }
    RD_DBG("Done read\n\r");
    fclose(fp);
    RD_DBG("Success in caching %d bytes (read %d)\n\r", _nFileLen, totCnt);
    _bCacheValid = true;
    return true;
#else
    return false;
#endif
}

#endif

bool RdWebServer::extractCmdArgs(char* buf, char* pCmdStr, int maxCmdStrLen, char* pArgStr, int maxArgStrLen)
{
    *pCmdStr = '\0';
    *pArgStr = '\0';
    int cmdStrLen = 0;
    int argStrLen = 0;
    if (buf == NULL)
        return false;
    // Check for first slash
    char* pSlash1 = strchr(buf, '/');
    if (pSlash1 == NULL)
        return false;
    pSlash1++;
    // Extract command
    while(*pSlash1)
    {
        if (cmdStrLen >= maxCmdStrLen-1)
            break;
        if ((*pSlash1 == '/') || (*pSlash1 == ' ') || (*pSlash1 == '\n') || (*pSlash1 == '?') || (*pSlash1 == '&'))
            break;
        *pCmdStr++ = *pSlash1++;
        *pCmdStr = '\0';
        cmdStrLen++;
    }
    if ((*pSlash1 == '\0') || (*pSlash1 == ' ') || (*pSlash1 == '\n'))
        return true;
    // Now args
    *pSlash1++;
    while(*pSlash1)
    {
        if (argStrLen >= maxArgStrLen-1)
            break;
        if ((*pSlash1 == ' ') || (*pSlash1 == '\n'))
            break;
        *pArgStr++ = *pSlash1++;
        *pArgStr = '\0';
        argStrLen++;
    }
    return true;
}