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:
Tue May 05 20:27:33 2015 +0000
Revision:
15:0865fa4b046a
Parent:
14:4b83670854f0
Child:
17:080f2bed8b36
Pared back for work on stability - ok on LPC1768

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 11:cec51b430b20 56
Bobty 6:46285c519af2 57 extern RawSerial pc;
Bobty 6:46285c519af2 58
Bobty 15:0865fa4b046a 59 #ifdef SUPPORT_LOCAL_FILE_CACHE
Bobty 15:0865fa4b046a 60
Bobty 2:9d8793c23b46 61 class RdFileCacheEntry
Bobty 2:9d8793c23b46 62 {
Bobty 2:9d8793c23b46 63 public:
Bobty 2:9d8793c23b46 64 static const int CACHE_MAX_FNAME_LEN = 15; // note local files on MBED are 8.3
Bobty 2:9d8793c23b46 65 RdFileCacheEntry(char* pFileName)
Bobty 2:9d8793c23b46 66 {
Bobty 2:9d8793c23b46 67 _bCacheValid = false;
Bobty 2:9d8793c23b46 68 strncpy(_fileName, pFileName, CACHE_MAX_FNAME_LEN-1);
Bobty 2:9d8793c23b46 69 _fileName[CACHE_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 2:9d8793c23b46 79 char _fileName[CACHE_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 15:0865fa4b046a 94 RdWebServerCmdDef(char* pStr, int cmdType, CmdCallbackType callback, char* subst8dot3FileName = 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 15:0865fa4b046a 99 #ifdef SUPPORT_LOCAL_FILE_CACHE
Bobty 15:0865fa4b046a 100 _subst8dot3FileName[0] = '\0';
Bobty 15:0865fa4b046a 101 if (subst8dot3FileName != NULL)
Bobty 2:9d8793c23b46 102 {
Bobty 15:0865fa4b046a 103 strncpy(_subst8dot3FileName, subst8dot3FileName, RdFileCacheEntry::CACHE_MAX_FNAME_LEN);
Bobty 15:0865fa4b046a 104 _subst8dot3FileName[RdFileCacheEntry::CACHE_MAX_FNAME_LEN-1] = '\0';
Bobty 2:9d8793c23b46 105 }
Bobty 15:0865fa4b046a 106 #endif
Bobty 2:9d8793c23b46 107 _bCacheIfPossible = bCacheIfPossible;
Bobty 1:75bb184de749 108 };
Bobty 1:75bb184de749 109 char* _pCmdStr;
Bobty 2:9d8793c23b46 110 int _cmdType;
Bobty 1:75bb184de749 111 CmdCallbackType _callback;
Bobty 15:0865fa4b046a 112 #ifdef SUPPORT_LOCAL_FILE_CACHE
Bobty 15:0865fa4b046a 113 char _subst8dot3FileName[RdFileCacheEntry::CACHE_MAX_FNAME_LEN];
Bobty 15:0865fa4b046a 114 #endif
Bobty 2:9d8793c23b46 115 bool _bCacheIfPossible;
Bobty 1:75bb184de749 116 };
Bobty 1:75bb184de749 117
Bobty 6:46285c519af2 118 const int HTTPD_MAX_HDR_LENGTH = 255;
Bobty 6:46285c519af2 119 const int HTTPD_MAX_REQ_LENGTH = 1023;
Bobty 6:46285c519af2 120
Bobty 0:b5b4d07f7827 121 class RdWebServer
Bobty 0:b5b4d07f7827 122 {
Bobty 0:b5b4d07f7827 123 public :
Bobty 7:fe7c33f7fbb8 124 static const int METHOD_OTHER = 0;
Bobty 7:fe7c33f7fbb8 125 static const int METHOD_GET = 1;
Bobty 7:fe7c33f7fbb8 126 static const int METHOD_POST = 2;
Bobty 0:b5b4d07f7827 127 RdWebServer();
Bobty 0:b5b4d07f7827 128 virtual ~RdWebServer();
Bobty 0:b5b4d07f7827 129
Bobty 4:6afb3bbf20a4 130 bool init(int port, DigitalOut* pStatusLed, char* pBaseWebFolder);
Bobty 0:b5b4d07f7827 131 void run();
Bobty 2:9d8793c23b46 132 void addCommand(char* pCmdStr, int cmdType, CmdCallbackType callback = NULL, char* substFileName = NULL, bool cacheIfPossible = false);
Bobty 1:75bb184de749 133
Bobty 0:b5b4d07f7827 134 private :
Bobty 0:b5b4d07f7827 135 int _port;
Bobty 0:b5b4d07f7827 136 DigitalOut* _pStatusLed;
Bobty 15:0865fa4b046a 137 TCPSocketServer _serverSocket;
Bobty 15:0865fa4b046a 138 bool _initOk;
Bobty 1:75bb184de749 139 std::vector<RdWebServerCmdDef*> _commands;
Bobty 15:0865fa4b046a 140 #ifdef SUPPORT_LOCAL_FILE_CACHE
Bobty 2:9d8793c23b46 141 std::vector<RdFileCacheEntry*> _cachedFiles;
Bobty 15:0865fa4b046a 142 #endif
Bobty 1:75bb184de749 143 bool extractCmdArgs(char* buf, char* pCmdStr, int maxCmdStrLen, char* pArgStr, int maxArgStrLen);
Bobty 4:6afb3bbf20a4 144 char* _pBaseWebFolder;
Bobty 6:46285c519af2 145 char _httpHeader[HTTPD_MAX_HDR_LENGTH+1];
Bobty 6:46285c519af2 146 char _buffer[HTTPD_MAX_REQ_LENGTH+1];
Bobty 1:75bb184de749 147
Bobty 6:46285c519af2 148 void handleLocalFileRequest(char* cmdStr, char* argStr, TCPSocketConnection &client, char* httpHeader, bool bCacheIfPossible);
Bobty 14:4b83670854f0 149 bool handleSDFileRequest(char* cmdStr, char* argStr, TCPSocketConnection &client, char* httpHeader);
Bobty 2:9d8793c23b46 150 void handleCGIRequest(char* pUriStr, char* pQueryStr);
Bobty 15:0865fa4b046a 151 #ifdef SUPPORT_LOCAL_FILE_CACHE
Bobty 6:46285c519af2 152 void sendFromCache(RdFileCacheEntry* pCacheEntry, TCPSocketConnection &client, char* httpHeader);
Bobty 15:0865fa4b046a 153 #endif
Bobty 14:4b83670854f0 154 bool handleReceivedHttp(TCPSocketConnection &client);
Bobty 2:9d8793c23b46 155
Bobty 0:b5b4d07f7827 156 };
Bobty 0:b5b4d07f7827 157
Bobty 0:b5b4d07f7827 158 #endif