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:
- 6:46285c519af2
- Parent:
- 4:6afb3bbf20a4
- Child:
- 7:fe7c33f7fbb8
--- a/RdWebServer.h Tue Feb 03 12:08:12 2015 +0000 +++ b/RdWebServer.h Fri Feb 06 14:17:47 2015 +0000 @@ -10,6 +10,8 @@ #include "mbed.h" #include "EthernetInterface.h" +extern RawSerial pc; + class RdFileCacheEntry { public: @@ -33,7 +35,7 @@ int _nFileLen; }; -typedef void (*CmdCallbackType)(char*cmdStr, char* argStr); +typedef char* (*CmdCallbackType)(char*cmdStr, char* argStr); class RdWebServerCmdDef { @@ -61,6 +63,9 @@ bool _bCacheIfPossible; }; +const int HTTPD_MAX_HDR_LENGTH = 255; +const int HTTPD_MAX_REQ_LENGTH = 1023; + class RdWebServer { public : @@ -86,11 +91,14 @@ std::vector<RdFileCacheEntry*> _cachedFiles; bool extractCmdArgs(char* buf, char* pCmdStr, int maxCmdStrLen, char* pArgStr, int maxArgStrLen); char* _pBaseWebFolder; + char _httpHeader[HTTPD_MAX_HDR_LENGTH+1]; + char _buffer[HTTPD_MAX_REQ_LENGTH+1]; - void handleLocalFileRequest(char* cmdStr, char* argStr, TCPSocketConnection* pClient, char* httpHeader, bool bCacheIfPossible); - void handleSDFileRequest(char* cmdStr, char* argStr, TCPSocketConnection* pClient, char* httpHeader); + void handleLocalFileRequest(char* cmdStr, char* argStr, TCPSocketConnection &client, char* httpHeader, bool bCacheIfPossible); + void handleSDFileRequest(char* cmdStr, char* argStr, TCPSocketConnection &client, char* httpHeader); void handleCGIRequest(char* pUriStr, char* pQueryStr); - void sendFromCache(RdFileCacheEntry* pCacheEntry, TCPSocketConnection* pClient, char* httpHeader); + void sendFromCache(RdFileCacheEntry* pCacheEntry, TCPSocketConnection &client, char* httpHeader); + void handleReceivedHttp(TCPSocketConnection &client); };