From 852a66bcec1d65fb54698e912ea2bc8cb91a24b8 Mon Sep 17 00:00:00 2001 From: Leng Niu <344562942+vtxe02023274@users.noreply.github.com> Date: Wed, 2 Dec 2026 13:21:30 +0800 Subject: [PATCH] Accept a language tag in a multipart filename* parameter (#2704) * Accept a language tag in a multipart filename* parameter RFC 8187 writes an ext-value as charset'[language]'value-chars. The parser only took the "UTF-8''" form, so filename*=UTF-8'en'%41.txt made the whole multipart body invalid and the request got 400. Skip the language tag and decode the value as before. * Shorten the comments and close the connection in the new test Without Connection: close the server kept the connection open, so the test waited for the 1-second read timeout of send_request(). --------- Co-authored-by: yhirose --- httplib.h | 13 ++++++++----- test/test.cc | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/httplib.h b/httplib.h index 6aa04375..32ad9c6e 100644 --- a/httplib.h +++ b/httplib.h @@ -10516,14 +10516,17 @@ public: it = params.find("filename*"); if (it != params.end()) { - // RFC 5987: only UTF-8 encoding is allowed + // RFC 8187: UTF-8'[language]'value-chars const auto &val = it->second; - constexpr const char utf8_prefix[] = "UTF-8''"; + constexpr const char utf8_prefix[] = "UTF-8'"; constexpr size_t prefix_len = str_len(utf8_prefix); - if (val.size() > prefix_len && - start_with_case_ignore(val, utf8_prefix)) { + const auto value_pos = start_with_case_ignore(val, utf8_prefix) + ? val.find('\'', prefix_len) + : std::string::npos; + if (value_pos != std::string::npos && + value_pos + 1 < val.size()) { file_.filename = decode_path_component( - val.substr(prefix_len)); // override... + val.substr(value_pos + 1)); // override... } else { is_valid_ = false; return false; diff --git a/test/test.cc b/test/test.cc index 6541dbbf..7f977bea 100644 --- a/test/test.cc +++ b/test/test.cc @@ -20444,6 +20444,50 @@ TEST(MultipartFormDataTest, AlternateFilenameLongValueAndCaseInsensitive) { ASSERT_TRUE(send_request(1, req)); } +TEST(MultipartFormDataTest, AlternateFilenameWithLanguageTag) { + auto handled = false; + + Server svr; + svr.Post("/test", [&](const Request &req, Response &res) { + ASSERT_EQ("A.txt", req.form.get_file("file1").filename); + ASSERT_EQ("na\xC3\xAFve.txt", req.form.get_file("file2").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(); + + std::string body = "----------\r\n" + "Content-Disposition: form-data; name=\"file1\"; " + "filename*=UTF-8'en'%41.txt\r\n" + "\r\n" + "hello\r\n" + "----------\r\n" + "Content-Disposition: form-data; name=\"file2\"; " + "filename*=utf-8'fr-CA'na%C3%AFve.txt\r\n" + "\r\n" + "bonjour\r\n" + "------------\r\n"; + + auto req = "POST /test HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\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) { auto handled = false;