From 924b5cecf0a5730c3a0801ef0042b93ae7dc6c68 Mon Sep 17 00:00:00 2001 From: mervindejong Date: Wed, 14 Oct 2026 21:35:27 +0200 Subject: [PATCH] Fix redirect for mounted folders (#2628) A directory request without a trailing slash was redirected to the path relative to the mount point, so /mount/dir under a mount at /mount sent Location: //dir/ instead of /mount/dir/. Redirect to the request path with the slash appended. --- httplib.h | 2 +- test/test.cc | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 675ca8b..96c24e3 100644 --- a/httplib.h +++ b/httplib.h @@ -13869,7 +13869,7 @@ inline bool Server::handle_file_request(Request &req, Response &res) { detail::FileStat stat(path); if (stat.is_dir()) { - res.set_redirect(sub_path + "/", StatusCode::MovedPermanently_301); + res.set_redirect(req.path + "/", StatusCode::MovedPermanently_301); return true; } diff --git a/test/test.cc b/test/test.cc index bd81b0c..663d902 100644 --- a/test/test.cc +++ b/test/test.cc @@ -6660,6 +6660,13 @@ TEST_F(ServerTest, GetMethodOutOfBaseDirMountWithBackslash) { EXPECT_EQ(StatusCode::NotFound_404, res->status); } +TEST_F(ServerTest, GetMethodDirMountWithoutTrailingSlash) { + auto res = cli_.Get("/mount/dir"); + ASSERT_TRUE(res) << "Error: " << to_string(res.error()); + EXPECT_EQ(StatusCode::MovedPermanently_301, res->status); + EXPECT_EQ("/mount/dir/", res->get_header_value("Location")); +} + TEST_F(ServerTest, PostMethod303) { auto res = cli_.Post("/1", "body", "text/plain"); ASSERT_TRUE(res) << "Error: " << to_string(res.error());