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 8:de915bd70ec1, committed 2015-02-20
- Comitter:
- Bobty
- Date:
- Fri Feb 20 08:53:08 2015 +0000
- Parent:
- 7:fe7c33f7fbb8
- Child:
- 9:35668248199b
- Commit message:
- Reinstated some code I'd missed out when changing to run in a thread. The code handles timeouts on the HTTP connection.
Changed in this revision
| RdWebServer.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/RdWebServer.cpp Sat Feb 07 10:34:09 2015 +0000
+++ b/RdWebServer.cpp Fri Feb 20 08:53:08 2015 +0000
@@ -68,15 +68,15 @@
char argStr[MAX_ARGSTR_LEN];
if (extractCmdArgs(_buffer+3, cmdStr, MAX_CMDSTR_LEN, argStr, MAX_ARGSTR_LEN))
{
- pc.printf("CmdStr %s\n\r", cmdStr);
- pc.printf("ArgStr %s\n\r", argStr);
+// pc.printf("CmdStr %s\n\r", cmdStr);
+// pc.printf("ArgStr %s\n\r", argStr);
bool cmdFound = false;
for (std::vector<RdWebServerCmdDef*>::iterator it = _commands.begin() ; it != _commands.end(); ++it)
{
// pc.printf("Testing <<%s>> with <<%s>>\r\n", (*it)->_pCmdStr, cmdStr);
if (strcasecmp((*it)->_pCmdStr, cmdStr) == 0)
{
- pc.printf("FoundCmd <%s> Type %d\n\r", cmdStr, (*it)->_cmdType);
+// pc.printf("FoundCmd <%s> Type %d\n\r", cmdStr, (*it)->_cmdType);
cmdFound = true;
if ((*it)->_cmdType == RdWebServerCmdDef::CMD_CALLBACK)
{
@@ -108,10 +108,11 @@
void RdWebServer::run()
{
+ TCPSocketConnection client;
+ Timer connectLimitTimer;
- while (1)
+ while (isListening())
{
- TCPSocketConnection client;
pc.printf("Waiting for TCP connection\r\n");
if(_socketSrv.accept(client)<0)
{
@@ -119,33 +120,55 @@
}
else
{
+ client.set_blocking(false, 1000);
pc.printf("Connection from IP: %s\n\r",client.get_address());
if (_pStatusLed != NULL)
*_pStatusLed = true;
-
- // Get received data
- int rxLen = client.receive(_buffer, HTTPD_MAX_REQ_LENGTH);
- if (rxLen == -1)
+
+ connectLimitTimer.reset();
+ connectLimitTimer.start();
+ while(true)
{
- continue;
- }
- else if (rxLen == 0)
- {
- pc.printf("received buffer is empty.\n\r");
- continue;
+ // Check connection timer - 10 seconds timeout on HTTP operation
+ if (connectLimitTimer.read() >= 10)
+ {
+ pc.printf("Connection timed out\n\r");
+ break;
+ }
+ // Get received data
+ int rxLen = client.receive(_buffer, HTTPD_MAX_REQ_LENGTH);
+ if (rxLen == -1)
+ {
+ continue;
+ }
+ else if (rxLen == 0)
+ {
+ pc.printf("received buffer is empty.\n\r");
+ continue;
+ }
+ else if (rxLen > HTTPD_MAX_REQ_LENGTH)
+ {
+ sprintf(_httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
+ client.send(_httpHeader,strlen(_httpHeader));
+ client.send(_buffer, rxLen);
+ continue;
+ }
+ _buffer[rxLen] = '\0';
+
+ // Handle buffer
+ handleReceivedHttp(client);
+
+ // Done
+ break;
}
- else if (rxLen > HTTPD_MAX_REQ_LENGTH)
- {
- sprintf(_httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
- client.send(_httpHeader,strlen(_httpHeader));
- client.send(_buffer, rxLen);
- continue;
- }
- _buffer[rxLen] = '\0';
-
- // Handle buffer
- handleReceivedHttp(client);
+
+ // Connection now closed
+ printf("Connection closed ...\r\n");
+ client.close();
+ if (_pStatusLed != NULL)
+ *_pStatusLed = false;
}
+
}
}
@@ -159,12 +182,12 @@
const int HTTPD_MAX_FNAME_LENGTH = 127;
char filename[HTTPD_MAX_FNAME_LENGTH+1];
- pc.printf("Requesting file %s\n\r", inFileName);
+// pc.printf("Requesting file %s\n\r", inFileName);
char *lstchr = strrchr(inFileName, NULL) -1;
if ('/' == *lstchr)
{
- pc.printf("Request file %s%s\n", _pBaseWebFolder, inFileName);
+ pc.printf("Request directory %s%s\n", _pBaseWebFolder, inFileName);
*lstchr = 0;
sprintf(filename, "%s%s", _pBaseWebFolder, inFileName);
DIR *d = opendir(filename);