It is the new miniTLS program which build the connect with the website httpbin.org

Fork of MiniTLS-HTTPSClient by Donatien Garnier

Committer:
shiyilei
Date:
Fri Feb 06 06:18:13 2015 +0000
Revision:
1:4309aff9b1db
Parent:
0:62a4a8ec4ab5
This is the new MiniTLS program which build the connection with the website httpbin.org

Who changed what in which revision?

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