From 95fb89eb301f1b2b1d1b11aa518ac297c92387fe Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 13 Nov 2024 19:32:56 -0500 Subject: [PATCH] Fix #1973 --- httplib.h | 11 ++++++++-- test/test.cc | 21 +++++++++++++++++++ ...6\227\245\346\234\254\350\252\236File.txt" | 1 + 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 "test/www/\346\227\245\346\234\254\350\252\236Dir/\346\227\245\346\234\254\350\252\236File.txt" diff --git a/httplib.h b/httplib.h index c621f5b53d..f4a6d9cc60 100644 --- a/httplib.h +++ b/httplib.h @@ -2910,8 +2910,15 @@ inline bool mmap::open(const char *path) { #if defined(_WIN32) std::wstring wpath; - for (size_t i = 0; i < strlen(path); i++) { - wpath += path[i]; + { + auto len = static_cast(strlen(path)); + auto wlen = ::MultiByteToWideChar(CP_UTF8, 0, path, len, nullptr, 0); + if (wlen <= 0) { return false; } + + wpath.resize(wlen); + auto pwpath = const_cast(reinterpret_cast(wpath.data())); + wlen = ::MultiByteToWideChar(CP_UTF8, 0, path, len, pwpath, wlen); + if (wlen != wpath.size()) { return false; } } #if _WIN32_WINNT >= _WIN32_WINNT_WIN8 diff --git a/test/test.cc b/test/test.cc index 154b60c5e2..dfa4e1a14e 100644 --- a/test/test.cc +++ b/test/test.cc @@ -5271,6 +5271,27 @@ TEST(MountTest, Redicect) { EXPECT_EQ(StatusCode::OK_200, res->status); } +TEST(MountTest, MultibytesPathName) { + Server svr; + + auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.set_mount_point("/", "./www"); + svr.wait_until_ready(); + + Client cli("localhost", PORT); + + auto res = cli.Get("/日本語Dir/日本語File.txt"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ("日本語コンテンツ", res->body); +} + TEST(KeepAliveTest, ReadTimeout) { Server svr; diff --git "a/test/www/\346\227\245\346\234\254\350\252\236Dir/\346\227\245\346\234\254\350\252\236File.txt" "b/test/www/\346\227\245\346\234\254\350\252\236Dir/\346\227\245\346\234\254\350\252\236File.txt" new file mode 100644 index 0000000000..3cc1ce7c92 --- /dev/null +++ "b/test/www/\346\227\245\346\234\254\350\252\236Dir/\346\227\245\346\234\254\350\252\236File.txt" @@ -0,0 +1 @@ +日本語コンテンツ \ No newline at end of file