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.

Revision:
2:9d8793c23b46
Parent:
1:75bb184de749
Child:
3:594136d34a32
diff -r 75bb184de749 -r 9d8793c23b46 RdWebServer.h
--- a/RdWebServer.h	Thu Sep 19 12:06:18 2013 +0000
+++ b/RdWebServer.h	Fri Oct 04 07:41:28 2013 +0000
@@ -6,18 +6,55 @@
 #include "mbed.h"
 #include "EthernetInterface.h"
 
-typedef void (*CmdCallbackType)(char* argStr);
+class RdFileCacheEntry
+{
+    public:
+        static const int CACHE_MAX_FNAME_LEN = 15;  // note local files on MBED are 8.3
+        RdFileCacheEntry(char* pFileName)
+        {
+            _bCacheValid = false;
+            strncpy(_fileName, pFileName, CACHE_MAX_FNAME_LEN-1);
+            _fileName[CACHE_MAX_FNAME_LEN-1] = '\0';
+            _pFileContent = NULL;
+        }
+        ~RdFileCacheEntry()
+        {
+            delete _pFileContent;
+        }
+        bool readLocalFileIntoCache(char* fileName);
+    public:
+        bool _bCacheValid;
+        char _fileName[CACHE_MAX_FNAME_LEN];
+        char* _pFileContent;
+        int _nFileLen;
+};
+
+typedef void (*CmdCallbackType)(char*cmdStr, char* argStr);
 
 class RdWebServerCmdDef
 {
     public:
-        RdWebServerCmdDef(char* pStr, CmdCallbackType callback)
+        static const int CMD_LOCALFILE = 1;
+        static const int CMD_CALLBACK = 2;
+        static const int CMD_SDCARDFILE = 3;
+        RdWebServerCmdDef(char* pStr, int cmdType, CmdCallbackType callback, char* substFileName, bool bCacheIfPossible)
         {
             _pCmdStr = pStr;
+            _cmdType = cmdType;
             _callback = callback;
+            _substFileName[0] = '\0';
+            if (substFileName != NULL)
+            {
+                strncpy(_substFileName, substFileName, RdFileCacheEntry::CACHE_MAX_FNAME_LEN);
+                _substFileName[RdFileCacheEntry::CACHE_MAX_FNAME_LEN-1] = '\0';
+            }
+            _bCacheIfPossible = bCacheIfPossible;
         };
         char* _pCmdStr;
+        int _cmdType;
         CmdCallbackType _callback;
+        char _substFileName[RdFileCacheEntry::CACHE_MAX_FNAME_LEN];
+        bool _bCacheIfPossible;
 };
 
 class RdWebServer
@@ -34,7 +71,7 @@
             return _serverIsListening;
         };
         
-        void addCommand(char* pCmdStr, CmdCallbackType callback);
+        void addCommand(char* pCmdStr, int cmdType, CmdCallbackType callback = NULL, char* substFileName = NULL, bool cacheIfPossible = false);
         
     private :
         int _port;
@@ -42,9 +79,14 @@
         TCPSocketServer _socketSrv;
         bool _serverIsListening;
         std::vector<RdWebServerCmdDef*> _commands;
+        std::vector<RdFileCacheEntry*> _cachedFiles;
         bool extractCmdArgs(char* buf, char* pCmdStr, int maxCmdStrLen, char* pArgStr, int maxArgStrLen);
 
-        
+        void handleLocalFileRequest(char* cmdStr, char* argStr, TCPSocketConnection* pClient, char* httpHeader, bool bCacheIfPossible);
+        void handleSDFileRequest(char* cmdStr, char* argStr, TCPSocketConnection* pClient, char* httpHeader);
+        void handleCGIRequest(char* pUriStr, char* pQueryStr);
+        void sendFromCache(RdFileCacheEntry* pCacheEntry, TCPSocketConnection* pClient, char* httpHeader);
+
 };
 
 #endif