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 Feb 08 14:08:42 2016 +0000
Revision:
29:46998f2e458f
Parent:
28:99036ff32459
Fixed RTOS namespace issue

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 0:b5b4d07f7827 10 #include "mbed.h"
Bobty 0:b5b4d07f7827 11
Bobty 18:5de680c4cfcb 12 // Debug level
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 24:27800de38eab 53 // Length of strings
Bobty 24:27800de38eab 54 const int MAX_CMDSTR_LEN = 100;
Bobty 24:27800de38eab 55 const int MAX_ARGSTR_LEN = 100;
Bobty 28:99036ff32459 56 const int MAX_WEB_SERVER_CMDS = 30;
Bobty 28:99036ff32459 57 const int MAX_WEB_SERVER_CACHED_FILES = 5;
Bobty 17:080f2bed8b36 58 const int SUBST_MAX_FNAME_LEN = 40; // note local files on MBED are 8.3 but this might include other files
Bobty 11:cec51b430b20 59
Bobty 6:46285c519af2 60 extern RawSerial pc;
Bobty 6:46285c519af2 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 24:27800de38eab 84 typedef char* (*CmdCallbackType)(int method, char*cmdStr, char* argStr, char* msgBuffer, int msgLen,
Bobty 24:27800de38eab 85 int contentLen, unsigned char* pPayload, int payloadLen, int splitPayloadPos);
Bobty 1:75bb184de749 86
Bobty 1:75bb184de749 87 class RdWebServerCmdDef
Bobty 1:75bb184de749 88 {
Bobty 1:75bb184de749 89 public:
Bobty 2:9d8793c23b46 90 static const int CMD_LOCALFILE = 1;
Bobty 2:9d8793c23b46 91 static const int CMD_CALLBACK = 2;
Bobty 4:6afb3bbf20a4 92 static const int CMD_SDORUSBFILE = 3;
Bobty 17:080f2bed8b36 93 RdWebServerCmdDef(char* pStr, int cmdType, CmdCallbackType callback, char* substFileName = NULL, bool bCacheIfPossible = false)
Bobty 1:75bb184de749 94 {
Bobty 1:75bb184de749 95 _pCmdStr = pStr;
Bobty 2:9d8793c23b46 96 _cmdType = cmdType;
Bobty 1:75bb184de749 97 _callback = callback;
Bobty 17:080f2bed8b36 98 _substFileName[0] = '\0';
Bobty 17:080f2bed8b36 99 if (substFileName != NULL)
Bobty 2:9d8793c23b46 100 {
Bobty 17:080f2bed8b36 101 strncpy(_substFileName, substFileName, SUBST_MAX_FNAME_LEN);
Bobty 17:080f2bed8b36 102 _substFileName[SUBST_MAX_FNAME_LEN-1] = '\0';
Bobty 2:9d8793c23b46 103 }
Bobty 2:9d8793c23b46 104 _bCacheIfPossible = bCacheIfPossible;
Bobty 1:75bb184de749 105 };
Bobty 1:75bb184de749 106 char* _pCmdStr;
Bobty 2:9d8793c23b46 107 int _cmdType;
Bobty 1:75bb184de749 108 CmdCallbackType _callback;
Bobty 17:080f2bed8b36 109 char _substFileName[SUBST_MAX_FNAME_LEN];
Bobty 2:9d8793c23b46 110 bool _bCacheIfPossible;
Bobty 1:75bb184de749 111 };
Bobty 1:75bb184de749 112
Bobty 6:46285c519af2 113 const int HTTPD_MAX_HDR_LENGTH = 255;
Bobty 23:0fc3d7b5e596 114 // The FRDM-K64F has more memory than the LPC1768 so allow a larger buffer
Bobty 28:99036ff32459 115 #if defined(MCU_MK64F12)
Bobty 19:f67ac231b570 116 const int HTTPD_MAX_REQ_LENGTH = 2048;
Bobty 19:f67ac231b570 117 #warning("TCP Request Length 2048")
Bobty 28:99036ff32459 118 #elif defined(TARGET_ARCH_BLE)
Bobty 28:99036ff32459 119 const int HTTPD_MAX_REQ_LENGTH = 1024;
Bobty 28:99036ff32459 120 #warning("TCP Request Length 1024")
Bobty 19:f67ac231b570 121 #else
Bobty 19:f67ac231b570 122 const int HTTPD_MAX_REQ_LENGTH = 1024;
Bobty 19:f67ac231b570 123 #warning("TCP Request Length 1024")
Bobty 19:f67ac231b570 124 #endif
Bobty 6:46285c519af2 125
Bobty 28:99036ff32459 126 class TCPSocketServer;
Bobty 28:99036ff32459 127 class TCPSocketConnection;
Bobty 29:46998f2e458f 128 class rtos::Mutex;
Bobty 28:99036ff32459 129
Bobty 0:b5b4d07f7827 130 class RdWebServer
Bobty 0:b5b4d07f7827 131 {
Bobty 0:b5b4d07f7827 132 public :
Bobty 7:fe7c33f7fbb8 133 static const int METHOD_OTHER = 0;
Bobty 7:fe7c33f7fbb8 134 static const int METHOD_GET = 1;
Bobty 7:fe7c33f7fbb8 135 static const int METHOD_POST = 2;
Bobty 23:0fc3d7b5e596 136 static const int METHOD_OPTIONS = 3;
Bobty 29:46998f2e458f 137 RdWebServer(TCPSocketServer& tcpServerSocket, rtos::Mutex* pSdCardMutex = NULL);
Bobty 0:b5b4d07f7827 138 virtual ~RdWebServer();
Bobty 0:b5b4d07f7827 139
Bobty 4:6afb3bbf20a4 140 bool init(int port, DigitalOut* pStatusLed, char* pBaseWebFolder);
Bobty 0:b5b4d07f7827 141 void run();
Bobty 2:9d8793c23b46 142 void addCommand(char* pCmdStr, int cmdType, CmdCallbackType callback = NULL, char* substFileName = NULL, bool cacheIfPossible = false);
Bobty 1:75bb184de749 143
Bobty 24:27800de38eab 144 static unsigned char* getPayloadDataFromMsg(char* msgBuf, int msgLen, int& payloadLen);
Bobty 24:27800de38eab 145 static int getContentLengthFromMsg(char* msgBuf);
Bobty 19:f67ac231b570 146
Bobty 0:b5b4d07f7827 147 private :
Bobty 0:b5b4d07f7827 148 int _port;
Bobty 0:b5b4d07f7827 149 DigitalOut* _pStatusLed;
Bobty 28:99036ff32459 150 TCPSocketServer& _serverSocket;
Bobty 15:0865fa4b046a 151 bool _initOk;
Bobty 28:99036ff32459 152 RdWebServerCmdDef* _pWebServerCmds[MAX_WEB_SERVER_CMDS];
Bobty 28:99036ff32459 153 int _numWebServerCmds;
Bobty 28:99036ff32459 154 RdFileCacheEntry* _pWebServerCachedFiles[MAX_WEB_SERVER_CACHED_FILES];
Bobty 28:99036ff32459 155 int _numWebServerCachedFiles;
Bobty 24:27800de38eab 156 bool extractCmdArgs(char* buf, char* pCmdStr, int maxCmdStrLen, char* pArgStr, int maxArgStrLen, int& contentLen);
Bobty 4:6afb3bbf20a4 157 char* _pBaseWebFolder;
Bobty 6:46285c519af2 158 char _httpHeader[HTTPD_MAX_HDR_LENGTH+1];
Bobty 6:46285c519af2 159 char _buffer[HTTPD_MAX_REQ_LENGTH+1];
Bobty 24:27800de38eab 160 int _bufferReceivedLen;
Bobty 1:75bb184de749 161
Bobty 17:080f2bed8b36 162 bool handleLocalFileRequest(char* cmdStr, char* argStr, TCPSocketConnection &client, bool bCacheIfPossible);
Bobty 17:080f2bed8b36 163 bool handleSDFileRequest(char* cmdStr, char* argStr, TCPSocketConnection &client);
Bobty 2:9d8793c23b46 164 void handleCGIRequest(char* pUriStr, char* pQueryStr);
Bobty 17:080f2bed8b36 165 void sendFromCache(RdFileCacheEntry* pCacheEntry, TCPSocketConnection &client);
Bobty 14:4b83670854f0 166 bool handleReceivedHttp(TCPSocketConnection &client);
Bobty 17:080f2bed8b36 167 void formHTTPHeader(const char* rsltCode, const char* contentType, int contentLen);
Bobty 17:080f2bed8b36 168
Bobty 17:080f2bed8b36 169 // Settings - see the constructor
Bobty 17:080f2bed8b36 170 bool _blockingOnAccept;
Bobty 17:080f2bed8b36 171 bool _blockingOnReceive;
Bobty 17:080f2bed8b36 172 int _timeoutOnBlocking;
Bobty 17:080f2bed8b36 173 bool _closeConnAfterSend;
Bobty 17:080f2bed8b36 174 bool _closeConnOnReceiveFail;
Bobty 24:27800de38eab 175
Bobty 24:27800de38eab 176 // Handling of split payloads on receipt (e.g. POST)
Bobty 24:27800de38eab 177 int _curSplitPayloadPos;
Bobty 24:27800de38eab 178 char _curCmdStr[MAX_CMDSTR_LEN];
Bobty 24:27800de38eab 179 char _curArgStr[MAX_ARGSTR_LEN];
Bobty 24:27800de38eab 180 int _curContentLen;
Bobty 24:27800de38eab 181 int _curHttpMethod;
Bobty 24:27800de38eab 182 RdWebServerCmdDef* _curWebServerCmdDef;
Bobty 26:3c4c10e989b1 183
Bobty 26:3c4c10e989b1 184 // Mutex for SD card access (if supplied in constructor)
Bobty 26:3c4c10e989b1 185 Mutex* _pSdCardMutex;
Bobty 0:b5b4d07f7827 186 };
Bobty 0:b5b4d07f7827 187
Bobty 0:b5b4d07f7827 188 #endif