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