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 "HTTPFile.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 HTTPFile::HTTPFile(const char* path) : HTTPData(), m_fp(NULL), m_path(path), m_len(0), m_chunked(false)
tax 0:66300c77c6e9 30 {
tax 0:66300c77c6e9 31
tax 0:66300c77c6e9 32 }
tax 0:66300c77c6e9 33
tax 0:66300c77c6e9 34 HTTPFile::~HTTPFile()
tax 0:66300c77c6e9 35 {
tax 0:66300c77c6e9 36 closeFile();
tax 0:66300c77c6e9 37 }
tax 0:66300c77c6e9 38
tax 0:66300c77c6e9 39 void HTTPFile::clear()
tax 0:66300c77c6e9 40 {
tax 0:66300c77c6e9 41 closeFile();
tax 0:66300c77c6e9 42 //Force reopening
tax 0:66300c77c6e9 43 }
tax 0:66300c77c6e9 44
tax 0:66300c77c6e9 45 int HTTPFile::read(char* buf, int len)
tax 0:66300c77c6e9 46 {
tax 0:66300c77c6e9 47 if(!openFile("r")) //File does not exist, or I/O error...
tax 0:66300c77c6e9 48 return 0;
tax 0:66300c77c6e9 49 len = fread(buf, 1, len, m_fp);
tax 0:66300c77c6e9 50 if( feof(m_fp) )
tax 0:66300c77c6e9 51 {
tax 0:66300c77c6e9 52 //File read completely, we can close it
tax 0:66300c77c6e9 53 closeFile();
tax 0:66300c77c6e9 54 }
tax 0:66300c77c6e9 55 return len;
tax 0:66300c77c6e9 56 }
tax 0:66300c77c6e9 57
tax 0:66300c77c6e9 58 int HTTPFile::write(const char* buf, int len)
tax 0:66300c77c6e9 59 {
tax 0:66300c77c6e9 60 if(!openFile("w")) //File does not exist, or I/O error...
tax 0:66300c77c6e9 61 return 0;
tax 0:66300c77c6e9 62 len = fwrite(buf, 1, len, m_fp);
tax 0:66300c77c6e9 63 DBG("Written %d bytes in %d\n", len, m_fp);
tax 0:66300c77c6e9 64 if( (!m_chunked && (ftell(m_fp) >= m_len)) ||
tax 0:66300c77c6e9 65 (m_chunked && !len) )
tax 0:66300c77c6e9 66 {
tax 0:66300c77c6e9 67 //File received completely, we can close it
tax 0:66300c77c6e9 68 closeFile();
tax 0:66300c77c6e9 69 }
tax 0:66300c77c6e9 70 return len;
tax 0:66300c77c6e9 71 }
tax 0:66300c77c6e9 72
tax 0:66300c77c6e9 73 string HTTPFile::getDataType() //Internet media type for Content-Type header
tax 0:66300c77c6e9 74 {
tax 0:66300c77c6e9 75 return ""; //Unknown
tax 0:66300c77c6e9 76 }
tax 0:66300c77c6e9 77
tax 0:66300c77c6e9 78 void HTTPFile::setDataType(const string& type) //Internet media type from Content-Type header
tax 0:66300c77c6e9 79 {
tax 0:66300c77c6e9 80 //Do not really care here
tax 0:66300c77c6e9 81 }
tax 0:66300c77c6e9 82
tax 0:66300c77c6e9 83 bool HTTPFile::getIsChunked() //For Transfer-Encoding header
tax 0:66300c77c6e9 84 {
tax 0:66300c77c6e9 85 return false;
tax 0:66300c77c6e9 86 }
tax 0:66300c77c6e9 87
tax 0:66300c77c6e9 88 void HTTPFile::setIsChunked(bool chunked) //From Transfer-Encoding header
tax 0:66300c77c6e9 89 {
tax 0:66300c77c6e9 90 m_chunked = chunked;
tax 0:66300c77c6e9 91 }
tax 0:66300c77c6e9 92
tax 0:66300c77c6e9 93 int HTTPFile::getDataLen() //For Content-Length header
tax 0:66300c77c6e9 94 {
tax 0:66300c77c6e9 95 return m_len;
tax 0:66300c77c6e9 96 }
tax 0:66300c77c6e9 97
tax 0:66300c77c6e9 98 void HTTPFile::setDataLen(int len) //From Content-Length header, or if the transfer is chunked, next chunk length
tax 0:66300c77c6e9 99 {
tax 0:66300c77c6e9 100 if(!m_chunked)
tax 0:66300c77c6e9 101 m_len = len; //Useful so that we can close file when last byte is written
tax 0:66300c77c6e9 102 }
tax 0:66300c77c6e9 103
tax 0:66300c77c6e9 104 bool HTTPFile::openFile(const char* mode) //true on success, false otherwise
tax 0:66300c77c6e9 105 {
tax 0:66300c77c6e9 106 if(m_fp)
tax 0:66300c77c6e9 107 return true;
tax 0:66300c77c6e9 108
tax 0:66300c77c6e9 109 m_fp = fopen(m_path.c_str(), mode);
tax 0:66300c77c6e9 110 if(m_fp && mode[0]=='r')
tax 0:66300c77c6e9 111 {
tax 0:66300c77c6e9 112 //Seek EOF to get length
tax 0:66300c77c6e9 113 fseek(m_fp, 0, SEEK_END);
tax 0:66300c77c6e9 114 m_len = ftell(m_fp);
tax 0:66300c77c6e9 115 fseek(m_fp, 0, SEEK_SET); //Goto SOF
tax 0:66300c77c6e9 116 }
tax 0:66300c77c6e9 117
tax 0:66300c77c6e9 118 DBG("fd = %d\n", m_fp);
tax 0:66300c77c6e9 119
tax 0:66300c77c6e9 120 if(!m_fp)
tax 0:66300c77c6e9 121 return false;
tax 0:66300c77c6e9 122
tax 0:66300c77c6e9 123 return true;
tax 0:66300c77c6e9 124 }
tax 0:66300c77c6e9 125
tax 0:66300c77c6e9 126 void HTTPFile::closeFile()
tax 0:66300c77c6e9 127 {
tax 0:66300c77c6e9 128 if(m_fp)
tax 0:66300c77c6e9 129 fclose(m_fp);
tax 0:66300c77c6e9 130 }