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:
2013-10-04
Revision:
2:9d8793c23b46
Parent:
1:75bb184de749
Child:
3:594136d34a32

File content as of revision 2:9d8793c23b46:

#include "RdWebServer.h"

#define MAX_CMDSTR_LEN 100
#define MAX_ARGSTR_LEN 100

bool RdFileCacheEntry::readLocalFileIntoCache(char* fileName)
{
    printf("Reading into cache %s\n\r", fileName);
    LocalFileSystem local("local");
    FILE* fp = fopen(fileName, "r");
    if (fp == NULL)
    {
        printf("Failed to open file\n\r");
        return false;
    }
    fseek(fp, 0, SEEK_END);
    _nFileLen = (int)ftell(fp);
    _pFileContent = new char[_nFileLen];
    if (!_pFileContent)
    {
        printf("Failed to allocate %lu\n\r", _nFileLen);
        fclose(fp);
        return false;
    }
    fseek(fp, 0, SEEK_SET);
    char* pMem = _pFileContent;
    char fileBuf[500];
    int totCnt = 0;
    int rdCnt;
    while ((rdCnt = fread(fileBuf, sizeof( char ), 500, fp)) == 500) 
    {
        memcpy(pMem, fileBuf, rdCnt);
        pMem += rdCnt;
        totCnt += rdCnt;
    }
    memcpy(pMem, fileBuf, rdCnt);
    totCnt += rdCnt;
    fclose(fp);
    printf("Success in caching %d bytes (read %d)\n\r", _nFileLen, totCnt);
    _bCacheValid = true;
    return true;
}

RdWebServer::RdWebServer()
{
    _serverIsListening = 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)
{
    _port = port;
    _pStatusLed = pStatusLed;
        
//    _socketSrv.set_blocking(true);


    //setup tcp socket
    if(_socketSrv.bind(port)< 0) 
    {
        printf("TCP server bind fail\n\r");
        return false;
    }
    else 
    {
//        printf("TCP server bind success\n\r");
        _serverIsListening = true;
    }

    if(_socketSrv.listen(1) < 0)
    {
        printf("TCP server listen fail\n\r");
        return false;
    }
    else 
    {
        printf("TCP server is listening...\r\n");
    }
    
    return true;
}

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;
    char* pSlash1 = strchr(buf, '/');
    if (pSlash1 == NULL)
        return false;
    pSlash1++;
    while(*pSlash1)
    {
        if (cmdStrLen >= maxCmdStrLen-1)
            break;
        if ((*pSlash1 == '/') || (*pSlash1 == ' ') || (*pSlash1 == '\n'))
            break;
        *pCmdStr++ = *pSlash1++;
        *pCmdStr = '\0';
        cmdStrLen++;
    }
    if ((*pSlash1 == '\0') || (*pSlash1 == ' ') || (*pSlash1 == '\n'))
        return true;
    *pSlash1++;
    while(*pSlash1)
    {
        if (argStrLen >= maxArgStrLen-1)
            break;
        if ((*pSlash1 == ' ') || (*pSlash1 == '\n'))
            break;
        *pArgStr++ = *pSlash1++;
        *pArgStr = '\0';
        argStrLen++;
    }
    return true;
}

void RdWebServer::sendFromCache(RdFileCacheEntry* pCacheEntry, TCPSocketConnection* pClient, char* httpHeader)
{
    printf ("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");
    pClient->send(httpHeader,strlen(httpHeader));
    char* pMem = pCacheEntry->_pFileContent;
    int nLenLeft = pCacheEntry->_nFileLen;
    int blkSize = 2048;
    while(nLenLeft > 0)
    {
        if (blkSize > nLenLeft)
            blkSize = nLenLeft;
        pClient->send_all(pMem, blkSize);
        pMem += blkSize;
        nLenLeft -= blkSize;
    }
}

void RdWebServer::handleLocalFileRequest(char* inFileName, char* argStr, TCPSocketConnection* pClient, char* httpHeader, bool bCacheIfPossible)
{
    const int HTTPD_MAX_FNAME_LENGTH = 127;
    char localFilename[HTTPD_MAX_FNAME_LENGTH+1];
    char reqFileNameStr[HTTPD_MAX_FNAME_LENGTH+1];

    printf("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, pClient, 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, pClient, httpHeader);
                    return;
                }
            }
        }        
    }
    
    LocalFileSystem local("local");
    
    FILE* fp = fopen(localFilename, "r");
    if (fp == NULL)
    {
        printf ("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");
        pClient->send(httpHeader,strlen(httpHeader));
        pClient->send(reqFileNameStr,strlen(reqFileNameStr));
    }
    else
    {
        printf ("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");
        pClient->send(httpHeader,strlen(httpHeader));
        int rdCnt = 0;
        char fileBuf[2000];
        while ((rdCnt = fread(fileBuf, sizeof( char ), 2000, fp)) == 2000) 
        {
            pClient->send_all(fileBuf, rdCnt);
        }
        pClient->send_all(fileBuf, rdCnt);
        fclose(fp);
    }
}

void RdWebServer::handleSDFileRequest(char* inFileName, char* argStr, TCPSocketConnection* pClient, char* httpHeader)
{
    const int HTTPD_MAX_FNAME_LENGTH = 127;
    char filename[HTTPD_MAX_FNAME_LENGTH+1];
    
    printf("Requesting file %s\n\r", inFileName);
    
    char *lstchr = strrchr(inFileName, NULL) -1;
    if ('/' == *lstchr)
    {
        // printf("Open directory /sd/%s\n", inFileName);
        *lstchr = 0;
        sprintf(filename, "/sd/%s", 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");
            pClient->send(httpHeader,strlen(httpHeader));
            sprintf(httpHeader,"<html><head><title>Directory Listing</title></head><body><h1>%s</h1><ul>", inFileName);
            pClient->send(httpHeader,strlen(httpHeader));
            struct dirent *p;
            while((p = readdir(d)) != NULL) {
                // printf("%s\n", p->d_name);
                sprintf(httpHeader,"<li>%s</li>", p->d_name);
                pClient->send(httpHeader,strlen(httpHeader));
            }
        }
        closedir(d);
        // printf("Directory closed\n");
        sprintf(httpHeader,"</ul></body></html>");
        pClient->send(httpHeader,strlen(httpHeader));
    }
    else
    {
        sprintf(filename, "/sd/%s", inFileName);
        // printf ("Filename %s\r\n", filename);
        FILE* fp = fopen(filename, "r");
        if (fp == NULL)
        {
            printf ("Filename %s not found\r\n", filename);
            sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
            pClient->send(httpHeader,strlen(httpHeader));
            pClient->send(inFileName,strlen(inFileName));
        }
        else
        {
            printf ("Sending file %s\r\n", filename);
            sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
            pClient->send(httpHeader,strlen(httpHeader));
            int rdCnt = 0;
            char fileBuf[1024];
            while ((rdCnt = fread(fileBuf, sizeof( char ), 1024, fp)) == 1024) 
            {
                pClient->send(fileBuf, rdCnt);
            }
            pClient->send(fileBuf, rdCnt);
            fclose(fp);
        }
    }
}

void RdWebServer::run()
{
    TCPSocketConnection client;
    const int HTTPD_MAX_HDR_LENGTH = 255;
    char httpHeader[HTTPD_MAX_HDR_LENGTH+1];
    const int HTTPD_MAX_REQ_LENGTH = 1023;
    char buffer[HTTPD_MAX_REQ_LENGTH+1];
    
    //listening for http GET request
    while (isListening())
    {
        //blocking mode(never timeout)
        if(_socketSrv.accept(client)<0) 
        {
            printf("TCP Socket failed to accept connection\n\r");
        }
        else
        {
            client.set_blocking(false, 1000);
            printf("Connection from IP: %s\n\r",client.get_address());
            if (_pStatusLed != NULL)
                *_pStatusLed = true;
            Timer connectLimitTimer;
            connectLimitTimer.start();
            
            while(connectLimitTimer.read() < 5)  // 5 seconds timeout on HTTP operation
            {
                int rxLen = client.receive(buffer, 1023);
                if (rxLen == -1)
                {
                    continue;
                }
                else if (rxLen == 0)
                {
                    printf("received buffer is empty.\n\r");
                    break;                
                }
                else if (rxLen >= 1024)
                {
                    sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
                    client.send(httpHeader,strlen(httpHeader));
                    client.send(buffer, rxLen);
                    break;
                }
                buffer[rxLen] = '\0';
//                printf("Received Data: %d\n\r\n\r%.*s\n\r",strlen(buffer),strlen(buffer),buffer);
                if (strncmp(buffer, "GET ", 4) == 0)
                {
//                    printf("GET request incomming.\n\r");
                    char cmdStr[MAX_CMDSTR_LEN];
                    char argStr[MAX_ARGSTR_LEN];
                    if (extractCmdArgs(buffer+3, cmdStr, MAX_CMDSTR_LEN, argStr, MAX_ARGSTR_LEN))
                    {
                        printf("CmdStr %s\n\r", cmdStr);
                        printf("ArgStr %s\n\r", argStr);
                        bool cmdFound = false;
                        for (std::vector<RdWebServerCmdDef*>::iterator it = _commands.begin() ; it != _commands.end(); ++it)
                        {
//                            printf("Testing <<%s>> with <<%s>>\r\n", (*it)->_pCmdStr, cmdStr);
                            if (strcasecmp((*it)->_pCmdStr, cmdStr) == 0)
                            {                                    
                                printf("FoundCmd <%s> Type %d\n\r", cmdStr, (*it)->_cmdType);
                                cmdFound = true;
                                if ((*it)->_cmdType == RdWebServerCmdDef::CMD_CALLBACK)
                                {
                                    ((*it)->_callback)(cmdStr, argStr);
                                }
                                else if ((*it)->_cmdType == RdWebServerCmdDef::CMD_LOCALFILE)
                                {
                                    if ((*it)->_substFileName[0] != '\0')
                                        handleLocalFileRequest((*it)->_substFileName, argStr, &client, httpHeader, (*it)->_bCacheIfPossible);
                                    else
                                        handleLocalFileRequest(cmdStr, argStr, &client, httpHeader, (*it)->_bCacheIfPossible);
                                }
                                else if ((*it)->_cmdType == RdWebServerCmdDef::CMD_SDCARDFILE)
                                {
                                    if ((*it)->_substFileName[0] != '\0')
                                        handleSDFileRequest((*it)->_substFileName, argStr, &client, httpHeader);
                                    else
                                        handleSDFileRequest(cmdStr, argStr, &client, httpHeader);
                                }
                                break;
                            }
                        }
                        // If command not found see if it is a local file
                        if (!cmdFound)
                            handleLocalFileRequest(cmdStr, argStr, &client, httpHeader, false);
                    }
                    
                    //setup http response header & data
/*                    char httpHeader[256] = {};
                    sprintf(httpHeader,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r\n\r",strlen(buffer));
                    client.send(httpHeader,strlen(httpHeader));
                    client.send(cmdStr,strlen(cmdStr)); */
                    //clientIsConnected = false;
//                    printf("Server done.\n\r");
                    break;
                }
                
            }
            printf("Connection closed ... TCP server is listening...\r\n");
            client.close();
            if (_pStatusLed != NULL)
                *_pStatusLed = false;
        }
    }
}

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