From 66c6441c96f8478330ce583c28c53462453d66c9 Mon Sep 17 00:00:00 2001 From: antao Date: Tue, 7 May 2019 23:19:26 +0800 Subject: [PATCH] Return index.html to the client requesting '/' --- config.example.json | 4 ++++ drogon_ctl/templates/config.csp | 6 +++++- lib/inc/drogon/HttpAppFramework.h | 9 +++++++++ lib/inc/drogon/HttpResponse.h | 4 ++-- lib/src/ConfigLoader.cc | 3 ++- lib/src/HttpAppFrameworkImpl.h | 9 +++++++-- lib/src/HttpControllersRouter.cc | 27 +++++++++++++++++++++------ lib/src/HttpResponseImpl.cc | 4 ++-- 8 files changed, 52 insertions(+), 14 deletions(-) diff --git a/config.example.json b/config.example.json index 1139cb57..e702f65b 100644 --- a/config.example.json +++ b/config.example.json @@ -60,6 +60,10 @@ "session_timeout": 0, //document_root: Root path of HTTP document, defaut path is ./ "document_root": "./", + //home_page: Set the HTML file of the home page, the default value is "index.html" + //If there isn't any handler registered to the path "/", the home page file in the "document_root" is send to clients as a response + //to the request for "/". + "home_page": "index.html", //upload_path: The path to save the uploaded file. "uploads" by default. //If the path isn't prefixed with /, ./ or ../, //it is relative path of document_root path diff --git a/drogon_ctl/templates/config.csp b/drogon_ctl/templates/config.csp index 1139cb57..a9fc0dbe 100644 --- a/drogon_ctl/templates/config.csp +++ b/drogon_ctl/templates/config.csp @@ -56,10 +56,14 @@ //is the number of CPU cores "threads_num": 1, //enable_session: False by default - "enable_session": true, + "enable_session": false, "session_timeout": 0, //document_root: Root path of HTTP document, defaut path is ./ "document_root": "./", + //home_page: Set the HTML file of the home page, the default value is "index.html" + //If there isn't any handler registered to the path "/", the home page file in the "document_root" is send to clients as a response + //to the request for "/". + "home_page": "index.html", //upload_path: The path to save the uploaded file. "uploads" by default. //If the path isn't prefixed with /, ./ or ../, //it is relative path of document_root path diff --git a/lib/inc/drogon/HttpAppFramework.h b/lib/inc/drogon/HttpAppFramework.h index c30d232e..2eee7e57 100755 --- a/lib/inc/drogon/HttpAppFramework.h +++ b/lib/inc/drogon/HttpAppFramework.h @@ -666,6 +666,15 @@ public: */ virtual void setClientMaxWebSocketMessageSize(size_t maxSize) = 0; + //Set the HTML file of the home page, the default value is "index.html" + /** + * If there isn't any handler registered to the path "/", the home page file in the "document_root" + * is send to clients as a response to the request for "/". + * NOTE: + * This operation can be performed by an option in the configuration file. + */ + virtual void setHomePage(const std::string &homePageFile) = 0; + #if USE_ORM ///Get a database client by @param name /** diff --git a/lib/inc/drogon/HttpResponse.h b/lib/inc/drogon/HttpResponse.h index b9bf96f7..6293aeab 100755 --- a/lib/inc/drogon/HttpResponse.h +++ b/lib/inc/drogon/HttpResponse.h @@ -131,8 +131,8 @@ public: /// @param data is the data displayed on the page. /// For more details, see the wiki pages, the "View" section. static HttpResponsePtr newHttpViewResponse(const std::string &viewName, const HttpViewData &data = HttpViewData()); - /// Create a response that returns a 302 Found page, redirecting to another page located in the @param path. - static HttpResponsePtr newLocationRedirectResponse(const std::string &path); + /// Create a response that returns a 302 Found page, redirecting to another page located in the @param location. + static HttpResponsePtr newLocationResponse(const std::string &location); /// Create a response that returns a file to the client. /** * @param fullPath is the full path to the file. diff --git a/lib/src/ConfigLoader.cc b/lib/src/ConfigLoader.cc index 8ebdfa1d..631ba65a 100644 --- a/lib/src/ConfigLoader.cc +++ b/lib/src/ConfigLoader.cc @@ -54,7 +54,7 @@ static bool bytesSize(std::string &sizeStr, size_t &size) //64bit system case 't': case 'T': - size = (1024 * 1024 * 1024 * 1024); + size = (1024L * 1024L * 1024L * 1024L); sizeStr.resize(sizeStr.length() - 1); break; #endif @@ -323,6 +323,7 @@ static void loadApp(const Json::Value &app) { exit(-1); } + drogon::app().setHomePage(app.get("home_page", "index.html").asString()); } static void loadDbClients(const Json::Value &dbClients) { diff --git a/lib/src/HttpAppFrameworkImpl.h b/lib/src/HttpAppFrameworkImpl.h index b8b14f06..faa5a559 100644 --- a/lib/src/HttpAppFrameworkImpl.h +++ b/lib/src/HttpAppFrameworkImpl.h @@ -90,6 +90,7 @@ public: resp->setStatusCode(k404NotFound); _custom404 = resp; } + const HttpResponsePtr &getCustom404Page() { return _custom404; @@ -171,10 +172,13 @@ public: virtual void setKeepaliveRequestsNumber(const size_t number) override { _keepaliveRequestsNumber = number; } virtual void setPipeliningRequestsNumber(const size_t number) override { _pipeliningRequestsNumber = number; } virtual void setGzipStatic(bool useGzipStatic) override { _gzipStaticFlag = useGzipStatic; } + bool getGzipStatic() const { return _gzipStaticFlag; } virtual void setClientMaxBodySize(size_t maxSize) override { _clientMaxBodySize = maxSize; } virtual void setClientMaxWebSocketMessageSize(size_t maxSize) override { _clientMaxWebSocketMessageSize = maxSize; } - size_t getClientMaxBodySize() { return _clientMaxBodySize; } - size_t getClientMaxWebSocketMessageSize() { return _clientMaxWebSocketMessageSize; } + virtual void setHomePage(const std::string &homePageFile) override { _homePageFile = homePageFile; } + const std::string &getHomePage() const { return _homePageFile; } + size_t getClientMaxBodySize() const { return _clientMaxBodySize; } + size_t getClientMaxWebSocketMessageSize() const { return _clientMaxWebSocketMessageSize; } virtual std::vector> getHandlersInfo() const override; size_t keepaliveRequestsNumber() const { return _keepaliveRequestsNumber; } @@ -301,6 +305,7 @@ private: bool _gzipStaticFlag = true; size_t _clientMaxBodySize = 1024 * 1024; size_t _clientMaxWebSocketMessageSize = 128 * 1024; + std::string _homePageFile = "index.html"; int _staticFilesCacheTime = 5; std::unordered_map> _staticFilesCache; std::mutex _staticFilesCacheMutex; diff --git a/lib/src/HttpControllersRouter.cc b/lib/src/HttpControllersRouter.cc index acd53067..e2e77ad9 100644 --- a/lib/src/HttpControllersRouter.cc +++ b/lib/src/HttpControllersRouter.cc @@ -20,6 +20,23 @@ using namespace drogon; +namespace drogon +{ + +static void doWhenNoHandlerFound(const HttpRequestImplPtr &req, + const std::function &callback) +{ + if (req->path() == "/" && !HttpAppFrameworkImpl::instance().getHomePage().empty()) + { + auto resp = drogon::HttpResponse::newLocationResponse("/" + HttpAppFrameworkImpl::instance().getHomePage()); + callback(resp); + return; + } + auto resp = drogon::HttpResponse::newNotFoundResponse(); + callback(resp); +} + +} // namespace drogon void HttpControllersRouter::init(const std::vector &ioLoops) { std::string regString; @@ -264,16 +281,14 @@ void HttpControllersRouter::route(const HttpRequestImplPtr &req, } else { - //No controller found - auto res = drogon::HttpResponse::newNotFoundResponse(); - callback(res); + //No handler found + doWhenNoHandlerFound(req, callback); } } else { - //No controller found - auto res = drogon::HttpResponse::newNotFoundResponse(); - callback(res); + //No handler found + doWhenNoHandlerFound(req, callback); } } diff --git a/lib/src/HttpResponseImpl.cc b/lib/src/HttpResponseImpl.cc index c27d5199..6bd10a32 100755 --- a/lib/src/HttpResponseImpl.cc +++ b/lib/src/HttpResponseImpl.cc @@ -66,11 +66,11 @@ HttpResponsePtr HttpResponse::newNotFoundResponse() return notFoundResp; } } -HttpResponsePtr HttpResponse::newLocationRedirectResponse(const std::string &path) +HttpResponsePtr HttpResponse::newLocationResponse(const std::string &location) { auto res = std::make_shared(); res->setStatusCode(k302Found); - res->redirect(path.c_str()); + res->redirect(location); return res; }