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.
RdWebServer.h@6:46285c519af2, 2015-02-06 (annotated)
- Committer:
- Bobty
- Date:
- Fri Feb 06 14:17:47 2015 +0000
- Revision:
- 6:46285c519af2
- Parent:
- 4:6afb3bbf20a4
- Child:
- 7:fe7c33f7fbb8
Working with threaded server and SD card stack extended to stop crashes
Who changed what in which revision?
| User | Revision | Line number | New 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 | 6:46285c519af2 | 13 | extern RawSerial pc; |
| Bobty | 6:46285c519af2 | 14 | |
| Bobty | 2:9d8793c23b46 | 15 | class RdFileCacheEntry |
| Bobty | 2:9d8793c23b46 | 16 | { |
| Bobty | 2:9d8793c23b46 | 17 | public: |
| Bobty | 2:9d8793c23b46 | 18 | static const int CACHE_MAX_FNAME_LEN = 15; // note local files on MBED are 8.3 |
| Bobty | 2:9d8793c23b46 | 19 | RdFileCacheEntry(char* pFileName) |
| Bobty | 2:9d8793c23b46 | 20 | { |
| Bobty | 2:9d8793c23b46 | 21 | _bCacheValid = false; |
| Bobty | 2:9d8793c23b46 | 22 | strncpy(_fileName, pFileName, CACHE_MAX_FNAME_LEN-1); |
| Bobty | 2:9d8793c23b46 | 23 | _fileName[CACHE_MAX_FNAME_LEN-1] = '\0'; |
| Bobty | 2:9d8793c23b46 | 24 | _pFileContent = NULL; |
| Bobty | 2:9d8793c23b46 | 25 | } |
| Bobty | 2:9d8793c23b46 | 26 | ~RdFileCacheEntry() |
| Bobty | 2:9d8793c23b46 | 27 | { |
| Bobty | 2:9d8793c23b46 | 28 | delete _pFileContent; |
| Bobty | 2:9d8793c23b46 | 29 | } |
| Bobty | 2:9d8793c23b46 | 30 | bool readLocalFileIntoCache(char* fileName); |
| Bobty | 2:9d8793c23b46 | 31 | public: |
| Bobty | 2:9d8793c23b46 | 32 | bool _bCacheValid; |
| Bobty | 2:9d8793c23b46 | 33 | char _fileName[CACHE_MAX_FNAME_LEN]; |
| Bobty | 2:9d8793c23b46 | 34 | char* _pFileContent; |
| Bobty | 2:9d8793c23b46 | 35 | int _nFileLen; |
| Bobty | 2:9d8793c23b46 | 36 | }; |
| Bobty | 2:9d8793c23b46 | 37 | |
| Bobty | 6:46285c519af2 | 38 | typedef char* (*CmdCallbackType)(char*cmdStr, char* argStr); |
| Bobty | 1:75bb184de749 | 39 | |
| Bobty | 1:75bb184de749 | 40 | class RdWebServerCmdDef |
| Bobty | 1:75bb184de749 | 41 | { |
| Bobty | 1:75bb184de749 | 42 | public: |
| Bobty | 2:9d8793c23b46 | 43 | static const int CMD_LOCALFILE = 1; |
| Bobty | 2:9d8793c23b46 | 44 | static const int CMD_CALLBACK = 2; |
| Bobty | 4:6afb3bbf20a4 | 45 | static const int CMD_SDORUSBFILE = 3; |
| Bobty | 2:9d8793c23b46 | 46 | RdWebServerCmdDef(char* pStr, int cmdType, CmdCallbackType callback, char* substFileName, bool bCacheIfPossible) |
| Bobty | 1:75bb184de749 | 47 | { |
| Bobty | 1:75bb184de749 | 48 | _pCmdStr = pStr; |
| Bobty | 2:9d8793c23b46 | 49 | _cmdType = cmdType; |
| Bobty | 1:75bb184de749 | 50 | _callback = callback; |
| Bobty | 2:9d8793c23b46 | 51 | _substFileName[0] = '\0'; |
| Bobty | 2:9d8793c23b46 | 52 | if (substFileName != NULL) |
| Bobty | 2:9d8793c23b46 | 53 | { |
| Bobty | 2:9d8793c23b46 | 54 | strncpy(_substFileName, substFileName, RdFileCacheEntry::CACHE_MAX_FNAME_LEN); |
| Bobty | 2:9d8793c23b46 | 55 | _substFileName[RdFileCacheEntry::CACHE_MAX_FNAME_LEN-1] = '\0'; |
| Bobty | 2:9d8793c23b46 | 56 | } |
| Bobty | 2:9d8793c23b46 | 57 | _bCacheIfPossible = bCacheIfPossible; |
| Bobty | 1:75bb184de749 | 58 | }; |
| Bobty | 1:75bb184de749 | 59 | char* _pCmdStr; |
| Bobty | 2:9d8793c23b46 | 60 | int _cmdType; |
| Bobty | 1:75bb184de749 | 61 | CmdCallbackType _callback; |
| Bobty | 2:9d8793c23b46 | 62 | char _substFileName[RdFileCacheEntry::CACHE_MAX_FNAME_LEN]; |
| Bobty | 2:9d8793c23b46 | 63 | bool _bCacheIfPossible; |
| Bobty | 1:75bb184de749 | 64 | }; |
| Bobty | 1:75bb184de749 | 65 | |
| Bobty | 6:46285c519af2 | 66 | const int HTTPD_MAX_HDR_LENGTH = 255; |
| Bobty | 6:46285c519af2 | 67 | const int HTTPD_MAX_REQ_LENGTH = 1023; |
| Bobty | 6:46285c519af2 | 68 | |
| Bobty | 0:b5b4d07f7827 | 69 | class RdWebServer |
| Bobty | 0:b5b4d07f7827 | 70 | { |
| Bobty | 0:b5b4d07f7827 | 71 | public : |
| Bobty | 0:b5b4d07f7827 | 72 | |
| Bobty | 0:b5b4d07f7827 | 73 | RdWebServer(); |
| Bobty | 0:b5b4d07f7827 | 74 | virtual ~RdWebServer(); |
| Bobty | 0:b5b4d07f7827 | 75 | |
| Bobty | 4:6afb3bbf20a4 | 76 | bool init(int port, DigitalOut* pStatusLed, char* pBaseWebFolder); |
| Bobty | 0:b5b4d07f7827 | 77 | void run(); |
| Bobty | 0:b5b4d07f7827 | 78 | bool isListening() |
| Bobty | 0:b5b4d07f7827 | 79 | { |
| Bobty | 0:b5b4d07f7827 | 80 | return _serverIsListening; |
| Bobty | 0:b5b4d07f7827 | 81 | }; |
| Bobty | 0:b5b4d07f7827 | 82 | |
| Bobty | 2:9d8793c23b46 | 83 | void addCommand(char* pCmdStr, int cmdType, CmdCallbackType callback = NULL, char* substFileName = NULL, bool cacheIfPossible = false); |
| Bobty | 1:75bb184de749 | 84 | |
| Bobty | 0:b5b4d07f7827 | 85 | private : |
| Bobty | 0:b5b4d07f7827 | 86 | int _port; |
| Bobty | 0:b5b4d07f7827 | 87 | DigitalOut* _pStatusLed; |
| Bobty | 0:b5b4d07f7827 | 88 | TCPSocketServer _socketSrv; |
| Bobty | 0:b5b4d07f7827 | 89 | bool _serverIsListening; |
| Bobty | 1:75bb184de749 | 90 | std::vector<RdWebServerCmdDef*> _commands; |
| Bobty | 2:9d8793c23b46 | 91 | std::vector<RdFileCacheEntry*> _cachedFiles; |
| Bobty | 1:75bb184de749 | 92 | bool extractCmdArgs(char* buf, char* pCmdStr, int maxCmdStrLen, char* pArgStr, int maxArgStrLen); |
| Bobty | 4:6afb3bbf20a4 | 93 | char* _pBaseWebFolder; |
| Bobty | 6:46285c519af2 | 94 | char _httpHeader[HTTPD_MAX_HDR_LENGTH+1]; |
| Bobty | 6:46285c519af2 | 95 | char _buffer[HTTPD_MAX_REQ_LENGTH+1]; |
| Bobty | 1:75bb184de749 | 96 | |
| Bobty | 6:46285c519af2 | 97 | void handleLocalFileRequest(char* cmdStr, char* argStr, TCPSocketConnection &client, char* httpHeader, bool bCacheIfPossible); |
| Bobty | 6:46285c519af2 | 98 | void handleSDFileRequest(char* cmdStr, char* argStr, TCPSocketConnection &client, char* httpHeader); |
| Bobty | 2:9d8793c23b46 | 99 | void handleCGIRequest(char* pUriStr, char* pQueryStr); |
| Bobty | 6:46285c519af2 | 100 | void sendFromCache(RdFileCacheEntry* pCacheEntry, TCPSocketConnection &client, char* httpHeader); |
| Bobty | 6:46285c519af2 | 101 | void handleReceivedHttp(TCPSocketConnection &client); |
| Bobty | 2:9d8793c23b46 | 102 | |
| Bobty | 0:b5b4d07f7827 | 103 | }; |
| Bobty | 0:b5b4d07f7827 | 104 | |
| Bobty | 0:b5b4d07f7827 | 105 | #endif |