httpclient

Dependents:   Lab_6

Committer:
rr387
Date:
Thu Dec 30 15:21:34 2021 +0000
Revision:
0:a988a72f184b
retes

Who changed what in which revision?

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