HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

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