Skip to content

Commit cddaeda

Browse files
committed
1 parent cefb5a8 commit cddaeda

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

httplib.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4332,10 +4332,19 @@ class MultipartFormDataParser {
43324332
break;
43334333
}
43344334

4335-
static const std::string header_name = "content-type:";
4335+
static const std::string header_content_type = "Content-Type:";
4336+
static const std::string header_content_length = "Content-Length:";
4337+
43364338
const auto header = buf_head(pos);
4337-
if (start_with_case_ignore(header, header_name)) {
4338-
file_.content_type = trim_copy(header.substr(header_name.size()));
4339+
if (start_with_case_ignore(header, header_content_type)) {
4340+
file_.content_type =
4341+
trim_copy(header.substr(header_content_type.size()));
4342+
} else if (start_with_case_ignore(header, header_content_length)) {
4343+
// NOTE: For now, we ignore the content length. In the future, the
4344+
// parser should check if the actual body length is same as this
4345+
// value.
4346+
// auto content_length = std::stoi(
4347+
// trim_copy(header.substr(header_content_length.size())));
43394348
} else {
43404349
static const std::regex re_content_disposition(
43414350
R"~(^Content-Disposition:\s*form-data;\s*(.*)$)~",

test/test.cc

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6298,6 +6298,53 @@ TEST(MultipartFormDataTest, CloseDelimiterWithoutCRLF) {
62986298
ASSERT_EQ("200", resonse.substr(9, 3));
62996299
}
63006300

6301+
TEST(MultipartFormDataTest, ContentLength) {
6302+
auto handled = false;
6303+
6304+
Server svr;
6305+
svr.Post("/test", [&](const Request &req, Response &) {
6306+
ASSERT_EQ(2u, req.files.size());
6307+
6308+
auto it = req.files.begin();
6309+
ASSERT_EQ("text1", it->second.name);
6310+
ASSERT_EQ("text1", it->second.content);
6311+
6312+
++it;
6313+
ASSERT_EQ("text2", it->second.name);
6314+
ASSERT_EQ("text2", it->second.content);
6315+
6316+
handled = true;
6317+
});
6318+
6319+
thread t = thread([&] { svr.listen(HOST, PORT); });
6320+
auto se = detail::scope_exit([&] {
6321+
svr.stop();
6322+
t.join();
6323+
ASSERT_FALSE(svr.is_running());
6324+
ASSERT_TRUE(handled);
6325+
});
6326+
6327+
svr.wait_until_ready();
6328+
6329+
auto req = "POST /test HTTP/1.1\r\n"
6330+
"Content-Type: multipart/form-data;boundary=--------\r\n"
6331+
"Content-Length: 167\r\n"
6332+
"\r\n----------\r\n"
6333+
"Content-Disposition: form-data; name=\"text1\"\r\n"
6334+
"Content-Length: 5\r\n"
6335+
"\r\n"
6336+
"text1"
6337+
"\r\n----------\r\n"
6338+
"Content-Disposition: form-data; name=\"text2\"\r\n"
6339+
"\r\n"
6340+
"text2"
6341+
"\r\n------------\r\n";
6342+
6343+
std::string resonse;
6344+
ASSERT_TRUE(send_request(1, req, &resonse));
6345+
ASSERT_EQ("200", resonse.substr(9, 3));
6346+
}
6347+
63016348
#endif
63026349

63036350
#ifndef _WIN32

0 commit comments

Comments
 (0)