Skip to content

Commit

Permalink
Modify static files router and 404 pages generator (#497)
Browse files Browse the repository at this point in the history
1.Except for the GET method, it is forbidden to use any other method for accessing static files.
2.Use following sequence to create 404 pages.
 * try to use user customized 404 handler;
 * try to use user customized error handler;
 * use default handler to create 404 pages;
  • Loading branch information
an-tao authored Jul 2, 2020
1 parent 630beed commit f871d16
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/src/HttpAppFrameworkImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,7 @@ HttpAppFramework &HttpAppFrameworkImpl::setCustomErrorHandler(
std::function<HttpResponsePtr(HttpStatusCode)> &&resp_generator)
{
customErrorHandler_ = std::move(resp_generator);
usingCustomErrorHandler_ = true;
return *this;
}

Expand Down
5 changes: 5 additions & 0 deletions lib/src/HttpAppFrameworkImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,10 @@ class HttpAppFrameworkImpl : public HttpAppFramework
virtual bool areAllDbClientsAvailable() const noexcept override;
const std::function<HttpResponsePtr(HttpStatusCode)>
&getCustomErrorHandler() const override;
bool isUsingCustomErrorHandler() const
{
return usingCustomErrorHandler_;
}

private:
virtual void registerHttpController(
Expand Down Expand Up @@ -558,6 +562,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework
bool useGzip_{true};
bool useBrotli_{false};
bool usingUnicodeEscaping_{true};
bool usingCustomErrorHandler_{false};
size_t clientMaxBodySize_{1024 * 1024};
size_t clientMaxMemoryBodySize_{64 * 1024};
size_t clientMaxWebSocketMessageSize_{128 * 1024};
Expand Down
32 changes: 23 additions & 9 deletions lib/src/HttpResponseImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,35 @@ HttpResponsePtr HttpResponse::newNotFoundResponse()
static std::once_flag threadOnce;
static IOThreadStorage<HttpResponsePtr> thread404Pages;
std::call_once(threadOnce, [] {
thread404Pages.init([](drogon::HttpResponsePtr &resp,
size_t index) {
HttpViewData data;
data.insert("version", drogon::getVersion());
resp = HttpResponse::newHttpViewResponse("drogon::NotFound",
data);
resp->setStatusCode(k404NotFound);
resp->setExpiredTime(0);
});
thread404Pages.init(
[](drogon::HttpResponsePtr &resp, size_t index) {
if (HttpAppFrameworkImpl::instance()
.isUsingCustomErrorHandler())
{
resp = app().getCustomErrorHandler()(k404NotFound);
resp->setExpiredTime(0);
}
else
{
HttpViewData data;
data.insert("version", drogon::getVersion());
resp = HttpResponse::newHttpViewResponse(
"drogon::NotFound", data);
resp->setStatusCode(k404NotFound);
resp->setExpiredTime(0);
}
});
});
LOG_TRACE << "Use cached 404 response";
return thread404Pages.getThreadData();
}
else
{
if (HttpAppFrameworkImpl::instance().isUsingCustomErrorHandler())
{
auto resp = app().getCustomErrorHandler()(k404NotFound);
return resp;
}
HttpViewData data;
data.insert("version", drogon::getVersion());
auto notFoundResp =
Expand Down
5 changes: 5 additions & 0 deletions lib/src/StaticFileRouter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ void StaticFileRouter::route(
callback(app().getCustomErrorHandler()(k403Forbidden));
return;
}
if (req->method() != Get)
{
callback(app().getCustomErrorHandler()(k405MethodNotAllowed));
return;
}
auto lPath = path;
std::transform(lPath.begin(), lPath.end(), lPath.begin(), tolower);

Expand Down

0 comments on commit f871d16

Please sign in to comment.