Skip to content

Commit

Permalink
Introduce operator+= to simplify response creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgautierfr committed Apr 27, 2022
1 parent 2863ab1 commit b7522f1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
17 changes: 7 additions & 10 deletions src/server/internalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,20 +683,17 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
} catch(std::runtime_error& e) {
// 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)
auto response = std::move(
HTTPErrorHtmlResponse(*this, request, MHD_HTTP_NOT_FOUND,
"fulltext-search-unavailable",
"404-page-heading",
m_root + "/skin/search_results.css")
+ nonParameterizedMessage("no-search-results")
);
HTTPErrorHtmlResponse response(*this, request, MHD_HTTP_NOT_FOUND,
"fulltext-search-unavailable",
"404-page-heading",
m_root + "/skin/search_results.css");
response += nonParameterizedMessage("no-search-results");
if(bookIds.size() == 1) {
auto bookId = *bookIds.begin();
auto bookName = mp_nameMapper->getNameForId(bookId);
return response + TaskbarInfo(bookName, mp_library->getArchiveById(bookId).get());
} else {
return response;
response += TaskbarInfo(bookName, mp_library->getArchiveById(bookId).get());
}
return response;
}

auto start = 0;
Expand Down
4 changes: 3 additions & 1 deletion src/server/internalServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ class InternalServer {

bool etag_not_needed(const RequestContext& r) const;
ETag get_matching_if_none_match_etag(const RequestContext& request) const;
Library::BookIdSet selectBooks(const RequestContext& r, const std::string& prefix) const;
Library::

BookIdSet selectBooks(const RequestContext& r, const std::string& prefix) const;
SearchInfo getSearchInfo(const RequestContext& r, const std::string& prefix) const;

private: // data
Expand Down
13 changes: 13 additions & 0 deletions src/server/response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,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)
Expand Down Expand Up @@ -202,6 +208,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> Response::build_416(const InternalServer& server, size_t resourceLength)
{
auto response = Response::build(server);
Expand Down
3 changes: 3 additions & 0 deletions src/server/response.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class ContentResponseBlueprint


ContentResponseBlueprint& operator+(const TaskbarInfo& taskbarInfo);
ContentResponseBlueprint& operator+=(const TaskbarInfo& taskbarInfo);

protected: // functions
std::string getMessage(const std::string& msgId) const;
Expand All @@ -193,8 +194,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 {};
Expand Down

0 comments on commit b7522f1

Please sign in to comment.