Enter UrlNotFoundMsg iomanipulator-like class

This commit is contained in:
Veloman Yunkan 2022-01-22 23:48:01 +04:00 committed by Matthieu Gautier
parent df98c58d07
commit 668063205c
4 changed files with 31 additions and 14 deletions

View File

@ -387,6 +387,25 @@ SuggestionsList_t getSuggestions(SuggestionSearcherCache& cache, const zim::Arch
return suggestions; return suggestions;
} }
namespace
{
class UrlNotFoundMsg {};
const UrlNotFoundMsg urlNotFoundMsg;
ContentResponseBlueprint&& operator+(ContentResponseBlueprint&& crb,
UrlNotFoundMsg /*unused*/)
{
const std::string requestUrl = crb.m_request.get_full_url();
kainjow::mustache::mustache msgTmpl(R"(The requested URL "{{url}}" was not found on this server.)");
const auto urlNotFoundMsgText = msgTmpl.render({"url", requestUrl});
crb.m_data["details"].push_back({"p", urlNotFoundMsgText});
return std::move(crb);
}
} // unnamed namespace
std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& request) std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& request)
{ {
@ -658,7 +677,7 @@ std::unique_ptr<Response> InternalServer::handle_catalog(const RequestContext& r
url = request.get_url_part(1); url = request.get_url_part(1);
} catch (const std::out_of_range&) { } catch (const std::out_of_range&) {
return make404Response(*this, request) return make404Response(*this, request)
+ make404ResponseData(request.get_full_url()); + urlNotFoundMsg;
} }
if (url == "v2") { if (url == "v2") {
@ -666,7 +685,8 @@ std::unique_ptr<Response> InternalServer::handle_catalog(const RequestContext& r
} }
if (url != "searchdescription.xml" && url != "root.xml" && url != "search") { if (url != "searchdescription.xml" && url != "root.xml" && url != "search") {
return Response::build_404(*this, request.get_full_url()); return make404Response(*this, request)
+ urlNotFoundMsg;
} }
if (url == "searchdescription.xml") { if (url == "searchdescription.xml") {

View File

@ -112,11 +112,14 @@ std::unique_ptr<ContentResponse> Response::build_404(const InternalServer& serve
ContentResponseBlueprint make404Response(const InternalServer& server, ContentResponseBlueprint make404Response(const InternalServer& server,
const RequestContext& request) const RequestContext& request)
{ {
return ContentResponseBlueprint(&server, auto crb = ContentResponseBlueprint(&server,
&request, &request,
MHD_HTTP_NOT_FOUND, MHD_HTTP_NOT_FOUND,
"text/html", "text/html",
RESOURCE::templates::_404_html); RESOURCE::templates::_404_html);
kainjow::mustache::list emptyList;
return crb + kainjow::mustache::object{{"details", emptyList}};
} }
std::unique_ptr<Response> Response::build_416(const InternalServer& server, size_t resourceLength) std::unique_ptr<Response> Response::build_416(const InternalServer& server, size_t resourceLength)

View File

@ -166,7 +166,7 @@ public: // functions
return operator std::unique_ptr<ContentResponse>(); return operator std::unique_ptr<ContentResponse>();
} }
private: // data public: //data
const InternalServer& m_server; const InternalServer& m_server;
const RequestContext& m_request; const RequestContext& m_request;
const int m_httpStatusCode; const int m_httpStatusCode;

View File

@ -549,9 +549,6 @@ TEST_F(ServerTest, 404WithBodyTesting)
<p> <p>
The requested URL "/ROOT/catalog/" was not found on this server. The requested URL "/ROOT/catalog/" was not found on this server.
</p> </p>
<p>
//EOLWHITESPACEMARKER
</p>
)" }, )" },
{ /* url */ "/ROOT/catalog/invalid_endpoint", { /* url */ "/ROOT/catalog/invalid_endpoint",
@ -560,9 +557,6 @@ TEST_F(ServerTest, 404WithBodyTesting)
<p> <p>
The requested URL "/ROOT/catalog/invalid_endpoint" was not found on this server. The requested URL "/ROOT/catalog/invalid_endpoint" was not found on this server.
</p> </p>
<p>
//EOLWHITESPACEMARKER
</p>
)" }, )" },
{ /* url */ "/ROOT/invalid-book/whatever", { /* url */ "/ROOT/invalid-book/whatever",