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 15:05:44 2015 +0000
Revision:
14:4b83670854f0
Parent:
12:c14ffd4ec125
Child:
15:0865fa4b046a
Improving robustness

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 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 11:cec51b430b20 55
Bobty 6:46285c519af2 56 extern RawSerial pc;
Bobty 6:46285c519af2 57
Bobty 2:9d8793c23b46 58 class RdFileCacheEntry
Bobty 2:9d8793c23b46 59 {
Bobty 2:9d8793c23b46 60 public:
Bobty 2:9d8793c23b46 61 static const int CACHE_MAX_FNAME_LEN = 15; // note local files on MBED are 8.3
Bobty 2:9d8793c23b46 62 RdFileCacheEntry(char* pFileName)
Bobty 2:9d8793c23b46 63 {
Bobty 2:9d8793c23b46 64 _bCacheValid = false;
Bobty 2:9d8793c23b46 65 strncpy(_fileName, pFileName, CACHE_MAX_FNAME_LEN-1);
Bobty 2:9d8793c23b46 66 _fileName[CACHE_MAX_FNAME_LEN-1] = '\0';
Bobty 2:9d8793c23b46 67 _pFileContent = NULL;
Bobty 2:9d8793c23b46 68 }
Bobty 2:9d8793c23b46 69 ~RdFileCacheEntry()
Bobty 2:9d8793c23b46 70 {
Bobty 2:9d8793c23b46 71 delete _pFileContent;
Bobty 2:9d8793c23b46 72 }
Bobty 2:9d8793c23b46 73 bool readLocalFileIntoCache(char* fileName);
Bobty 2:9d8793c23b46 74 public:
Bobty 2:9d8793c23b46 75 bool _bCacheValid;
Bobty 2:9d8793c23b46 76 char _fileName[CACHE_MAX_FNAME_LEN];
Bobty 2:9d8793c23b46 77 char* _pFileContent;
Bobty 2:9d8793c23b46 78 int _nFileLen;
Bobty 2:9d8793c23b46 79 };
Bobty 2:9d8793c23b46 80
Bobty 7:fe7c33f7fbb8 81 typedef char* (*CmdCallbackType)(int method, char*cmdStr, char* argStr);
Bobty 1:75bb184de749 82
Bobty 1:75bb184de749 83 class RdWebServerCmdDef
Bobty 1:75bb184de749 84 {
Bobty 1:75bb184de749 85 public:
Bobty 2:9d8793c23b46 86 static const int CMD_LOCALFILE = 1;
Bobty 2:9d8793c23b46 87 static const int CMD_CALLBACK = 2;
Bobty 4:6afb3bbf20a4 88 static const int CMD_SDORUSBFILE = 3;
Bobty 2:9d8793c23b46 89 RdWebServerCmdDef(char* pStr, int cmdType, CmdCallbackType callback, char* substFileName, bool bCacheIfPossible)
Bobty 1:75bb184de749 90 {
Bobty 1:75bb184de749 91 _pCmdStr = pStr;
Bobty 2:9d8793c23b46 92 _cmdType = cmdType;
Bobty 1:75bb184de749 93 _callback = callback;
Bobty 2:9d8793c23b46 94 _substFileName[0] = '\0';
Bobty 2:9d8793c23b46 95 if (substFileName != NULL)
Bobty 2:9d8793c23b46 96 {
Bobty 2:9d8793c23b46 97 strncpy(_substFileName, substFileName, RdFileCacheEntry::CACHE_MAX_FNAME_LEN);
Bobty 2:9d8793c23b46 98 _substFileName[RdFileCacheEntry::CACHE_MAX_FNAME_LEN-1] = '\0';
Bobty 2:9d8793c23b46 99 }
Bobty 2:9d8793c23b46 100 _bCacheIfPossible = bCacheIfPossible;
Bobty 1:75bb184de749 101 };
Bobty 1:75bb184de749 102 char* _pCmdStr;
Bobty 2:9d8793c23b46 103 int _cmdType;
Bobty 1:75bb184de749 104 CmdCallbackType _callback;
Bobty 2:9d8793c23b46 105 char _substFileName[RdFileCacheEntry::CACHE_MAX_FNAME_LEN];
Bobty 2:9d8793c23b46 106 bool _bCacheIfPossible;
Bobty 1:75bb184de749 107 };
Bobty 1:75bb184de749 108
Bobty 6:46285c519af2 109 const int HTTPD_MAX_HDR_LENGTH = 255;
Bobty 6:46285c519af2 110 const int HTTPD_MAX_REQ_LENGTH = 1023;
Bobty 6:46285c519af2 111
Bobty 0:b5b4d07f7827 112 class RdWebServer
Bobty 0:b5b4d07f7827 113 {
Bobty 0:b5b4d07f7827 114 public :
Bobty 7:fe7c33f7fbb8 115 static const int METHOD_OTHER = 0;
Bobty 7:fe7c33f7fbb8 116 static const int METHOD_GET = 1;
Bobty 7:fe7c33f7fbb8 117 static const int METHOD_POST = 2;
Bobty 0:b5b4d07f7827 118 RdWebServer();
Bobty 0:b5b4d07f7827 119 virtual ~RdWebServer();
Bobty 0:b5b4d07f7827 120
Bobty 4:6afb3bbf20a4 121 bool init(int port, DigitalOut* pStatusLed, char* pBaseWebFolder);
Bobty 0:b5b4d07f7827 122 void run();
Bobty 0:b5b4d07f7827 123 bool isListening()
Bobty 0:b5b4d07f7827 124 {
Bobty 0:b5b4d07f7827 125 return _serverIsListening;
Bobty 0:b5b4d07f7827 126 };
Bobty 0:b5b4d07f7827 127
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 0:b5b4d07f7827 133 TCPSocketServer _socketSrv;
Bobty 0:b5b4d07f7827 134 bool _serverIsListening;
Bobty 1:75bb184de749 135 std::vector<RdWebServerCmdDef*> _commands;
Bobty 2:9d8793c23b46 136 std::vector<RdFileCacheEntry*> _cachedFiles;
Bobty 1:75bb184de749 137 bool extractCmdArgs(char* buf, char* pCmdStr, int maxCmdStrLen, char* pArgStr, int maxArgStrLen);
Bobty 4:6afb3bbf20a4 138 char* _pBaseWebFolder;
Bobty 6:46285c519af2 139 char _httpHeader[HTTPD_MAX_HDR_LENGTH+1];
Bobty 6:46285c519af2 140 char _buffer[HTTPD_MAX_REQ_LENGTH+1];
Bobty 1:75bb184de749 141
Bobty 6:46285c519af2 142 void handleLocalFileRequest(char* cmdStr, char* argStr, TCPSocketConnection &client, char* httpHeader, bool bCacheIfPossible);
Bobty 14:4b83670854f0 143 bool handleSDFileRequest(char* cmdStr, char* argStr, TCPSocketConnection &client, char* httpHeader);
Bobty 2:9d8793c23b46 144 void handleCGIRequest(char* pUriStr, char* pQueryStr);
Bobty 6:46285c519af2 145 void sendFromCache(RdFileCacheEntry* pCacheEntry, TCPSocketConnection &client, char* httpHeader);
Bobty 14:4b83670854f0 146 bool handleReceivedHttp(TCPSocketConnection &client);
Bobty 2:9d8793c23b46 147
Bobty 0:b5b4d07f7827 148 };
Bobty 0:b5b4d07f7827 149
Bobty 0:b5b4d07f7827 150 #endif