Simple IoT Board用のライブラリです。 ESP8266ライブラリの軽量化 送信のみのソフトシリアルライブラリを含んでいます。

Dependents:   SITB_HttpGetSample SITB_IFTTTSample SITB_INA226PRC AmbientExampleSITB ... more

Committer:
jksoft
Date:
Sun Nov 15 13:36:44 2015 +0000
Revision:
0:890c12951e96
??

Who changed what in which revision?

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