5
5
* Author: kolban
6
6
*/
7
7
8
- #include " HttpParser.h"
9
8
#include < string>
9
+ #include < iostream>
10
+ #include < cstdlib>
11
+ #include " HttpParser.h"
12
+ #include " HttpRequest.h"
13
+
10
14
#include < esp_log.h>
11
15
/* *
12
16
* RFC7230 - Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing
@@ -70,7 +74,6 @@ static std::string toStringToken(std::string::iterator &it, std::string &str, st
70
74
}
71
75
ret += *it;
72
76
}
73
-
74
77
} // for
75
78
return ret;
76
79
} // toStringToken
@@ -96,8 +99,6 @@ static std::string toCharToken(std::string::iterator &it, std::string &str, char
96
99
} // toCharToken
97
100
98
101
99
-
100
-
101
102
/* *
102
103
* @brief Parse a header line.
103
104
* An HTTP Header is of the form:
@@ -139,34 +140,49 @@ void HttpParser::dump() {
139
140
ESP_LOGD (LOG_TAG, " Body: \" %s\" " , m_body.c_str ());
140
141
} // dump
141
142
143
+
142
144
std::string HttpParser::getBody () {
143
145
return m_body;
144
146
}
145
147
146
- std::string HttpParser::getHeader (std::string& name) {
147
- if (m_headers.find (name) == m_headers.end ()) {
148
+
149
+ std::string HttpParser::getHeader (const std::string& name) {
150
+ if (!hasHeader (name)) {
148
151
return " " ;
149
152
}
150
153
return m_headers.at (name);
151
154
}
152
155
156
+
153
157
std::map<std::string, std::string> HttpParser::getHeaders () {
154
158
return m_headers;
155
159
}
156
160
161
+
157
162
std::string HttpParser::getMethod () {
158
163
return m_method;
159
164
} // getMethod
160
165
166
+
161
167
std::string HttpParser::getURL () {
162
168
return m_url;
163
169
} // getURL
164
170
171
+
165
172
std::string HttpParser::getVersion () {
166
173
return m_version;
167
174
} // getVersion
168
175
169
176
177
+ /* *
178
+ * @brief Determine if we have a header of the given name.
179
+ * @param [in] name The name of the header to find.
180
+ * @return True if the header is present and false otherwise.
181
+ */
182
+ bool HttpParser::hasHeader (const std::string& name) {
183
+ return m_headers.find (name) != m_headers.end ();
184
+ } // hasHeader
185
+
170
186
171
187
/* *
172
188
* @brief Parse socket data.
@@ -181,13 +197,34 @@ void HttpParser::parse(Socket s) {
181
197
m_headers.insert (parseHeader (line));
182
198
line = s.readToDelim (lineTerminator);
183
199
}
200
+ // Only PUT and POST requests have a body
201
+ if (getMethod () != " POST" && getMethod () != " PUT" ) {
202
+ return ;
203
+ }
204
+
205
+ // We have now parsed up to and including the separator ... we are now at the point where we
206
+ // want to read the body. There are two stories here. The first is that we know the exact length
207
+ // of the body or we read until we can't read anymore.
208
+ if (hasHeader (HttpRequest::HTTP_HEADER_CONTENT_LENGTH)) {
209
+ std::string val = getHeader (HttpRequest::HTTP_HEADER_CONTENT_LENGTH);
210
+ int length = std::atoi (val.c_str ());
211
+ uint8_t data[length];
212
+ s.receive_cpp (data, length, true );
213
+ m_body = std::string ((char *)data, length);
214
+ } else {
9E88
215
+ uint8_t data[512 ];
216
+ int rc = s.receive_cpp (data, sizeof (data));
217
+ m_body = std::string ((char *)data, rc);
218
+ }
219
+ ESP_LOGD (LOG_TAG, " Size of body: %d" , m_body.length ());
184
220
} // parse
185
221
186
222
187
223
/* *
188
224
* @brief Parse a string message.
189
225
* @param [in] message The HTTP message to parse.
190
226
*/
227
+ /*
191
228
void HttpParser::parse(std::string message) {
192
229
auto it = message.begin();
193
230
auto line = toStringToken(it, message, lineTerminator);
@@ -202,7 +239,7 @@ void HttpParser::parse(std::string message) {
202
239
203
240
m_body = message.substr(std::distance(message.begin(), it));
204
241
} // parse
205
-
242
+ */
206
243
207
244
/* *
208
245
* @brief Parse A request line.
@@ -224,6 +261,3 @@ void HttpParser::parseRequestLine(std::string &line) {
224
261
// Get the version
225
262
m_version = toCharToken (it, line, ' ' );
226
263
} // parseRequestLine
227
-
228
-
229
-
0 commit comments