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.

Committer:
Bobty
Date:
Mon May 11 11:17:15 2015 +0000
Revision:
17:080f2bed8b36
Parent:
15:0865fa4b046a
Child:
18:5de680c4cfcb
Improved settings

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bobty 3:594136d34a32 1 /* RdWebServer.h
Bobty 15:0865fa4b046a 2 Rob Dobson 2013-2015
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 12:c14ffd4ec125 13 #ifdef RDWEB_DEBUG
Bobty 12:c14ffd4ec125 14 #if (RDWEB_DEBUG > 3)
Bobty 11:cec51b430b20 15 #define RD_DBG(x, ...) std::printf("[RD_DBG: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
Bobty 11:cec51b430b20 16 #else
Bobty 11:cec51b430b20 17 #define RD_DBG(x, ...)
Bobty 11:cec51b430b20 18 #endif
Bobty 12:c14ffd4ec125 19 #else
Bobty 12:c14ffd4ec125 20 #define RD_DBG(x, ...)
Bobty 12:c14ffd4ec125 21 #endif
Bobty 12:c14ffd4ec125 22
Bobty 12:c14ffd4ec125 23 #ifdef RDWEB_DEBUG
Bobty 12:c14ffd4ec125 24 #if (RDWEB_DEBUG > 2)
Bobty 11:cec51b430b20 25 #define RD_INFO(x, ...) std::printf("[RD_INFO: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
Bobty 11:cec51b430b20 26 #else
Bobty 11:cec51b430b20 27 #define RD_INFO(x, ...)
Bobty 11:cec51b430b20 28 #endif
Bobty 12:c14ffd4ec125 29 #else
Bobty 12:c14ffd4ec125 30 #define RD_INFO(x, ...)
Bobty 12:c14ffd4ec125 31 #endif
Bobty 12:c14ffd4ec125 32
Bobty 12:c14ffd4ec125 33 #ifdef RDWEB_DEBUG
Bobty 12:c14ffd4ec125 34 #if (RDWEB_DEBUG > 1)
Bobty 11:cec51b430b20 35 #define RD_WARN(x, ...) std::printf("[RD_WARNING: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
Bobty 11:cec51b430b20 36 #else
Bobty 11:cec51b430b20 37 #define RD_WARN(x, ...)
Bobty 11:cec51b430b20 38 #endif
Bobty 12:c14ffd4ec125 39 #else
Bobty 12:c14ffd4ec125 40 #define RD_WARN(x, ...)
Bobty 12:c14ffd4ec125 41 #endif
Bobty 12:c14ffd4ec125 42
Bobty 12:c14ffd4ec125 43 #ifdef RDWEB_DEBUG
Bobty 12:c14ffd4ec125 44 #if (RDWEB_DEBUG > 0)
Bobty 11:cec51b430b20 45 #define RD_ERR(x, ...) std::printf("[RD_ERR: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
Bobty 11:cec51b430b20 46 #else
Bobty 11:cec51b430b20 47 #define RD_ERR(x, ...)
Bobty 11:cec51b430b20 48 #endif
Bobty 12:c14ffd4ec125 49 #else
Bobty 12:c14ffd4ec125 50 #define RD_ERR(x, ...)
Bobty 12:c14ffd4ec125 51 #endif
Bobty 12:c14ffd4ec125 52
Bobty 12:c14ffd4ec125 53 //#define SUPPORT_LOCAL_FILESYSTEM 1
Bobty 12:c14ffd4ec125 54 //#define SUPPORT_FOLDER_VIEW 1
Bobty 15:0865fa4b046a 55 //#define SUPPORT_LOCAL_FILE_CACHE 1
Bobty 17:080f2bed8b36 56 const int SUBST_MAX_FNAME_LEN = 40; // note local files on MBED are 8.3 but this might include other files
Bobty 11:cec51b430b20 57
Bobty 6:46285c519af2 58 extern RawSerial pc;
Bobty 6:46285c519af2 59
Bobty 15:0865fa4b046a 60 #ifdef SUPPORT_LOCAL_FILE_CACHE
Bobty 15:0865fa4b046a 61
Bobty 2:9d8793c23b46 62 class RdFileCacheEntry
Bobty 2:9d8793c23b46 63 {
Bobty 2:9d8793c23b46 64 public:
Bobty 2:9d8793c23b46 65 RdFileCacheEntry(char* pFileName)
Bobty 2:9d8793c23b46 66 {
Bobty 2:9d8793c23b46 67 _bCacheValid = false;
Bobty 17:080f2bed8b36 68 strncpy(_fileName, pFileName, SUBST_MAX_FNAME_LEN-1);
Bobty 17:080f2bed8b36 69 _fileName[SUBST_MAX_FNAME_LEN-1] = '\0';
Bobty 2:9d8793c23b46 70 _pFileContent = NULL;
Bobty 2:9d8793c23b46 71 }
Bobty 2:9d8793c23b46 72 ~RdFileCacheEntry()
Bobty 2:9d8793c23b46 73 {
Bobty 2:9d8793c23b46 74 delete _pFileContent;
Bobty 2:9d8793c23b46 75 }
Bobty 2:9d8793c23b46 76 bool readLocalFileIntoCache(char* fileName);
Bobty 2:9d8793c23b46 77 public:
Bobty 2:9d8793c23b46 78 bool _bCacheValid;
Bobty 17:080f2bed8b36 79 char _fileName[SUBST_MAX_FNAME_LEN];
Bobty 2:9d8793c23b46 80 char* _pFileContent;
Bobty 2:9d8793c23b46 81 int _nFileLen;
Bobty 2:9d8793c23b46 82 };
Bobty 2:9d8793c23b46 83
Bobty 15:0865fa4b046a 84 #endif
Bobty 15:0865fa4b046a 85
Bobty 7:fe7c33f7fbb8 86 typedef char* (*CmdCallbackType)(int method, char*cmdStr, char* argStr);
Bobty 1:75bb184de749 87
Bobty 1:75bb184de749 88 class RdWebServerCmdDef
Bobty 1:75bb184de749 89 {
Bobty 1:75bb184de749 90 public:
Bobty 2:9d8793c23b46 91 static const int CMD_LOCALFILE = 1;
Bobty 2:9d8793c23b46 92 static const int CMD_CALLBACK = 2;
Bobty 4:6afb3bbf20a4 93 static const int CMD_SDORUSBFILE = 3;
Bobty 17:080f2bed8b36 94 RdWebServerCmdDef(char* pStr, int cmdType, CmdCallbackType callback, char* substFileName = NULL, bool bCacheIfPossible = false)
Bobty 1:75bb184de749 95 {
Bobty 1:75bb184de749 96 _pCmdStr = pStr;
Bobty 2:9d8793c23b46 97 _cmdType = cmdType;
Bobty 1:75bb184de749 98 _callback = callback;
Bobty 17:080f2bed8b36 99 _substFileName[0] = '\0';
Bobty 17:080f2bed8b36 100 if (substFileName != NULL)
Bobty 2:9d8793c23b46 101 {
Bobty 17:080f2bed8b36 102 strncpy(_substFileName, substFileName, SUBST_MAX_FNAME_LEN);
Bobty 17:080f2bed8b36 103 _substFileName[SUBST_MAX_FNAME_LEN-1] = '\0';
Bobty 2:9d8793c23b46 104 }
Bobty 2:9d8793c23b46 105 _bCacheIfPossible = bCacheIfPossible;
Bobty 1:75bb184de749 106 };
Bobty 1:75bb184de749 107 char* _pCmdStr;
Bobty 2:9d8793c23b46 108 int _cmdType;
Bobty 1:75bb184de749 109 CmdCallbackType _callback;
Bobty 17:080f2bed8b36 110 char _substFileName[SUBST_MAX_FNAME_LEN];
Bobty 2:9d8793c23b46 111 bool _bCacheIfPossible;
Bobty 1:75bb184de749 112 };
Bobty 1:75bb184de749 113
Bobty 6:46285c519af2 114 const int HTTPD_MAX_HDR_LENGTH = 255;
Bobty 6:46285c519af2 115 const int HTTPD_MAX_REQ_LENGTH = 1023;
Bobty 6:46285c519af2 116
Bobty 0:b5b4d07f7827 117 class RdWebServer
Bobty 0:b5b4d07f7827 118 {
Bobty 0:b5b4d07f7827 119 public :
Bobty 7:fe7c33f7fbb8 120 static const int METHOD_OTHER = 0;
Bobty 7:fe7c33f7fbb8 121 static const int METHOD_GET = 1;
Bobty 7:fe7c33f7fbb8 122 static const int METHOD_POST = 2;
Bobty 0:b5b4d07f7827 123 RdWebServer();
Bobty 0:b5b4d07f7827 124 virtual ~RdWebServer();
Bobty 0:b5b4d07f7827 125
Bobty 4:6afb3bbf20a4 126 bool init(int port, DigitalOut* pStatusLed, char* pBaseWebFolder);
Bobty 0:b5b4d07f7827 127 void run();
Bobty 2:9d8793c23b46 128 void addCommand(char* pCmdStr, int cmdType, CmdCallbackType callback = NULL, char* substFileName = NULL, bool cacheIfPossible = false);
Bobty 1:75bb184de749 129
Bobty 0:b5b4d07f7827 130 private :
Bobty 0:b5b4d07f7827 131 int _port;
Bobty 0:b5b4d07f7827 132 DigitalOut* _pStatusLed;
Bobty 15:0865fa4b046a 133 TCPSocketServer _serverSocket;
Bobty 15:0865fa4b046a 134 bool _initOk;
Bobty 1:75bb184de749 135 std::vector<RdWebServerCmdDef*> _commands;
Bobty 15:0865fa4b046a 136 #ifdef SUPPORT_LOCAL_FILE_CACHE
Bobty 2:9d8793c23b46 137 std::vector<RdFileCacheEntry*> _cachedFiles;
Bobty 15:0865fa4b046a 138 #endif
Bobty 1:75bb184de749 139 bool extractCmdArgs(char* buf, char* pCmdStr, int maxCmdStrLen, char* pArgStr, int maxArgStrLen);
Bobty 4:6afb3bbf20a4 140 char* _pBaseWebFolder;
Bobty 6:46285c519af2 141 char _httpHeader[HTTPD_MAX_HDR_LENGTH+1];
Bobty 6:46285c519af2 142 char _buffer[HTTPD_MAX_REQ_LENGTH+1];
Bobty 1:75bb184de749 143
Bobty 17:080f2bed8b36 144 bool handleLocalFileRequest(char* cmdStr, char* argStr, TCPSocketConnection &client, bool bCacheIfPossible);
Bobty 17:080f2bed8b36 145 bool handleSDFileRequest(char* cmdStr, char* argStr, TCPSocketConnection &client);
Bobty 2:9d8793c23b46 146 void handleCGIRequest(char* pUriStr, char* pQueryStr);
Bobty 15:0865fa4b046a 147 #ifdef SUPPORT_LOCAL_FILE_CACHE
Bobty 17:080f2bed8b36 148 void sendFromCache(RdFileCacheEntry* pCacheEntry, TCPSocketConnection &client);
Bobty 15:0865fa4b046a 149 #endif
Bobty 14:4b83670854f0 150 bool handleReceivedHttp(TCPSocketConnection &client);
Bobty 17:080f2bed8b36 151 void formHTTPHeader(const char* rsltCode, const char* contentType, int contentLen);
Bobty 17:080f2bed8b36 152
Bobty 17:080f2bed8b36 153 // Settings - see the constructor
Bobty 17:080f2bed8b36 154 bool _blockingOnAccept;
Bobty 17:080f2bed8b36 155 bool _blockingOnReceive;
Bobty 17:080f2bed8b36 156 int _timeoutOnBlocking;
Bobty 17:080f2bed8b36 157 bool _closeConnAfterSend;
Bobty 17:080f2bed8b36 158 bool _closeConnOnReceiveFail;
Bobty 2:9d8793c23b46 159
Bobty 0:b5b4d07f7827 160 };
Bobty 0:b5b4d07f7827 161
Bobty 0:b5b4d07f7827 162 #endif