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:
Sat Feb 07 10:34:09 2015 +0000
Revision:
7:fe7c33f7fbb8
Parent:
6:46285c519af2
Child:
8:de915bd70ec1
Working with gas count - not tested on actual meter reed switch

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 6:46285c519af2 36 pc.printf("TCP server bind fail\n\r");
Bobty 0:b5b4d07f7827 37 return false;
Bobty 0:b5b4d07f7827 38 }
Bobty 0:b5b4d07f7827 39 else
Bobty 0:b5b4d07f7827 40 {
Bobty 6:46285c519af2 41 // pc.printf("TCP server bind success\n\r");
Bobty 0:b5b4d07f7827 42 _serverIsListening = true;
Bobty 0:b5b4d07f7827 43 }
Bobty 0:b5b4d07f7827 44
Bobty 0:b5b4d07f7827 45 if(_socketSrv.listen(1) < 0)
Bobty 0:b5b4d07f7827 46 {
Bobty 6:46285c519af2 47 pc.printf("TCP server listen fail\n\r");
Bobty 0:b5b4d07f7827 48 return false;
Bobty 0:b5b4d07f7827 49 }
Bobty 0:b5b4d07f7827 50 else
Bobty 0:b5b4d07f7827 51 {
Bobty 6:46285c519af2 52 pc.printf("TCP server is listening...\r\n");
Bobty 6:46285c519af2 53 }
Bobty 6:46285c519af2 54
Bobty 6:46285c519af2 55 return true;
Bobty 6:46285c519af2 56 }
Bobty 6:46285c519af2 57
Bobty 6:46285c519af2 58 void RdWebServer::handleReceivedHttp(TCPSocketConnection &client)
Bobty 6:46285c519af2 59 {
Bobty 7:fe7c33f7fbb8 60 // pc.printf("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 7:fe7c33f7fbb8 71 pc.printf("CmdStr %s\n\r", cmdStr);
Bobty 7:fe7c33f7fbb8 72 pc.printf("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 7:fe7c33f7fbb8 76 // pc.printf("Testing <<%s>> with <<%s>>\r\n", (*it)->_pCmdStr, cmdStr);
Bobty 7:fe7c33f7fbb8 77 if (strcasecmp((*it)->_pCmdStr, cmdStr) == 0)
Bobty 7:fe7c33f7fbb8 78 {
Bobty 7:fe7c33f7fbb8 79 pc.printf("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 6:46285c519af2 111
Bobty 6:46285c519af2 112 while (1)
Bobty 6:46285c519af2 113 {
Bobty 6:46285c519af2 114 TCPSocketConnection client;
Bobty 6:46285c519af2 115 pc.printf("Waiting for TCP connection\r\n");
Bobty 6:46285c519af2 116 if(_socketSrv.accept(client)<0)
Bobty 6:46285c519af2 117 {
Bobty 6:46285c519af2 118 pc.printf("TCP Socket failed to accept connection\n\r");
Bobty 6:46285c519af2 119 }
Bobty 6:46285c519af2 120 else
Bobty 6:46285c519af2 121 {
Bobty 6:46285c519af2 122 pc.printf("Connection from IP: %s\n\r",client.get_address());
Bobty 6:46285c519af2 123 if (_pStatusLed != NULL)
Bobty 6:46285c519af2 124 *_pStatusLed = true;
Bobty 6:46285c519af2 125
Bobty 6:46285c519af2 126 // Get received data
Bobty 6:46285c519af2 127 int rxLen = client.receive(_buffer, HTTPD_MAX_REQ_LENGTH);
Bobty 6:46285c519af2 128 if (rxLen == -1)
Bobty 6:46285c519af2 129 {
Bobty 6:46285c519af2 130 continue;
Bobty 6:46285c519af2 131 }
Bobty 6:46285c519af2 132 else if (rxLen == 0)
Bobty 6:46285c519af2 133 {
Bobty 6:46285c519af2 134 pc.printf("received buffer is empty.\n\r");
Bobty 6:46285c519af2 135 continue;
Bobty 6:46285c519af2 136 }
Bobty 6:46285c519af2 137 else if (rxLen > HTTPD_MAX_REQ_LENGTH)
Bobty 6:46285c519af2 138 {
Bobty 6:46285c519af2 139 sprintf(_httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
Bobty 6:46285c519af2 140 client.send(_httpHeader,strlen(_httpHeader));
Bobty 6:46285c519af2 141 client.send(_buffer, rxLen);
Bobty 6:46285c519af2 142 continue;
Bobty 6:46285c519af2 143 }
Bobty 6:46285c519af2 144 _buffer[rxLen] = '\0';
Bobty 6:46285c519af2 145
Bobty 6:46285c519af2 146 // Handle buffer
Bobty 6:46285c519af2 147 handleReceivedHttp(client);
Bobty 6:46285c519af2 148 }
Bobty 0:b5b4d07f7827 149 }
Bobty 6:46285c519af2 150 }
Bobty 6:46285c519af2 151
Bobty 6:46285c519af2 152 void RdWebServer::addCommand(char* pCmdStr, int cmdType, CmdCallbackType callback, char* substFileName, bool cacheIfPossible)
Bobty 6:46285c519af2 153 {
Bobty 6:46285c519af2 154 _commands.push_back(new RdWebServerCmdDef(pCmdStr, cmdType, callback, substFileName, cacheIfPossible));
Bobty 6:46285c519af2 155 }
Bobty 6:46285c519af2 156
Bobty 6:46285c519af2 157 void RdWebServer::handleSDFileRequest(char* inFileName, char* argStr, TCPSocketConnection &client, char* httpHeader)
Bobty 6:46285c519af2 158 {
Bobty 6:46285c519af2 159 const int HTTPD_MAX_FNAME_LENGTH = 127;
Bobty 6:46285c519af2 160 char filename[HTTPD_MAX_FNAME_LENGTH+1];
Bobty 6:46285c519af2 161
Bobty 6:46285c519af2 162 pc.printf("Requesting file %s\n\r", inFileName);
Bobty 6:46285c519af2 163
Bobty 6:46285c519af2 164 char *lstchr = strrchr(inFileName, NULL) -1;
Bobty 6:46285c519af2 165 if ('/' == *lstchr)
Bobty 6:46285c519af2 166 {
Bobty 6:46285c519af2 167 pc.printf("Request file %s%s\n", _pBaseWebFolder, inFileName);
Bobty 6:46285c519af2 168 *lstchr = 0;
Bobty 6:46285c519af2 169 sprintf(filename, "%s%s", _pBaseWebFolder, inFileName);
Bobty 6:46285c519af2 170 DIR *d = opendir(filename);
Bobty 6:46285c519af2 171 if (d != NULL) {
Bobty 6:46285c519af2 172 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
Bobty 6:46285c519af2 173 client.send(httpHeader,strlen(httpHeader));
Bobty 6:46285c519af2 174 sprintf(httpHeader,"<html><head><title>Directory Listing</title></head><body><h1>%s</h1><ul>", inFileName);
Bobty 6:46285c519af2 175 client.send(httpHeader,strlen(httpHeader));
Bobty 6:46285c519af2 176 struct dirent *p;
Bobty 6:46285c519af2 177 while((p = readdir(d)) != NULL) {
Bobty 6:46285c519af2 178 pc.printf("%s\n", p->d_name);
Bobty 6:46285c519af2 179 sprintf(httpHeader,"<li>%s</li>", p->d_name);
Bobty 6:46285c519af2 180 client.send(httpHeader,strlen(httpHeader));
Bobty 6:46285c519af2 181 }
Bobty 6:46285c519af2 182 }
Bobty 6:46285c519af2 183 closedir(d);
Bobty 6:46285c519af2 184 // pc.printf("Directory closed\n");
Bobty 6:46285c519af2 185 sprintf(httpHeader,"</ul></body></html>");
Bobty 6:46285c519af2 186 client.send(httpHeader,strlen(httpHeader));
Bobty 6:46285c519af2 187 }
Bobty 6:46285c519af2 188 else
Bobty 6:46285c519af2 189 {
Bobty 6:46285c519af2 190 sprintf(filename, "%s%s", _pBaseWebFolder, inFileName);
Bobty 6:46285c519af2 191 pc.printf ("Filename %s\r\n", filename);
Bobty 6:46285c519af2 192
Bobty 6:46285c519af2 193 FILE* fp = fopen(filename, "r");
Bobty 6:46285c519af2 194 if (fp == NULL)
Bobty 6:46285c519af2 195 {
Bobty 6:46285c519af2 196 pc.printf ("Filename %s not found\r\n", filename);
Bobty 6:46285c519af2 197 sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
Bobty 6:46285c519af2 198 client.send(httpHeader,strlen(httpHeader));
Bobty 6:46285c519af2 199 client.send(inFileName,strlen(inFileName));
Bobty 6:46285c519af2 200 }
Bobty 6:46285c519af2 201 else
Bobty 6:46285c519af2 202 {
Bobty 6:46285c519af2 203 pc.printf ("Sending file %s\r\n", filename);
Bobty 6:46285c519af2 204 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
Bobty 6:46285c519af2 205 client.send(httpHeader,strlen(httpHeader));
Bobty 6:46285c519af2 206 int rdCnt = 0;
Bobty 6:46285c519af2 207 char fileBuf[1024];
Bobty 6:46285c519af2 208 while ((rdCnt = fread(fileBuf, sizeof( char ), 1024, fp)) == 1024)
Bobty 6:46285c519af2 209 {
Bobty 6:46285c519af2 210 client.send(fileBuf, rdCnt);
Bobty 6:46285c519af2 211 }
Bobty 6:46285c519af2 212 client.send(fileBuf, rdCnt);
Bobty 6:46285c519af2 213 fclose(fp);
Bobty 6:46285c519af2 214 }
Bobty 6:46285c519af2 215 }
Bobty 6:46285c519af2 216 }
Bobty 6:46285c519af2 217
Bobty 6:46285c519af2 218 void RdWebServer::sendFromCache(RdFileCacheEntry* pCacheEntry, TCPSocketConnection &client, char* httpHeader)
Bobty 6:46285c519af2 219 {
Bobty 6:46285c519af2 220 pc.printf ("Sending file %s from cache %d bytes\r\n", pCacheEntry->_fileName, pCacheEntry->_nFileLen);
Bobty 6:46285c519af2 221 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
Bobty 6:46285c519af2 222 client.send(httpHeader,strlen(httpHeader));
Bobty 6:46285c519af2 223 char* pMem = pCacheEntry->_pFileContent;
Bobty 6:46285c519af2 224 int nLenLeft = pCacheEntry->_nFileLen;
Bobty 6:46285c519af2 225 int blkSize = 2048;
Bobty 6:46285c519af2 226 while(nLenLeft > 0)
Bobty 6:46285c519af2 227 {
Bobty 6:46285c519af2 228 if (blkSize > nLenLeft)
Bobty 6:46285c519af2 229 blkSize = nLenLeft;
Bobty 6:46285c519af2 230 client.send_all(pMem, blkSize);
Bobty 6:46285c519af2 231 pMem += blkSize;
Bobty 6:46285c519af2 232 nLenLeft -= blkSize;
Bobty 6:46285c519af2 233 }
Bobty 6:46285c519af2 234 }
Bobty 6:46285c519af2 235
Bobty 6:46285c519af2 236 void RdWebServer::handleLocalFileRequest(char* inFileName, char* argStr, TCPSocketConnection &client, char* httpHeader, bool bCacheIfPossible)
Bobty 6:46285c519af2 237 {
Bobty 6:46285c519af2 238 const int HTTPD_MAX_FNAME_LENGTH = 127;
Bobty 6:46285c519af2 239 char localFilename[HTTPD_MAX_FNAME_LENGTH+1];
Bobty 6:46285c519af2 240 char reqFileNameStr[HTTPD_MAX_FNAME_LENGTH+1];
Bobty 6:46285c519af2 241
Bobty 6:46285c519af2 242 pc.printf("Requesting local file %s\n\r", inFileName);
Bobty 6:46285c519af2 243 sprintf(reqFileNameStr, "/%s", inFileName);
Bobty 6:46285c519af2 244 sprintf(localFilename, "/local/%s", inFileName);
Bobty 6:46285c519af2 245
Bobty 6:46285c519af2 246 if (bCacheIfPossible)
Bobty 6:46285c519af2 247 {
Bobty 6:46285c519af2 248 // Check if file already cached
Bobty 6:46285c519af2 249 bool bTryToCache = true;
Bobty 6:46285c519af2 250 for (std::vector<RdFileCacheEntry*>::iterator it = _cachedFiles.begin() ; it != _cachedFiles.end(); ++it)
Bobty 6:46285c519af2 251 {
Bobty 6:46285c519af2 252 if (strcmp(inFileName, (*it)->_fileName) == 0)
Bobty 6:46285c519af2 253 {
Bobty 6:46285c519af2 254 if ((*it)->_bCacheValid)
Bobty 6:46285c519af2 255 {
Bobty 6:46285c519af2 256 sendFromCache(*it, client, httpHeader);
Bobty 6:46285c519af2 257 return;
Bobty 6:46285c519af2 258 }
Bobty 6:46285c519af2 259 bTryToCache = false; // don't try to cache as cacheing must have already failed
Bobty 6:46285c519af2 260 }
Bobty 6:46285c519af2 261 }
Bobty 6:46285c519af2 262
Bobty 6:46285c519af2 263 // See if we can cache the file
Bobty 6:46285c519af2 264 if (bTryToCache)
Bobty 6:46285c519af2 265 {
Bobty 6:46285c519af2 266 RdFileCacheEntry* pCacheEntry = new RdFileCacheEntry(inFileName);
Bobty 6:46285c519af2 267 if (pCacheEntry)
Bobty 6:46285c519af2 268 {
Bobty 6:46285c519af2 269 bool bCacheSuccess = pCacheEntry->readLocalFileIntoCache(localFilename);
Bobty 6:46285c519af2 270 // 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 271 _cachedFiles.push_back(pCacheEntry);
Bobty 6:46285c519af2 272 if (bCacheSuccess)
Bobty 6:46285c519af2 273 {
Bobty 6:46285c519af2 274 sendFromCache(pCacheEntry, client, httpHeader);
Bobty 6:46285c519af2 275 return;
Bobty 6:46285c519af2 276 }
Bobty 6:46285c519af2 277 }
Bobty 6:46285c519af2 278 }
Bobty 6:46285c519af2 279 }
Bobty 6:46285c519af2 280
Bobty 6:46285c519af2 281 LocalFileSystem local("local");
Bobty 6:46285c519af2 282
Bobty 6:46285c519af2 283 FILE* fp = fopen(localFilename, "r");
Bobty 6:46285c519af2 284 if (fp == NULL)
Bobty 6:46285c519af2 285 {
Bobty 6:46285c519af2 286 pc.printf ("Local file %s not found\r\n", localFilename);
Bobty 6:46285c519af2 287 sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
Bobty 6:46285c519af2 288 client.send(httpHeader,strlen(httpHeader));
Bobty 6:46285c519af2 289 client.send(reqFileNameStr,strlen(reqFileNameStr));
Bobty 6:46285c519af2 290 }
Bobty 6:46285c519af2 291 else
Bobty 6:46285c519af2 292 {
Bobty 6:46285c519af2 293 pc.printf ("Sending file %s from disk\r\n", localFilename);
Bobty 6:46285c519af2 294 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
Bobty 6:46285c519af2 295 client.send(httpHeader,strlen(httpHeader));
Bobty 6:46285c519af2 296 int rdCnt = 0;
Bobty 6:46285c519af2 297 char fileBuf[2000];
Bobty 6:46285c519af2 298 while ((rdCnt = fread(fileBuf, sizeof( char ), 2000, fp)) == 2000)
Bobty 6:46285c519af2 299 {
Bobty 6:46285c519af2 300 client.send_all(fileBuf, rdCnt);
Bobty 6:46285c519af2 301 }
Bobty 6:46285c519af2 302 client.send_all(fileBuf, rdCnt);
Bobty 6:46285c519af2 303 fclose(fp);
Bobty 6:46285c519af2 304 }
Bobty 6:46285c519af2 305 }
Bobty 6:46285c519af2 306
Bobty 6:46285c519af2 307 bool RdFileCacheEntry::readLocalFileIntoCache(char* fileName)
Bobty 6:46285c519af2 308 {
Bobty 6:46285c519af2 309 pc.printf("Reading into cache %s\n\r", fileName);
Bobty 6:46285c519af2 310 LocalFileSystem local("local");
Bobty 6:46285c519af2 311 FILE* fp = fopen(fileName, "r");
Bobty 6:46285c519af2 312 if (fp == NULL)
Bobty 6:46285c519af2 313 {
Bobty 6:46285c519af2 314 pc.printf("Failed to open file\n\r");
Bobty 6:46285c519af2 315 return false;
Bobty 6:46285c519af2 316 }
Bobty 6:46285c519af2 317 pc.printf("Seeking\n\r");
Bobty 6:46285c519af2 318 fseek(fp, 0, SEEK_END);
Bobty 6:46285c519af2 319 _nFileLen = (int)ftell(fp);
Bobty 6:46285c519af2 320 _pFileContent = new char[_nFileLen];
Bobty 6:46285c519af2 321 pc.printf("Len %d Buf %08x\n\r", _nFileLen, _pFileContent);
Bobty 6:46285c519af2 322 if (!_pFileContent)
Bobty 6:46285c519af2 323 {
Bobty 6:46285c519af2 324 pc.printf("Failed to allocate %lu\n\r", _nFileLen);
Bobty 6:46285c519af2 325 fclose(fp);
Bobty 6:46285c519af2 326 return false;
Bobty 6:46285c519af2 327 }
Bobty 6:46285c519af2 328 pc.printf("Allocated\n\r");
Bobty 6:46285c519af2 329 memset(_pFileContent, 0, _nFileLen);
Bobty 6:46285c519af2 330 fseek(fp, 0, SEEK_SET);
Bobty 6:46285c519af2 331 char* pMem = _pFileContent;
Bobty 6:46285c519af2 332 char fileBuf[100];
Bobty 6:46285c519af2 333 int totCnt = 0;
Bobty 6:46285c519af2 334 int rdCnt = 0;
Bobty 6:46285c519af2 335 pc.printf("Reading\n\r");
Bobty 6:46285c519af2 336 while (true)
Bobty 6:46285c519af2 337 {
Bobty 6:46285c519af2 338 int toRead = _nFileLen - totCnt;
Bobty 6:46285c519af2 339 if (toRead > sizeof(fileBuf))
Bobty 6:46285c519af2 340 toRead = sizeof(fileBuf);
Bobty 6:46285c519af2 341 if (toRead <= 0)
Bobty 6:46285c519af2 342 break;
Bobty 6:46285c519af2 343 rdCnt = fread(fileBuf, sizeof(char), toRead, fp);
Bobty 6:46285c519af2 344 if (rdCnt <= 0)
Bobty 6:46285c519af2 345 break;
Bobty 6:46285c519af2 346 pc.printf("Read %d tot %d of %d\n\r", rdCnt, totCnt, _nFileLen);
Bobty 6:46285c519af2 347 memcpy(pMem, fileBuf, rdCnt);
Bobty 6:46285c519af2 348 pMem += rdCnt;
Bobty 6:46285c519af2 349 totCnt += rdCnt;
Bobty 6:46285c519af2 350 }
Bobty 6:46285c519af2 351 pc.printf("Done read\n\r");
Bobty 6:46285c519af2 352 fclose(fp);
Bobty 6:46285c519af2 353 pc.printf("Success in caching %d bytes (read %d)\n\r", _nFileLen, totCnt);
Bobty 6:46285c519af2 354 _bCacheValid = true;
Bobty 0:b5b4d07f7827 355 return true;
Bobty 0:b5b4d07f7827 356 }
Bobty 0:b5b4d07f7827 357
Bobty 1:75bb184de749 358 bool RdWebServer::extractCmdArgs(char* buf, char* pCmdStr, int maxCmdStrLen, char* pArgStr, int maxArgStrLen)
Bobty 1:75bb184de749 359 {
Bobty 1:75bb184de749 360 *pCmdStr = '\0';
Bobty 1:75bb184de749 361 *pArgStr = '\0';
Bobty 1:75bb184de749 362 int cmdStrLen = 0;
Bobty 1:75bb184de749 363 int argStrLen = 0;
Bobty 1:75bb184de749 364 if (buf == NULL)
Bobty 1:75bb184de749 365 return false;
Bobty 7:fe7c33f7fbb8 366 // Check for first slash
Bobty 1:75bb184de749 367 char* pSlash1 = strchr(buf, '/');
Bobty 1:75bb184de749 368 if (pSlash1 == NULL)
Bobty 1:75bb184de749 369 return false;
Bobty 1:75bb184de749 370 pSlash1++;
Bobty 7:fe7c33f7fbb8 371 // Extract command
Bobty 1:75bb184de749 372 while(*pSlash1)
Bobty 1:75bb184de749 373 {
Bobty 1:75bb184de749 374 if (cmdStrLen >= maxCmdStrLen-1)
Bobty 1:75bb184de749 375 break;
Bobty 7:fe7c33f7fbb8 376 if ((*pSlash1 == '/') || (*pSlash1 == ' ') || (*pSlash1 == '\n') || (*pSlash1 == '?') || (*pSlash1 == '&'))
Bobty 1:75bb184de749 377 break;
Bobty 1:75bb184de749 378 *pCmdStr++ = *pSlash1++;
Bobty 1:75bb184de749 379 *pCmdStr = '\0';
Bobty 1:75bb184de749 380 cmdStrLen++;
Bobty 1:75bb184de749 381 }
Bobty 1:75bb184de749 382 if ((*pSlash1 == '\0') || (*pSlash1 == ' ') || (*pSlash1 == '\n'))
Bobty 1:75bb184de749 383 return true;
Bobty 7:fe7c33f7fbb8 384 // Now args
Bobty 1:75bb184de749 385 *pSlash1++;
Bobty 1:75bb184de749 386 while(*pSlash1)
Bobty 1:75bb184de749 387 {
Bobty 1:75bb184de749 388 if (argStrLen >= maxArgStrLen-1)
Bobty 1:75bb184de749 389 break;
Bobty 1:75bb184de749 390 if ((*pSlash1 == ' ') || (*pSlash1 == '\n'))
Bobty 1:75bb184de749 391 break;
Bobty 1:75bb184de749 392 *pArgStr++ = *pSlash1++;
Bobty 1:75bb184de749 393 *pArgStr = '\0';
Bobty 1:75bb184de749 394 argStrLen++;
Bobty 1:75bb184de749 395 }
Bobty 1:75bb184de749 396 return true;
Bobty 1:75bb184de749 397 }