Skip to content

Commit 0bc40d6

Browse files
committed
Refactors header parsing logic
Improves header parsing by using a dedicated parsing function, resulting in cleaner and more robust code. This change also adds error handling during header parsing, returning an error and marking the request as invalid if parsing fails.
1 parent 3a492c1 commit 0bc40d6

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

httplib.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5046,12 +5046,14 @@ class MultipartFormDataParser {
50465046
return false;
50475047
}
50485048

5049-
// split header string by ':' and emplace space trimmed into headers map
5050-
auto colon_pos = header.find(':');
5051-
if (colon_pos != std::string::npos) {
5052-
auto key = trim_copy(header.substr(0, colon_pos));
5053-
auto val = trim_copy(header.substr(colon_pos + 1));
5054-
file_.headers.emplace(key, val);
5049+
// parse and emplace space trimmed headers into a map
5050+
if (!parse_header(
5051+
header.data(), header.data() + header.size(),
5052+
[&](const std::string &key, const std::string &val) {
5053+
file_.headers.emplace(key, val);
5054+
})) {
5055+
is_valid_ = false;
5056+
return false;
50555057
}
50565058

50575059
constexpr const char header_content_type[] = "Content-Type:";

0 commit comments

Comments
 (0)