HTTPClient for CyaSSL

Dependencies:   CyaSSL

Dependents:   kintone-POST kintone-POST

Fork of HTTPClient by wolf SSL

Committer:
donatien
Date:
Thu Apr 26 13:04:56 2012 +0000
Revision:
0:2ccb9960a044
Child:
10:e1351de84c16

        

Who changed what in which revision?

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