Merge pull request #119 from an-tao/dev

Add the setCustom404Page() method
This commit is contained in:
An Tao 2019-04-09 11:28:06 +08:00 committed by GitHub
commit dc4f8aac38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 12 deletions

View File

@ -109,6 +109,13 @@ class HttpAppFramework : public trantor::NonCopyable
*/ */
virtual trantor::EventLoop *getLoop() = 0; 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 ///Get the plugin object registered in the framework
/** /**
* NOTE: * NOTE:

View File

@ -83,6 +83,16 @@ class HttpAppFrameworkImpl : public HttpAppFramework
const std::string &crtlName, const std::string &crtlName,
const std::vector<any> &filtersAndMethods = const std::vector<any> &filtersAndMethods =
std::vector<any>()) override; 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 virtual void enableSession(const size_t timeout = 0) override
{ {
_useSession = true; _useSession = true;
@ -238,6 +248,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework
//Json::Value _customConfig; //Json::Value _customConfig;
Json::Value _jsonConfig; Json::Value _jsonConfig;
PluginsManager _pluginsManager; PluginsManager _pluginsManager;
HttpResponsePtr _custom404;
#if USE_ORM #if USE_ORM
std::map<std::string, orm::DbClientPtr> _dbClientsMap; std::map<std::string, orm::DbClientPtr> _dbClientsMap;
struct DbInfo struct DbInfo

View File

@ -47,13 +47,25 @@ HttpResponsePtr HttpResponse::newHttpJsonResponse(const Json::Value &data)
HttpResponsePtr HttpResponse::newNotFoundResponse() HttpResponsePtr HttpResponse::newNotFoundResponse()
{ {
HttpViewData data; auto &resp = HttpAppFrameworkImpl::instance().getCustom404Page();
data.insert("version", getVersion()); if(resp)
auto res = HttpResponse::newHttpViewResponse("NotFound", data); {
res->setStatusCode(k404NotFound); return resp;
//res->setCloseConnection(true); }
else
return res; {
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) HttpResponsePtr HttpResponse::newLocationRedirectResponse(const std::string &path)
{ {

View File

@ -119,7 +119,6 @@ void HttpServer::onMessage(const TcpConnectionPtr &conn,
MsgBuffer *buf) MsgBuffer *buf)
{ {
HttpRequestParser *requestParser = any_cast<HttpRequestParser>(conn->getMutableContext()); 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 // With the pipelining feature or web socket, it is possible to receice multiple messages at once, so
// the while loop is necessary // the while loop is necessary
if (requestParser->webSocketConn()) if (requestParser->webSocketConn())
@ -166,9 +165,6 @@ void HttpServer::onMessage(const TcpConnectionPtr &conn,
else else
onRequest(conn, requestParser->requestImpl()); onRequest(conn, requestParser->requestImpl());
requestParser->reset(); requestParser->reset();
counter++;
if (counter > 1)
LOG_TRACE << "More than one requests are parsed (" << counter << ")";
} }
else else
{ {

View File

@ -118,7 +118,7 @@ void WebSocketConnectionImpl::sendWsData(const char *msg, size_t len, unsigned c
bytesFormatted.resize(indexStartRawData); bytesFormatted.resize(indexStartRawData);
bytesFormatted.append(msg, len); bytesFormatted.append(msg, len);
} }
_tcpConn->send(bytesFormatted); _tcpConn->send(std::move(bytesFormatted));
} }
void WebSocketConnectionImpl::send(const std::string &msg, const WebSocketMessageType &type) void WebSocketConnectionImpl::send(const std::string &msg, const WebSocketMessageType &type)
{ {