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:
Sun May 03 18:59:42 2015 +0000
Revision:
11:cec51b430b20
Parent:
10:b4b9d4d5e5be
Child:
12:c14ffd4ec125
Added debugging macros

Who changed what in which revision?

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