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