Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions lib/src/StaticFileRouter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,18 @@ void StaticFileRouter::route(
if (std::filesystem::is_directory(fsFilePath, err))
{
// Check if path is eligible for an implicit index.html
if (implicitPageEnable_)
if (implicitPageEnable_ && req->path().back() != '/')
{
std::string newLocation = req->path() + "/";
if (!req->query().empty())
{
newLocation += "?" + req->query();
}
callback(HttpResponse::newRedirectionResponse(
newLocation, k301MovedPermanently));
return;
}
else if (implicitPageEnable_)
{
filePath = filePath + "/" + implicitPage_;
}
Expand Down Expand Up @@ -244,7 +255,19 @@ void StaticFileRouter::route(
if (std::filesystem::is_directory(fsDirectoryPath, err))
{
// Check if path is eligible for an implicit index.html
if (implicitPageEnable_)
if (implicitPageEnable_ && req->path().back() != '/')
{
std::string newLocation = req->path() + "/";
if (!req->query().empty())
{
newLocation += "?" + req->query();
}
callback(
HttpResponse::newRedirectionResponse(newLocation,
k301MovedPermanently));
return;
}
else if (implicitPageEnable_)
{
std::string filePath = directoryPath + "/" + implicitPage_;
sendStaticFileResponse(filePath, req, std::move(callback), "");
Expand Down
9 changes: 9 additions & 0 deletions lib/tests/integration_test/client/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,15 @@ void doTest(const HttpClientPtr &client, std::shared_ptr<test::Case> TEST_CTX)
req = HttpRequest::newHttpRequest();
req->setMethod(drogon::Get);
req->setPath("/a-directory");
client->sendRequest(
req, [req, TEST_CTX](ReqResult result, const HttpResponsePtr &resp) {
REQUIRE(result == ReqResult::Ok);
CHECK(resp->getStatusCode() == k301MovedPermanently);
CHECK(resp->getHeader("Location") == "/a-directory/");
});
req = HttpRequest::newHttpRequest();
req->setMethod(drogon::Get);
req->setPath("/a-directory/");
client->sendRequest(req,
[req, TEST_CTX, body](ReqResult result,
const HttpResponsePtr &resp) {
Expand Down
Loading