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@6:46285c519af2, 2015-02-06 (annotated)
- Committer:
- Bobty
- Date:
- Fri Feb 06 14:17:47 2015 +0000
- Revision:
- 6:46285c519af2
- Parent:
- 5:e6286b529d6b
- 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.cpp |
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 | More details at http://robdobson.com/2013/10/moving-my-window-shades-control-to-mbed/ |
Bobty | 3:594136d34a32 | 5 | */ |
Bobty | 3:594136d34a32 | 6 | |
Bobty | 0:b5b4d07f7827 | 7 | #include "RdWebServer.h" |
Bobty | 0:b5b4d07f7827 | 8 | |
Bobty | 1:75bb184de749 | 9 | #define MAX_CMDSTR_LEN 100 |
Bobty | 1:75bb184de749 | 10 | #define MAX_ARGSTR_LEN 100 |
Bobty | 1:75bb184de749 | 11 | |
Bobty | 0:b5b4d07f7827 | 12 | RdWebServer::RdWebServer() |
Bobty | 0:b5b4d07f7827 | 13 | { |
Bobty | 0:b5b4d07f7827 | 14 | _serverIsListening = false; |
Bobty | 0:b5b4d07f7827 | 15 | _pStatusLed = NULL; |
Bobty | 0:b5b4d07f7827 | 16 | } |
Bobty | 0:b5b4d07f7827 | 17 | |
Bobty | 0:b5b4d07f7827 | 18 | RdWebServer::~RdWebServer() |
Bobty | 0:b5b4d07f7827 | 19 | { |
Bobty | 2:9d8793c23b46 | 20 | // Clean-up - probably never called as we're on a microcontroller! |
Bobty | 2:9d8793c23b46 | 21 | for (std::vector<RdWebServerCmdDef*>::iterator it = _commands.begin() ; it != _commands.end(); ++it) |
Bobty | 2:9d8793c23b46 | 22 | delete (*it); |
Bobty | 0:b5b4d07f7827 | 23 | } |
Bobty | 0:b5b4d07f7827 | 24 | |
Bobty | 4:6afb3bbf20a4 | 25 | bool RdWebServer::init(int port, DigitalOut* pStatusLed, char* pBaseWebFolder) |
Bobty | 0:b5b4d07f7827 | 26 | { |
Bobty | 0:b5b4d07f7827 | 27 | _port = port; |
Bobty | 0:b5b4d07f7827 | 28 | _pStatusLed = pStatusLed; |
Bobty | 4:6afb3bbf20a4 | 29 | _pBaseWebFolder = pBaseWebFolder; |
Bobty | 0:b5b4d07f7827 | 30 | |
Bobty | 6:46285c519af2 | 31 | _socketSrv.set_blocking(true); |
Bobty | 0:b5b4d07f7827 | 32 | |
Bobty | 0:b5b4d07f7827 | 33 | //setup tcp socket |
Bobty | 0:b5b4d07f7827 | 34 | if(_socketSrv.bind(port)< 0) |
Bobty | 0:b5b4d07f7827 | 35 | { |
Bobty | 6:46285c519af2 | 36 | pc.printf("TCP server bind fail\n\r"); |
Bobty | 0:b5b4d07f7827 | 37 | return false; |
Bobty | 0:b5b4d07f7827 | 38 | } |
Bobty | 0:b5b4d07f7827 | 39 | else |
Bobty | 0:b5b4d07f7827 | 40 | { |
Bobty | 6:46285c519af2 | 41 | // pc.printf("TCP server bind success\n\r"); |
Bobty | 0:b5b4d07f7827 | 42 | _serverIsListening = true; |
Bobty | 0:b5b4d07f7827 | 43 | } |
Bobty | 0:b5b4d07f7827 | 44 | |
Bobty | 0:b5b4d07f7827 | 45 | if(_socketSrv.listen(1) < 0) |
Bobty | 0:b5b4d07f7827 | 46 | { |
Bobty | 6:46285c519af2 | 47 | pc.printf("TCP server listen fail\n\r"); |
Bobty | 0:b5b4d07f7827 | 48 | return false; |
Bobty | 0:b5b4d07f7827 | 49 | } |
Bobty | 0:b5b4d07f7827 | 50 | else |
Bobty | 0:b5b4d07f7827 | 51 | { |
Bobty | 6:46285c519af2 | 52 | pc.printf("TCP server is listening...\r\n"); |
Bobty | 6:46285c519af2 | 53 | } |
Bobty | 6:46285c519af2 | 54 | |
Bobty | 6:46285c519af2 | 55 | return true; |
Bobty | 6:46285c519af2 | 56 | } |
Bobty | 6:46285c519af2 | 57 | |
Bobty | 6:46285c519af2 | 58 | void RdWebServer::handleReceivedHttp(TCPSocketConnection &client) |
Bobty | 6:46285c519af2 | 59 | { |
Bobty | 6:46285c519af2 | 60 | pc.printf("Received Data: %d\n\r\n\r%.*s\n\r",strlen(_buffer),strlen(_buffer),_buffer); |
Bobty | 6:46285c519af2 | 61 | if (strncmp(_buffer, "GET ", 4) == 0) |
Bobty | 6:46285c519af2 | 62 | { |
Bobty | 6:46285c519af2 | 63 | pc.printf("GET request\n\r"); |
Bobty | 6:46285c519af2 | 64 | char cmdStr[MAX_CMDSTR_LEN]; |
Bobty | 6:46285c519af2 | 65 | char argStr[MAX_ARGSTR_LEN]; |
Bobty | 6:46285c519af2 | 66 | if (extractCmdArgs(_buffer+3, cmdStr, MAX_CMDSTR_LEN, argStr, MAX_ARGSTR_LEN)) |
Bobty | 6:46285c519af2 | 67 | { |
Bobty | 6:46285c519af2 | 68 | pc.printf("CmdStr %s\n\r", cmdStr); |
Bobty | 6:46285c519af2 | 69 | pc.printf("ArgStr %s\n\r", argStr); |
Bobty | 6:46285c519af2 | 70 | bool cmdFound = false; |
Bobty | 6:46285c519af2 | 71 | for (std::vector<RdWebServerCmdDef*>::iterator it = _commands.begin() ; it != _commands.end(); ++it) |
Bobty | 6:46285c519af2 | 72 | { |
Bobty | 6:46285c519af2 | 73 | // pc.printf("Testing <<%s>> with <<%s>>\r\n", (*it)->_pCmdStr, cmdStr); |
Bobty | 6:46285c519af2 | 74 | if (strcasecmp((*it)->_pCmdStr, cmdStr) == 0) |
Bobty | 6:46285c519af2 | 75 | { |
Bobty | 6:46285c519af2 | 76 | pc.printf("FoundCmd <%s> Type %d\n\r", cmdStr, (*it)->_cmdType); |
Bobty | 6:46285c519af2 | 77 | cmdFound = true; |
Bobty | 6:46285c519af2 | 78 | if ((*it)->_cmdType == RdWebServerCmdDef::CMD_CALLBACK) |
Bobty | 6:46285c519af2 | 79 | { |
Bobty | 6:46285c519af2 | 80 | char* respStr = ((*it)->_callback)(cmdStr, argStr); |
Bobty | 6:46285c519af2 | 81 | client.send(respStr, strlen(respStr)); |
Bobty | 6:46285c519af2 | 82 | } |
Bobty | 6:46285c519af2 | 83 | else if ((*it)->_cmdType == RdWebServerCmdDef::CMD_LOCALFILE) |
Bobty | 6:46285c519af2 | 84 | { |
Bobty | 6:46285c519af2 | 85 | if ((*it)->_substFileName[0] != '\0') |
Bobty | 6:46285c519af2 | 86 | handleLocalFileRequest((*it)->_substFileName, argStr, client, _httpHeader, (*it)->_bCacheIfPossible); |
Bobty | 6:46285c519af2 | 87 | else |
Bobty | 6:46285c519af2 | 88 | handleLocalFileRequest(cmdStr, argStr, client, _httpHeader, (*it)->_bCacheIfPossible); |
Bobty | 6:46285c519af2 | 89 | } |
Bobty | 6:46285c519af2 | 90 | else if ((*it)->_cmdType == RdWebServerCmdDef::CMD_SDORUSBFILE) |
Bobty | 6:46285c519af2 | 91 | { |
Bobty | 6:46285c519af2 | 92 | if ((*it)->_substFileName[0] != '\0') |
Bobty | 6:46285c519af2 | 93 | handleSDFileRequest((*it)->_substFileName, argStr, client, _httpHeader); |
Bobty | 6:46285c519af2 | 94 | else |
Bobty | 6:46285c519af2 | 95 | handleSDFileRequest(cmdStr, argStr, client, _httpHeader); |
Bobty | 6:46285c519af2 | 96 | } |
Bobty | 6:46285c519af2 | 97 | break; |
Bobty | 6:46285c519af2 | 98 | } |
Bobty | 6:46285c519af2 | 99 | } |
Bobty | 6:46285c519af2 | 100 | // If command not found see if it is a local file |
Bobty | 6:46285c519af2 | 101 | if (!cmdFound) |
Bobty | 6:46285c519af2 | 102 | handleSDFileRequest(cmdStr, argStr, client, _httpHeader); |
Bobty | 6:46285c519af2 | 103 | } |
Bobty | 6:46285c519af2 | 104 | } |
Bobty | 6:46285c519af2 | 105 | } |
Bobty | 6:46285c519af2 | 106 | |
Bobty | 6:46285c519af2 | 107 | void RdWebServer::run() |
Bobty | 6:46285c519af2 | 108 | { |
Bobty | 6:46285c519af2 | 109 | |
Bobty | 6:46285c519af2 | 110 | while (1) |
Bobty | 6:46285c519af2 | 111 | { |
Bobty | 6:46285c519af2 | 112 | TCPSocketConnection client; |
Bobty | 6:46285c519af2 | 113 | pc.printf("Waiting for TCP connection\r\n"); |
Bobty | 6:46285c519af2 | 114 | if(_socketSrv.accept(client)<0) |
Bobty | 6:46285c519af2 | 115 | { |
Bobty | 6:46285c519af2 | 116 | pc.printf("TCP Socket failed to accept connection\n\r"); |
Bobty | 6:46285c519af2 | 117 | } |
Bobty | 6:46285c519af2 | 118 | else |
Bobty | 6:46285c519af2 | 119 | { |
Bobty | 6:46285c519af2 | 120 | pc.printf("Connection from IP: %s\n\r",client.get_address()); |
Bobty | 6:46285c519af2 | 121 | if (_pStatusLed != NULL) |
Bobty | 6:46285c519af2 | 122 | *_pStatusLed = true; |
Bobty | 6:46285c519af2 | 123 | |
Bobty | 6:46285c519af2 | 124 | // Get received data |
Bobty | 6:46285c519af2 | 125 | int rxLen = client.receive(_buffer, HTTPD_MAX_REQ_LENGTH); |
Bobty | 6:46285c519af2 | 126 | if (rxLen == -1) |
Bobty | 6:46285c519af2 | 127 | { |
Bobty | 6:46285c519af2 | 128 | continue; |
Bobty | 6:46285c519af2 | 129 | } |
Bobty | 6:46285c519af2 | 130 | else if (rxLen == 0) |
Bobty | 6:46285c519af2 | 131 | { |
Bobty | 6:46285c519af2 | 132 | pc.printf("received buffer is empty.\n\r"); |
Bobty | 6:46285c519af2 | 133 | continue; |
Bobty | 6:46285c519af2 | 134 | } |
Bobty | 6:46285c519af2 | 135 | else if (rxLen > HTTPD_MAX_REQ_LENGTH) |
Bobty | 6:46285c519af2 | 136 | { |
Bobty | 6:46285c519af2 | 137 | sprintf(_httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); |
Bobty | 6:46285c519af2 | 138 | client.send(_httpHeader,strlen(_httpHeader)); |
Bobty | 6:46285c519af2 | 139 | client.send(_buffer, rxLen); |
Bobty | 6:46285c519af2 | 140 | continue; |
Bobty | 6:46285c519af2 | 141 | } |
Bobty | 6:46285c519af2 | 142 | _buffer[rxLen] = '\0'; |
Bobty | 6:46285c519af2 | 143 | |
Bobty | 6:46285c519af2 | 144 | // Handle buffer |
Bobty | 6:46285c519af2 | 145 | handleReceivedHttp(client); |
Bobty | 6:46285c519af2 | 146 | // const char * msg = "Hello World\n\r\n\r"; |
Bobty | 6:46285c519af2 | 147 | // sprintf(_buffer,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text/html\n\rConnection: Close\n\r\n\r", strlen(msg)); |
Bobty | 6:46285c519af2 | 148 | // client.send(_buffer,strlen(_buffer)); |
Bobty | 6:46285c519af2 | 149 | // client.send( const_cast<char*>(msg), strlen(msg)); |
Bobty | 6:46285c519af2 | 150 | } |
Bobty | 0:b5b4d07f7827 | 151 | } |
Bobty | 0:b5b4d07f7827 | 152 | |
Bobty | 6:46285c519af2 | 153 | // while (1) |
Bobty | 6:46285c519af2 | 154 | // { |
Bobty | 6:46285c519af2 | 155 | // TCPSocketConnection Clnt; |
Bobty | 6:46285c519af2 | 156 | // printf("Accept\n"); |
Bobty | 6:46285c519af2 | 157 | // int ret = _socketSrv.accept(Clnt); |
Bobty | 6:46285c519af2 | 158 | // if ( ret < 0) { |
Bobty | 6:46285c519af2 | 159 | // printf("no connection\n"); |
Bobty | 6:46285c519af2 | 160 | // } else { |
Bobty | 6:46285c519af2 | 161 | // printf("Client (IP=%s) is connected\n", Clnt.get_address()); |
Bobty | 6:46285c519af2 | 162 | // char buff[512]; |
Bobty | 6:46285c519af2 | 163 | // int result = Clnt.receive(buff, 511); |
Bobty | 6:46285c519af2 | 164 | // if(result < 0 ) |
Bobty | 6:46285c519af2 | 165 | // printf("The horror\n "); |
Bobty | 6:46285c519af2 | 166 | // else { |
Bobty | 6:46285c519af2 | 167 | // if(result >= 511) |
Bobty | 6:46285c519af2 | 168 | // buff[511] = 0; |
Bobty | 6:46285c519af2 | 169 | // else |
Bobty | 6:46285c519af2 | 170 | // buff[result + 1] = 0; |
Bobty | 6:46285c519af2 | 171 | // |
Bobty | 6:46285c519af2 | 172 | // printf("%s\n",buff); |
Bobty | 6:46285c519af2 | 173 | // } |
Bobty | 6:46285c519af2 | 174 | // const char * msg = "Hello World\n\r\n\r"; |
Bobty | 6:46285c519af2 | 175 | // sprintf(buff,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text/html\n\rConnection: Close\n\r\n\r", strlen(msg)); |
Bobty | 6:46285c519af2 | 176 | // Clnt.send(buff,strlen(buff)); |
Bobty | 6:46285c519af2 | 177 | // Clnt.send( const_cast<char*>(msg), strlen(msg)); |
Bobty | 6:46285c519af2 | 178 | // } |
Bobty | 6:46285c519af2 | 179 | // } |
Bobty | 6:46285c519af2 | 180 | // |
Bobty | 6:46285c519af2 | 181 | // //listening for http GET request |
Bobty | 6:46285c519af2 | 182 | // while (isListening()) |
Bobty | 6:46285c519af2 | 183 | // { |
Bobty | 6:46285c519af2 | 184 | // //blocking mode(never timeout) |
Bobty | 6:46285c519af2 | 185 | // if(_socketSrv.accept(client)<0) |
Bobty | 6:46285c519af2 | 186 | // { |
Bobty | 6:46285c519af2 | 187 | // pc.printf("TCP Socket failed to accept connection\n\r"); |
Bobty | 6:46285c519af2 | 188 | // } |
Bobty | 6:46285c519af2 | 189 | // else |
Bobty | 6:46285c519af2 | 190 | // { |
Bobty | 6:46285c519af2 | 191 | // client.set_blocking(false, 1000); |
Bobty | 6:46285c519af2 | 192 | // pc.printf("Connection from IP: %s\n\r",client.get_address()); |
Bobty | 6:46285c519af2 | 193 | // if (_pStatusLed != NULL) |
Bobty | 6:46285c519af2 | 194 | // *_pStatusLed = true; |
Bobty | 6:46285c519af2 | 195 | // Timer connectLimitTimer; |
Bobty | 6:46285c519af2 | 196 | // connectLimitTimer.start(); |
Bobty | 6:46285c519af2 | 197 | // |
Bobty | 6:46285c519af2 | 198 | // while(connectLimitTimer.read() < 5) // 5 seconds timeout on HTTP operation |
Bobty | 6:46285c519af2 | 199 | // { |
Bobty | 6:46285c519af2 | 200 | // int rxLen = client.receive(buffer, 1023); |
Bobty | 6:46285c519af2 | 201 | // if (rxLen == -1) |
Bobty | 6:46285c519af2 | 202 | // { |
Bobty | 6:46285c519af2 | 203 | // continue; |
Bobty | 6:46285c519af2 | 204 | // } |
Bobty | 6:46285c519af2 | 205 | // else if (rxLen == 0) |
Bobty | 6:46285c519af2 | 206 | // { |
Bobty | 6:46285c519af2 | 207 | // pc.printf("received buffer is empty.\n\r"); |
Bobty | 6:46285c519af2 | 208 | // break; |
Bobty | 6:46285c519af2 | 209 | // } |
Bobty | 6:46285c519af2 | 210 | // else if (rxLen >= 1024) |
Bobty | 6:46285c519af2 | 211 | // { |
Bobty | 6:46285c519af2 | 212 | // sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); |
Bobty | 6:46285c519af2 | 213 | // client.send(httpHeader,strlen(httpHeader)); |
Bobty | 6:46285c519af2 | 214 | // client.send(buffer, rxLen); |
Bobty | 6:46285c519af2 | 215 | // break; |
Bobty | 6:46285c519af2 | 216 | // } |
Bobty | 6:46285c519af2 | 217 | // buffer[rxLen] = '\0'; |
Bobty | 6:46285c519af2 | 218 | //// pc.printf("Received Data: %d\n\r\n\r%.*s\n\r",strlen(buffer),strlen(buffer),buffer); |
Bobty | 6:46285c519af2 | 219 | // if (strncmp(buffer, "GET ", 4) == 0) |
Bobty | 6:46285c519af2 | 220 | // { |
Bobty | 6:46285c519af2 | 221 | //// pc.printf("GET request\n\r"); |
Bobty | 6:46285c519af2 | 222 | // char cmdStr[MAX_CMDSTR_LEN]; |
Bobty | 6:46285c519af2 | 223 | // char argStr[MAX_ARGSTR_LEN]; |
Bobty | 6:46285c519af2 | 224 | // if (extractCmdArgs(buffer+3, cmdStr, MAX_CMDSTR_LEN, argStr, MAX_ARGSTR_LEN)) |
Bobty | 6:46285c519af2 | 225 | // { |
Bobty | 6:46285c519af2 | 226 | // pc.printf("CmdStr %s\n\r", cmdStr); |
Bobty | 6:46285c519af2 | 227 | // pc.printf("ArgStr %s\n\r", argStr); |
Bobty | 6:46285c519af2 | 228 | // bool cmdFound = false; |
Bobty | 6:46285c519af2 | 229 | // for (std::vector<RdWebServerCmdDef*>::iterator it = _commands.begin() ; it != _commands.end(); ++it) |
Bobty | 6:46285c519af2 | 230 | // { |
Bobty | 6:46285c519af2 | 231 | //// pc.printf("Testing <<%s>> with <<%s>>\r\n", (*it)->_pCmdStr, cmdStr); |
Bobty | 6:46285c519af2 | 232 | // if (strcasecmp((*it)->_pCmdStr, cmdStr) == 0) |
Bobty | 6:46285c519af2 | 233 | // { |
Bobty | 6:46285c519af2 | 234 | // pc.printf("FoundCmd <%s> Type %d\n\r", cmdStr, (*it)->_cmdType); |
Bobty | 6:46285c519af2 | 235 | // cmdFound = true; |
Bobty | 6:46285c519af2 | 236 | // if ((*it)->_cmdType == RdWebServerCmdDef::CMD_CALLBACK) |
Bobty | 6:46285c519af2 | 237 | // { |
Bobty | 6:46285c519af2 | 238 | // char* respStr = ((*it)->_callback)(cmdStr, argStr); |
Bobty | 6:46285c519af2 | 239 | // client.send(respStr, strlen(respStr)); |
Bobty | 6:46285c519af2 | 240 | // } |
Bobty | 6:46285c519af2 | 241 | // else if ((*it)->_cmdType == RdWebServerCmdDef::CMD_LOCALFILE) |
Bobty | 6:46285c519af2 | 242 | // { |
Bobty | 6:46285c519af2 | 243 | // if ((*it)->_substFileName[0] != '\0') |
Bobty | 6:46285c519af2 | 244 | // handleLocalFileRequest((*it)->_substFileName, argStr, &client, httpHeader, (*it)->_bCacheIfPossible); |
Bobty | 6:46285c519af2 | 245 | // else |
Bobty | 6:46285c519af2 | 246 | // handleLocalFileRequest(cmdStr, argStr, &client, httpHeader, (*it)->_bCacheIfPossible); |
Bobty | 6:46285c519af2 | 247 | // } |
Bobty | 6:46285c519af2 | 248 | // else if ((*it)->_cmdType == RdWebServerCmdDef::CMD_SDORUSBFILE) |
Bobty | 6:46285c519af2 | 249 | // { |
Bobty | 6:46285c519af2 | 250 | // if ((*it)->_substFileName[0] != '\0') |
Bobty | 6:46285c519af2 | 251 | // handleSDFileRequest((*it)->_substFileName, argStr, &client, httpHeader); |
Bobty | 6:46285c519af2 | 252 | // else |
Bobty | 6:46285c519af2 | 253 | // handleSDFileRequest(cmdStr, argStr, &client, httpHeader); |
Bobty | 6:46285c519af2 | 254 | // } |
Bobty | 6:46285c519af2 | 255 | // break; |
Bobty | 6:46285c519af2 | 256 | // } |
Bobty | 6:46285c519af2 | 257 | // } |
Bobty | 6:46285c519af2 | 258 | // // If command not found see if it is a local file |
Bobty | 6:46285c519af2 | 259 | // if (!cmdFound) |
Bobty | 6:46285c519af2 | 260 | // handleLocalFileRequest(cmdStr, argStr, &client, httpHeader, false); |
Bobty | 6:46285c519af2 | 261 | // } |
Bobty | 6:46285c519af2 | 262 | // |
Bobty | 6:46285c519af2 | 263 | // break; |
Bobty | 6:46285c519af2 | 264 | // } |
Bobty | 6:46285c519af2 | 265 | // |
Bobty | 6:46285c519af2 | 266 | // } |
Bobty | 6:46285c519af2 | 267 | // pc.printf("Connection closed ... TCP server is listening...\r\n"); |
Bobty | 6:46285c519af2 | 268 | // client.close(); |
Bobty | 6:46285c519af2 | 269 | // if (_pStatusLed != NULL) |
Bobty | 6:46285c519af2 | 270 | // *_pStatusLed = false; |
Bobty | 6:46285c519af2 | 271 | // } |
Bobty | 6:46285c519af2 | 272 | // } |
Bobty | 6:46285c519af2 | 273 | } |
Bobty | 6:46285c519af2 | 274 | |
Bobty | 6:46285c519af2 | 275 | void RdWebServer::addCommand(char* pCmdStr, int cmdType, CmdCallbackType callback, char* substFileName, bool cacheIfPossible) |
Bobty | 6:46285c519af2 | 276 | { |
Bobty | 6:46285c519af2 | 277 | _commands.push_back(new RdWebServerCmdDef(pCmdStr, cmdType, callback, substFileName, cacheIfPossible)); |
Bobty | 6:46285c519af2 | 278 | } |
Bobty | 6:46285c519af2 | 279 | |
Bobty | 6:46285c519af2 | 280 | void RdWebServer::handleSDFileRequest(char* inFileName, char* argStr, TCPSocketConnection &client, char* httpHeader) |
Bobty | 6:46285c519af2 | 281 | { |
Bobty | 6:46285c519af2 | 282 | const int HTTPD_MAX_FNAME_LENGTH = 127; |
Bobty | 6:46285c519af2 | 283 | char filename[HTTPD_MAX_FNAME_LENGTH+1]; |
Bobty | 6:46285c519af2 | 284 | |
Bobty | 6:46285c519af2 | 285 | pc.printf("Requesting file %s\n\r", inFileName); |
Bobty | 6:46285c519af2 | 286 | |
Bobty | 6:46285c519af2 | 287 | char *lstchr = strrchr(inFileName, NULL) -1; |
Bobty | 6:46285c519af2 | 288 | if ('/' == *lstchr) |
Bobty | 6:46285c519af2 | 289 | { |
Bobty | 6:46285c519af2 | 290 | pc.printf("Request file %s%s\n", _pBaseWebFolder, inFileName); |
Bobty | 6:46285c519af2 | 291 | *lstchr = 0; |
Bobty | 6:46285c519af2 | 292 | sprintf(filename, "%s%s", _pBaseWebFolder, inFileName); |
Bobty | 6:46285c519af2 | 293 | DIR *d = opendir(filename); |
Bobty | 6:46285c519af2 | 294 | if (d != NULL) { |
Bobty | 6:46285c519af2 | 295 | sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n"); |
Bobty | 6:46285c519af2 | 296 | client.send(httpHeader,strlen(httpHeader)); |
Bobty | 6:46285c519af2 | 297 | sprintf(httpHeader,"<html><head><title>Directory Listing</title></head><body><h1>%s</h1><ul>", inFileName); |
Bobty | 6:46285c519af2 | 298 | client.send(httpHeader,strlen(httpHeader)); |
Bobty | 6:46285c519af2 | 299 | struct dirent *p; |
Bobty | 6:46285c519af2 | 300 | while((p = readdir(d)) != NULL) { |
Bobty | 6:46285c519af2 | 301 | pc.printf("%s\n", p->d_name); |
Bobty | 6:46285c519af2 | 302 | sprintf(httpHeader,"<li>%s</li>", p->d_name); |
Bobty | 6:46285c519af2 | 303 | client.send(httpHeader,strlen(httpHeader)); |
Bobty | 6:46285c519af2 | 304 | } |
Bobty | 6:46285c519af2 | 305 | } |
Bobty | 6:46285c519af2 | 306 | closedir(d); |
Bobty | 6:46285c519af2 | 307 | // pc.printf("Directory closed\n"); |
Bobty | 6:46285c519af2 | 308 | sprintf(httpHeader,"</ul></body></html>"); |
Bobty | 6:46285c519af2 | 309 | client.send(httpHeader,strlen(httpHeader)); |
Bobty | 6:46285c519af2 | 310 | } |
Bobty | 6:46285c519af2 | 311 | else |
Bobty | 6:46285c519af2 | 312 | { |
Bobty | 6:46285c519af2 | 313 | sprintf(filename, "%s%s", _pBaseWebFolder, inFileName); |
Bobty | 6:46285c519af2 | 314 | pc.printf ("Filename %s\r\n", filename); |
Bobty | 6:46285c519af2 | 315 | |
Bobty | 6:46285c519af2 | 316 | FILE* fp = fopen(filename, "r"); |
Bobty | 6:46285c519af2 | 317 | if (fp == NULL) |
Bobty | 6:46285c519af2 | 318 | { |
Bobty | 6:46285c519af2 | 319 | pc.printf ("Filename %s not found\r\n", filename); |
Bobty | 6:46285c519af2 | 320 | sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); |
Bobty | 6:46285c519af2 | 321 | client.send(httpHeader,strlen(httpHeader)); |
Bobty | 6:46285c519af2 | 322 | client.send(inFileName,strlen(inFileName)); |
Bobty | 6:46285c519af2 | 323 | } |
Bobty | 6:46285c519af2 | 324 | else |
Bobty | 6:46285c519af2 | 325 | { |
Bobty | 6:46285c519af2 | 326 | pc.printf ("Sending file %s\r\n", filename); |
Bobty | 6:46285c519af2 | 327 | sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n"); |
Bobty | 6:46285c519af2 | 328 | client.send(httpHeader,strlen(httpHeader)); |
Bobty | 6:46285c519af2 | 329 | int rdCnt = 0; |
Bobty | 6:46285c519af2 | 330 | char fileBuf[1024]; |
Bobty | 6:46285c519af2 | 331 | while ((rdCnt = fread(fileBuf, sizeof( char ), 1024, fp)) == 1024) |
Bobty | 6:46285c519af2 | 332 | { |
Bobty | 6:46285c519af2 | 333 | client.send(fileBuf, rdCnt); |
Bobty | 6:46285c519af2 | 334 | } |
Bobty | 6:46285c519af2 | 335 | client.send(fileBuf, rdCnt); |
Bobty | 6:46285c519af2 | 336 | fclose(fp); |
Bobty | 6:46285c519af2 | 337 | } |
Bobty | 6:46285c519af2 | 338 | } |
Bobty | 6:46285c519af2 | 339 | // FILE* fp = fopen("/sd/index.htm", "r"); |
Bobty | 6:46285c519af2 | 340 | // if (fp == NULL) |
Bobty | 6:46285c519af2 | 341 | // { |
Bobty | 6:46285c519af2 | 342 | // pc.printf ("Filename %s not found\r\n", inFileName); |
Bobty | 6:46285c519af2 | 343 | // } |
Bobty | 6:46285c519af2 | 344 | // else |
Bobty | 6:46285c519af2 | 345 | // { |
Bobty | 6:46285c519af2 | 346 | // pc.printf ("Sending file %s\r\n", inFileName); |
Bobty | 6:46285c519af2 | 347 | // fclose(fp); |
Bobty | 6:46285c519af2 | 348 | // } |
Bobty | 6:46285c519af2 | 349 | |
Bobty | 6:46285c519af2 | 350 | // const char * msg = "Hello World\n\r\n\r"; |
Bobty | 6:46285c519af2 | 351 | // sprintf(_buffer,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text/html\n\rConnection: Close\n\r\n\r", strlen(msg)); |
Bobty | 6:46285c519af2 | 352 | // client.send(_buffer,strlen(_buffer)); |
Bobty | 6:46285c519af2 | 353 | // client.send( const_cast<char*>(msg), strlen(msg)); |
Bobty | 6:46285c519af2 | 354 | } |
Bobty | 6:46285c519af2 | 355 | |
Bobty | 6:46285c519af2 | 356 | void RdWebServer::sendFromCache(RdFileCacheEntry* pCacheEntry, TCPSocketConnection &client, char* httpHeader) |
Bobty | 6:46285c519af2 | 357 | { |
Bobty | 6:46285c519af2 | 358 | pc.printf ("Sending file %s from cache %d bytes\r\n", pCacheEntry->_fileName, pCacheEntry->_nFileLen); |
Bobty | 6:46285c519af2 | 359 | sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n"); |
Bobty | 6:46285c519af2 | 360 | client.send(httpHeader,strlen(httpHeader)); |
Bobty | 6:46285c519af2 | 361 | char* pMem = pCacheEntry->_pFileContent; |
Bobty | 6:46285c519af2 | 362 | int nLenLeft = pCacheEntry->_nFileLen; |
Bobty | 6:46285c519af2 | 363 | int blkSize = 2048; |
Bobty | 6:46285c519af2 | 364 | while(nLenLeft > 0) |
Bobty | 6:46285c519af2 | 365 | { |
Bobty | 6:46285c519af2 | 366 | if (blkSize > nLenLeft) |
Bobty | 6:46285c519af2 | 367 | blkSize = nLenLeft; |
Bobty | 6:46285c519af2 | 368 | client.send_all(pMem, blkSize); |
Bobty | 6:46285c519af2 | 369 | pMem += blkSize; |
Bobty | 6:46285c519af2 | 370 | nLenLeft -= blkSize; |
Bobty | 6:46285c519af2 | 371 | } |
Bobty | 6:46285c519af2 | 372 | } |
Bobty | 6:46285c519af2 | 373 | |
Bobty | 6:46285c519af2 | 374 | void RdWebServer::handleLocalFileRequest(char* inFileName, char* argStr, TCPSocketConnection &client, char* httpHeader, bool bCacheIfPossible) |
Bobty | 6:46285c519af2 | 375 | { |
Bobty | 6:46285c519af2 | 376 | const int HTTPD_MAX_FNAME_LENGTH = 127; |
Bobty | 6:46285c519af2 | 377 | char localFilename[HTTPD_MAX_FNAME_LENGTH+1]; |
Bobty | 6:46285c519af2 | 378 | char reqFileNameStr[HTTPD_MAX_FNAME_LENGTH+1]; |
Bobty | 6:46285c519af2 | 379 | |
Bobty | 6:46285c519af2 | 380 | pc.printf("Requesting local file %s\n\r", inFileName); |
Bobty | 6:46285c519af2 | 381 | sprintf(reqFileNameStr, "/%s", inFileName); |
Bobty | 6:46285c519af2 | 382 | sprintf(localFilename, "/local/%s", inFileName); |
Bobty | 6:46285c519af2 | 383 | |
Bobty | 6:46285c519af2 | 384 | if (bCacheIfPossible) |
Bobty | 6:46285c519af2 | 385 | { |
Bobty | 6:46285c519af2 | 386 | // Check if file already cached |
Bobty | 6:46285c519af2 | 387 | bool bTryToCache = true; |
Bobty | 6:46285c519af2 | 388 | for (std::vector<RdFileCacheEntry*>::iterator it = _cachedFiles.begin() ; it != _cachedFiles.end(); ++it) |
Bobty | 6:46285c519af2 | 389 | { |
Bobty | 6:46285c519af2 | 390 | if (strcmp(inFileName, (*it)->_fileName) == 0) |
Bobty | 6:46285c519af2 | 391 | { |
Bobty | 6:46285c519af2 | 392 | if ((*it)->_bCacheValid) |
Bobty | 6:46285c519af2 | 393 | { |
Bobty | 6:46285c519af2 | 394 | sendFromCache(*it, client, httpHeader); |
Bobty | 6:46285c519af2 | 395 | return; |
Bobty | 6:46285c519af2 | 396 | } |
Bobty | 6:46285c519af2 | 397 | bTryToCache = false; // don't try to cache as cacheing must have already failed |
Bobty | 6:46285c519af2 | 398 | } |
Bobty | 6:46285c519af2 | 399 | } |
Bobty | 6:46285c519af2 | 400 | |
Bobty | 6:46285c519af2 | 401 | // See if we can cache the file |
Bobty | 6:46285c519af2 | 402 | if (bTryToCache) |
Bobty | 6:46285c519af2 | 403 | { |
Bobty | 6:46285c519af2 | 404 | RdFileCacheEntry* pCacheEntry = new RdFileCacheEntry(inFileName); |
Bobty | 6:46285c519af2 | 405 | if (pCacheEntry) |
Bobty | 6:46285c519af2 | 406 | { |
Bobty | 6:46285c519af2 | 407 | bool bCacheSuccess = pCacheEntry->readLocalFileIntoCache(localFilename); |
Bobty | 6:46285c519af2 | 408 | // Store the cache entry even if reading failed (mem alloc or file not found, etc) so we don't try to cache again |
Bobty | 6:46285c519af2 | 409 | _cachedFiles.push_back(pCacheEntry); |
Bobty | 6:46285c519af2 | 410 | if (bCacheSuccess) |
Bobty | 6:46285c519af2 | 411 | { |
Bobty | 6:46285c519af2 | 412 | sendFromCache(pCacheEntry, client, httpHeader); |
Bobty | 6:46285c519af2 | 413 | return; |
Bobty | 6:46285c519af2 | 414 | } |
Bobty | 6:46285c519af2 | 415 | } |
Bobty | 6:46285c519af2 | 416 | } |
Bobty | 6:46285c519af2 | 417 | } |
Bobty | 6:46285c519af2 | 418 | |
Bobty | 6:46285c519af2 | 419 | LocalFileSystem local("local"); |
Bobty | 6:46285c519af2 | 420 | |
Bobty | 6:46285c519af2 | 421 | FILE* fp = fopen(localFilename, "r"); |
Bobty | 6:46285c519af2 | 422 | if (fp == NULL) |
Bobty | 6:46285c519af2 | 423 | { |
Bobty | 6:46285c519af2 | 424 | pc.printf ("Local file %s not found\r\n", localFilename); |
Bobty | 6:46285c519af2 | 425 | sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); |
Bobty | 6:46285c519af2 | 426 | client.send(httpHeader,strlen(httpHeader)); |
Bobty | 6:46285c519af2 | 427 | client.send(reqFileNameStr,strlen(reqFileNameStr)); |
Bobty | 6:46285c519af2 | 428 | } |
Bobty | 6:46285c519af2 | 429 | else |
Bobty | 6:46285c519af2 | 430 | { |
Bobty | 6:46285c519af2 | 431 | pc.printf ("Sending file %s from disk\r\n", localFilename); |
Bobty | 6:46285c519af2 | 432 | sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n"); |
Bobty | 6:46285c519af2 | 433 | client.send(httpHeader,strlen(httpHeader)); |
Bobty | 6:46285c519af2 | 434 | int rdCnt = 0; |
Bobty | 6:46285c519af2 | 435 | char fileBuf[2000]; |
Bobty | 6:46285c519af2 | 436 | while ((rdCnt = fread(fileBuf, sizeof( char ), 2000, fp)) == 2000) |
Bobty | 6:46285c519af2 | 437 | { |
Bobty | 6:46285c519af2 | 438 | client.send_all(fileBuf, rdCnt); |
Bobty | 6:46285c519af2 | 439 | } |
Bobty | 6:46285c519af2 | 440 | client.send_all(fileBuf, rdCnt); |
Bobty | 6:46285c519af2 | 441 | fclose(fp); |
Bobty | 6:46285c519af2 | 442 | } |
Bobty | 6:46285c519af2 | 443 | } |
Bobty | 6:46285c519af2 | 444 | |
Bobty | 6:46285c519af2 | 445 | bool RdFileCacheEntry::readLocalFileIntoCache(char* fileName) |
Bobty | 6:46285c519af2 | 446 | { |
Bobty | 6:46285c519af2 | 447 | pc.printf("Reading into cache %s\n\r", fileName); |
Bobty | 6:46285c519af2 | 448 | LocalFileSystem local("local"); |
Bobty | 6:46285c519af2 | 449 | FILE* fp = fopen(fileName, "r"); |
Bobty | 6:46285c519af2 | 450 | if (fp == NULL) |
Bobty | 6:46285c519af2 | 451 | { |
Bobty | 6:46285c519af2 | 452 | pc.printf("Failed to open file\n\r"); |
Bobty | 6:46285c519af2 | 453 | return false; |
Bobty | 6:46285c519af2 | 454 | } |
Bobty | 6:46285c519af2 | 455 | pc.printf("Seeking\n\r"); |
Bobty | 6:46285c519af2 | 456 | fseek(fp, 0, SEEK_END); |
Bobty | 6:46285c519af2 | 457 | _nFileLen = (int)ftell(fp); |
Bobty | 6:46285c519af2 | 458 | _pFileContent = new char[_nFileLen]; |
Bobty | 6:46285c519af2 | 459 | pc.printf("Len %d Buf %08x\n\r", _nFileLen, _pFileContent); |
Bobty | 6:46285c519af2 | 460 | if (!_pFileContent) |
Bobty | 6:46285c519af2 | 461 | { |
Bobty | 6:46285c519af2 | 462 | pc.printf("Failed to allocate %lu\n\r", _nFileLen); |
Bobty | 6:46285c519af2 | 463 | fclose(fp); |
Bobty | 6:46285c519af2 | 464 | return false; |
Bobty | 6:46285c519af2 | 465 | } |
Bobty | 6:46285c519af2 | 466 | pc.printf("Allocated\n\r"); |
Bobty | 6:46285c519af2 | 467 | memset(_pFileContent, 0, _nFileLen); |
Bobty | 6:46285c519af2 | 468 | fseek(fp, 0, SEEK_SET); |
Bobty | 6:46285c519af2 | 469 | char* pMem = _pFileContent; |
Bobty | 6:46285c519af2 | 470 | char fileBuf[100]; |
Bobty | 6:46285c519af2 | 471 | int totCnt = 0; |
Bobty | 6:46285c519af2 | 472 | int rdCnt = 0; |
Bobty | 6:46285c519af2 | 473 | pc.printf("Reading\n\r"); |
Bobty | 6:46285c519af2 | 474 | while (true) |
Bobty | 6:46285c519af2 | 475 | { |
Bobty | 6:46285c519af2 | 476 | int toRead = _nFileLen - totCnt; |
Bobty | 6:46285c519af2 | 477 | if (toRead > sizeof(fileBuf)) |
Bobty | 6:46285c519af2 | 478 | toRead = sizeof(fileBuf); |
Bobty | 6:46285c519af2 | 479 | if (toRead <= 0) |
Bobty | 6:46285c519af2 | 480 | break; |
Bobty | 6:46285c519af2 | 481 | rdCnt = fread(fileBuf, sizeof(char), toRead, fp); |
Bobty | 6:46285c519af2 | 482 | if (rdCnt <= 0) |
Bobty | 6:46285c519af2 | 483 | break; |
Bobty | 6:46285c519af2 | 484 | pc.printf("Read %d tot %d of %d\n\r", rdCnt, totCnt, _nFileLen); |
Bobty | 6:46285c519af2 | 485 | memcpy(pMem, fileBuf, rdCnt); |
Bobty | 6:46285c519af2 | 486 | pMem += rdCnt; |
Bobty | 6:46285c519af2 | 487 | totCnt += rdCnt; |
Bobty | 6:46285c519af2 | 488 | } |
Bobty | 6:46285c519af2 | 489 | pc.printf("Done read\n\r"); |
Bobty | 6:46285c519af2 | 490 | fclose(fp); |
Bobty | 6:46285c519af2 | 491 | pc.printf("Success in caching %d bytes (read %d)\n\r", _nFileLen, totCnt); |
Bobty | 6:46285c519af2 | 492 | _bCacheValid = true; |
Bobty | 0:b5b4d07f7827 | 493 | return true; |
Bobty | 0:b5b4d07f7827 | 494 | } |
Bobty | 0:b5b4d07f7827 | 495 | |
Bobty | 1:75bb184de749 | 496 | bool RdWebServer::extractCmdArgs(char* buf, char* pCmdStr, int maxCmdStrLen, char* pArgStr, int maxArgStrLen) |
Bobty | 1:75bb184de749 | 497 | { |
Bobty | 1:75bb184de749 | 498 | *pCmdStr = '\0'; |
Bobty | 1:75bb184de749 | 499 | *pArgStr = '\0'; |
Bobty | 1:75bb184de749 | 500 | int cmdStrLen = 0; |
Bobty | 1:75bb184de749 | 501 | int argStrLen = 0; |
Bobty | 1:75bb184de749 | 502 | if (buf == NULL) |
Bobty | 1:75bb184de749 | 503 | return false; |
Bobty | 1:75bb184de749 | 504 | char* pSlash1 = strchr(buf, '/'); |
Bobty | 1:75bb184de749 | 505 | if (pSlash1 == NULL) |
Bobty | 1:75bb184de749 | 506 | return false; |
Bobty | 1:75bb184de749 | 507 | pSlash1++; |
Bobty | 1:75bb184de749 | 508 | while(*pSlash1) |
Bobty | 1:75bb184de749 | 509 | { |
Bobty | 1:75bb184de749 | 510 | if (cmdStrLen >= maxCmdStrLen-1) |
Bobty | 1:75bb184de749 | 511 | break; |
Bobty | 1:75bb184de749 | 512 | if ((*pSlash1 == '/') || (*pSlash1 == ' ') || (*pSlash1 == '\n')) |
Bobty | 1:75bb184de749 | 513 | break; |
Bobty | 1:75bb184de749 | 514 | *pCmdStr++ = *pSlash1++; |
Bobty | 1:75bb184de749 | 515 | *pCmdStr = '\0'; |
Bobty | 1:75bb184de749 | 516 | cmdStrLen++; |
Bobty | 1:75bb184de749 | 517 | } |
Bobty | 1:75bb184de749 | 518 | if ((*pSlash1 == '\0') || (*pSlash1 == ' ') || (*pSlash1 == '\n')) |
Bobty | 1:75bb184de749 | 519 | return true; |
Bobty | 1:75bb184de749 | 520 | *pSlash1++; |
Bobty | 1:75bb184de749 | 521 | while(*pSlash1) |
Bobty | 1:75bb184de749 | 522 | { |
Bobty | 1:75bb184de749 | 523 | if (argStrLen >= maxArgStrLen-1) |
Bobty | 1:75bb184de749 | 524 | break; |
Bobty | 1:75bb184de749 | 525 | if ((*pSlash1 == ' ') || (*pSlash1 == '\n')) |
Bobty | 1:75bb184de749 | 526 | break; |
Bobty | 1:75bb184de749 | 527 | *pArgStr++ = *pSlash1++; |
Bobty | 1:75bb184de749 | 528 | *pArgStr = '\0'; |
Bobty | 1:75bb184de749 | 529 | argStrLen++; |
Bobty | 1:75bb184de749 | 530 | } |
Bobty | 1:75bb184de749 | 531 | return true; |
Bobty | 1:75bb184de749 | 532 | } |