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.

Mon Feb 02 16:36:14 2015 +0000
Updated to use SD or USB

Who changed what in which revision?

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