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 Aug 31 08:54:17 2015 +0000
Revision:
23:0fc3d7b5e596
Parent:
21:2dfb56648b93
Child:
24:27800de38eab
Changed blocking options to default non-blocking; Detected OPTIONS HTTP method; Changed to always use keep-alive

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bobty 20:e6c7db867593 1 // RdWebServer - Simple Web Server for MBED
Bobty 20:e6c7db867593 2 // Copyright (C) Rob Dobson 2013-2015, MIT License
Bobty 20:e6c7db867593 3 // Inspired by Jasper Schuurmans multi-threaded web server for Netduino which now seems to have gone from ...
Bobty 20:e6c7db867593 4 // http://www.schuurmans.cc/multi-threaded-web-server-for-netduino-plus
Bobty 20:e6c7db867593 5 // More details at http://robdobson.com/2013/10/moving-my-window-shades-control-to-mbed/
Bobty 20:e6c7db867593 6
Bobty 0:b5b4d07f7827 7 #ifndef RD_WEB_SERVER
Bobty 0:b5b4d07f7827 8 #define RD_WEB_SERVER
Bobty 0:b5b4d07f7827 9
Bobty 1:75bb184de749 10 #include <vector>
Bobty 0:b5b4d07f7827 11
Bobty 0:b5b4d07f7827 12 #include "mbed.h"
Bobty 0:b5b4d07f7827 13 #include "EthernetInterface.h"
Bobty 0:b5b4d07f7827 14
Bobty 18:5de680c4cfcb 15 // Debug level
Bobty 12:c14ffd4ec125 16 #ifdef RDWEB_DEBUG
Bobty 12:c14ffd4ec125 17 #if (RDWEB_DEBUG > 3)
Bobty 11:cec51b430b20 18 #define RD_DBG(x, ...) std::printf("[RD_DBG: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
Bobty 11:cec51b430b20 19 #else
Bobty 11:cec51b430b20 20 #define RD_DBG(x, ...)
Bobty 11:cec51b430b20 21 #endif
Bobty 12:c14ffd4ec125 22 #else
Bobty 12:c14ffd4ec125 23 #define RD_DBG(x, ...)
Bobty 12:c14ffd4ec125 24 #endif
Bobty 12:c14ffd4ec125 25
Bobty 12:c14ffd4ec125 26 #ifdef RDWEB_DEBUG
Bobty 12:c14ffd4ec125 27 #if (RDWEB_DEBUG > 2)
Bobty 11:cec51b430b20 28 #define RD_INFO(x, ...) std::printf("[RD_INFO: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
Bobty 11:cec51b430b20 29 #else
Bobty 11:cec51b430b20 30 #define RD_INFO(x, ...)
Bobty 11:cec51b430b20 31 #endif
Bobty 12:c14ffd4ec125 32 #else
Bobty 12:c14ffd4ec125 33 #define RD_INFO(x, ...)
Bobty 12:c14ffd4ec125 34 #endif
Bobty 12:c14ffd4ec125 35
Bobty 12:c14ffd4ec125 36 #ifdef RDWEB_DEBUG
Bobty 12:c14ffd4ec125 37 #if (RDWEB_DEBUG > 1)
Bobty 11:cec51b430b20 38 #define RD_WARN(x, ...) std::printf("[RD_WARNING: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
Bobty 11:cec51b430b20 39 #else
Bobty 11:cec51b430b20 40 #define RD_WARN(x, ...)
Bobty 11:cec51b430b20 41 #endif
Bobty 12:c14ffd4ec125 42 #else
Bobty 12:c14ffd4ec125 43 #define RD_WARN(x, ...)
Bobty 12:c14ffd4ec125 44 #endif
Bobty 12:c14ffd4ec125 45
Bobty 12:c14ffd4ec125 46 #ifdef RDWEB_DEBUG
Bobty 12:c14ffd4ec125 47 #if (RDWEB_DEBUG > 0)
Bobty 11:cec51b430b20 48 #define RD_ERR(x, ...) std::printf("[RD_ERR: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
Bobty 11:cec51b430b20 49 #else
Bobty 11:cec51b430b20 50 #define RD_ERR(x, ...)
Bobty 11:cec51b430b20 51 #endif
Bobty 12:c14ffd4ec125 52 #else
Bobty 12:c14ffd4ec125 53 #define RD_ERR(x, ...)
Bobty 12:c14ffd4ec125 54 #endif
Bobty 12:c14ffd4ec125 55
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 2:9d8793c23b46 60 class RdFileCacheEntry
Bobty 2:9d8793c23b46 61 {
Bobty 2:9d8793c23b46 62 public:
Bobty 2:9d8793c23b46 63 RdFileCacheEntry(char* pFileName)
Bobty 2:9d8793c23b46 64 {
Bobty 2:9d8793c23b46 65 _bCacheValid = false;
Bobty 17:080f2bed8b36 66 strncpy(_fileName, pFileName, SUBST_MAX_FNAME_LEN-1);
Bobty 17:080f2bed8b36 67 _fileName[SUBST_MAX_FNAME_LEN-1] = '\0';
Bobty 2:9d8793c23b46 68 _pFileContent = NULL;
Bobty 2:9d8793c23b46 69 }
Bobty 2:9d8793c23b46 70 ~RdFileCacheEntry()
Bobty 2:9d8793c23b46 71 {
Bobty 2:9d8793c23b46 72 delete _pFileContent;
Bobty 2:9d8793c23b46 73 }
Bobty 2:9d8793c23b46 74 bool readLocalFileIntoCache(char* fileName);
Bobty 2:9d8793c23b46 75 public:
Bobty 2:9d8793c23b46 76 bool _bCacheValid;
Bobty 17:080f2bed8b36 77 char _fileName[SUBST_MAX_FNAME_LEN];
Bobty 2:9d8793c23b46 78 char* _pFileContent;
Bobty 2:9d8793c23b46 79 int _nFileLen;
Bobty 2:9d8793c23b46 80 };
Bobty 2:9d8793c23b46 81
Bobty 19:f67ac231b570 82 typedef char* (*CmdCallbackType)(int method, char*cmdStr, char* argStr, char* msgBuffer);
Bobty 1:75bb184de749 83
Bobty 1:75bb184de749 84 class RdWebServerCmdDef
Bobty 1:75bb184de749 85 {
Bobty 1:75bb184de749 86 public:
Bobty 2:9d8793c23b46 87 static const int CMD_LOCALFILE = 1;
Bobty 2:9d8793c23b46 88 static const int CMD_CALLBACK = 2;
Bobty 4:6afb3bbf20a4 89 static const int CMD_SDORUSBFILE = 3;
Bobty 17:080f2bed8b36 90 RdWebServerCmdDef(char* pStr, int cmdType, CmdCallbackType callback, char* substFileName = NULL, bool bCacheIfPossible = false)
Bobty 1:75bb184de749 91 {
Bobty 1:75bb184de749 92 _pCmdStr = pStr;
Bobty 2:9d8793c23b46 93 _cmdType = cmdType;
Bobty 1:75bb184de749 94 _callback = callback;
Bobty 17:080f2bed8b36 95 _substFileName[0] = '\0';
Bobty 17:080f2bed8b36 96 if (substFileName != NULL)
Bobty 2:9d8793c23b46 97 {
Bobty 17:080f2bed8b36 98 strncpy(_substFileName, substFileName, SUBST_MAX_FNAME_LEN);
Bobty 17:080f2bed8b36 99 _substFileName[SUBST_MAX_FNAME_LEN-1] = '\0';
Bobty 2:9d8793c23b46 100 }
Bobty 2:9d8793c23b46 101 _bCacheIfPossible = bCacheIfPossible;
Bobty 1:75bb184de749 102 };
Bobty 1:75bb184de749 103 char* _pCmdStr;
Bobty 2:9d8793c23b46 104 int _cmdType;
Bobty 1:75bb184de749 105 CmdCallbackType _callback;
Bobty 17:080f2bed8b36 106 char _substFileName[SUBST_MAX_FNAME_LEN];
Bobty 2:9d8793c23b46 107 bool _bCacheIfPossible;
Bobty 1:75bb184de749 108 };
Bobty 1:75bb184de749 109
Bobty 6:46285c519af2 110 const int HTTPD_MAX_HDR_LENGTH = 255;
Bobty 23:0fc3d7b5e596 111 // The FRDM-K64F has more memory than the LPC1768 so allow a larger buffer
Bobty 19:f67ac231b570 112 #ifdef MCU_MK64F12
Bobty 19:f67ac231b570 113 const int HTTPD_MAX_REQ_LENGTH = 2048;
Bobty 19:f67ac231b570 114 #warning("TCP Request Length 2048")
Bobty 19:f67ac231b570 115 #else
Bobty 19:f67ac231b570 116 const int HTTPD_MAX_REQ_LENGTH = 1024;
Bobty 19:f67ac231b570 117 #warning("TCP Request Length 1024")
Bobty 19:f67ac231b570 118 #endif
Bobty 6:46285c519af2 119
Bobty 0:b5b4d07f7827 120 class RdWebServer
Bobty 0:b5b4d07f7827 121 {
Bobty 0:b5b4d07f7827 122 public :
Bobty 7:fe7c33f7fbb8 123 static const int METHOD_OTHER = 0;
Bobty 7:fe7c33f7fbb8 124 static const int METHOD_GET = 1;
Bobty 7:fe7c33f7fbb8 125 static const int METHOD_POST = 2;
Bobty 23:0fc3d7b5e596 126 static const int METHOD_OPTIONS = 3;
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 21:2dfb56648b93 134 static unsigned char* getPayloadDataFromMsg(char* msgBuf);
Bobty 21:2dfb56648b93 135 static int getPayloadLengthFromMsg(char* msgBuf);
Bobty 19:f67ac231b570 136
Bobty 0:b5b4d07f7827 137 private :
Bobty 0:b5b4d07f7827 138 int _port;
Bobty 0:b5b4d07f7827 139 DigitalOut* _pStatusLed;
Bobty 15:0865fa4b046a 140 TCPSocketServer _serverSocket;
Bobty 15:0865fa4b046a 141 bool _initOk;
Bobty 1:75bb184de749 142 std::vector<RdWebServerCmdDef*> _commands;
Bobty 2:9d8793c23b46 143 std::vector<RdFileCacheEntry*> _cachedFiles;
Bobty 1:75bb184de749 144 bool extractCmdArgs(char* buf, char* pCmdStr, int maxCmdStrLen, char* pArgStr, int maxArgStrLen);
Bobty 4:6afb3bbf20a4 145 char* _pBaseWebFolder;
Bobty 6:46285c519af2 146 char _httpHeader[HTTPD_MAX_HDR_LENGTH+1];
Bobty 6:46285c519af2 147 char _buffer[HTTPD_MAX_REQ_LENGTH+1];
Bobty 1:75bb184de749 148
Bobty 17:080f2bed8b36 149 bool handleLocalFileRequest(char* cmdStr, char* argStr, TCPSocketConnection &client, bool bCacheIfPossible);
Bobty 17:080f2bed8b36 150 bool handleSDFileRequest(char* cmdStr, char* argStr, TCPSocketConnection &client);
Bobty 2:9d8793c23b46 151 void handleCGIRequest(char* pUriStr, char* pQueryStr);
Bobty 17:080f2bed8b36 152 void sendFromCache(RdFileCacheEntry* pCacheEntry, TCPSocketConnection &client);
Bobty 14:4b83670854f0 153 bool handleReceivedHttp(TCPSocketConnection &client);
Bobty 17:080f2bed8b36 154 void formHTTPHeader(const char* rsltCode, const char* contentType, int contentLen);
Bobty 17:080f2bed8b36 155
Bobty 17:080f2bed8b36 156 // Settings - see the constructor
Bobty 17:080f2bed8b36 157 bool _blockingOnAccept;
Bobty 17:080f2bed8b36 158 bool _blockingOnReceive;
Bobty 17:080f2bed8b36 159 int _timeoutOnBlocking;
Bobty 17:080f2bed8b36 160 bool _closeConnAfterSend;
Bobty 17:080f2bed8b36 161 bool _closeConnOnReceiveFail;
Bobty 2:9d8793c23b46 162
Bobty 0:b5b4d07f7827 163 };
Bobty 0:b5b4d07f7827 164
Bobty 0:b5b4d07f7827 165 #endif