diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 17e4111be..61b7409e4 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -387,6 +387,25 @@ SuggestionsList_t getSuggestions(SuggestionSearcherCache& cache, const zim::Arch 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 InternalServer::handle_suggest(const RequestContext& request) { @@ -658,7 +677,7 @@ std::unique_ptr InternalServer::handle_catalog(const RequestContext& r url = request.get_url_part(1); } catch (const std::out_of_range&) { return make404Response(*this, request) - + make404ResponseData(request.get_full_url()); + + urlNotFoundMsg; } if (url == "v2") { @@ -666,7 +685,8 @@ std::unique_ptr InternalServer::handle_catalog(const RequestContext& r } 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") { diff --git a/src/server/response.cpp b/src/server/response.cpp index 8bcd2bb3a..5a403bf9f 100644 --- a/src/server/response.cpp +++ b/src/server/response.cpp @@ -112,11 +112,14 @@ std::unique_ptr Response::build_404(const InternalServer& serve ContentResponseBlueprint make404Response(const InternalServer& server, const RequestContext& request) { - return ContentResponseBlueprint(&server, - &request, - MHD_HTTP_NOT_FOUND, - "text/html", - RESOURCE::templates::_404_html); + auto crb = ContentResponseBlueprint(&server, + &request, + MHD_HTTP_NOT_FOUND, + "text/html", + RESOURCE::templates::_404_html); + + kainjow::mustache::list emptyList; + return crb + kainjow::mustache::object{{"details", emptyList}}; } std::unique_ptr Response::build_416(const InternalServer& server, size_t resourceLength) diff --git a/src/server/response.h b/src/server/response.h index fa0d9e42d..f77517581 100644 --- a/src/server/response.h +++ b/src/server/response.h @@ -166,7 +166,7 @@ public: // functions return operator std::unique_ptr(); } -private: // data +public: //data const InternalServer& m_server; const RequestContext& m_request; const int m_httpStatusCode; diff --git a/test/server.cpp b/test/server.cpp index 81af4c864..cf57826e6 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -549,9 +549,6 @@ TEST_F(ServerTest, 404WithBodyTesting)

The requested URL "/ROOT/catalog/" was not found on this server.

-

- //EOLWHITESPACEMARKER -

)" }, { /* url */ "/ROOT/catalog/invalid_endpoint", @@ -560,9 +557,6 @@ TEST_F(ServerTest, 404WithBodyTesting)

The requested URL "/ROOT/catalog/invalid_endpoint" was not found on this server.

-

- //EOLWHITESPACEMARKER -

)" }, { /* url */ "/ROOT/invalid-book/whatever",