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 "HTTPMap.h"
tax 0:66300c77c6e9 25 #include "../../util/url.h"
tax 0:66300c77c6e9 26
tax 0:66300c77c6e9 27 //#define __DEBUG
tax 0:66300c77c6e9 28 #include "dbg/dbg.h"
tax 0:66300c77c6e9 29
tax 0:66300c77c6e9 30 HTTPMap::HTTPMap(const string& keyValueSep /*= "="*/, const string& pairSep /*= "&"*/) :
tax 0:66300c77c6e9 31 HTTPData(), Dictionary(), m_buf(), m_len(0), m_chunked(false), m_keyValueSep(keyValueSep), m_pairSep(pairSep)
tax 0:66300c77c6e9 32 {
tax 0:66300c77c6e9 33
tax 0:66300c77c6e9 34 }
tax 0:66300c77c6e9 35
tax 0:66300c77c6e9 36 HTTPMap::~HTTPMap()
tax 0:66300c77c6e9 37 {
tax 0:66300c77c6e9 38
tax 0:66300c77c6e9 39 }
tax 0:66300c77c6e9 40
tax 0:66300c77c6e9 41 void HTTPMap::clear()
tax 0:66300c77c6e9 42 {
tax 0:66300c77c6e9 43 m_buf.clear();
tax 0:66300c77c6e9 44 }
tax 0:66300c77c6e9 45
tax 0:66300c77c6e9 46 int HTTPMap::read(char* buf, int len)
tax 0:66300c77c6e9 47 {
tax 0:66300c77c6e9 48 memcpy( (void*)buf, (void*)m_buf.data(), len );
tax 0:66300c77c6e9 49 m_buf.erase(0, len); //Erase these chars
tax 0:66300c77c6e9 50 return len;
tax 0:66300c77c6e9 51 }
tax 0:66300c77c6e9 52
tax 0:66300c77c6e9 53 int HTTPMap::write(const char* buf, int len)
tax 0:66300c77c6e9 54 {
tax 0:66300c77c6e9 55 m_buf.append( buf, len );
tax 0:66300c77c6e9 56 if( ( m_chunked && !len ) ||
tax 0:66300c77c6e9 57 ( !m_chunked && ( m_buf.length() >= m_len ) ) ) //Last chunk or EOF
tax 0:66300c77c6e9 58 {
tax 0:66300c77c6e9 59 parseString();
tax 0:66300c77c6e9 60 }
tax 0:66300c77c6e9 61 return len;
tax 0:66300c77c6e9 62 }
tax 0:66300c77c6e9 63
tax 0:66300c77c6e9 64 string HTTPMap::getDataType() //Internet media type for Content-Type header
tax 0:66300c77c6e9 65 {
tax 0:66300c77c6e9 66 return "application/x-www-form-urlencoded";
tax 0:66300c77c6e9 67 }
tax 0:66300c77c6e9 68
tax 0:66300c77c6e9 69 void HTTPMap::setDataType(const string& type) //Internet media type from Content-Type header
tax 0:66300c77c6e9 70 {
tax 0:66300c77c6e9 71 //Do not really care here
tax 0:66300c77c6e9 72 }
tax 0:66300c77c6e9 73
tax 0:66300c77c6e9 74 int HTTPMap::getDataLen() //For Content-Length header
tax 0:66300c77c6e9 75 {
tax 0:66300c77c6e9 76 //This will be called before any read occurs, so let's generate our string object
tax 0:66300c77c6e9 77 //Generate string
tax 0:66300c77c6e9 78 generateString();
tax 0:66300c77c6e9 79 return m_len;
tax 0:66300c77c6e9 80 }
tax 0:66300c77c6e9 81
tax 0:66300c77c6e9 82 bool HTTPMap::getIsChunked() //For Transfer-Encoding header
tax 0:66300c77c6e9 83 {
tax 0:66300c77c6e9 84 return false; //We don't want to chunk this
tax 0:66300c77c6e9 85 }
tax 0:66300c77c6e9 86
tax 0:66300c77c6e9 87 void HTTPMap::setIsChunked(bool chunked) //From Transfer-Encoding header
tax 0:66300c77c6e9 88 {
tax 0:66300c77c6e9 89 m_chunked = chunked;
tax 0:66300c77c6e9 90 }
tax 0:66300c77c6e9 91
tax 0:66300c77c6e9 92 void HTTPMap::setDataLen(int len) //From Content-Length header, or if the transfer is chunked, next chunk length
tax 0:66300c77c6e9 93 {
tax 0:66300c77c6e9 94 m_len += len; //Useful so that we can parse string when last byte is written if not chunked
tax 0:66300c77c6e9 95 m_buf.reserve( m_len );
tax 0:66300c77c6e9 96 }
tax 0:66300c77c6e9 97
tax 0:66300c77c6e9 98 void HTTPMap::generateString()
tax 0:66300c77c6e9 99 {
tax 0:66300c77c6e9 100 Dictionary::iterator it;
tax 0:66300c77c6e9 101 bool first = true;
tax 0:66300c77c6e9 102 while( !Dictionary::empty() )
tax 0:66300c77c6e9 103 {
tax 0:66300c77c6e9 104 if(!first)
tax 0:66300c77c6e9 105 {
tax 0:66300c77c6e9 106 m_buf.append( m_pairSep );
tax 0:66300c77c6e9 107 }
tax 0:66300c77c6e9 108 else
tax 0:66300c77c6e9 109 {
tax 0:66300c77c6e9 110 first = false;
tax 0:66300c77c6e9 111 }
tax 0:66300c77c6e9 112 it = Dictionary::begin();
tax 0:66300c77c6e9 113 m_buf.append( Url::encode( (*it).first ) );
tax 0:66300c77c6e9 114 m_buf.append( m_keyValueSep );
tax 0:66300c77c6e9 115 m_buf.append( Url::encode( (*it).second ) );
tax 0:66300c77c6e9 116 Dictionary::erase(it); //Free memory as we process data
tax 0:66300c77c6e9 117 }
tax 0:66300c77c6e9 118 m_len = m_buf.length();
tax 0:66300c77c6e9 119 DBG("\r\nData [len %d]:\r\n%s\r\n", m_len, m_buf.c_str());
tax 0:66300c77c6e9 120 }
tax 0:66300c77c6e9 121
tax 0:66300c77c6e9 122 void HTTPMap::parseString()
tax 0:66300c77c6e9 123 {
tax 0:66300c77c6e9 124 string key;
tax 0:66300c77c6e9 125 string value;
tax 0:66300c77c6e9 126
tax 0:66300c77c6e9 127 int pos = 0;
tax 0:66300c77c6e9 128
tax 0:66300c77c6e9 129 int key_pos;
tax 0:66300c77c6e9 130 int key_len;
tax 0:66300c77c6e9 131
tax 0:66300c77c6e9 132 int value_pos;
tax 0:66300c77c6e9 133 int value_len;
tax 0:66300c77c6e9 134
tax 0:66300c77c6e9 135 bool eof = false;
tax 0:66300c77c6e9 136 while(!eof)
tax 0:66300c77c6e9 137 {
tax 0:66300c77c6e9 138 key_pos = pos;
tax 0:66300c77c6e9 139 value_pos = m_buf.find(m_keyValueSep, pos);
tax 0:66300c77c6e9 140 if(value_pos < 0)
tax 0:66300c77c6e9 141 {
tax 0:66300c77c6e9 142 //Parse error, break
tax 0:66300c77c6e9 143 break;
tax 0:66300c77c6e9 144 }
tax 0:66300c77c6e9 145
tax 0:66300c77c6e9 146 key_len = value_pos - key_pos;
tax 0:66300c77c6e9 147
tax 0:66300c77c6e9 148 value_pos+= m_keyValueSep.length();
tax 0:66300c77c6e9 149
tax 0:66300c77c6e9 150 pos = m_buf.find( m_pairSep, value_pos );
tax 0:66300c77c6e9 151 if(pos < 0) //Not found
tax 0:66300c77c6e9 152 {
tax 0:66300c77c6e9 153 pos = m_buf.length();
tax 0:66300c77c6e9 154 eof = true;
tax 0:66300c77c6e9 155 }
tax 0:66300c77c6e9 156
tax 0:66300c77c6e9 157 value_len = pos - value_pos;
tax 0:66300c77c6e9 158
tax 0:66300c77c6e9 159 pos += m_pairSep.length();
tax 0:66300c77c6e9 160
tax 0:66300c77c6e9 161 //Append elenent
tax 0:66300c77c6e9 162 Dictionary::operator[]( Url::decode( m_buf.substr(key_pos, key_len) ) ) = Url::decode( m_buf.substr(value_pos, value_len) );
tax 0:66300c77c6e9 163 }
tax 0:66300c77c6e9 164
tax 0:66300c77c6e9 165 m_buf.clear(); //Free data
tax 0:66300c77c6e9 166
tax 0:66300c77c6e9 167 }