Got rid of Response::build_404()

This commit is contained in:
Veloman Yunkan 2022-01-23 22:39:28 +04:00 committed by Matthieu Gautier
parent b1f03385e4
commit 800cc5b68a
3 changed files with 19 additions and 49 deletions

View File

@ -660,8 +660,9 @@ std::unique_ptr<Response> InternalServer::handle_random(const RequestContext& re
return build_redirect(bookName, getFinalItem(*archive, entry)); return build_redirect(bookName, getFinalItem(*archive, entry));
} catch(zim::EntryNotFound& e) { } catch(zim::EntryNotFound& e) {
const std::string error_details = "Oops! Failed to pick a random article :("; const std::string error_details = "Oops! Failed to pick a random article :(";
auto response = Response::build_404(*this, "", error_details); return HTTP404HtmlResponse(*this, request)
return withTaskbarInfo(bookName, archive.get(), std::move(response)); + error_details
+ TaskbarInfo(bookName, archive.get());
} }
} }
@ -838,11 +839,11 @@ std::unique_ptr<Response> InternalServer::handle_content(const RequestContext& r
} catch (const std::out_of_range& e) {} } catch (const std::out_of_range& e) {}
if (archive == nullptr) { if (archive == nullptr) {
std::string searchURL = m_root + "/search?pattern=" + kiwix::urlEncode(pattern, true); // Make a full search on the entire library. const std::string searchURL = m_root + "/search?pattern=" + kiwix::urlEncode(pattern, true);
const std::string details = searchSuggestionHTML(searchURL, kiwix::urlDecode(pattern)); return HTTP404HtmlResponse(*this, request)
+ urlNotFoundMsg
auto response = Response::build_404(*this, request.get_full_url(), details); + searchSuggestionHTML(searchURL, kiwix::urlDecode(pattern))
return withTaskbarInfo(bookName, nullptr, std::move(response)); + TaskbarInfo(bookName);
} }
auto urlStr = request.get_url().substr(bookName.size()+1); auto urlStr = request.get_url().substr(bookName.size()+1);
@ -872,11 +873,11 @@ std::unique_ptr<Response> InternalServer::handle_content(const RequestContext& r
if (m_verbose.load()) if (m_verbose.load())
printf("Failed to find %s\n", urlStr.c_str()); printf("Failed to find %s\n", urlStr.c_str());
std::string searchURL = m_root + "/search?content=" + bookName + "&pattern=" + kiwix::urlEncode(pattern, true); // Make a search on this specific book only. std::string searchURL = m_root + "/search?content=" + bookName + "&pattern=" + kiwix::urlEncode(pattern, true);
const std::string details = searchSuggestionHTML(searchURL, kiwix::urlDecode(pattern)); return HTTP404HtmlResponse(*this, request)
+ urlNotFoundMsg
auto response = Response::build_404(*this, request.get_full_url(), details); + searchSuggestionHTML(searchURL, kiwix::urlDecode(pattern))
return withTaskbarInfo(bookName, archive.get(), std::move(response)); + TaskbarInfo(bookName, archive.get());
} }
} }
@ -899,7 +900,9 @@ std::unique_ptr<Response> InternalServer::handle_raw(const RequestContext& reque
if (kind != "meta" && kind!= "content") { if (kind != "meta" && kind!= "content") {
const std::string error_details = kind + " is not a valid request for raw content."; const std::string error_details = kind + " is not a valid request for raw content.";
return Response::build_404(*this, request.get_full_url(), error_details); return HTTP404HtmlResponse(*this, request)
+ urlNotFoundMsg
+ error_details;
} }
std::shared_ptr<zim::Archive> archive; std::shared_ptr<zim::Archive> archive;
@ -936,7 +939,9 @@ std::unique_ptr<Response> InternalServer::handle_raw(const RequestContext& reque
printf("Failed to find %s\n", itemPath.c_str()); printf("Failed to find %s\n", itemPath.c_str());
} }
const std::string error_details = "Cannot find " + kind + " entry " + itemPath; const std::string error_details = "Cannot find " + kind + " entry " + itemPath;
return Response::build_404(*this, request.get_full_url(), error_details); return HTTP404HtmlResponse(*this, request)
+ urlNotFoundMsg
+ error_details;
} }
} }

View File

@ -84,31 +84,6 @@ std::unique_ptr<Response> Response::build_304(const InternalServer& server, cons
return response; return response;
} }
kainjow::mustache::data make404ResponseData(const std::string& url, const std::string& details)
{
kainjow::mustache::list pList;
if ( !url.empty() ) {
kainjow::mustache::mustache msgTmpl(R"(The requested URL "{{url}}" was not found on this server.)");
const auto urlNotFoundMsg = msgTmpl.render({"url", url});
pList.push_back({"p", urlNotFoundMsg});
}
pList.push_back({"p", details});
return {"details", pList};
}
std::unique_ptr<ContentResponse> Response::build_404(const InternalServer& server, const std::string& url, const std::string& details)
{
return build_404(server, make404ResponseData(url, details));
}
std::unique_ptr<ContentResponse> Response::build_404(const InternalServer& server, const kainjow::mustache::data& data)
{
auto response = ContentResponse::build(server, RESOURCE::templates::_404_html, data, "text/html");
response->set_code(MHD_HTTP_NOT_FOUND);
return response;
}
const UrlNotFoundMsg urlNotFoundMsg; const UrlNotFoundMsg urlNotFoundMsg;
const InvalidUrlMsg invalidUrlMsg; const InvalidUrlMsg invalidUrlMsg;

View File

@ -42,8 +42,6 @@ namespace kiwix {
class InternalServer; class InternalServer;
class RequestContext; class RequestContext;
class ContentResponse;
class Response { class Response {
public: public:
Response(bool verbose); Response(bool verbose);
@ -51,8 +49,6 @@ class Response {
static std::unique_ptr<Response> build(const InternalServer& server); static std::unique_ptr<Response> build(const InternalServer& server);
static std::unique_ptr<Response> build_304(const InternalServer& server, const ETag& etag); static std::unique_ptr<Response> build_304(const InternalServer& server, const ETag& etag);
static std::unique_ptr<ContentResponse> build_404(const InternalServer& server, const kainjow::mustache::data& data);
static std::unique_ptr<ContentResponse> build_404(const InternalServer& server, const std::string& url, const std::string& details="");
static std::unique_ptr<Response> build_416(const InternalServer& server, size_t resourceLength); static std::unique_ptr<Response> build_416(const InternalServer& server, size_t resourceLength);
static std::unique_ptr<Response> build_500(const InternalServer& server, const std::string& msg); static std::unique_ptr<Response> build_500(const InternalServer& server, const std::string& msg);
static std::unique_ptr<Response> build_redirect(const InternalServer& server, const std::string& redirectUrl); static std::unique_ptr<Response> build_redirect(const InternalServer& server, const std::string& redirectUrl);
@ -161,12 +157,6 @@ public: // functions
virtual ~ContentResponseBlueprint() = default; virtual ~ContentResponseBlueprint() = default;
ContentResponseBlueprint& operator+(kainjow::mustache::data&& data)
{
this->m_data = std::move(data);
return *this;
}
operator std::unique_ptr<ContentResponse>() const operator std::unique_ptr<ContentResponse>() const
{ {
return generateResponseObject(); return generateResponseObject();