parent
913314f1b1
commit
12c829f6d3
@ -6540,6 +6540,11 @@ inline bool Server::handle_file_request(const Request &req, Response &res,
|
|||||||
auto path = entry.base_dir + sub_path;
|
auto path = entry.base_dir + sub_path;
|
||||||
if (path.back() == '/') { path += "index.html"; }
|
if (path.back() == '/') { path += "index.html"; }
|
||||||
|
|
||||||
|
if (detail::is_dir(path)) {
|
||||||
|
res.set_redirect(sub_path + "/", StatusCode::MovedPermanently_301);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
if (detail::is_file(path)) {
|
if (detail::is_file(path)) {
|
||||||
for (const auto &kv : entry.headers) {
|
for (const auto &kv : entry.headers) {
|
||||||
res.set_header(kv.first, kv.second);
|
res.set_header(kv.first, kv.second);
|
||||||
|
32
test/test.cc
32
test/test.cc
@ -5068,6 +5068,38 @@ TEST(MountTest, Unmount) {
|
|||||||
EXPECT_EQ(StatusCode::NotFound_404, res->status);
|
EXPECT_EQ(StatusCode::NotFound_404, res->status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(MountTest, Redicect) {
|
||||||
|
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/");
|
||||||
|
ASSERT_TRUE(res);
|
||||||
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||||
|
|
||||||
|
res = cli.Get("/dir");
|
||||||
|
ASSERT_TRUE(res);
|
||||||
|
EXPECT_EQ(StatusCode::MovedPermanently_301, res->status);
|
||||||
|
|
||||||
|
res = cli.Get("/file");
|
||||||
|
ASSERT_TRUE(res);
|
||||||
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||||
|
|
||||||
|
res = cli.Get("/file/");
|
||||||
|
ASSERT_TRUE(res);
|
||||||
|
EXPECT_EQ(StatusCode::NotFound_404, res->status);
|
||||||
|
}
|
||||||
|
|
||||||
#ifndef CPPHTTPLIB_NO_EXCEPTIONS
|
#ifndef CPPHTTPLIB_NO_EXCEPTIONS
|
||||||
TEST(ExceptionTest, ThrowExceptionInHandler) {
|
TEST(ExceptionTest, ThrowExceptionInHandler) {
|
||||||
Server svr;
|
Server svr;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user