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

    // Connect ethernet

    // 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);


// 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.

Fri Oct 04 07:41:28 2013 +0000
Initial Revision Working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bobty 0:b5b4d07f7827 1 #ifndef RD_WEB_SERVER
Bobty 0:b5b4d07f7827 2 #define RD_WEB_SERVER
Bobty 0:b5b4d07f7827 3
Bobty 1:75bb184de749 4 #include <vector>
Bobty 0:b5b4d07f7827 5
Bobty 0:b5b4d07f7827 6 #include "mbed.h"
Bobty 0:b5b4d07f7827 7 #include "EthernetInterface.h"
Bobty 0:b5b4d07f7827 8
Bobty 2:9d8793c23b46 9 class RdFileCacheEntry
Bobty 2:9d8793c23b46 10 {
Bobty 2:9d8793c23b46 11 public:
Bobty 2:9d8793c23b46 12 static const int CACHE_MAX_FNAME_LEN = 15; // note local files on MBED are 8.3
Bobty 2:9d8793c23b46 13 RdFileCacheEntry(char* pFileName)
Bobty 2:9d8793c23b46 14 {
Bobty 2:9d8793c23b46 15 _bCacheValid = false;
Bobty 2:9d8793c23b46 16 strncpy(_fileName, pFileName, CACHE_MAX_FNAME_LEN-1);
Bobty 2:9d8793c23b46 17 _fileName[CACHE_MAX_FNAME_LEN-1] = '\0';
Bobty 2:9d8793c23b46 18 _pFileContent = NULL;
Bobty 2:9d8793c23b46 19 }
Bobty 2:9d8793c23b46 20 ~RdFileCacheEntry()
Bobty 2:9d8793c23b46 21 {
Bobty 2:9d8793c23b46 22 delete _pFileContent;
Bobty 2:9d8793c23b46 23 }
Bobty 2:9d8793c23b46 24 bool readLocalFileIntoCache(char* fileName);
Bobty 2:9d8793c23b46 25 public:
Bobty 2:9d8793c23b46 26 bool _bCacheValid;
Bobty 2:9d8793c23b46 27 char _fileName[CACHE_MAX_FNAME_LEN];
Bobty 2:9d8793c23b46 28 char* _pFileContent;
Bobty 2:9d8793c23b46 29 int _nFileLen;
Bobty 2:9d8793c23b46 30 };
Bobty 2:9d8793c23b46 31
Bobty 2:9d8793c23b46 32 typedef void (*CmdCallbackType)(char*cmdStr, char* argStr);
Bobty 1:75bb184de749 33
Bobty 1:75bb184de749 34 class RdWebServerCmdDef
Bobty 1:75bb184de749 35 {
Bobty 1:75bb184de749 36 public:
Bobty 2:9d8793c23b46 37 static const int CMD_LOCALFILE = 1;
Bobty 2:9d8793c23b46 38 static const int CMD_CALLBACK = 2;
Bobty 2:9d8793c23b46 39 static const int CMD_SDCARDFILE = 3;
Bobty 2:9d8793c23b46 40 RdWebServerCmdDef(char* pStr, int cmdType, CmdCallbackType callback, char* substFileName, bool bCacheIfPossible)
Bobty 1:75bb184de749 41 {
Bobty 1:75bb184de749 42 _pCmdStr = pStr;
Bobty 2:9d8793c23b46 43 _cmdType = cmdType;
Bobty 1:75bb184de749 44 _callback = callback;
Bobty 2:9d8793c23b46 45 _substFileName[0] = '\0';
Bobty 2:9d8793c23b46 46 if (substFileName != NULL)
Bobty 2:9d8793c23b46 47 {
Bobty 2:9d8793c23b46 48 strncpy(_substFileName, substFileName, RdFileCacheEntry::CACHE_MAX_FNAME_LEN);
Bobty 2:9d8793c23b46 49 _substFileName[RdFileCacheEntry::CACHE_MAX_FNAME_LEN-1] = '\0';
Bobty 2:9d8793c23b46 50 }
Bobty 2:9d8793c23b46 51 _bCacheIfPossible = bCacheIfPossible;
Bobty 1:75bb184de749 52 };
Bobty 1:75bb184de749 53 char* _pCmdStr;
Bobty 2:9d8793c23b46 54 int _cmdType;
Bobty 1:75bb184de749 55 CmdCallbackType _callback;
Bobty 2:9d8793c23b46 56 char _substFileName[RdFileCacheEntry::CACHE_MAX_FNAME_LEN];
Bobty 2:9d8793c23b46 57 bool _bCacheIfPossible;
Bobty 1:75bb184de749 58 };
Bobty 1:75bb184de749 59
Bobty 0:b5b4d07f7827 60 class RdWebServer
Bobty 0:b5b4d07f7827 61 {
Bobty 0:b5b4d07f7827 62 public :
Bobty 0:b5b4d07f7827 63
Bobty 0:b5b4d07f7827 64 RdWebServer();
Bobty 0:b5b4d07f7827 65 virtual ~RdWebServer();
Bobty 0:b5b4d07f7827 66
Bobty 0:b5b4d07f7827 67 bool init(int port, DigitalOut* pStatusLed);
Bobty 0:b5b4d07f7827 68 void run();
Bobty 0:b5b4d07f7827 69 bool isListening()
Bobty 0:b5b4d07f7827 70 {
Bobty 0:b5b4d07f7827 71 return _serverIsListening;
Bobty 0:b5b4d07f7827 72 };
Bobty 0:b5b4d07f7827 73
Bobty 2:9d8793c23b46 74 void addCommand(char* pCmdStr, int cmdType, CmdCallbackType callback = NULL, char* substFileName = NULL, bool cacheIfPossible = false);
Bobty 1:75bb184de749 75
Bobty 0:b5b4d07f7827 76 private :
Bobty 0:b5b4d07f7827 77 int _port;
Bobty 0:b5b4d07f7827 78 DigitalOut* _pStatusLed;
Bobty 0:b5b4d07f7827 79 TCPSocketServer _socketSrv;
Bobty 0:b5b4d07f7827 80 bool _serverIsListening;
Bobty 1:75bb184de749 81 std::vector<RdWebServerCmdDef*> _commands;
Bobty 2:9d8793c23b46 82 std::vector<RdFileCacheEntry*> _cachedFiles;
Bobty 1:75bb184de749 83 bool extractCmdArgs(char* buf, char* pCmdStr, int maxCmdStrLen, char* pArgStr, int maxArgStrLen);
Bobty 1:75bb184de749 84
Bobty 2:9d8793c23b46 85 void handleLocalFileRequest(char* cmdStr, char* argStr, TCPSocketConnection* pClient, char* httpHeader, bool bCacheIfPossible);
Bobty 2:9d8793c23b46 86 void handleSDFileRequest(char* cmdStr, char* argStr, TCPSocketConnection* pClient, char* httpHeader);
Bobty 2:9d8793c23b46 87 void handleCGIRequest(char* pUriStr, char* pQueryStr);
Bobty 2:9d8793c23b46 88 void sendFromCache(RdFileCacheEntry* pCacheEntry, TCPSocketConnection* pClient, char* httpHeader);
Bobty 2:9d8793c23b46 89
Bobty 0:b5b4d07f7827 90 };
Bobty 0:b5b4d07f7827 91
Bobty 0:b5b4d07f7827 92 #endif