Buffer size extended

Dependents:   Real-time_Bus_Arrival_Alarm

Fork of HTTPClient by Donatien Garnier

Committer:
donatien
Date:
Wed Jul 18 15:40:04 2012 +0000
Revision:
10:e1351de84c16
Parent:
0:2ccb9960a044
Child:
13:be61104f4e91
Corrected licence headers

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 "core/fwk.h"
donatien 0:2ccb9960a044 21
donatien 0:2ccb9960a044 22 #include "HTTPMap.h"
donatien 0:2ccb9960a044 23
donatien 0:2ccb9960a044 24 #include <cstring>
donatien 0:2ccb9960a044 25
donatien 0:2ccb9960a044 26 #include <cctype>
donatien 0:2ccb9960a044 27
donatien 0:2ccb9960a044 28 HTTPMap::HTTPMap() : m_pos(0), m_count(0)
donatien 0:2ccb9960a044 29 {
donatien 0:2ccb9960a044 30
donatien 0:2ccb9960a044 31 }
donatien 0:2ccb9960a044 32
donatien 0:2ccb9960a044 33 void HTTPMap::put(const char* key, const char* value)
donatien 0:2ccb9960a044 34 {
donatien 0:2ccb9960a044 35 if(m_count >= HTTPMAP_TABLE_SIZE)
donatien 0:2ccb9960a044 36 {
donatien 0:2ccb9960a044 37 return;
donatien 0:2ccb9960a044 38 }
donatien 0:2ccb9960a044 39 m_keys[m_count] = key;
donatien 0:2ccb9960a044 40 m_values[m_count] = value;
donatien 0:2ccb9960a044 41 m_count++;
donatien 0:2ccb9960a044 42 }
donatien 0:2ccb9960a044 43
donatien 0:2ccb9960a044 44 void HTTPMap::clear()
donatien 0:2ccb9960a044 45 {
donatien 0:2ccb9960a044 46 m_count = 0;
donatien 0:2ccb9960a044 47 m_pos = 0;
donatien 0:2ccb9960a044 48 }
donatien 0:2ccb9960a044 49
donatien 0:2ccb9960a044 50
donatien 0:2ccb9960a044 51 /*virtual*/ int HTTPMap::read(char* buf, size_t len, size_t* pReadLen)
donatien 0:2ccb9960a044 52 {
donatien 0:2ccb9960a044 53 if(m_pos >= m_count)
donatien 0:2ccb9960a044 54 {
donatien 0:2ccb9960a044 55 *pReadLen = 0;
donatien 0:2ccb9960a044 56 m_pos = 0;
donatien 0:2ccb9960a044 57 return OK;
donatien 0:2ccb9960a044 58 }
donatien 0:2ccb9960a044 59
donatien 0:2ccb9960a044 60 //URL encode
donatien 0:2ccb9960a044 61 char* out = buf;
donatien 0:2ccb9960a044 62 const char* in = m_keys[m_pos];
donatien 0:2ccb9960a044 63 if( (m_pos != 0) && (out - buf < len - 1) )
donatien 0:2ccb9960a044 64 {
donatien 0:2ccb9960a044 65 *out='&';
donatien 0:2ccb9960a044 66 out++;
donatien 0:2ccb9960a044 67 }
donatien 0:2ccb9960a044 68
donatien 0:2ccb9960a044 69 while( (*in != '\0') && (out - buf < len - 3) )
donatien 0:2ccb9960a044 70 {
donatien 0:2ccb9960a044 71 if (std::isalnum(*in) || *in == '-' || *in == '_' || *in == '.' || *in == '~')
donatien 0:2ccb9960a044 72 {
donatien 0:2ccb9960a044 73 *out = *in;
donatien 0:2ccb9960a044 74 out++;
donatien 0:2ccb9960a044 75 }
donatien 0:2ccb9960a044 76 else if( *in == ' ' )
donatien 0:2ccb9960a044 77 {
donatien 0:2ccb9960a044 78 *out='+';
donatien 0:2ccb9960a044 79 out++;
donatien 0:2ccb9960a044 80 }
donatien 0:2ccb9960a044 81 else
donatien 0:2ccb9960a044 82 {
donatien 0:2ccb9960a044 83 char hex[] = "0123456789abcdef";
donatien 0:2ccb9960a044 84 *out='%';
donatien 0:2ccb9960a044 85 out++;
donatien 0:2ccb9960a044 86 *out=hex[(*in>>4)&0xf];
donatien 0:2ccb9960a044 87 out++;
donatien 0:2ccb9960a044 88 *out=hex[(*in)&0xf];
donatien 0:2ccb9960a044 89 out++;
donatien 0:2ccb9960a044 90 }
donatien 0:2ccb9960a044 91 in++;
donatien 0:2ccb9960a044 92 }
donatien 0:2ccb9960a044 93
donatien 0:2ccb9960a044 94 if( out - buf < len - 1 )
donatien 0:2ccb9960a044 95 {
donatien 0:2ccb9960a044 96 *out='=';
donatien 0:2ccb9960a044 97 out++;
donatien 0:2ccb9960a044 98 }
donatien 0:2ccb9960a044 99
donatien 0:2ccb9960a044 100 in = m_values[m_pos];
donatien 0:2ccb9960a044 101 while( (*in != '\0') && (out - buf < len - 3) )
donatien 0:2ccb9960a044 102 {
donatien 0:2ccb9960a044 103 if (std::isalnum(*in) || *in == '-' || *in == '_' || *in == '.' || *in == '~')
donatien 0:2ccb9960a044 104 {
donatien 0:2ccb9960a044 105 *out = *in;
donatien 0:2ccb9960a044 106 out++;
donatien 0:2ccb9960a044 107 }
donatien 0:2ccb9960a044 108 else if( *in == ' ' )
donatien 0:2ccb9960a044 109 {
donatien 0:2ccb9960a044 110 *out='+';
donatien 0:2ccb9960a044 111 out++;
donatien 0:2ccb9960a044 112 }
donatien 0:2ccb9960a044 113 else
donatien 0:2ccb9960a044 114 {
donatien 0:2ccb9960a044 115 char hex[] = "0123456789abcdef";
donatien 0:2ccb9960a044 116 *out='%';
donatien 0:2ccb9960a044 117 out++;
donatien 0:2ccb9960a044 118 *out=hex[(*in>>4)&0xf];
donatien 0:2ccb9960a044 119 out++;
donatien 0:2ccb9960a044 120 *out=hex[(*in)&0xf];
donatien 0:2ccb9960a044 121 out++;
donatien 0:2ccb9960a044 122 }
donatien 0:2ccb9960a044 123 in++;
donatien 0:2ccb9960a044 124 }
donatien 0:2ccb9960a044 125
donatien 0:2ccb9960a044 126 *pReadLen = out - buf;
donatien 0:2ccb9960a044 127
donatien 0:2ccb9960a044 128 m_pos++;
donatien 0:2ccb9960a044 129 return OK;
donatien 0:2ccb9960a044 130 }
donatien 0:2ccb9960a044 131
donatien 0:2ccb9960a044 132 /*virtual*/ int HTTPMap::getDataType(char* type, size_t maxTypeLen) //Internet media type for Content-Type header
donatien 0:2ccb9960a044 133 {
donatien 0:2ccb9960a044 134 strncpy(type, "application/x-www-form-urlencoded", maxTypeLen-1);
donatien 0:2ccb9960a044 135 type[maxTypeLen-1] = '\0';
donatien 0:2ccb9960a044 136 return OK;
donatien 0:2ccb9960a044 137 }
donatien 0:2ccb9960a044 138
donatien 0:2ccb9960a044 139 /*virtual*/ bool HTTPMap::getIsChunked() //For Transfer-Encoding header
donatien 0:2ccb9960a044 140 {
donatien 0:2ccb9960a044 141 return false; ////Data is computed one key/value pair at a time
donatien 0:2ccb9960a044 142 }
donatien 0:2ccb9960a044 143
donatien 0:2ccb9960a044 144 /*virtual*/ size_t HTTPMap::getDataLen() //For Content-Length header
donatien 0:2ccb9960a044 145 {
donatien 0:2ccb9960a044 146 size_t count = 0;
donatien 0:2ccb9960a044 147 for(size_t i = 0; i< m_count; i++)
donatien 0:2ccb9960a044 148 {
donatien 0:2ccb9960a044 149 //URL encode
donatien 0:2ccb9960a044 150 const char* in = m_keys[i];
donatien 0:2ccb9960a044 151 if( i != 0 )
donatien 0:2ccb9960a044 152 {
donatien 0:2ccb9960a044 153 count++;
donatien 0:2ccb9960a044 154 }
donatien 0:2ccb9960a044 155
donatien 0:2ccb9960a044 156 while( (*in != '\0') )
donatien 0:2ccb9960a044 157 {
donatien 0:2ccb9960a044 158 if (std::isalnum(*in) || *in == '-' || *in == '_' || *in == '.' || *in == '~')
donatien 0:2ccb9960a044 159 {
donatien 0:2ccb9960a044 160 count++;
donatien 0:2ccb9960a044 161 }
donatien 0:2ccb9960a044 162 else if( *in == ' ' )
donatien 0:2ccb9960a044 163 {
donatien 0:2ccb9960a044 164 count++;
donatien 0:2ccb9960a044 165 }
donatien 0:2ccb9960a044 166 else
donatien 0:2ccb9960a044 167 {
donatien 0:2ccb9960a044 168 count+=3;
donatien 0:2ccb9960a044 169 }
donatien 0:2ccb9960a044 170 in++;
donatien 0:2ccb9960a044 171 }
donatien 0:2ccb9960a044 172
donatien 0:2ccb9960a044 173 count ++;
donatien 0:2ccb9960a044 174
donatien 0:2ccb9960a044 175 in = m_values[i];
donatien 0:2ccb9960a044 176 while( (*in != '\0') )
donatien 0:2ccb9960a044 177 {
donatien 0:2ccb9960a044 178 if (std::isalnum(*in) || *in == '-' || *in == '_' || *in == '.' || *in == '~')
donatien 0:2ccb9960a044 179 {
donatien 0:2ccb9960a044 180 count++;
donatien 0:2ccb9960a044 181 }
donatien 0:2ccb9960a044 182 else if( *in == ' ' )
donatien 0:2ccb9960a044 183 {
donatien 0:2ccb9960a044 184 count++;
donatien 0:2ccb9960a044 185 }
donatien 0:2ccb9960a044 186 else
donatien 0:2ccb9960a044 187 {
donatien 0:2ccb9960a044 188 count+=3;
donatien 0:2ccb9960a044 189 }
donatien 0:2ccb9960a044 190 in++;
donatien 0:2ccb9960a044 191 }
donatien 0:2ccb9960a044 192 }
donatien 0:2ccb9960a044 193 return count;
donatien 0:2ccb9960a044 194 }