Copy of NetServicesMin with the HTTP Client library. Includes modification for HTTP servers which send the HTTP status line in its own packet.
services/http/client/data/HTTPMap.cpp@0:18dd877d2c77, 2011-05-26 (annotated)
- Committer:
- andrewbonney
- Date:
- Thu May 26 10:02:40 2011 +0000
- Revision:
- 0:18dd877d2c77
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewbonney | 0:18dd877d2c77 | 1 | |
andrewbonney | 0:18dd877d2c77 | 2 | /* |
andrewbonney | 0:18dd877d2c77 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
andrewbonney | 0:18dd877d2c77 | 4 | |
andrewbonney | 0:18dd877d2c77 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
andrewbonney | 0:18dd877d2c77 | 6 | of this software and associated documentation files (the "Software"), to deal |
andrewbonney | 0:18dd877d2c77 | 7 | in the Software without restriction, including without limitation the rights |
andrewbonney | 0:18dd877d2c77 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
andrewbonney | 0:18dd877d2c77 | 9 | copies of the Software, and to permit persons to whom the Software is |
andrewbonney | 0:18dd877d2c77 | 10 | furnished to do so, subject to the following conditions: |
andrewbonney | 0:18dd877d2c77 | 11 | |
andrewbonney | 0:18dd877d2c77 | 12 | The above copyright notice and this permission notice shall be included in |
andrewbonney | 0:18dd877d2c77 | 13 | all copies or substantial portions of the Software. |
andrewbonney | 0:18dd877d2c77 | 14 | |
andrewbonney | 0:18dd877d2c77 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
andrewbonney | 0:18dd877d2c77 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
andrewbonney | 0:18dd877d2c77 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
andrewbonney | 0:18dd877d2c77 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
andrewbonney | 0:18dd877d2c77 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
andrewbonney | 0:18dd877d2c77 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
andrewbonney | 0:18dd877d2c77 | 21 | THE SOFTWARE. |
andrewbonney | 0:18dd877d2c77 | 22 | */ |
andrewbonney | 0:18dd877d2c77 | 23 | |
andrewbonney | 0:18dd877d2c77 | 24 | #include "HTTPMap.h" |
andrewbonney | 0:18dd877d2c77 | 25 | #include "../../util/url.h" |
andrewbonney | 0:18dd877d2c77 | 26 | |
andrewbonney | 0:18dd877d2c77 | 27 | //#define __DEBUG |
andrewbonney | 0:18dd877d2c77 | 28 | #include "dbg/dbg.h" |
andrewbonney | 0:18dd877d2c77 | 29 | |
andrewbonney | 0:18dd877d2c77 | 30 | HTTPMap::HTTPMap(const string& keyValueSep /*= "="*/, const string& pairSep /*= "&"*/) : |
andrewbonney | 0:18dd877d2c77 | 31 | HTTPData(), Dictionary(), m_buf(), m_len(0), m_chunked(false), m_keyValueSep(keyValueSep), m_pairSep(pairSep) |
andrewbonney | 0:18dd877d2c77 | 32 | { |
andrewbonney | 0:18dd877d2c77 | 33 | |
andrewbonney | 0:18dd877d2c77 | 34 | } |
andrewbonney | 0:18dd877d2c77 | 35 | |
andrewbonney | 0:18dd877d2c77 | 36 | HTTPMap::~HTTPMap() |
andrewbonney | 0:18dd877d2c77 | 37 | { |
andrewbonney | 0:18dd877d2c77 | 38 | |
andrewbonney | 0:18dd877d2c77 | 39 | } |
andrewbonney | 0:18dd877d2c77 | 40 | |
andrewbonney | 0:18dd877d2c77 | 41 | void HTTPMap::clear() |
andrewbonney | 0:18dd877d2c77 | 42 | { |
andrewbonney | 0:18dd877d2c77 | 43 | m_buf.clear(); |
andrewbonney | 0:18dd877d2c77 | 44 | } |
andrewbonney | 0:18dd877d2c77 | 45 | |
andrewbonney | 0:18dd877d2c77 | 46 | int HTTPMap::read(char* buf, int len) |
andrewbonney | 0:18dd877d2c77 | 47 | { |
andrewbonney | 0:18dd877d2c77 | 48 | memcpy( (void*)buf, (void*)m_buf.data(), len ); |
andrewbonney | 0:18dd877d2c77 | 49 | m_buf.erase(0, len); //Erase these chars |
andrewbonney | 0:18dd877d2c77 | 50 | return len; |
andrewbonney | 0:18dd877d2c77 | 51 | } |
andrewbonney | 0:18dd877d2c77 | 52 | |
andrewbonney | 0:18dd877d2c77 | 53 | int HTTPMap::write(const char* buf, int len) |
andrewbonney | 0:18dd877d2c77 | 54 | { |
andrewbonney | 0:18dd877d2c77 | 55 | m_buf.append( buf, len ); |
andrewbonney | 0:18dd877d2c77 | 56 | if( ( m_chunked && !len ) || |
andrewbonney | 0:18dd877d2c77 | 57 | ( !m_chunked && ( m_buf.length() >= m_len ) ) ) //Last chunk or EOF |
andrewbonney | 0:18dd877d2c77 | 58 | { |
andrewbonney | 0:18dd877d2c77 | 59 | parseString(); |
andrewbonney | 0:18dd877d2c77 | 60 | } |
andrewbonney | 0:18dd877d2c77 | 61 | return len; |
andrewbonney | 0:18dd877d2c77 | 62 | } |
andrewbonney | 0:18dd877d2c77 | 63 | |
andrewbonney | 0:18dd877d2c77 | 64 | string HTTPMap::getDataType() //Internet media type for Content-Type header |
andrewbonney | 0:18dd877d2c77 | 65 | { |
andrewbonney | 0:18dd877d2c77 | 66 | return "application/x-www-form-urlencoded"; |
andrewbonney | 0:18dd877d2c77 | 67 | } |
andrewbonney | 0:18dd877d2c77 | 68 | |
andrewbonney | 0:18dd877d2c77 | 69 | void HTTPMap::setDataType(const string& type) //Internet media type from Content-Type header |
andrewbonney | 0:18dd877d2c77 | 70 | { |
andrewbonney | 0:18dd877d2c77 | 71 | //Do not really care here |
andrewbonney | 0:18dd877d2c77 | 72 | } |
andrewbonney | 0:18dd877d2c77 | 73 | |
andrewbonney | 0:18dd877d2c77 | 74 | int HTTPMap::getDataLen() //For Content-Length header |
andrewbonney | 0:18dd877d2c77 | 75 | { |
andrewbonney | 0:18dd877d2c77 | 76 | //This will be called before any read occurs, so let's generate our string object |
andrewbonney | 0:18dd877d2c77 | 77 | //Generate string |
andrewbonney | 0:18dd877d2c77 | 78 | generateString(); |
andrewbonney | 0:18dd877d2c77 | 79 | return m_len; |
andrewbonney | 0:18dd877d2c77 | 80 | } |
andrewbonney | 0:18dd877d2c77 | 81 | |
andrewbonney | 0:18dd877d2c77 | 82 | bool HTTPMap::getIsChunked() //For Transfer-Encoding header |
andrewbonney | 0:18dd877d2c77 | 83 | { |
andrewbonney | 0:18dd877d2c77 | 84 | return false; //We don't want to chunk this |
andrewbonney | 0:18dd877d2c77 | 85 | } |
andrewbonney | 0:18dd877d2c77 | 86 | |
andrewbonney | 0:18dd877d2c77 | 87 | void HTTPMap::setIsChunked(bool chunked) //From Transfer-Encoding header |
andrewbonney | 0:18dd877d2c77 | 88 | { |
andrewbonney | 0:18dd877d2c77 | 89 | m_chunked = chunked; |
andrewbonney | 0:18dd877d2c77 | 90 | } |
andrewbonney | 0:18dd877d2c77 | 91 | |
andrewbonney | 0:18dd877d2c77 | 92 | void HTTPMap::setDataLen(int len) //From Content-Length header, or if the transfer is chunked, next chunk length |
andrewbonney | 0:18dd877d2c77 | 93 | { |
andrewbonney | 0:18dd877d2c77 | 94 | m_len += len; //Useful so that we can parse string when last byte is written if not chunked |
andrewbonney | 0:18dd877d2c77 | 95 | m_buf.reserve( m_len ); |
andrewbonney | 0:18dd877d2c77 | 96 | } |
andrewbonney | 0:18dd877d2c77 | 97 | |
andrewbonney | 0:18dd877d2c77 | 98 | void HTTPMap::generateString() |
andrewbonney | 0:18dd877d2c77 | 99 | { |
andrewbonney | 0:18dd877d2c77 | 100 | Dictionary::iterator it; |
andrewbonney | 0:18dd877d2c77 | 101 | bool first = true; |
andrewbonney | 0:18dd877d2c77 | 102 | while( !Dictionary::empty() ) |
andrewbonney | 0:18dd877d2c77 | 103 | { |
andrewbonney | 0:18dd877d2c77 | 104 | if(!first) |
andrewbonney | 0:18dd877d2c77 | 105 | { |
andrewbonney | 0:18dd877d2c77 | 106 | m_buf.append( m_pairSep ); |
andrewbonney | 0:18dd877d2c77 | 107 | } |
andrewbonney | 0:18dd877d2c77 | 108 | else |
andrewbonney | 0:18dd877d2c77 | 109 | { |
andrewbonney | 0:18dd877d2c77 | 110 | first = false; |
andrewbonney | 0:18dd877d2c77 | 111 | } |
andrewbonney | 0:18dd877d2c77 | 112 | it = Dictionary::begin(); |
andrewbonney | 0:18dd877d2c77 | 113 | m_buf.append( Url::encode( (*it).first ) ); |
andrewbonney | 0:18dd877d2c77 | 114 | m_buf.append( m_keyValueSep ); |
andrewbonney | 0:18dd877d2c77 | 115 | m_buf.append( Url::encode( (*it).second ) ); |
andrewbonney | 0:18dd877d2c77 | 116 | Dictionary::erase(it); //Free memory as we process data |
andrewbonney | 0:18dd877d2c77 | 117 | } |
andrewbonney | 0:18dd877d2c77 | 118 | m_len = m_buf.length(); |
andrewbonney | 0:18dd877d2c77 | 119 | DBG("\r\nData [len %d]:\r\n%s\r\n", m_len, m_buf.c_str()); |
andrewbonney | 0:18dd877d2c77 | 120 | } |
andrewbonney | 0:18dd877d2c77 | 121 | |
andrewbonney | 0:18dd877d2c77 | 122 | void HTTPMap::parseString() |
andrewbonney | 0:18dd877d2c77 | 123 | { |
andrewbonney | 0:18dd877d2c77 | 124 | string key; |
andrewbonney | 0:18dd877d2c77 | 125 | string value; |
andrewbonney | 0:18dd877d2c77 | 126 | |
andrewbonney | 0:18dd877d2c77 | 127 | int pos = 0; |
andrewbonney | 0:18dd877d2c77 | 128 | |
andrewbonney | 0:18dd877d2c77 | 129 | int key_pos; |
andrewbonney | 0:18dd877d2c77 | 130 | int key_len; |
andrewbonney | 0:18dd877d2c77 | 131 | |
andrewbonney | 0:18dd877d2c77 | 132 | int value_pos; |
andrewbonney | 0:18dd877d2c77 | 133 | int value_len; |
andrewbonney | 0:18dd877d2c77 | 134 | |
andrewbonney | 0:18dd877d2c77 | 135 | bool eof = false; |
andrewbonney | 0:18dd877d2c77 | 136 | while(!eof) |
andrewbonney | 0:18dd877d2c77 | 137 | { |
andrewbonney | 0:18dd877d2c77 | 138 | key_pos = pos; |
andrewbonney | 0:18dd877d2c77 | 139 | value_pos = m_buf.find(m_keyValueSep, pos); |
andrewbonney | 0:18dd877d2c77 | 140 | if(value_pos < 0) |
andrewbonney | 0:18dd877d2c77 | 141 | { |
andrewbonney | 0:18dd877d2c77 | 142 | //Parse error, break |
andrewbonney | 0:18dd877d2c77 | 143 | break; |
andrewbonney | 0:18dd877d2c77 | 144 | } |
andrewbonney | 0:18dd877d2c77 | 145 | |
andrewbonney | 0:18dd877d2c77 | 146 | key_len = value_pos - key_pos; |
andrewbonney | 0:18dd877d2c77 | 147 | |
andrewbonney | 0:18dd877d2c77 | 148 | value_pos+= m_keyValueSep.length(); |
andrewbonney | 0:18dd877d2c77 | 149 | |
andrewbonney | 0:18dd877d2c77 | 150 | pos = m_buf.find( m_pairSep, value_pos ); |
andrewbonney | 0:18dd877d2c77 | 151 | if(pos < 0) //Not found |
andrewbonney | 0:18dd877d2c77 | 152 | { |
andrewbonney | 0:18dd877d2c77 | 153 | pos = m_buf.length(); |
andrewbonney | 0:18dd877d2c77 | 154 | eof = true; |
andrewbonney | 0:18dd877d2c77 | 155 | } |
andrewbonney | 0:18dd877d2c77 | 156 | |
andrewbonney | 0:18dd877d2c77 | 157 | value_len = pos - value_pos; |
andrewbonney | 0:18dd877d2c77 | 158 | |
andrewbonney | 0:18dd877d2c77 | 159 | pos += m_pairSep.length(); |
andrewbonney | 0:18dd877d2c77 | 160 | |
andrewbonney | 0:18dd877d2c77 | 161 | //Append elenent |
andrewbonney | 0:18dd877d2c77 | 162 | Dictionary::operator[]( Url::decode( m_buf.substr(key_pos, key_len) ) ) = Url::decode( m_buf.substr(value_pos, value_len) ); |
andrewbonney | 0:18dd877d2c77 | 163 | } |
andrewbonney | 0:18dd877d2c77 | 164 | |
andrewbonney | 0:18dd877d2c77 | 165 | m_buf.clear(); //Free data |
andrewbonney | 0:18dd877d2c77 | 166 | |
andrewbonney | 0:18dd877d2c77 | 167 | } |