mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-10-19 11:19:36 +00:00
Unescape quoted Content-Type parameters and escape backslashes
A quoted boundary kept its quoted-pairs, as only the quotes were taken off, and the multipart writer sent a backslash as it was, so a name ending in one escaped the closing quote for this library's own parser. Unescape \" and \\ in media type parameters as in Content-Disposition, and write a backslash as \\.
This commit is contained in:
1 parent
aa0a588ed5
commit
a10338a6ee
2 files changed
+82
-36
No files matched your search
@@ -1739,6 +1739,37 @@ TEST(MultipartOrderTest, RepeatedNamesKeepTheirOrder) {
|
||||
EXPECT_EQ("", out_of_range);
|
||||
}
|
||||
|
||||
TEST(MultipartFormDataTest, BackslashesInNamesSurvive) {
|
||||
Server svr;
|
||||
std::string file_names;
|
||||
svr.Post("/upload", [&](const Request &req, Response &res) {
|
||||
for (const auto &file : req.form.files) {
|
||||
file_names += file.first + "|" + file.second.filename + " ";
|
||||
}
|
||||
res.set_content("ok", "text/plain");
|
||||
});
|
||||
|
||||
auto port = svr.bind_to_any_port(HOST);
|
||||
thread t = thread([&] { svr.listen_after_bind(); });
|
||||
auto se = detail::scope_exit([&] {
|
||||
svr.stop();
|
||||
t.join();
|
||||
ASSERT_FALSE(svr.is_running());
|
||||
});
|
||||
svr.wait_until_ready();
|
||||
|
||||
// A trailing backslash would otherwise escape the closing quote.
|
||||
UploadFormDataItems items = {
|
||||
{"dir\\", "x", "a.txt", "text/plain"},
|
||||
{"b", "y", "a\\\\b.txt", "text/plain"},
|
||||
};
|
||||
|
||||
Client cli(HOST, port);
|
||||
auto res = cli.Post("/upload", items);
|
||||
ASSERT_TRUE(res);
|
||||
EXPECT_EQ("dir\\|a.txt b|a\\\\b.txt ", file_names);
|
||||
}
|
||||
|
||||
TEST(MultipartOrderTest, ContentSurvivesContainerGrowth) {
|
||||
// Server::read_content() keeps a FormFields::iterator alive across the
|
||||
// content callbacks that fill the part it points at. The container is
|
||||
@@ -1872,6 +1903,16 @@ TEST(ParseMultipartBoundaryTest, QuotedValueWithSemicolon) {
|
||||
EXPECT_EQ(parsed, "a;b");
|
||||
}
|
||||
|
||||
TEST(ParseMultipartBoundaryTest, QuotedPairsAreUnescaped) {
|
||||
string parsed;
|
||||
EXPECT_TRUE(detail::parse_multipart_boundary(
|
||||
"multipart/form-data; boundary=\"a\\\\b\"", parsed));
|
||||
EXPECT_EQ("a\\b", parsed);
|
||||
EXPECT_TRUE(detail::parse_multipart_boundary(
|
||||
"multipart/form-data; boundary=\"a\\\"b\"", parsed));
|
||||
EXPECT_EQ("a\"b", parsed);
|
||||
}
|
||||
|
||||
TEST(ParseDispositionParamsTest, QuotedValues) {
|
||||
// RFC 9110 Section 5.6.6: a parameter value may be a quoted-string, and
|
||||
// ';' and '=' are ordinary characters inside one.
|
||||
|
||||
Reference in new issue
Block a user