Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:41:36 2011 +0000
Revision:
0:8f5825f330b0
setDataLen hacked to 180bytes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RodColeman 0:8f5825f330b0 1
RodColeman 0:8f5825f330b0 2 /*
RodColeman 0:8f5825f330b0 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
RodColeman 0:8f5825f330b0 4
RodColeman 0:8f5825f330b0 5 Permission is hereby granted, free of charge, to any person obtaining a copy
RodColeman 0:8f5825f330b0 6 of this software and associated documentation files (the "Software"), to deal
RodColeman 0:8f5825f330b0 7 in the Software without restriction, including without limitation the rights
RodColeman 0:8f5825f330b0 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
RodColeman 0:8f5825f330b0 9 copies of the Software, and to permit persons to whom the Software is
RodColeman 0:8f5825f330b0 10 furnished to do so, subject to the following conditions:
RodColeman 0:8f5825f330b0 11
RodColeman 0:8f5825f330b0 12 The above copyright notice and this permission notice shall be included in
RodColeman 0:8f5825f330b0 13 all copies or substantial portions of the Software.
RodColeman 0:8f5825f330b0 14
RodColeman 0:8f5825f330b0 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
RodColeman 0:8f5825f330b0 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
RodColeman 0:8f5825f330b0 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
RodColeman 0:8f5825f330b0 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
RodColeman 0:8f5825f330b0 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
RodColeman 0:8f5825f330b0 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
RodColeman 0:8f5825f330b0 21 THE SOFTWARE.
RodColeman 0:8f5825f330b0 22 */
RodColeman 0:8f5825f330b0 23
RodColeman 0:8f5825f330b0 24 #include "FSHandler.h"
RodColeman 0:8f5825f330b0 25
RodColeman 0:8f5825f330b0 26 //#define __DEBUG
RodColeman 0:8f5825f330b0 27 #include "dbg/dbg.h"
RodColeman 0:8f5825f330b0 28
RodColeman 0:8f5825f330b0 29 #define CHUNK_SIZE 128
RodColeman 0:8f5825f330b0 30
RodColeman 0:8f5825f330b0 31 #define DEFAULT_PAGE "/index.htm"
RodColeman 0:8f5825f330b0 32
RodColeman 0:8f5825f330b0 33 FSHandler::FSHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket), m_err404(false)
RodColeman 0:8f5825f330b0 34 {}
RodColeman 0:8f5825f330b0 35
RodColeman 0:8f5825f330b0 36 FSHandler::~FSHandler()
RodColeman 0:8f5825f330b0 37 {
RodColeman 0:8f5825f330b0 38 if(m_fp)
RodColeman 0:8f5825f330b0 39 fclose(m_fp);
RodColeman 0:8f5825f330b0 40 DBG("\r\nHandler destroyed\r\n");
RodColeman 0:8f5825f330b0 41 }
RodColeman 0:8f5825f330b0 42
RodColeman 0:8f5825f330b0 43 //static init
RodColeman 0:8f5825f330b0 44 map<string,string> FSHandler::m_lFsPath = map<string,string>();
RodColeman 0:8f5825f330b0 45
RodColeman 0:8f5825f330b0 46 void FSHandler::mount(const string& fsPath, const string& rootPath)
RodColeman 0:8f5825f330b0 47 {
RodColeman 0:8f5825f330b0 48 m_lFsPath[rootPath]=fsPath;
RodColeman 0:8f5825f330b0 49 }
RodColeman 0:8f5825f330b0 50
RodColeman 0:8f5825f330b0 51 void FSHandler::doGet()
RodColeman 0:8f5825f330b0 52 {
RodColeman 0:8f5825f330b0 53 DBG("\r\nIn FSHandler::doGet() - rootPath=%s, path=%s\r\n", rootPath().c_str(), path().c_str());
RodColeman 0:8f5825f330b0 54 //FIXME: Translate path to local/path
RodColeman 0:8f5825f330b0 55 string checkedRootPath = rootPath();
RodColeman 0:8f5825f330b0 56 if(checkedRootPath.empty())
RodColeman 0:8f5825f330b0 57 checkedRootPath="/";
RodColeman 0:8f5825f330b0 58 string filePath = m_lFsPath[checkedRootPath];
RodColeman 0:8f5825f330b0 59 if (path().size() > 1)
RodColeman 0:8f5825f330b0 60 {
RodColeman 0:8f5825f330b0 61 filePath += path();
RodColeman 0:8f5825f330b0 62 }
RodColeman 0:8f5825f330b0 63 else
RodColeman 0:8f5825f330b0 64 {
RodColeman 0:8f5825f330b0 65 filePath += DEFAULT_PAGE;
RodColeman 0:8f5825f330b0 66 }
RodColeman 0:8f5825f330b0 67
RodColeman 0:8f5825f330b0 68 DBG("Trying to open %s\n", filePath.c_str());
RodColeman 0:8f5825f330b0 69
RodColeman 0:8f5825f330b0 70 m_fp = fopen(filePath.c_str(), "r"); //FIXME: if null, error 404
RodColeman 0:8f5825f330b0 71
RodColeman 0:8f5825f330b0 72 if(!m_fp)
RodColeman 0:8f5825f330b0 73 {
RodColeman 0:8f5825f330b0 74 m_err404 = true;
RodColeman 0:8f5825f330b0 75 setErrCode(404);
RodColeman 0:8f5825f330b0 76 const char* msg = "File not found.";
RodColeman 0:8f5825f330b0 77 setContentLen(strlen(msg));
RodColeman 0:8f5825f330b0 78 respHeaders()["Content-Type"] = "text/html";
RodColeman 0:8f5825f330b0 79 respHeaders()["Connection"] = "close";
RodColeman 0:8f5825f330b0 80 writeData(msg,strlen(msg)); //Only send header
RodColeman 0:8f5825f330b0 81 DBG("\r\nExit FSHandler::doGet() w Error 404\r\n");
RodColeman 0:8f5825f330b0 82 return;
RodColeman 0:8f5825f330b0 83 }
RodColeman 0:8f5825f330b0 84
RodColeman 0:8f5825f330b0 85 //Seek EOF to get length
RodColeman 0:8f5825f330b0 86 fseek(m_fp, 0, SEEK_END);
RodColeman 0:8f5825f330b0 87 setContentLen( ftell(m_fp) );
RodColeman 0:8f5825f330b0 88 fseek(m_fp, 0, SEEK_SET); //Goto SOF
RodColeman 0:8f5825f330b0 89
RodColeman 0:8f5825f330b0 90 respHeaders()["Connection"] = "close";
RodColeman 0:8f5825f330b0 91 onWriteable();
RodColeman 0:8f5825f330b0 92 DBG("\r\nExit SimpleHandler::doGet()\r\n");
RodColeman 0:8f5825f330b0 93 }
RodColeman 0:8f5825f330b0 94
RodColeman 0:8f5825f330b0 95 void FSHandler::doPost()
RodColeman 0:8f5825f330b0 96 {
RodColeman 0:8f5825f330b0 97
RodColeman 0:8f5825f330b0 98 }
RodColeman 0:8f5825f330b0 99
RodColeman 0:8f5825f330b0 100 void FSHandler::doHead()
RodColeman 0:8f5825f330b0 101 {
RodColeman 0:8f5825f330b0 102
RodColeman 0:8f5825f330b0 103 }
RodColeman 0:8f5825f330b0 104
RodColeman 0:8f5825f330b0 105 void FSHandler::onReadable() //Data has been read
RodColeman 0:8f5825f330b0 106 {
RodColeman 0:8f5825f330b0 107
RodColeman 0:8f5825f330b0 108 }
RodColeman 0:8f5825f330b0 109
RodColeman 0:8f5825f330b0 110 void FSHandler::onWriteable() //Data has been written & buf is free
RodColeman 0:8f5825f330b0 111 {
RodColeman 0:8f5825f330b0 112 DBG("\r\nFSHandler::onWriteable() event\r\n");
RodColeman 0:8f5825f330b0 113 if(m_err404)
RodColeman 0:8f5825f330b0 114 {
RodColeman 0:8f5825f330b0 115 //Error has been served, now exit
RodColeman 0:8f5825f330b0 116 close();
RodColeman 0:8f5825f330b0 117 return;
RodColeman 0:8f5825f330b0 118 }
RodColeman 0:8f5825f330b0 119
RodColeman 0:8f5825f330b0 120 static char rBuf[CHUNK_SIZE];
RodColeman 0:8f5825f330b0 121 while(true)
RodColeman 0:8f5825f330b0 122 {
RodColeman 0:8f5825f330b0 123 int len = fread(rBuf, 1, CHUNK_SIZE, m_fp);
RodColeman 0:8f5825f330b0 124 if(len>0)
RodColeman 0:8f5825f330b0 125 {
RodColeman 0:8f5825f330b0 126 int writtenLen = writeData(rBuf, len);
RodColeman 0:8f5825f330b0 127 if(writtenLen < 0) //Socket error
RodColeman 0:8f5825f330b0 128 {
RodColeman 0:8f5825f330b0 129 DBG("FSHandler: Socket error %d\n", writtenLen);
RodColeman 0:8f5825f330b0 130 if(writtenLen == TCPSOCKET_MEM)
RodColeman 0:8f5825f330b0 131 {
RodColeman 0:8f5825f330b0 132 fseek(m_fp, -len, SEEK_CUR);
RodColeman 0:8f5825f330b0 133 return; //Wait for the queued TCP segments to be transmitted
RodColeman 0:8f5825f330b0 134 }
RodColeman 0:8f5825f330b0 135 else
RodColeman 0:8f5825f330b0 136 {
RodColeman 0:8f5825f330b0 137 //This is a critical error
RodColeman 0:8f5825f330b0 138 close();
RodColeman 0:8f5825f330b0 139 return;
RodColeman 0:8f5825f330b0 140 }
RodColeman 0:8f5825f330b0 141 }
RodColeman 0:8f5825f330b0 142 else if(writtenLen < len) //Short write, socket's buffer is full
RodColeman 0:8f5825f330b0 143 {
RodColeman 0:8f5825f330b0 144 fseek(m_fp, writtenLen - len, SEEK_CUR);
RodColeman 0:8f5825f330b0 145 return;
RodColeman 0:8f5825f330b0 146 }
RodColeman 0:8f5825f330b0 147 }
RodColeman 0:8f5825f330b0 148 else
RodColeman 0:8f5825f330b0 149 {
RodColeman 0:8f5825f330b0 150 close(); //Data written, we can close the connection
RodColeman 0:8f5825f330b0 151 return;
RodColeman 0:8f5825f330b0 152 }
RodColeman 0:8f5825f330b0 153 }
RodColeman 0:8f5825f330b0 154 }
RodColeman 0:8f5825f330b0 155
RodColeman 0:8f5825f330b0 156 void FSHandler::onClose() //Connection is closing
RodColeman 0:8f5825f330b0 157 {
RodColeman 0:8f5825f330b0 158 if(m_fp)
RodColeman 0:8f5825f330b0 159 fclose(m_fp);
RodColeman 0:8f5825f330b0 160 }