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.
Diff: RdWebServer.h
- Revision:
- 17:080f2bed8b36
- Parent:
- 15:0865fa4b046a
- Child:
- 18:5de680c4cfcb
diff -r 0248bbfdb6c1 -r 080f2bed8b36 RdWebServer.h --- a/RdWebServer.h Tue May 05 22:14:16 2015 +0000 +++ b/RdWebServer.h Mon May 11 11:17:15 2015 +0000 @@ -53,6 +53,7 @@ //#define SUPPORT_LOCAL_FILESYSTEM 1 //#define SUPPORT_FOLDER_VIEW 1 //#define SUPPORT_LOCAL_FILE_CACHE 1 +const int SUBST_MAX_FNAME_LEN = 40; // note local files on MBED are 8.3 but this might include other files extern RawSerial pc; @@ -61,12 +62,11 @@ class RdFileCacheEntry { public: - static const int CACHE_MAX_FNAME_LEN = 15; // note local files on MBED are 8.3 RdFileCacheEntry(char* pFileName) { _bCacheValid = false; - strncpy(_fileName, pFileName, CACHE_MAX_FNAME_LEN-1); - _fileName[CACHE_MAX_FNAME_LEN-1] = '\0'; + strncpy(_fileName, pFileName, SUBST_MAX_FNAME_LEN-1); + _fileName[SUBST_MAX_FNAME_LEN-1] = '\0'; _pFileContent = NULL; } ~RdFileCacheEntry() @@ -76,7 +76,7 @@ bool readLocalFileIntoCache(char* fileName); public: bool _bCacheValid; - char _fileName[CACHE_MAX_FNAME_LEN]; + char _fileName[SUBST_MAX_FNAME_LEN]; char* _pFileContent; int _nFileLen; }; @@ -91,27 +91,23 @@ static const int CMD_LOCALFILE = 1; static const int CMD_CALLBACK = 2; static const int CMD_SDORUSBFILE = 3; - RdWebServerCmdDef(char* pStr, int cmdType, CmdCallbackType callback, char* subst8dot3FileName = NULL, bool bCacheIfPossible = false) + RdWebServerCmdDef(char* pStr, int cmdType, CmdCallbackType callback, char* substFileName = NULL, bool bCacheIfPossible = false) { _pCmdStr = pStr; _cmdType = cmdType; _callback = callback; -#ifdef SUPPORT_LOCAL_FILE_CACHE - _subst8dot3FileName[0] = '\0'; - if (subst8dot3FileName != NULL) + _substFileName[0] = '\0'; + if (substFileName != NULL) { - strncpy(_subst8dot3FileName, subst8dot3FileName, RdFileCacheEntry::CACHE_MAX_FNAME_LEN); - _subst8dot3FileName[RdFileCacheEntry::CACHE_MAX_FNAME_LEN-1] = '\0'; + strncpy(_substFileName, substFileName, SUBST_MAX_FNAME_LEN); + _substFileName[SUBST_MAX_FNAME_LEN-1] = '\0'; } -#endif _bCacheIfPossible = bCacheIfPossible; }; char* _pCmdStr; int _cmdType; CmdCallbackType _callback; -#ifdef SUPPORT_LOCAL_FILE_CACHE - char _subst8dot3FileName[RdFileCacheEntry::CACHE_MAX_FNAME_LEN]; -#endif + char _substFileName[SUBST_MAX_FNAME_LEN]; bool _bCacheIfPossible; }; @@ -145,13 +141,21 @@ char _httpHeader[HTTPD_MAX_HDR_LENGTH+1]; char _buffer[HTTPD_MAX_REQ_LENGTH+1]; - void handleLocalFileRequest(char* cmdStr, char* argStr, TCPSocketConnection &client, char* httpHeader, bool bCacheIfPossible); - bool handleSDFileRequest(char* cmdStr, char* argStr, TCPSocketConnection &client, char* httpHeader); + bool handleLocalFileRequest(char* cmdStr, char* argStr, TCPSocketConnection &client, bool bCacheIfPossible); + bool handleSDFileRequest(char* cmdStr, char* argStr, TCPSocketConnection &client); void handleCGIRequest(char* pUriStr, char* pQueryStr); #ifdef SUPPORT_LOCAL_FILE_CACHE - void sendFromCache(RdFileCacheEntry* pCacheEntry, TCPSocketConnection &client, char* httpHeader); + void sendFromCache(RdFileCacheEntry* pCacheEntry, TCPSocketConnection &client); #endif bool handleReceivedHttp(TCPSocketConnection &client); + void formHTTPHeader(const char* rsltCode, const char* contentType, int contentLen); + + // Settings - see the constructor + bool _blockingOnAccept; + bool _blockingOnReceive; + int _timeoutOnBlocking; + bool _closeConnAfterSend; + bool _closeConnOnReceiveFail; };