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