From 647118dd5e18cd9e17aa43f3c34edc9c33f868a0 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Sun, 30 Jan 2022 16:40:16 +0400 Subject: [PATCH] Enter HTTPErrorHtmlResponse In addition to serving as a base class for `HTTP404HtmlResponse`, `HTTPErrorHtmlResponse` is going to be used for a couple of other error pages. --- src/server/response.cpp | 33 ++++++++++++++++++++++++--------- src/server/response.h | 20 ++++++++++++++++---- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/src/server/response.cpp b/src/server/response.cpp index 608d616a1..74d8ff9fd 100644 --- a/src/server/response.cpp +++ b/src/server/response.cpp @@ -97,30 +97,45 @@ std::unique_ptr ContentResponseBlueprint::generateResponseObjec return r; } -HTTP404HtmlResponse::HTTP404HtmlResponse(const InternalServer& server, - const RequestContext& request) +HTTPErrorHtmlResponse::HTTPErrorHtmlResponse(const InternalServer& server, + const RequestContext& request, + int httpStatusCode, + const std::string& templateStr, + const std::string& pageTitleMsg, + const std::string& headingMsg) : ContentResponseBlueprint(&server, &request, - MHD_HTTP_NOT_FOUND, - "text/html", - RESOURCE::templates::_404_html) + httpStatusCode, + "text/html; charset=utf-8", + templateStr) { kainjow::mustache::list emptyList; this->m_data = kainjow::mustache::object{ - {"PAGE_TITLE", "Content not found"}, - {"PAGE_HEADING", "Not Found"}, + {"PAGE_TITLE", pageTitleMsg}, + {"PAGE_HEADING", headingMsg}, {"details", emptyList} }; } -HTTP404HtmlResponse& HTTP404HtmlResponse::operator+(UrlNotFoundMsg /*unused*/) +HTTP404HtmlResponse::HTTP404HtmlResponse(const InternalServer& server, + const RequestContext& request) + : HTTPErrorHtmlResponse(server, + request, + MHD_HTTP_NOT_FOUND, + RESOURCE::templates::_404_html, + "Content not found", + "Not Found") +{ +} + +HTTPErrorHtmlResponse& HTTP404HtmlResponse::operator+(UrlNotFoundMsg /*unused*/) { const std::string requestUrl = m_request.get_full_url(); kainjow::mustache::mustache msgTmpl(R"(The requested URL "{{url}}" was not found on this server.)"); return *this + msgTmpl.render({"url", requestUrl}); } -HTTP404HtmlResponse& HTTP404HtmlResponse::operator+(const std::string& msg) +HTTPErrorHtmlResponse& HTTPErrorHtmlResponse::operator+(const std::string& msg) { m_data["details"].push_back({"p", msg}); return *this; diff --git a/src/server/response.h b/src/server/response.h index 160798c96..7481cbddc 100644 --- a/src/server/response.h +++ b/src/server/response.h @@ -179,18 +179,30 @@ public: //data std::unique_ptr m_taskbarInfo; }; +struct HTTPErrorHtmlResponse : ContentResponseBlueprint +{ + HTTPErrorHtmlResponse(const InternalServer& server, + const RequestContext& request, + int httpStatusCode, + const std::string& templateStr, + const std::string& pageTitleMsg, + const std::string& headingMsg); + + using ContentResponseBlueprint::operator+; + HTTPErrorHtmlResponse& operator+(const std::string& msg); +}; + class UrlNotFoundMsg {}; extern const UrlNotFoundMsg urlNotFoundMsg; -struct HTTP404HtmlResponse : ContentResponseBlueprint +struct HTTP404HtmlResponse : HTTPErrorHtmlResponse { HTTP404HtmlResponse(const InternalServer& server, const RequestContext& request); - using ContentResponseBlueprint::operator+; - HTTP404HtmlResponse& operator+(UrlNotFoundMsg /*unused*/); - HTTP404HtmlResponse& operator+(const std::string& errorDetails); + using HTTPErrorHtmlResponse::operator+; + HTTPErrorHtmlResponse& operator+(UrlNotFoundMsg /*unused*/); }; class InvalidUrlMsg {};