mirror of https://github.com/kiwix/libkiwix.git
Got rid of Response::build_404()
This commit is contained in:
parent
b1f03385e4
commit
800cc5b68a
|
@ -660,8 +660,9 @@ std::unique_ptr<Response> InternalServer::handle_random(const RequestContext& re
|
|||
return build_redirect(bookName, getFinalItem(*archive, entry));
|
||||
} catch(zim::EntryNotFound& e) {
|
||||
const std::string error_details = "Oops! Failed to pick a random article :(";
|
||||
auto response = Response::build_404(*this, "", error_details);
|
||||
return withTaskbarInfo(bookName, archive.get(), std::move(response));
|
||||
return HTTP404HtmlResponse(*this, request)
|
||||
+ 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) {}
|
||||
|
||||
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 details = searchSuggestionHTML(searchURL, kiwix::urlDecode(pattern));
|
||||
|
||||
auto response = Response::build_404(*this, request.get_full_url(), details);
|
||||
return withTaskbarInfo(bookName, nullptr, std::move(response));
|
||||
const std::string searchURL = m_root + "/search?pattern=" + kiwix::urlEncode(pattern, true);
|
||||
return HTTP404HtmlResponse(*this, request)
|
||||
+ urlNotFoundMsg
|
||||
+ searchSuggestionHTML(searchURL, kiwix::urlDecode(pattern))
|
||||
+ TaskbarInfo(bookName);
|
||||
}
|
||||
|
||||
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())
|
||||
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.
|
||||
const std::string details = searchSuggestionHTML(searchURL, kiwix::urlDecode(pattern));
|
||||
|
||||
auto response = Response::build_404(*this, request.get_full_url(), details);
|
||||
return withTaskbarInfo(bookName, archive.get(), std::move(response));
|
||||
std::string searchURL = m_root + "/search?content=" + bookName + "&pattern=" + kiwix::urlEncode(pattern, true);
|
||||
return HTTP404HtmlResponse(*this, request)
|
||||
+ urlNotFoundMsg
|
||||
+ searchSuggestionHTML(searchURL, kiwix::urlDecode(pattern))
|
||||
+ TaskbarInfo(bookName, archive.get());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -899,7 +900,9 @@ std::unique_ptr<Response> InternalServer::handle_raw(const RequestContext& reque
|
|||
|
||||
if (kind != "meta" && kind!= "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;
|
||||
|
@ -936,7 +939,9 @@ std::unique_ptr<Response> InternalServer::handle_raw(const RequestContext& reque
|
|||
printf("Failed to find %s\n", itemPath.c_str());
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -84,31 +84,6 @@ std::unique_ptr<Response> Response::build_304(const InternalServer& server, cons
|
|||
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 InvalidUrlMsg invalidUrlMsg;
|
||||
|
||||
|
|
|
@ -42,8 +42,6 @@ namespace kiwix {
|
|||
class InternalServer;
|
||||
class RequestContext;
|
||||
|
||||
class ContentResponse;
|
||||
|
||||
class Response {
|
||||
public:
|
||||
Response(bool verbose);
|
||||
|
@ -51,8 +49,6 @@ class Response {
|
|||
|
||||
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<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_500(const InternalServer& server, const std::string& msg);
|
||||
static std::unique_ptr<Response> build_redirect(const InternalServer& server, const std::string& redirectUrl);
|
||||
|
@ -161,12 +157,6 @@ public: // functions
|
|||
|
||||
virtual ~ContentResponseBlueprint() = default;
|
||||
|
||||
ContentResponseBlueprint& operator+(kainjow::mustache::data&& data)
|
||||
{
|
||||
this->m_data = std::move(data);
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator std::unique_ptr<ContentResponse>() const
|
||||
{
|
||||
return generateResponseObject();
|
||||
|
|
Loading…
Reference in New Issue