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