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.
Revision 21:2dfb56648b93, committed 2015-08-20
- Comitter:
- Bobty
- Date:
- Thu Aug 20 07:40:20 2015 +0000
- Parent:
- 20:e6c7db867593
- Child:
- 22:598a21373539
- Commit message:
- Now defaults to support local file system
Changed in this revision
| RdWebServer.cpp | Show annotated file Show diff for this revision Revisions of this file |
| RdWebServer.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/RdWebServer.cpp Fri Aug 14 11:52:16 2015 +0000
+++ b/RdWebServer.cpp Thu Aug 20 07:40:20 2015 +0000
@@ -5,11 +5,11 @@
// More details at http://robdobson.com/2013/10/moving-my-window-shades-control-to-mbed/
// Setting RDWEB_DEBUG to 4 causes all debugging to be shown
-#define RDWEB_DEBUG 4
+#define RDWEB_DEBUG 2
// Change the settings below to support a local file system (not available on some MBEDs)
-//#define SUPPORT_LOCAL_FILESYSTEM 1
-//#define SUPPORT_LOCAL_FILE_CACHE 1
+#define SUPPORT_LOCAL_FILESYSTEM 1
+#define SUPPORT_LOCAL_FILE_CACHE 1
// Change this to support display of files on the server
//#define SUPPORT_FOLDER_VIEW 1
@@ -377,7 +377,7 @@
{
if ((*it)->_bCacheValid)
{
- sendFromCache(*it, clientSocketConn, httpHeader);
+ sendFromCache(*it, clientSocketConn);
return true;
}
bTryToCache = false; // don't try to cache as cacheing must have already failed
@@ -395,7 +395,7 @@
_cachedFiles.push_back(pCacheEntry);
if (bCacheSuccess)
{
- sendFromCache(pCacheEntry, clientSocketConn, httpHeader);
+ sendFromCache(pCacheEntry, clientSocketConn);
return true;
}
}
@@ -460,11 +460,12 @@
RD_WARN("Failed to open file");
return false;
}
- RD_DBG("Seeking");
+ RD_DBG("Seeking ");
fseek(fp, 0, SEEK_END);
_nFileLen = (int)ftell(fp);
+ RD_DBG("Len %d ", _nFileLen);
_pFileContent = new char[_nFileLen];
- RD_DBG("Len %d Buf %08x", _nFileLen, _pFileContent);
+ RD_DBG("Buf %08x ", _pFileContent);
if (!_pFileContent)
{
RD_WARN("Failed to allocate %lu", _nFileLen);
@@ -548,10 +549,23 @@
return true;
}
-char* RdWebServer::getPayloadDataFromMsg(char* msgBuf)
+unsigned char* RdWebServer::getPayloadDataFromMsg(char* msgBuf)
{
char* ptr = strstr(msgBuf, "\r\n\r\n");
if (ptr)
- return ptr+4;
- return "\0";
+ return (unsigned char*) (ptr+4);
+ return (unsigned char*) "\0";
}
+
+int RdWebServer::getPayloadLengthFromMsg(char* msgBuf)
+{
+ char* ptr = strstr(msgBuf, "Content-Length:");
+ if (ptr)
+ {
+ ptr += 15;
+ int contentLen = atoi(ptr);
+ if (contentLen >= 0)
+ return contentLen;
+ }
+ return 0;
+}
--- a/RdWebServer.h Fri Aug 14 11:52:16 2015 +0000
+++ b/RdWebServer.h Thu Aug 20 07:40:20 2015 +0000
@@ -129,7 +129,8 @@
void run();
void addCommand(char* pCmdStr, int cmdType, CmdCallbackType callback = NULL, char* substFileName = NULL, bool cacheIfPossible = false);
- static char* getPayloadDataFromMsg(char* msgBuf);
+ static unsigned char* getPayloadDataFromMsg(char* msgBuf);
+ static int getPayloadLengthFromMsg(char* msgBuf);
private :
int _port;