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.cpp@0:b5b4d07f7827, 2013-09-18 (annotated)
- Committer:
- Bobty
- Date:
- Wed Sep 18 21:36:50 2013 +0000
- Revision:
- 0:b5b4d07f7827
- Child:
- 1:75bb184de749
Initial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bobty | 0:b5b4d07f7827 | 1 | #include "RdWebServer.h" |
Bobty | 0:b5b4d07f7827 | 2 | |
Bobty | 0:b5b4d07f7827 | 3 | RdWebServer::RdWebServer() |
Bobty | 0:b5b4d07f7827 | 4 | { |
Bobty | 0:b5b4d07f7827 | 5 | _serverIsListening = false; |
Bobty | 0:b5b4d07f7827 | 6 | _pStatusLed = NULL; |
Bobty | 0:b5b4d07f7827 | 7 | } |
Bobty | 0:b5b4d07f7827 | 8 | |
Bobty | 0:b5b4d07f7827 | 9 | RdWebServer::~RdWebServer() |
Bobty | 0:b5b4d07f7827 | 10 | { |
Bobty | 0:b5b4d07f7827 | 11 | } |
Bobty | 0:b5b4d07f7827 | 12 | |
Bobty | 0:b5b4d07f7827 | 13 | bool RdWebServer::init(int port, DigitalOut* pStatusLed) |
Bobty | 0:b5b4d07f7827 | 14 | { |
Bobty | 0:b5b4d07f7827 | 15 | _port = port; |
Bobty | 0:b5b4d07f7827 | 16 | _pStatusLed = pStatusLed; |
Bobty | 0:b5b4d07f7827 | 17 | |
Bobty | 0:b5b4d07f7827 | 18 | // _socketSrv.set_blocking(true); |
Bobty | 0:b5b4d07f7827 | 19 | |
Bobty | 0:b5b4d07f7827 | 20 | |
Bobty | 0:b5b4d07f7827 | 21 | //setup tcp socket |
Bobty | 0:b5b4d07f7827 | 22 | if(_socketSrv.bind(port)< 0) |
Bobty | 0:b5b4d07f7827 | 23 | { |
Bobty | 0:b5b4d07f7827 | 24 | printf("tcp server bind failed.\n\r"); |
Bobty | 0:b5b4d07f7827 | 25 | return false; |
Bobty | 0:b5b4d07f7827 | 26 | } |
Bobty | 0:b5b4d07f7827 | 27 | else |
Bobty | 0:b5b4d07f7827 | 28 | { |
Bobty | 0:b5b4d07f7827 | 29 | printf("tcp server bind successed.\n\r"); |
Bobty | 0:b5b4d07f7827 | 30 | _serverIsListening = true; |
Bobty | 0:b5b4d07f7827 | 31 | } |
Bobty | 0:b5b4d07f7827 | 32 | |
Bobty | 0:b5b4d07f7827 | 33 | if(_socketSrv.listen(1) < 0) |
Bobty | 0:b5b4d07f7827 | 34 | { |
Bobty | 0:b5b4d07f7827 | 35 | printf("tcp server listen failed.\n\r"); |
Bobty | 0:b5b4d07f7827 | 36 | return false; |
Bobty | 0:b5b4d07f7827 | 37 | } |
Bobty | 0:b5b4d07f7827 | 38 | else |
Bobty | 0:b5b4d07f7827 | 39 | { |
Bobty | 0:b5b4d07f7827 | 40 | printf("tcp server is listening...\n\r"); |
Bobty | 0:b5b4d07f7827 | 41 | } |
Bobty | 0:b5b4d07f7827 | 42 | |
Bobty | 0:b5b4d07f7827 | 43 | return true; |
Bobty | 0:b5b4d07f7827 | 44 | } |
Bobty | 0:b5b4d07f7827 | 45 | |
Bobty | 0:b5b4d07f7827 | 46 | |
Bobty | 0:b5b4d07f7827 | 47 | void RdWebServer::run() |
Bobty | 0:b5b4d07f7827 | 48 | { |
Bobty | 0:b5b4d07f7827 | 49 | TCPSocketConnection client; |
Bobty | 0:b5b4d07f7827 | 50 | bool clientIsConnected = false; |
Bobty | 0:b5b4d07f7827 | 51 | |
Bobty | 0:b5b4d07f7827 | 52 | //listening for http GET request |
Bobty | 0:b5b4d07f7827 | 53 | while (isListening()) |
Bobty | 0:b5b4d07f7827 | 54 | { |
Bobty | 0:b5b4d07f7827 | 55 | //blocking mode(never timeout) |
Bobty | 0:b5b4d07f7827 | 56 | if(_socketSrv.accept(client)<0) |
Bobty | 0:b5b4d07f7827 | 57 | { |
Bobty | 0:b5b4d07f7827 | 58 | printf("failed to accept connection.\n\r"); |
Bobty | 0:b5b4d07f7827 | 59 | } |
Bobty | 0:b5b4d07f7827 | 60 | else |
Bobty | 0:b5b4d07f7827 | 61 | { |
Bobty | 0:b5b4d07f7827 | 62 | printf("connection success!\n\rIP: %s\n\r",client.get_address()); |
Bobty | 0:b5b4d07f7827 | 63 | clientIsConnected = true; |
Bobty | 0:b5b4d07f7827 | 64 | if (_pStatusLed != NULL) |
Bobty | 0:b5b4d07f7827 | 65 | *_pStatusLed = true; |
Bobty | 0:b5b4d07f7827 | 66 | |
Bobty | 0:b5b4d07f7827 | 67 | while(clientIsConnected) |
Bobty | 0:b5b4d07f7827 | 68 | { |
Bobty | 0:b5b4d07f7827 | 69 | char buffer[1024] = {}; |
Bobty | 0:b5b4d07f7827 | 70 | switch(client.receive(buffer, 1023)) |
Bobty | 0:b5b4d07f7827 | 71 | { |
Bobty | 0:b5b4d07f7827 | 72 | case 0: |
Bobty | 0:b5b4d07f7827 | 73 | printf("recieved buffer is empty.\n\r"); |
Bobty | 0:b5b4d07f7827 | 74 | clientIsConnected = false; |
Bobty | 0:b5b4d07f7827 | 75 | break; |
Bobty | 0:b5b4d07f7827 | 76 | case -1: |
Bobty | 0:b5b4d07f7827 | 77 | printf("failed to read data from client.\n\r"); |
Bobty | 0:b5b4d07f7827 | 78 | clientIsConnected = false; |
Bobty | 0:b5b4d07f7827 | 79 | break; |
Bobty | 0:b5b4d07f7827 | 80 | default: |
Bobty | 0:b5b4d07f7827 | 81 | printf("Recieved Data: %d\n\r\n\r%.*s\n\r",strlen(buffer),strlen(buffer),buffer); |
Bobty | 0:b5b4d07f7827 | 82 | if(buffer[0] == 'G' && buffer[1] == 'E' && buffer[2] == 'T' ) |
Bobty | 0:b5b4d07f7827 | 83 | { |
Bobty | 0:b5b4d07f7827 | 84 | printf("GET request incomming.\n\r"); |
Bobty | 0:b5b4d07f7827 | 85 | |
Bobty | 0:b5b4d07f7827 | 86 | |
Bobty | 0:b5b4d07f7827 | 87 | //setup http response header & data |
Bobty | 0:b5b4d07f7827 | 88 | char echoHeader[256] = {}; |
Bobty | 0:b5b4d07f7827 | 89 | sprintf(echoHeader,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r\n\r",strlen(buffer)); |
Bobty | 0:b5b4d07f7827 | 90 | client.send(echoHeader,strlen(echoHeader)); |
Bobty | 0:b5b4d07f7827 | 91 | client.send(buffer,strlen(buffer)); |
Bobty | 0:b5b4d07f7827 | 92 | clientIsConnected = false; |
Bobty | 0:b5b4d07f7827 | 93 | printf("echo back done.\n\r"); |
Bobty | 0:b5b4d07f7827 | 94 | } |
Bobty | 0:b5b4d07f7827 | 95 | break; |
Bobty | 0:b5b4d07f7827 | 96 | } |
Bobty | 0:b5b4d07f7827 | 97 | } |
Bobty | 0:b5b4d07f7827 | 98 | printf("close connection.\n\rtcp server is listening...\n\r"); |
Bobty | 0:b5b4d07f7827 | 99 | client.close(); |
Bobty | 0:b5b4d07f7827 | 100 | if (_pStatusLed != NULL) |
Bobty | 0:b5b4d07f7827 | 101 | *_pStatusLed = false; |
Bobty | 0:b5b4d07f7827 | 102 | } |
Bobty | 0:b5b4d07f7827 | 103 | } |
Bobty | 0:b5b4d07f7827 | 104 | } |
Bobty | 0:b5b4d07f7827 | 105 |