diff --git a/lib/inc/drogon/HttpAppFramework.h b/lib/inc/drogon/HttpAppFramework.h index 900596c3..1a712902 100755 --- a/lib/inc/drogon/HttpAppFramework.h +++ b/lib/inc/drogon/HttpAppFramework.h @@ -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: diff --git a/lib/src/HttpAppFrameworkImpl.h b/lib/src/HttpAppFrameworkImpl.h index 45b1220f..8fe3f48f 100644 --- a/lib/src/HttpAppFrameworkImpl.h +++ b/lib/src/HttpAppFrameworkImpl.h @@ -83,6 +83,16 @@ class HttpAppFrameworkImpl : public HttpAppFramework const std::string &crtlName, const std::vector &filtersAndMethods = std::vector()) 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 _dbClientsMap; struct DbInfo diff --git a/lib/src/HttpResponseImpl.cc b/lib/src/HttpResponseImpl.cc index 0e417e74..8a9cf47b 100755 --- a/lib/src/HttpResponseImpl.cc +++ b/lib/src/HttpResponseImpl.cc @@ -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) { diff --git a/lib/src/HttpServer.cc b/lib/src/HttpServer.cc index 1de0f98e..094a0160 100755 --- a/lib/src/HttpServer.cc +++ b/lib/src/HttpServer.cc @@ -119,7 +119,6 @@ void HttpServer::onMessage(const TcpConnectionPtr &conn, MsgBuffer *buf) { HttpRequestParser *requestParser = any_cast(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 { diff --git a/lib/src/WebSocketConnectionImpl.cc b/lib/src/WebSocketConnectionImpl.cc index 994ce77b..76745061 100644 --- a/lib/src/WebSocketConnectionImpl.cc +++ b/lib/src/WebSocketConnectionImpl.cc @@ -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) {