Skip to content

Commit

Permalink
Dropped root from HTTPErrorResponse & friends
Browse files Browse the repository at this point in the history
  • Loading branch information
veloman-yunkan committed Nov 29, 2023
1 parent 6e2be48 commit 7a85c92
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 62 deletions.
44 changes: 22 additions & 22 deletions src/server/internalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ std::unique_ptr<Response> InternalServer::handle_request(const RequestContext& r
{
try {
if (! request.is_valid_url()) {
return UrlNotFoundResponse(m_root, request);
return UrlNotFoundResponse(request);
}

if ( request.get_url() == "" ) {
Expand Down Expand Up @@ -643,11 +643,11 @@ std::unique_ptr<Response> InternalServer::handle_request(const RequestContext& r
return Response::build_redirect(contentUrl + query);
} catch (std::exception& e) {
fprintf(stderr, "===== Unhandled error : %s\n", e.what());
return HTTP500Response(m_root, request)
return HTTP500Response(request)
+ ParameterizedMessage("non-translated-text", {{"MSG", e.what()}});
} catch (...) {
fprintf(stderr, "===== Unhandled unknown error\n");
return HTTP500Response(m_root, request)
return HTTP500Response(request)
+ nonParameterizedMessage("unknown-error");
}
}
Expand Down Expand Up @@ -690,7 +690,7 @@ std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& r
}

if ( startsWith(request.get_url(), "/suggest/") ) {
return UrlNotFoundResponse(m_root, request);
return UrlNotFoundResponse(request);
}

std::string bookName, bookId;
Expand All @@ -704,7 +704,7 @@ std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& r
}

if (archive == nullptr) {
return HTTP404Response(m_root, request)
return HTTP404Response(request)
+ noSuchBookErrorMsg(bookName);
}

Expand Down Expand Up @@ -809,10 +809,10 @@ std::unique_ptr<Response> InternalServer::handle_no_js(const RequestContext& req
const auto bookId = mp_nameMapper->getIdForName(urlParts[2]);
content = getNoJSDownloadPageHTML(bookId, userLang);
} catch (const std::out_of_range&) {
return UrlNotFoundResponse(m_root, request);
return UrlNotFoundResponse(request);
}
} else {
return UrlNotFoundResponse(m_root, request);
return UrlNotFoundResponse(request);
}

return ContentResponse::build(content, "text/html; charset=utf-8");
Expand Down Expand Up @@ -858,7 +858,7 @@ std::unique_ptr<Response> InternalServer::handle_skin(const RequestContext& requ
response->set_kind(accessType);
return std::move(response);
} catch (const ResourceNotFound& e) {
return UrlNotFoundResponse(m_root, request);
return UrlNotFoundResponse(request);
}
}

Expand All @@ -875,13 +875,13 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
get_default_data(),
"application/opensearchdescription+xml");
}
return UrlNotFoundResponse(m_root, request);
return UrlNotFoundResponse(request);
}

try {
return handle_search_request(request);
} catch (const Error& e) {
return HTTP400Response(m_root, request)
return HTTP400Response(request)
+ e.message();
}
}
Expand Down Expand Up @@ -923,7 +923,7 @@ std::unique_ptr<Response> InternalServer::handle_search_request(const RequestCon
// 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)
const auto cssUrl = renderUrl(m_root, RESOURCE::templates::url_of_search_results_css);
HTTPErrorResponse response(m_root, request, MHD_HTTP_NOT_FOUND,
HTTPErrorResponse response(request, MHD_HTTP_NOT_FOUND,
"fulltext-search-unavailable",
"404-page-heading",
cssUrl);
Expand Down Expand Up @@ -980,7 +980,7 @@ std::unique_ptr<Response> InternalServer::handle_random(const RequestContext& re
}

if ( startsWith(request.get_url(), "/random/") ) {
return UrlNotFoundResponse(m_root, request);
return UrlNotFoundResponse(request);
}

std::string bookName;
Expand All @@ -994,15 +994,15 @@ std::unique_ptr<Response> InternalServer::handle_random(const RequestContext& re
}

if (archive == nullptr) {
return HTTP404Response(m_root, request)
return HTTP404Response(request)
+ noSuchBookErrorMsg(bookName);
}

try {
auto entry = archive->getRandomEntry();
return build_redirect(bookName, getFinalItem(*archive, entry));
} catch(zim::EntryNotFound& e) {
return HTTP404Response(m_root, request)
return HTTP404Response(request)
+ nonParameterizedMessage("random-article-failure");
}
}
Expand All @@ -1015,7 +1015,7 @@ std::unique_ptr<Response> InternalServer::handle_captured_external(const Request
} catch (const std::out_of_range& e) {}

if (source.empty()) {
return UrlNotFoundResponse(m_root, request);
return UrlNotFoundResponse(request);
}

auto data = get_default_data();
Expand All @@ -1033,7 +1033,7 @@ std::unique_ptr<Response> InternalServer::handle_catch(const RequestContext& req
return handle_captured_external(request);
}

return UrlNotFoundResponse(m_root, request);
return UrlNotFoundResponse(request);
}

std::vector<std::string>
Expand Down Expand Up @@ -1117,7 +1117,7 @@ std::unique_ptr<Response> InternalServer::handle_content(const RequestContext& r

if (archive == nullptr) {
const std::string searchURL = m_root + "/search?pattern=" + kiwix::urlEncode(pattern);
return UrlNotFoundResponse(m_root, request)
return UrlNotFoundResponse(request)
+ suggestSearchMsg(searchURL, kiwix::urlDecode(pattern));
}

Expand Down Expand Up @@ -1164,7 +1164,7 @@ std::unique_ptr<Response> InternalServer::handle_content(const RequestContext& r
printf("Failed to find %s\n", urlStr.c_str());

std::string searchURL = m_root + "/search?content=" + bookName + "&pattern=" + kiwix::urlEncode(pattern);
return UrlNotFoundResponse(m_root, request)
return UrlNotFoundResponse(request)
+ suggestSearchMsg(searchURL, kiwix::urlDecode(pattern));
}
}
Expand All @@ -1182,11 +1182,11 @@ std::unique_ptr<Response> InternalServer::handle_raw(const RequestContext& reque
bookName = request.get_url_part(1);
kind = request.get_url_part(2);
} catch (const std::out_of_range& e) {
return UrlNotFoundResponse(m_root, request);
return UrlNotFoundResponse(request);
}

if (kind != "meta" && kind!= "content") {
return UrlNotFoundResponse(m_root, request)
return UrlNotFoundResponse(request)
+ invalidRawAccessMsg(kind);
}

Expand All @@ -1197,7 +1197,7 @@ std::unique_ptr<Response> InternalServer::handle_raw(const RequestContext& reque
} catch (const std::out_of_range& e) {}

if (archive == nullptr) {
return UrlNotFoundResponse(m_root, request)
return UrlNotFoundResponse(request)
+ noSuchBookErrorMsg(bookName);
}

Expand Down Expand Up @@ -1231,7 +1231,7 @@ std::unique_ptr<Response> InternalServer::handle_raw(const RequestContext& reque
if (m_verbose.load()) {
printf("Failed to find %s\n", itemPath.c_str());
}
return UrlNotFoundResponse(m_root, request)
return UrlNotFoundResponse(request)
+ rawEntryNotFoundMsg(kind, itemPath);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/server/internalServer_catalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ std::unique_ptr<Response> InternalServer::handle_catalog(const RequestContext& r
host = request.get_header("Host");
url = request.get_url_part(1);
} catch (const std::out_of_range&) {
return UrlNotFoundResponse(m_root, request);
return UrlNotFoundResponse(request);
}

if (url == "v2") {
return handle_catalog_v2(request);
}

if (url != "searchdescription.xml" && url != "root.xml" && url != "search") {
return UrlNotFoundResponse(m_root, request);
return UrlNotFoundResponse(request);
}

if (url == "searchdescription.xml") {
Expand Down Expand Up @@ -108,7 +108,7 @@ std::unique_ptr<Response> InternalServer::handle_catalog_v2(const RequestContext
try {
url = request.get_url_part(2);
} catch (const std::out_of_range&) {
return UrlNotFoundResponse(m_root, request);
return UrlNotFoundResponse(request);
}

if (url == "root.xml") {
Expand All @@ -134,7 +134,7 @@ std::unique_ptr<Response> InternalServer::handle_catalog_v2(const RequestContext
} else if (url == "illustration") {
return handle_catalog_v2_illustration(request);
} else {
return UrlNotFoundResponse(m_root, request);
return UrlNotFoundResponse(request);
}
}

Expand Down Expand Up @@ -174,7 +174,7 @@ std::unique_ptr<Response> InternalServer::handle_catalog_v2_complete_entry(const
try {
mp_library->getBookById(entryId);
} catch (const std::out_of_range&) {
return UrlNotFoundResponse(m_root, request);
return UrlNotFoundResponse(request);
}

OPDSDumper opdsDumper(mp_library.get(), mp_nameMapper.get());
Expand Down Expand Up @@ -221,7 +221,7 @@ std::unique_ptr<Response> InternalServer::handle_catalog_v2_illustration(const R
illustration->mimeType
);
} catch(...) {
return UrlNotFoundResponse(m_root, request);
return UrlNotFoundResponse(request);
}
}

Expand Down
29 changes: 10 additions & 19 deletions src/server/response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,12 @@ std::unique_ptr<ContentResponse> ContentResponseBlueprint::generateResponseObjec
return r;
}

HTTPErrorResponse::HTTPErrorResponse(const std::string& root,
const RequestContext& request,
HTTPErrorResponse::HTTPErrorResponse(const RequestContext& request,
int httpStatusCode,
const std::string& pageTitleMsgId,
const std::string& headingMsgId,
const std::string& cssUrl)
: ContentResponseBlueprint(root,
&request,
: ContentResponseBlueprint(&request,
httpStatusCode,
request.get_requested_format() == "html" ? "text/html; charset=utf-8" : "application/xml; charset=utf-8",
request.get_requested_format() == "html" ? RESOURCE::templates::error_html : RESOURCE::templates::error_xml)
Expand All @@ -184,19 +182,16 @@ HTTPErrorResponse::HTTPErrorResponse(const std::string& root,
};
}

HTTP404Response::HTTP404Response(const std::string& root,
const RequestContext& request)
: HTTPErrorResponse(root,
request,
HTTP404Response::HTTP404Response(const RequestContext& request)
: HTTPErrorResponse(request,
MHD_HTTP_NOT_FOUND,
"404-page-title",
"404-page-heading")
{
}

UrlNotFoundResponse::UrlNotFoundResponse(const std::string& root,
const RequestContext& request)
: HTTP404Response(root, request)
UrlNotFoundResponse::UrlNotFoundResponse(const RequestContext& request)
: HTTP404Response(request)
{
const std::string requestUrl = urlDecode(m_request.get_full_url(), false);
*this += ParameterizedMessage("url-not-found", {{"url", requestUrl}});
Expand All @@ -216,10 +211,8 @@ HTTPErrorResponse& HTTPErrorResponse::operator+=(const ParameterizedMessage& det
}


HTTP400Response::HTTP400Response(const std::string& root,
const RequestContext& request)
: HTTPErrorResponse(root,
request,
HTTP400Response::HTTP400Response(const RequestContext& request)
: HTTPErrorResponse(request,
MHD_HTTP_BAD_REQUEST,
"400-page-title",
"400-page-heading")
Expand All @@ -232,10 +225,8 @@ HTTP400Response::HTTP400Response(const std::string& root,
*this += ParameterizedMessage("invalid-request", {{"url", requestUrl}});
}

HTTP500Response::HTTP500Response(const std::string& root,
const RequestContext& request)
: HTTPErrorResponse(root,
request,
HTTP500Response::HTTP500Response(const RequestContext& request)
: HTTPErrorResponse(request,
MHD_HTTP_INTERNAL_SERVER_ERROR,
"500-page-title",
"500-page-heading")
Expand Down
22 changes: 7 additions & 15 deletions src/server/response.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,11 @@ class ContentResponse : public Response {
class ContentResponseBlueprint
{
public: // functions
ContentResponseBlueprint(const std::string& root,
const RequestContext* request,
ContentResponseBlueprint(const RequestContext* request,
int httpStatusCode,
const std::string& mimeType,
const std::string& templateStr)
: m_root(root)
, m_request(*request)
: m_request(*request)
, m_httpStatusCode(httpStatusCode)
, m_mimeType(mimeType)
, m_template(templateStr)
Expand All @@ -140,7 +138,6 @@ class ContentResponseBlueprint
virtual std::unique_ptr<ContentResponse> generateResponseObject() const;

public: //data
const std::string m_root;
const RequestContext& m_request;
const int m_httpStatusCode;
const std::string m_mimeType;
Expand All @@ -150,8 +147,7 @@ class ContentResponseBlueprint

struct HTTPErrorResponse : ContentResponseBlueprint
{
HTTPErrorResponse(const std::string& root,
const RequestContext& request,
HTTPErrorResponse(const RequestContext& request,
int httpStatusCode,
const std::string& pageTitleMsgId,
const std::string& headingMsgId,
Expand All @@ -163,26 +159,22 @@ struct HTTPErrorResponse : ContentResponseBlueprint

struct HTTP404Response : HTTPErrorResponse
{
HTTP404Response(const std::string& root,
const RequestContext& request);
explicit HTTP404Response(const RequestContext& request);
};

struct UrlNotFoundResponse : HTTP404Response
{
UrlNotFoundResponse(const std::string& root,
const RequestContext& request);
explicit UrlNotFoundResponse(const RequestContext& request);
};

struct HTTP400Response : HTTPErrorResponse
{
HTTP400Response(const std::string& root,
const RequestContext& request);
explicit HTTP400Response(const RequestContext& request);
};

struct HTTP500Response : HTTPErrorResponse
{
HTTP500Response(const std::string& root,
const RequestContext& request);
explicit HTTP500Response(const RequestContext& request);

private: // overrides
// generateResponseObject() is overriden in order to produce a minimal
Expand Down

0 comments on commit 7a85c92

Please sign in to comment.