mirror of
https://gitee.com/an-tao/drogon.git
synced 2024-12-02 03:38:03 +08:00
Merge pull request #119 from an-tao/dev
Add the setCustom404Page() method
This commit is contained in:
commit
dc4f8aac38
@ -109,6 +109,13 @@ class HttpAppFramework : public trantor::NonCopyable
|
||||
*/
|
||||
virtual trantor::EventLoop *getLoop() = 0;
|
||||
|
||||
///Set custom 404 page
|
||||
/**
|
||||
* After calling this method, the @param resp object is returned
|
||||
* by the HttpResponse::newNotFoundResponse() method.
|
||||
*/
|
||||
virtual void setCustom404Page(const HttpResponsePtr &resp) = 0;
|
||||
|
||||
///Get the plugin object registered in the framework
|
||||
/**
|
||||
* NOTE:
|
||||
|
@ -83,6 +83,16 @@ class HttpAppFrameworkImpl : public HttpAppFramework
|
||||
const std::string &crtlName,
|
||||
const std::vector<any> &filtersAndMethods =
|
||||
std::vector<any>()) override;
|
||||
|
||||
virtual void setCustom404Page(const HttpResponsePtr &resp) override
|
||||
{
|
||||
resp->setStatusCode(k404NotFound);
|
||||
_custom404 = resp;
|
||||
}
|
||||
const HttpResponsePtr &getCustom404Page()
|
||||
{
|
||||
return _custom404;
|
||||
}
|
||||
virtual void enableSession(const size_t timeout = 0) override
|
||||
{
|
||||
_useSession = true;
|
||||
@ -238,6 +248,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework
|
||||
//Json::Value _customConfig;
|
||||
Json::Value _jsonConfig;
|
||||
PluginsManager _pluginsManager;
|
||||
HttpResponsePtr _custom404;
|
||||
#if USE_ORM
|
||||
std::map<std::string, orm::DbClientPtr> _dbClientsMap;
|
||||
struct DbInfo
|
||||
|
@ -47,13 +47,25 @@ HttpResponsePtr HttpResponse::newHttpJsonResponse(const Json::Value &data)
|
||||
|
||||
HttpResponsePtr HttpResponse::newNotFoundResponse()
|
||||
{
|
||||
HttpViewData data;
|
||||
data.insert("version", getVersion());
|
||||
auto res = HttpResponse::newHttpViewResponse("NotFound", data);
|
||||
res->setStatusCode(k404NotFound);
|
||||
//res->setCloseConnection(true);
|
||||
|
||||
return res;
|
||||
auto &resp = HttpAppFrameworkImpl::instance().getCustom404Page();
|
||||
if(resp)
|
||||
{
|
||||
return resp;
|
||||
}
|
||||
else
|
||||
{
|
||||
static std::once_flag once;
|
||||
static HttpResponsePtr notFoundResp;
|
||||
std::call_once(once, []() {
|
||||
HttpViewData data;
|
||||
data.insert("version", getVersion());
|
||||
notFoundResp = HttpResponse::newHttpViewResponse("NotFound", data);
|
||||
notFoundResp->setStatusCode(k404NotFound);
|
||||
});
|
||||
return notFoundResp;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
HttpResponsePtr HttpResponse::newLocationRedirectResponse(const std::string &path)
|
||||
{
|
||||
|
@ -119,7 +119,6 @@ void HttpServer::onMessage(const TcpConnectionPtr &conn,
|
||||
MsgBuffer *buf)
|
||||
{
|
||||
HttpRequestParser *requestParser = any_cast<HttpRequestParser>(conn->getMutableContext());
|
||||
int counter = 0;
|
||||
// With the pipelining feature or web socket, it is possible to receice multiple messages at once, so
|
||||
// the while loop is necessary
|
||||
if (requestParser->webSocketConn())
|
||||
@ -166,9 +165,6 @@ void HttpServer::onMessage(const TcpConnectionPtr &conn,
|
||||
else
|
||||
onRequest(conn, requestParser->requestImpl());
|
||||
requestParser->reset();
|
||||
counter++;
|
||||
if (counter > 1)
|
||||
LOG_TRACE << "More than one requests are parsed (" << counter << ")";
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -118,7 +118,7 @@ void WebSocketConnectionImpl::sendWsData(const char *msg, size_t len, unsigned c
|
||||
bytesFormatted.resize(indexStartRawData);
|
||||
bytesFormatted.append(msg, len);
|
||||
}
|
||||
_tcpConn->send(bytesFormatted);
|
||||
_tcpConn->send(std::move(bytesFormatted));
|
||||
}
|
||||
void WebSocketConnectionImpl::send(const std::string &msg, const WebSocketMessageType &type)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user