Httpclient changes

Dependents:   Final_ver2 Final_test

Fork of HTTPClient by Toyomasa Watarai

Committer:
jderiso2
Date:
Mon Apr 27 17:38:13 2015 +0000
Revision:
22:9e246a41abe8
Parent:
16:1f743885e7de
updates;

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