I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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