mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-10-19 11:19:36 +00:00
Merge commit from fork
This commit is contained in:
1 parent
0e4f104141
commit
8bffbe3ff2
2 files changed
+54
-7
No files matched your search
@@ -7770,13 +7770,14 @@ public:
|
|||||||
|
|
||||||
it = params.find("filename*");
|
it = params.find("filename*");
|
||||||
if (it != params.end()) {
|
if (it != params.end()) {
|
||||||
// Only allow UTF-8 encoding...
|
// RFC 5987: only UTF-8 encoding is allowed
|
||||||
thread_local const std::regex re_rfc5987_encoding(
|
const auto &val = it->second;
|
||||||
R"~(^UTF-8''(.+?)$)~", std::regex_constants::icase);
|
constexpr size_t prefix_len = 7; // length of "UTF-8''"
|
||||||
|
if (val.size() > prefix_len &&
|
||||||
std::smatch m2;
|
case_ignore::to_lower(val.substr(0, prefix_len)) ==
|
||||||
if (std::regex_match(it->second, m2, re_rfc5987_encoding)) {
|
"utf-8''") {
|
||||||
file_.filename = decode_path_component(m2[1]); // override...
|
file_.filename = decode_path_component(
|
||||||
|
val.substr(prefix_len)); // override...
|
||||||
} else {
|
} else {
|
||||||
is_valid_ = false;
|
is_valid_ = false;
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -11609,6 +11609,52 @@ TEST(MultipartFormDataTest, AlternateFilename) {
|
|||||||
ASSERT_TRUE(send_request(1, req));
|
ASSERT_TRUE(send_request(1, req));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(MultipartFormDataTest, AlternateFilenameLongValueAndCaseInsensitive) {
|
||||||
|
auto handled = false;
|
||||||
|
|
||||||
|
Server svr;
|
||||||
|
svr.Post("/test", [&](const Request &req, Response &res) {
|
||||||
|
// Case-insensitive "utf-8''" prefix with a long value
|
||||||
|
const auto &file = req.form.get_file("file1");
|
||||||
|
ASSERT_EQ("file1", file.name);
|
||||||
|
std::string expected_filename(2000, 'A');
|
||||||
|
ASSERT_EQ(expected_filename, file.filename);
|
||||||
|
|
||||||
|
res.set_content("ok", "text/plain");
|
||||||
|
handled = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
thread t = thread([&] { svr.listen(HOST, PORT); });
|
||||||
|
auto se = detail::scope_exit([&] {
|
||||||
|
svr.stop();
|
||||||
|
t.join();
|
||||||
|
ASSERT_FALSE(svr.is_running());
|
||||||
|
ASSERT_TRUE(handled);
|
||||||
|
});
|
||||||
|
|
||||||
|
svr.wait_until_ready();
|
||||||
|
|
||||||
|
// Build body with a long filename* value using mixed-case prefix "Utf-8''"
|
||||||
|
// Prior to the fix, this would cause O(N) stack recursion in std::regex
|
||||||
|
std::string long_filename(2000, 'A');
|
||||||
|
std::string body = "----------\r\n"
|
||||||
|
"Content-Disposition: form-data; name=\"file1\"; "
|
||||||
|
"filename*=\"Utf-8''" +
|
||||||
|
long_filename +
|
||||||
|
"\"\r\n"
|
||||||
|
"\r\n"
|
||||||
|
"hello\r\n"
|
||||||
|
"------------\r\n";
|
||||||
|
|
||||||
|
auto req = "POST /test HTTP/1.1\r\n"
|
||||||
|
"Content-Type: multipart/form-data;boundary=--------\r\n"
|
||||||
|
"Content-Length: " +
|
||||||
|
std::to_string(body.size()) +
|
||||||
|
"\r\n\r\n" + body;
|
||||||
|
|
||||||
|
ASSERT_TRUE(send_request(1, req));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(MultipartFormDataTest, CloseDelimiterWithoutCRLF) {
|
TEST(MultipartFormDataTest, CloseDelimiterWithoutCRLF) {
|
||||||
auto handled = false;
|
auto handled = false;
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user