]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/tests/net/cellular/http/common/HTTPClient/data/HTTPMap.cpp
Merge commit 'f6d56675f9f981c5464f0ca7a1fbb0162154e8c5'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / tests / net / cellular / http / common / 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 /*virtual*/ void HTTPMap::readReset()
53 {
54 m_pos = 0;
55 }
56
57 /*virtual*/ int HTTPMap::read(char* buf, size_t len, size_t* pReadLen)
58 {
59 if(m_pos >= m_count)
60 {
61 *pReadLen = 0;
62 m_pos = 0;
63 return OK;
64 }
65
66 //URL encode
67 char* out = buf;
68 const char* in = m_keys[m_pos];
69 if( (m_pos != 0) && (out - buf < len - 1) )
70 {
71 *out='&';
72 out++;
73 }
74
75 while( (*in != '\0') && (out - buf < len - 3) )
76 {
77 if (std::isalnum(*in) || *in == '-' || *in == '_' || *in == '.' || *in == '~')
78 {
79 *out = *in;
80 out++;
81 }
82 else if( *in == ' ' )
83 {
84 *out='+';
85 out++;
86 }
87 else
88 {
89 char hex[] = "0123456789abcdef";
90 *out='%';
91 out++;
92 *out=hex[(*in>>4)&0xf];
93 out++;
94 *out=hex[(*in)&0xf];
95 out++;
96 }
97 in++;
98 }
99
100 if( out - buf < len - 1 )
101 {
102 *out='=';
103 out++;
104 }
105
106 in = m_values[m_pos];
107 while( (*in != '\0') && (out - buf < len - 3) )
108 {
109 if (std::isalnum(*in) || *in == '-' || *in == '_' || *in == '.' || *in == '~')
110 {
111 *out = *in;
112 out++;
113 }
114 else if( *in == ' ' )
115 {
116 *out='+';
117 out++;
118 }
119 else
120 {
121 char hex[] = "0123456789abcdef";
122 *out='%';
123 out++;
124 *out=hex[(*in>>4)&0xf];
125 out++;
126 *out=hex[(*in)&0xf];
127 out++;
128 }
129 in++;
130 }
131
132 *pReadLen = out - buf;
133
134 m_pos++;
135 return OK;
136 }
137
138 /*virtual*/ int HTTPMap::getDataType(char* type, size_t maxTypeLen) //Internet media type for Content-Type header
139 {
140 strncpy(type, "application/x-www-form-urlencoded", maxTypeLen-1);
141 type[maxTypeLen-1] = '\0';
142 return OK;
143 }
144
145 /*virtual*/ bool HTTPMap::getIsChunked() //For Transfer-Encoding header
146 {
147 return false; ////Data is computed one key/value pair at a time
148 }
149
150 /*virtual*/ size_t HTTPMap::getDataLen() //For Content-Length header
151 {
152 size_t count = 0;
153 for(size_t i = 0; i< m_count; i++)
154 {
155 //URL encode
156 const char* in = m_keys[i];
157 if( i != 0 )
158 {
159 count++;
160 }
161
162 while( (*in != '\0') )
163 {
164 if (std::isalnum(*in) || *in == '-' || *in == '_' || *in == '.' || *in == '~')
165 {
166 count++;
167 }
168 else if( *in == ' ' )
169 {
170 count++;
171 }
172 else
173 {
174 count+=3;
175 }
176 in++;
177 }
178
179 count ++;
180
181 in = m_values[i];
182 while( (*in != '\0') )
183 {
184 if (std::isalnum(*in) || *in == '-' || *in == '_' || *in == '.' || *in == '~')
185 {
186 count++;
187 }
188 else if( *in == ' ' )
189 {
190 count++;
191 }
192 else
193 {
194 count+=3;
195 }
196 in++;
197 }
198 }
199 return count;
200 }
Imprint / Impressum