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