Skip to content

Commit 70f4d7d

Browse files
Add test for multipart form data with boundary split
Add a test for multipart form data requests with a large header which leads to a split inside the boundary because of the read buffer size inside the SocketStream class.
1 parent 08a0452 commit 70f4d7d

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

test/test.cc

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8074,6 +8074,60 @@ TEST(MultipartFormDataTest, AccessPartHeaders) {
80748074
}
80758075
#endif
80768076

8077+
TEST(MultipartFormDataTest, LargeHeader) {
8078+
auto handled = false;
8079+
8080+
Server svr;
8081+
svr.Post("/test", [&](const Request &req, Response &) {
8082+
ASSERT_EQ(1u, req.files.size());
8083+
8084+
auto it = req.files.begin();
8085+
ASSERT_EQ("name1", it->second.name);
8086+
ASSERT_EQ("text1", it->second.content);
8087+
8088+
handled = true;
8089+
});
8090+
8091+
thread t = thread([&] { svr.listen(HOST, PORT); });
8092+
auto se = detail::scope_exit([&] {
8093+
svr.stop();
8094+
t.join();
8095+
ASSERT_FALSE(svr.is_running());
8096+
ASSERT_TRUE(handled);
8097+
});
8098+
8099+
svr.wait_until_ready();
8100+
8101+
auto boundary = std::string("cpp-httplib-multipart-data");
8102+
std::string content = "--" + boundary +
8103+
"\r\n"
8104+
"Content-Disposition: form-data; name=\"name1\"\r\n"
8105+
"\r\n"
8106+
"text1\r\n"
8107+
"--" +
8108+
boundary + "--\r\n";
8109+
std::string header_prefix = "POST /test HTTP/1.1\r\n"
8110+
"Content-Type: multipart/form-data;boundary=" +
8111+
boundary +
8112+
"\r\n"
8113+
"Content-Length: " +
8114+
std::to_string(content.size()) +
8115+
"\r\n"
8116+
"Dummy-Header: ";
8117+
std::string header_suffix = "\r\n"
8118+
"\r\n";
8119+
size_t read_buff_size = 1024u * 4; // SocketStream::read_buff_size_
8120+
size_t header_dummy_size =
8121+
read_buff_size -
8122+
(header_prefix.size() + header_suffix.size() + boundary.size() / 2);
8123+
auto header_dummy = std::string(header_dummy_size, '@');
8124+
auto req = header_prefix + header_dummy + header_suffix + content;
8125+
8126+
std::string response;
8127+
ASSERT_TRUE(send_request(1, req, &response));
8128+
ASSERT_EQ("200", response.substr(9, 3));
8129+
}
8130+
80778131
TEST(TaskQueueTest, IncreaseAtomicInteger) {
80788132
static constexpr unsigned int number_of_tasks{1000000};
80798133
std::atomic_uint count{0};

0 commit comments

Comments
 (0)