From b1643e422eb94b0444cf0b4e133aabca081344ab Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Mon, 28 Mar 2022 16:23:48 +0200 Subject: [PATCH] Introduce HTTP400HtmlResponse. HTTP400HtmlResponse is build on the same design than HTTP404HtmlResponse. --- src/server/response.cpp | 31 +++++++++++++++++++++++++++++++ src/server/response.h | 14 ++++++++++++++ static/resources_list.txt | 1 + static/templates/400.html | 15 +++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 static/templates/400.html diff --git a/src/server/response.cpp b/src/server/response.cpp index 50a66c344..4b82659c8 100644 --- a/src/server/response.cpp +++ b/src/server/response.cpp @@ -110,6 +110,7 @@ std::unique_ptr Response::build_404(const InternalServer& serve } extern const UrlNotFoundMsg urlNotFoundMsg; +extern const InvalidUrlMsg invalidUrlMsg; std::unique_ptr ContentResponseBlueprint::generateResponseObject() const { @@ -145,6 +146,36 @@ HTTP404HtmlResponse& HTTP404HtmlResponse::operator+(const std::string& msg) return *this; } +HTTP400HtmlResponse::HTTP400HtmlResponse(const InternalServer& server, + const RequestContext& request) + : ContentResponseBlueprint(&server, + &request, + MHD_HTTP_BAD_REQUEST, + "text/html", + RESOURCE::templates::_400_html) +{ + kainjow::mustache::list emptyList; + this->m_data = kainjow::mustache::object{{"details", emptyList}}; +} + +HTTP400HtmlResponse& HTTP400HtmlResponse::operator+(InvalidUrlMsg /*unused*/) +{ + std::string requestUrl = m_request.get_full_url(); + const auto query = m_request.get_query(); + if (!query.empty()) { + requestUrl += "?" + encodeDiples(query); + } + kainjow::mustache::mustache msgTmpl(R"(The requested URL "{{{url}}}" is not a valid request.)"); + return *this + msgTmpl.render({"url", requestUrl}); +} + +HTTP400HtmlResponse& HTTP400HtmlResponse::operator+(const std::string& msg) +{ + m_data["details"].push_back({"p", msg}); + return *this; +} + + ContentResponseBlueprint& ContentResponseBlueprint::operator+(const TaskbarInfo& taskbarInfo) { this->m_taskbarInfo.reset(new TaskbarInfo(taskbarInfo)); diff --git a/src/server/response.h b/src/server/response.h index b10524bc8..d46898923 100644 --- a/src/server/response.h +++ b/src/server/response.h @@ -207,6 +207,20 @@ struct HTTP404HtmlResponse : ContentResponseBlueprint HTTP404HtmlResponse& operator+(const std::string& errorDetails); }; +class InvalidUrlMsg {}; + +extern const InvalidUrlMsg invalidUrlMsg; + +struct HTTP400HtmlResponse : ContentResponseBlueprint +{ + HTTP400HtmlResponse(const InternalServer& server, + const RequestContext& request); + + using ContentResponseBlueprint::operator+; + HTTP400HtmlResponse& operator+(InvalidUrlMsg /*unused*/); + HTTP400HtmlResponse& operator+(const std::string& errorDetails); +}; + class ItemResponse : public Response { public: ItemResponse(bool verbose, const zim::Item& item, const std::string& mimetype, const ByteRange& byterange); diff --git a/static/resources_list.txt b/static/resources_list.txt index 5587111c0..ab583d7a6 100644 --- a/static/resources_list.txt +++ b/static/resources_list.txt @@ -35,6 +35,7 @@ skin/block_external.js skin/search_results.css templates/search_result.html templates/no_search_result.html +templates/400.html templates/404.html templates/500.html templates/index.html diff --git a/static/templates/400.html b/static/templates/400.html new file mode 100644 index 000000000..5f4ecf42f --- /dev/null +++ b/static/templates/400.html @@ -0,0 +1,15 @@ + + + + + Invalid request + + +

Invalid request

+{{#details}} +

+ {{{p}}} +

+{{/details}} + +