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:
Sun May 03 18:59:42 2015 +0000
Revision:
11:cec51b430b20
Parent:
7:fe7c33f7fbb8
Child:
12:c14ffd4ec125
Added debugging macros

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 11:cec51b430b20 13 #if defined(DEBUG) && (DEBUG > 3)
Bobty 11:cec51b430b20 14 #define RD_DBG(x, ...) std::printf("[RD_DBG: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
Bobty 11:cec51b430b20 15 #else
Bobty 11:cec51b430b20 16 #define RD_DBG(x, ...)
Bobty 11:cec51b430b20 17 #endif
Bobty 11:cec51b430b20 18 #if defined(DEBUG) && (DEBUG > 2)
Bobty 11:cec51b430b20 19 #define RD_INFO(x, ...) std::printf("[RD_INFO: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
Bobty 11:cec51b430b20 20 #else
Bobty 11:cec51b430b20 21 #define RD_INFO(x, ...)
Bobty 11:cec51b430b20 22 #endif
Bobty 11:cec51b430b20 23 #if defined(DEBUG) && (DEBUG > 1)
Bobty 11:cec51b430b20 24 #define RD_WARN(x, ...) std::printf("[RD_WARNING: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
Bobty 11:cec51b430b20 25 #else
Bobty 11:cec51b430b20 26 #define RD_WARN(x, ...)
Bobty 11:cec51b430b20 27 #endif
Bobty 11:cec51b430b20 28 #if defined(DEBUG) && (DEBUG > 0)
Bobty 11:cec51b430b20 29 #define RD_ERR(x, ...) std::printf("[RD_ERR: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
Bobty 11:cec51b430b20 30 #else
Bobty 11:cec51b430b20 31 #define RD_ERR(x, ...)
Bobty 11:cec51b430b20 32 #endif
Bobty 11:cec51b430b20 33
Bobty 6:46285c519af2 34 extern RawSerial pc;
Bobty 6:46285c519af2 35
Bobty 2:9d8793c23b46 36 class RdFileCacheEntry
Bobty 2:9d8793c23b46 37 {
Bobty 2:9d8793c23b46 38 public:
Bobty 2:9d8793c23b46 39 static const int CACHE_MAX_FNAME_LEN = 15; // note local files on MBED are 8.3
Bobty 2:9d8793c23b46 40 RdFileCacheEntry(char* pFileName)
Bobty 2:9d8793c23b46 41 {
Bobty 2:9d8793c23b46 42 _bCacheValid = false;
Bobty 2:9d8793c23b46 43 strncpy(_fileName, pFileName, CACHE_MAX_FNAME_LEN-1);
Bobty 2:9d8793c23b46 44 _fileName[CACHE_MAX_FNAME_LEN-1] = '\0';
Bobty 2:9d8793c23b46 45 _pFileContent = NULL;
Bobty 2:9d8793c23b46 46 }
Bobty 2:9d8793c23b46 47 ~RdFileCacheEntry()
Bobty 2:9d8793c23b46 48 {
Bobty 2:9d8793c23b46 49 delete _pFileContent;
Bobty 2:9d8793c23b46 50 }
Bobty 2:9d8793c23b46 51 bool readLocalFileIntoCache(char* fileName);
Bobty 2:9d8793c23b46 52 public:
Bobty 2:9d8793c23b46 53 bool _bCacheValid;
Bobty 2:9d8793c23b46 54 char _fileName[CACHE_MAX_FNAME_LEN];
Bobty 2:9d8793c23b46 55 char* _pFileContent;
Bobty 2:9d8793c23b46 56 int _nFileLen;
Bobty 2:9d8793c23b46 57 };
Bobty 2:9d8793c23b46 58
Bobty 7:fe7c33f7fbb8 59 typedef char* (*CmdCallbackType)(int method, char*cmdStr, char* argStr);
Bobty 1:75bb184de749 60
Bobty 1:75bb184de749 61 class RdWebServerCmdDef
Bobty 1:75bb184de749 62 {
Bobty 1:75bb184de749 63 public:
Bobty 2:9d8793c23b46 64 static const int CMD_LOCALFILE = 1;
Bobty 2:9d8793c23b46 65 static const int CMD_CALLBACK = 2;
Bobty 4:6afb3bbf20a4 66 static const int CMD_SDORUSBFILE = 3;
Bobty 2:9d8793c23b46 67 RdWebServerCmdDef(char* pStr, int cmdType, CmdCallbackType callback, char* substFileName, bool bCacheIfPossible)
Bobty 1:75bb184de749 68 {
Bobty 1:75bb184de749 69 _pCmdStr = pStr;
Bobty 2:9d8793c23b46 70 _cmdType = cmdType;
Bobty 1:75bb184de749 71 _callback = callback;
Bobty 2:9d8793c23b46 72 _substFileName[0] = '\0';
Bobty 2:9d8793c23b46 73 if (substFileName != NULL)
Bobty 2:9d8793c23b46 74 {
Bobty 2:9d8793c23b46 75 strncpy(_substFileName, substFileName, RdFileCacheEntry::CACHE_MAX_FNAME_LEN);
Bobty 2:9d8793c23b46 76 _substFileName[RdFileCacheEntry::CACHE_MAX_FNAME_LEN-1] = '\0';
Bobty 2:9d8793c23b46 77 }
Bobty 2:9d8793c23b46 78 _bCacheIfPossible = bCacheIfPossible;
Bobty 1:75bb184de749 79 };
Bobty 1:75bb184de749 80 char* _pCmdStr;
Bobty 2:9d8793c23b46 81 int _cmdType;
Bobty 1:75bb184de749 82 CmdCallbackType _callback;
Bobty 2:9d8793c23b46 83 char _substFileName[RdFileCacheEntry::CACHE_MAX_FNAME_LEN];
Bobty 2:9d8793c23b46 84 bool _bCacheIfPossible;
Bobty 1:75bb184de749 85 };
Bobty 1:75bb184de749 86
Bobty 6:46285c519af2 87 const int HTTPD_MAX_HDR_LENGTH = 255;
Bobty 6:46285c519af2 88 const int HTTPD_MAX_REQ_LENGTH = 1023;
Bobty 6:46285c519af2 89
Bobty 0:b5b4d07f7827 90 class RdWebServer
Bobty 0:b5b4d07f7827 91 {
Bobty 0:b5b4d07f7827 92 public :
Bobty 7:fe7c33f7fbb8 93 static const int METHOD_OTHER = 0;
Bobty 7:fe7c33f7fbb8 94 static const int METHOD_GET = 1;
Bobty 7:fe7c33f7fbb8 95 static const int METHOD_POST = 2;
Bobty 0:b5b4d07f7827 96 RdWebServer();
Bobty 0:b5b4d07f7827 97 virtual ~RdWebServer();
Bobty 0:b5b4d07f7827 98
Bobty 4:6afb3bbf20a4 99 bool init(int port, DigitalOut* pStatusLed, char* pBaseWebFolder);
Bobty 0:b5b4d07f7827 100 void run();
Bobty 0:b5b4d07f7827 101 bool isListening()
Bobty 0:b5b4d07f7827 102 {
Bobty 0:b5b4d07f7827 103 return _serverIsListening;
Bobty 0:b5b4d07f7827 104 };
Bobty 0:b5b4d07f7827 105
Bobty 2:9d8793c23b46 106 void addCommand(char* pCmdStr, int cmdType, CmdCallbackType callback = NULL, char* substFileName = NULL, bool cacheIfPossible = false);
Bobty 1:75bb184de749 107
Bobty 0:b5b4d07f7827 108 private :
Bobty 0:b5b4d07f7827 109 int _port;
Bobty 0:b5b4d07f7827 110 DigitalOut* _pStatusLed;
Bobty 0:b5b4d07f7827 111 TCPSocketServer _socketSrv;
Bobty 0:b5b4d07f7827 112 bool _serverIsListening;
Bobty 1:75bb184de749 113 std::vector<RdWebServerCmdDef*> _commands;
Bobty 2:9d8793c23b46 114 std::vector<RdFileCacheEntry*> _cachedFiles;
Bobty 1:75bb184de749 115 bool extractCmdArgs(char* buf, char* pCmdStr, int maxCmdStrLen, char* pArgStr, int maxArgStrLen);
Bobty 4:6afb3bbf20a4 116 char* _pBaseWebFolder;
Bobty 6:46285c519af2 117 char _httpHeader[HTTPD_MAX_HDR_LENGTH+1];
Bobty 6:46285c519af2 118 char _buffer[HTTPD_MAX_REQ_LENGTH+1];
Bobty 1:75bb184de749 119
Bobty 6:46285c519af2 120 void handleLocalFileRequest(char* cmdStr, char* argStr, TCPSocketConnection &client, char* httpHeader, bool bCacheIfPossible);
Bobty 6:46285c519af2 121 void handleSDFileRequest(char* cmdStr, char* argStr, TCPSocketConnection &client, char* httpHeader);
Bobty 2:9d8793c23b46 122 void handleCGIRequest(char* pUriStr, char* pQueryStr);
Bobty 6:46285c519af2 123 void sendFromCache(RdFileCacheEntry* pCacheEntry, TCPSocketConnection &client, char* httpHeader);
Bobty 6:46285c519af2 124 void handleReceivedHttp(TCPSocketConnection &client);
Bobty 2:9d8793c23b46 125
Bobty 0:b5b4d07f7827 126 };
Bobty 0:b5b4d07f7827 127
Bobty 0:b5b4d07f7827 128 #endif