RobT's fork of HTTPClient library
Fork of HTTPClient_ToBeRemoved by
data/HTTPMap.cpp@7:d97a4fc01c86, 2012-04-19 (annotated)
- Committer:
- donatien
- Date:
- Thu Apr 19 09:52:54 2012 +0000
- Revision:
- 7:d97a4fc01c86
- Parent:
- 6:3d3824893be1
Documentation++
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 6:3d3824893be1 | 1 | /* HTTPMap.cpp */ |
donatien | 6:3d3824893be1 | 2 | /* |
donatien | 6:3d3824893be1 | 3 | Copyright (C) 2012 ARM Limited. |
donatien | 6:3d3824893be1 | 4 | |
donatien | 6:3d3824893be1 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of |
donatien | 6:3d3824893be1 | 6 | this software and associated documentation files (the "Software"), to deal in |
donatien | 6:3d3824893be1 | 7 | the Software without restriction, including without limitation the rights to |
donatien | 6:3d3824893be1 | 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
donatien | 6:3d3824893be1 | 9 | of the Software, and to permit persons to whom the Software is furnished to do |
donatien | 6:3d3824893be1 | 10 | so, subject to the following conditions: |
donatien | 6:3d3824893be1 | 11 | |
donatien | 6:3d3824893be1 | 12 | The above copyright notice and this permission notice shall be included in all |
donatien | 6:3d3824893be1 | 13 | copies or substantial portions of the Software. |
donatien | 6:3d3824893be1 | 14 | |
donatien | 6:3d3824893be1 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
donatien | 6:3d3824893be1 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
donatien | 6:3d3824893be1 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
donatien | 6:3d3824893be1 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
donatien | 6:3d3824893be1 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 6:3d3824893be1 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
donatien | 6:3d3824893be1 | 21 | SOFTWARE. |
donatien | 6:3d3824893be1 | 22 | */ |
donatien | 6:3d3824893be1 | 23 | |
donatien | 6:3d3824893be1 | 24 | #include "core/fwk.h" |
donatien | 6:3d3824893be1 | 25 | |
donatien | 6:3d3824893be1 | 26 | #include "HTTPMap.h" |
donatien | 6:3d3824893be1 | 27 | |
donatien | 6:3d3824893be1 | 28 | #include <cstring> |
donatien | 6:3d3824893be1 | 29 | |
donatien | 6:3d3824893be1 | 30 | #include <cctype> |
donatien | 6:3d3824893be1 | 31 | |
donatien | 6:3d3824893be1 | 32 | HTTPMap::HTTPMap() : m_pos(0), m_count(0) |
donatien | 6:3d3824893be1 | 33 | { |
donatien | 6:3d3824893be1 | 34 | |
donatien | 6:3d3824893be1 | 35 | } |
donatien | 6:3d3824893be1 | 36 | |
donatien | 6:3d3824893be1 | 37 | void HTTPMap::put(const char* key, const char* value) |
donatien | 6:3d3824893be1 | 38 | { |
donatien | 6:3d3824893be1 | 39 | if(m_count >= HTTPMAP_TABLE_SIZE) |
donatien | 6:3d3824893be1 | 40 | { |
donatien | 6:3d3824893be1 | 41 | return; |
donatien | 6:3d3824893be1 | 42 | } |
donatien | 6:3d3824893be1 | 43 | m_keys[m_count] = key; |
donatien | 6:3d3824893be1 | 44 | m_values[m_count] = value; |
donatien | 6:3d3824893be1 | 45 | m_count++; |
donatien | 6:3d3824893be1 | 46 | } |
donatien | 6:3d3824893be1 | 47 | |
donatien | 6:3d3824893be1 | 48 | void HTTPMap::clear() |
donatien | 6:3d3824893be1 | 49 | { |
donatien | 6:3d3824893be1 | 50 | m_count = 0; |
donatien | 6:3d3824893be1 | 51 | m_pos = 0; |
donatien | 6:3d3824893be1 | 52 | } |
donatien | 6:3d3824893be1 | 53 | |
donatien | 6:3d3824893be1 | 54 | |
donatien | 6:3d3824893be1 | 55 | /*virtual*/ int HTTPMap::read(char* buf, size_t len, size_t* pReadLen) |
donatien | 6:3d3824893be1 | 56 | { |
donatien | 6:3d3824893be1 | 57 | if(m_pos >= m_count) |
donatien | 6:3d3824893be1 | 58 | { |
donatien | 6:3d3824893be1 | 59 | *pReadLen = 0; |
donatien | 6:3d3824893be1 | 60 | m_pos = 0; |
donatien | 6:3d3824893be1 | 61 | return OK; |
donatien | 6:3d3824893be1 | 62 | } |
donatien | 6:3d3824893be1 | 63 | |
donatien | 6:3d3824893be1 | 64 | //URL encode |
donatien | 6:3d3824893be1 | 65 | char* out = buf; |
donatien | 6:3d3824893be1 | 66 | const char* in = m_keys[m_pos]; |
donatien | 6:3d3824893be1 | 67 | if( (m_pos != 0) && (out - buf < len - 1) ) |
donatien | 6:3d3824893be1 | 68 | { |
donatien | 6:3d3824893be1 | 69 | *out='&'; |
donatien | 6:3d3824893be1 | 70 | out++; |
donatien | 6:3d3824893be1 | 71 | } |
donatien | 6:3d3824893be1 | 72 | |
donatien | 6:3d3824893be1 | 73 | while( (*in != '\0') && (out - buf < len - 3) ) |
donatien | 6:3d3824893be1 | 74 | { |
donatien | 6:3d3824893be1 | 75 | if (std::isalnum(*in) || *in == '-' || *in == '_' || *in == '.' || *in == '~') |
donatien | 6:3d3824893be1 | 76 | { |
donatien | 6:3d3824893be1 | 77 | *out = *in; |
donatien | 6:3d3824893be1 | 78 | out++; |
donatien | 6:3d3824893be1 | 79 | } |
donatien | 6:3d3824893be1 | 80 | else if( *in == ' ' ) |
donatien | 6:3d3824893be1 | 81 | { |
donatien | 6:3d3824893be1 | 82 | *out='+'; |
donatien | 6:3d3824893be1 | 83 | out++; |
donatien | 6:3d3824893be1 | 84 | } |
donatien | 6:3d3824893be1 | 85 | else |
donatien | 6:3d3824893be1 | 86 | { |
donatien | 6:3d3824893be1 | 87 | char hex[] = "0123456789abcdef"; |
donatien | 6:3d3824893be1 | 88 | *out='%'; |
donatien | 6:3d3824893be1 | 89 | out++; |
donatien | 6:3d3824893be1 | 90 | *out=hex[(*in>>4)&0xf]; |
donatien | 6:3d3824893be1 | 91 | out++; |
donatien | 6:3d3824893be1 | 92 | *out=hex[(*in)&0xf]; |
donatien | 6:3d3824893be1 | 93 | out++; |
donatien | 6:3d3824893be1 | 94 | } |
donatien | 6:3d3824893be1 | 95 | in++; |
donatien | 6:3d3824893be1 | 96 | } |
donatien | 6:3d3824893be1 | 97 | |
donatien | 6:3d3824893be1 | 98 | if( out - buf < len - 1 ) |
donatien | 6:3d3824893be1 | 99 | { |
donatien | 6:3d3824893be1 | 100 | *out='='; |
donatien | 6:3d3824893be1 | 101 | out++; |
donatien | 6:3d3824893be1 | 102 | } |
donatien | 6:3d3824893be1 | 103 | |
donatien | 6:3d3824893be1 | 104 | in = m_values[m_pos]; |
donatien | 6:3d3824893be1 | 105 | while( (*in != '\0') && (out - buf < len - 3) ) |
donatien | 6:3d3824893be1 | 106 | { |
donatien | 6:3d3824893be1 | 107 | if (std::isalnum(*in) || *in == '-' || *in == '_' || *in == '.' || *in == '~') |
donatien | 6:3d3824893be1 | 108 | { |
donatien | 6:3d3824893be1 | 109 | *out = *in; |
donatien | 6:3d3824893be1 | 110 | out++; |
donatien | 6:3d3824893be1 | 111 | } |
donatien | 6:3d3824893be1 | 112 | else if( *in == ' ' ) |
donatien | 6:3d3824893be1 | 113 | { |
donatien | 6:3d3824893be1 | 114 | *out='+'; |
donatien | 6:3d3824893be1 | 115 | out++; |
donatien | 6:3d3824893be1 | 116 | } |
donatien | 6:3d3824893be1 | 117 | else |
donatien | 6:3d3824893be1 | 118 | { |
donatien | 6:3d3824893be1 | 119 | char hex[] = "0123456789abcdef"; |
donatien | 6:3d3824893be1 | 120 | *out='%'; |
donatien | 6:3d3824893be1 | 121 | out++; |
donatien | 6:3d3824893be1 | 122 | *out=hex[(*in>>4)&0xf]; |
donatien | 6:3d3824893be1 | 123 | out++; |
donatien | 6:3d3824893be1 | 124 | *out=hex[(*in)&0xf]; |
donatien | 6:3d3824893be1 | 125 | out++; |
donatien | 6:3d3824893be1 | 126 | } |
donatien | 6:3d3824893be1 | 127 | in++; |
donatien | 6:3d3824893be1 | 128 | } |
donatien | 6:3d3824893be1 | 129 | |
donatien | 6:3d3824893be1 | 130 | *pReadLen = out - buf; |
donatien | 6:3d3824893be1 | 131 | |
donatien | 6:3d3824893be1 | 132 | m_pos++; |
donatien | 6:3d3824893be1 | 133 | return OK; |
donatien | 6:3d3824893be1 | 134 | } |
donatien | 6:3d3824893be1 | 135 | |
donatien | 6:3d3824893be1 | 136 | /*virtual*/ int HTTPMap::getDataType(char* type, size_t maxTypeLen) //Internet media type for Content-Type header |
donatien | 6:3d3824893be1 | 137 | { |
donatien | 6:3d3824893be1 | 138 | strncpy(type, "application/x-www-form-urlencoded", maxTypeLen-1); |
donatien | 6:3d3824893be1 | 139 | type[maxTypeLen-1] = '\0'; |
donatien | 6:3d3824893be1 | 140 | return OK; |
donatien | 6:3d3824893be1 | 141 | } |
donatien | 6:3d3824893be1 | 142 | |
donatien | 6:3d3824893be1 | 143 | /*virtual*/ bool HTTPMap::getIsChunked() //For Transfer-Encoding header |
donatien | 6:3d3824893be1 | 144 | { |
donatien | 6:3d3824893be1 | 145 | return false; ////Data is computed one key/value pair at a time |
donatien | 6:3d3824893be1 | 146 | } |
donatien | 6:3d3824893be1 | 147 | |
donatien | 6:3d3824893be1 | 148 | /*virtual*/ size_t HTTPMap::getDataLen() //For Content-Length header |
donatien | 6:3d3824893be1 | 149 | { |
donatien | 6:3d3824893be1 | 150 | size_t count = 0; |
donatien | 6:3d3824893be1 | 151 | for(size_t i = 0; i< m_count; i++) |
donatien | 6:3d3824893be1 | 152 | { |
donatien | 6:3d3824893be1 | 153 | //URL encode |
donatien | 6:3d3824893be1 | 154 | const char* in = m_keys[i]; |
donatien | 6:3d3824893be1 | 155 | if( i != 0 ) |
donatien | 6:3d3824893be1 | 156 | { |
donatien | 6:3d3824893be1 | 157 | count++; |
donatien | 6:3d3824893be1 | 158 | } |
donatien | 6:3d3824893be1 | 159 | |
donatien | 6:3d3824893be1 | 160 | while( (*in != '\0') ) |
donatien | 6:3d3824893be1 | 161 | { |
donatien | 6:3d3824893be1 | 162 | if (std::isalnum(*in) || *in == '-' || *in == '_' || *in == '.' || *in == '~') |
donatien | 6:3d3824893be1 | 163 | { |
donatien | 6:3d3824893be1 | 164 | count++; |
donatien | 6:3d3824893be1 | 165 | } |
donatien | 6:3d3824893be1 | 166 | else if( *in == ' ' ) |
donatien | 6:3d3824893be1 | 167 | { |
donatien | 6:3d3824893be1 | 168 | count++; |
donatien | 6:3d3824893be1 | 169 | } |
donatien | 6:3d3824893be1 | 170 | else |
donatien | 6:3d3824893be1 | 171 | { |
donatien | 6:3d3824893be1 | 172 | count+=3; |
donatien | 6:3d3824893be1 | 173 | } |
donatien | 6:3d3824893be1 | 174 | in++; |
donatien | 6:3d3824893be1 | 175 | } |
donatien | 6:3d3824893be1 | 176 | |
donatien | 6:3d3824893be1 | 177 | count ++; |
donatien | 6:3d3824893be1 | 178 | |
donatien | 6:3d3824893be1 | 179 | in = m_values[i]; |
donatien | 6:3d3824893be1 | 180 | while( (*in != '\0') ) |
donatien | 6:3d3824893be1 | 181 | { |
donatien | 6:3d3824893be1 | 182 | if (std::isalnum(*in) || *in == '-' || *in == '_' || *in == '.' || *in == '~') |
donatien | 6:3d3824893be1 | 183 | { |
donatien | 6:3d3824893be1 | 184 | count++; |
donatien | 6:3d3824893be1 | 185 | } |
donatien | 6:3d3824893be1 | 186 | else if( *in == ' ' ) |
donatien | 6:3d3824893be1 | 187 | { |
donatien | 6:3d3824893be1 | 188 | count++; |
donatien | 6:3d3824893be1 | 189 | } |
donatien | 6:3d3824893be1 | 190 | else |
donatien | 6:3d3824893be1 | 191 | { |
donatien | 6:3d3824893be1 | 192 | count+=3; |
donatien | 6:3d3824893be1 | 193 | } |
donatien | 6:3d3824893be1 | 194 | in++; |
donatien | 6:3d3824893be1 | 195 | } |
donatien | 6:3d3824893be1 | 196 | } |
donatien | 6:3d3824893be1 | 197 | return count; |
donatien | 6:3d3824893be1 | 198 | } |