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.

--- a/RdWebServer.h	Fri Oct 16 08:41:02 2015 +0000
+++ b/RdWebServer.h	Mon Feb 08 13:47:29 2016 +0000
@@ -7,10 +7,7 @@
 #ifndef RD_WEB_SERVER
 #define RD_WEB_SERVER
-#include <vector>
 #include "mbed.h"
-#include "EthernetInterface.h"
 // Debug level
 #ifdef RDWEB_DEBUG 
@@ -56,6 +53,8 @@
 // Length of strings
 const int MAX_CMDSTR_LEN = 100;
 const int MAX_ARGSTR_LEN = 100;
+const int MAX_WEB_SERVER_CMDS = 30;
 const int SUBST_MAX_FNAME_LEN = 40;  // note local files on MBED are 8.3 but this might include other files
 extern RawSerial pc;
@@ -113,14 +112,21 @@
 const int HTTPD_MAX_HDR_LENGTH = 255;
 // The FRDM-K64F has more memory than the LPC1768 so allow a larger buffer
-#ifdef MCU_MK64F12
+#if defined(MCU_MK64F12)
 const int HTTPD_MAX_REQ_LENGTH = 2048;
 #warning("TCP Request Length 2048")
+#elif defined(TARGET_ARCH_BLE)
+const int HTTPD_MAX_REQ_LENGTH = 1024;
+#warning("TCP Request Length 1024")
 const int HTTPD_MAX_REQ_LENGTH = 1024;
 #warning("TCP Request Length 1024")
+class TCPSocketServer;
+class TCPSocketConnection;
+class Mutex;
 class RdWebServer
     public :
@@ -128,7 +134,7 @@
         static const int METHOD_GET = 1;
         static const int METHOD_POST = 2;
         static const int METHOD_OPTIONS = 3;
-        RdWebServer(Mutex* pSdCardMutex = NULL);
+        RdWebServer(TCPSocketServer& tcpServerSocket, Mutex* pSdCardMutex = NULL);
         virtual ~RdWebServer();
         bool init(int port, DigitalOut* pStatusLed, char* pBaseWebFolder);
@@ -141,10 +147,12 @@
     private :
         int _port;
         DigitalOut* _pStatusLed;
-        TCPSocketServer _serverSocket;
+        TCPSocketServer& _serverSocket;
         bool _initOk;
-        std::vector<RdWebServerCmdDef*> _commands;
-        std::vector<RdFileCacheEntry*> _cachedFiles;
+        RdWebServerCmdDef* _pWebServerCmds[MAX_WEB_SERVER_CMDS];
+        int _numWebServerCmds;
+        RdFileCacheEntry* _pWebServerCachedFiles[MAX_WEB_SERVER_CACHED_FILES];
+        int _numWebServerCachedFiles;
         bool extractCmdArgs(char* buf, char* pCmdStr, int maxCmdStrLen, char* pArgStr, int maxArgStrLen, int& contentLen);
         char* _pBaseWebFolder;
         char _httpHeader[HTTPD_MAX_HDR_LENGTH+1];