Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.

Committer:
andrewbonney
Date:
Fri Apr 08 14:39:41 2011 +0000
Revision:
0:ec559500a63f

        

Who changed what in which revision?

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