Fork HTTPClient and Modfiy code for mbed 6.0

Dependents:   mbed-demo-http-get-json

Committer:
donatien
Date:
Sun Aug 05 16:12:10 2012 +0000
Revision:
13:be61104f4e91
Parent:
10:e1351de84c16
Child:
16:1f743885e7de
Fixed blocking mode

Who changed what in which revision?

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