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.

Committer:
Bobty
Date:
Tue May 05 22:14:16 2015 +0000
Revision:
16:0248bbfdb6c1
Parent:
15:0865fa4b046a
Child:
17:080f2bed8b36
Working on FRDM K64F with two clients ok - browser tends to reopen socket so curl has to wait for timeout but seems to retry ok

Who changed what in which revision?

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