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