10000 Sync 2017-09-03 · EdWeller/esp32-snippets@10aa8d1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 10aa8d1

Browse files
committed
Sync 2017-09-03
1 parent f51aca1 commit 10aa8d1

14 files changed

+461
-192
lines changed

cpp_utils/FATFS_VFS.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
#include "FATFS_VFS.h"
99
#include <esp_err.h>
10-
extern "C" {
10+
1111
#include <esp_vfs_fat.h>
12-
}
12+
1313

1414
/**
1515
* @brief Constructor.

cpp_utils/HttpParser.cpp

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
* Author: kolban
66
*/
77

8-
#include "HttpParser.h"
98
#include <string>
9+
#include <iostream>
10+
#include <cstdlib>
11+
#include "HttpParser.h"
12+
#include "HttpRequest.h"
13+
1014
#include <esp_log.h>
1115
/**
1216
* 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
7074
}
7175
ret += *it;
7276
}
73-
7477
} // for
7578
return ret;
7679
} // toStringToken
@@ -96,8 +99,6 @@ static std::string toCharToken(std::string::iterator &it, std::string &str, char
9699
} // toCharToken
97100

98101

99-
100-
101102
/**
102103
* @brief Parse a header line.
103104
* An HTTP Header is of the form:
@@ -139,34 +140,49 @@ void HttpParser::dump() {
139140
ESP_LOGD(LOG_TAG, "Body: \"%s\"", m_body.c_str());
140141
} // dump
141142

143+
142144
std::string HttpParser::getBody() {
143145
return m_body;
144146
}
145147

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)) {
148151
return "";
149152
}
150153
return m_headers.at(name);
151154
}
152155

156+
153157
std::map<std::string, std::string> HttpParser::getHeaders() {
154158
return m_headers;
155159
}
156160

161+
157162
std::string HttpParser::getMethod() {
158163
return m_method;
159164
} // getMethod
160165

166+
161167
std::string HttpParser::getURL() {
162168
return m_url;
163169
} // getURL
164170

171+
165172
std::string HttpParser::getVersion() {
166173
return m_version;
167174
} // getVersion
168175

169176

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+
170186

171187
/**
172188
* @brief Parse socket data.
@@ -181,13 +197,34 @@ void HttpParser::parse(Socket s) {
181197
m_headers.insert(parseHeader(line));
182198
line = s.readToDelim(lineTerminator);
183199
}
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());
184220
} // parse
185221

186222

187223
/**
188224
* @brief Parse a string message.
189225
* @param [in] message The HTTP message to parse.
190226
*/
227+
/*
191228
void HttpParser::parse(std::string message) {
192229
auto it = message.begin();
193230
auto line = toStringToken(it, message, lineTerminator);
@@ -202,7 +239,7 @@ void HttpParser::parse(std::string message) {
202239
203240
m_body = message.substr(std::distance(message.begin(), it));
204241
} // parse
205-
242+
*/
206243

207244
/**
208245
* @brief Parse A request line.
@@ -224,6 +261,3 @@ void HttpParser::parseRequestLine(std::string &line) {
224261
// Get the version
225262
m_version = toCharToken(it, line, ' ');
226263
} // parseRequestLine
227-
228-
229-

cpp_utils/HttpParser.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ class HttpParser {
2424
HttpParser();
2525
virtual ~HttpParser();
2626
std::string getBody();
27-
std::string getHeader(std::string& name);
27+
std::string getHeader(const std::string& name);
2828
std::map<std::string, std::string> getHeaders();
2929
std::string getMethod();
3030
std::string getURL();
3131
std::string getVersion();
32+
bool hasHeader(const std::string& name);
3233
void parse(std::string message);
3334
void parse(Socket s);
3435
};

0 commit comments

Comments
 (0)
0