From 4695f47dd27a6f1f8a388298dc854987ffb9ee3f Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 27 Apr 2022 15:58:37 +0200 Subject: [PATCH] Introduce operator+= to simplify response creation. --- src/server/internalServer.cpp | 13 +++++++------ src/server/response.cpp | 13 +++++++++++++ src/server/response.h | 3 +++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 95f999f99..abafbba38 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -633,12 +633,13 @@ std::unique_ptr InternalServer::handle_search(const RequestContext& re // Searcher->search will throw a runtime error if there is no valid xapian database to do the search. // (in case of zim file not containing a index) const auto cssUrl = renderUrl(m_root, RESOURCE::templates::url_of_search_results_css); - return HTTPErrorHtmlResponse(*this, request, MHD_HTTP_NOT_FOUND, - "fulltext-search-unavailable", - "404-page-heading", - cssUrl) - + nonParameterizedMessage("no-search-results") - + TaskbarInfo(searchInfo.bookName, archive.get()); + HTTPErrorHtmlResponse response(*this, request, MHD_HTTP_NOT_FOUND, + "fulltext-search-unavailable", + "404-page-heading", + cssUrl); + response += nonParameterizedMessage("no-search-results"); + response += TaskbarInfo(searchInfo.bookName, archive.get()); + return response; } diff --git a/src/server/response.cpp b/src/server/response.cpp index ae80a5846..5543499fd 100644 --- a/src/server/response.cpp +++ b/src/server/response.cpp @@ -194,6 +194,12 @@ HTTPErrorHtmlResponse& HTTPErrorHtmlResponse::operator+(const ParameterizedMessa return *this + details.getText(m_request.get_user_language()); } +HTTPErrorHtmlResponse& HTTPErrorHtmlResponse::operator+=(const ParameterizedMessage& details) +{ + // operator+() is already a state-modifying operator (akin to operator+=) + return *this + details; +} + HTTP400HtmlResponse::HTTP400HtmlResponse(const InternalServer& server, const RequestContext& request) @@ -246,6 +252,13 @@ ContentResponseBlueprint& ContentResponseBlueprint::operator+(const TaskbarInfo& return *this; } +ContentResponseBlueprint& ContentResponseBlueprint::operator+=(const TaskbarInfo& taskbarInfo) +{ + // operator+() is already a state-modifying operator (akin to operator+=) + return *this + taskbarInfo; +} + + std::unique_ptr Response::build_416(const InternalServer& server, size_t resourceLength) { auto response = Response::build(server); diff --git a/src/server/response.h b/src/server/response.h index 2eba46d2b..5c14e7786 100644 --- a/src/server/response.h +++ b/src/server/response.h @@ -165,6 +165,7 @@ public: // functions ContentResponseBlueprint& operator+(const TaskbarInfo& taskbarInfo); + ContentResponseBlueprint& operator+=(const TaskbarInfo& taskbarInfo); protected: // functions std::string getMessage(const std::string& msgId) const; @@ -190,8 +191,10 @@ struct HTTPErrorHtmlResponse : ContentResponseBlueprint const std::string& cssUrl = ""); using ContentResponseBlueprint::operator+; + using ContentResponseBlueprint::operator+=; HTTPErrorHtmlResponse& operator+(const std::string& msg); HTTPErrorHtmlResponse& operator+(const ParameterizedMessage& errorDetails); + HTTPErrorHtmlResponse& operator+=(const ParameterizedMessage& errorDetails); }; class UrlNotFoundMsg {};