Got rid of Response::build_500()

This change is not tested (mostly due to the difficulties of triggering
an internal server error).
This commit is contained in:
Veloman Yunkan 2022-03-30 20:05:37 +04:00 committed by Matthieu Gautier
parent 2028bf3a98
commit 2a20e87341
6 changed files with 41 additions and 43 deletions

View File

@ -359,10 +359,12 @@ std::unique_ptr<Response> InternalServer::handle_request(const RequestContext& r
return handle_content(request); return handle_content(request);
} catch (std::exception& e) { } catch (std::exception& e) {
fprintf(stderr, "===== Unhandled error : %s\n", e.what()); fprintf(stderr, "===== Unhandled error : %s\n", e.what());
return Response::build_500(*this, e.what()); return HTTP500HtmlResponse(*this, request)
+ e.what();
} catch (...) { } catch (...) {
fprintf(stderr, "===== Unhandled unknown error\n"); fprintf(stderr, "===== Unhandled unknown error\n");
return Response::build_500(*this, "Unknown error"); return HTTP500HtmlResponse(*this, request)
+ "Unknown error";
} }
} }
@ -631,7 +633,8 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
+ std::string(e.what()); + std::string(e.what());
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;
return Response::build_500(*this, e.what()); return HTTP500HtmlResponse(*this, request)
+ e.what();
} }
} }

View File

@ -177,8 +177,6 @@ class InternalServer {
friend std::unique_ptr<Response> Response::build(const InternalServer& server); friend std::unique_ptr<Response> Response::build(const InternalServer& server);
friend std::unique_ptr<ContentResponse> ContentResponse::build(const InternalServer& server, const std::string& content, const std::string& mimetype, bool isHomePage, bool raw); friend std::unique_ptr<ContentResponse> ContentResponse::build(const InternalServer& server, const std::string& content, const std::string& mimetype, bool isHomePage, bool raw);
friend std::unique_ptr<Response> ItemResponse::build(const InternalServer& server, const RequestContext& request, const zim::Item& item, bool raw); friend std::unique_ptr<Response> ItemResponse::build(const InternalServer& server, const RequestContext& request, const zim::Item& item, bool raw);
friend std::unique_ptr<Response> Response::build_500(const InternalServer& server, const std::string& msg);
}; };
} }

View File

@ -160,6 +160,30 @@ HTTPErrorHtmlResponse& HTTP400HtmlResponse::operator+(InvalidUrlMsg /*unused*/)
return *this + msgTmpl.render({"url", requestUrl}); return *this + msgTmpl.render({"url", requestUrl});
} }
HTTP500HtmlResponse::HTTP500HtmlResponse(const InternalServer& server,
const RequestContext& request)
: HTTPErrorHtmlResponse(server,
request,
MHD_HTTP_INTERNAL_SERVER_ERROR,
"Internal Server Error",
"Internal Server Error")
{
// operator+() is a state-modifying operator (akin to operator+=)
*this + "An internal server error occured. We are sorry about that :/";
}
std::unique_ptr<ContentResponse> HTTP500HtmlResponse::generateResponseObject() const
{
// We want a 500 response to be a minimalistic one (so that the server doesn't
// have to provide additional resources required for its proper rendering)
// ";raw=true" in the MIME-type below disables response decoration
// (see ContentResponse::contentDecorationAllowed())
const std::string mimeType = "text/html;charset=utf-8;raw=true";
auto r = ContentResponse::build(m_server, m_template, m_data, mimeType);
r->set_code(m_httpStatusCode);
return r;
}
ContentResponseBlueprint& ContentResponseBlueprint::operator+(const TaskbarInfo& taskbarInfo) ContentResponseBlueprint& ContentResponseBlueprint::operator+(const TaskbarInfo& taskbarInfo)
{ {
this->m_taskbarInfo.reset(new TaskbarInfo(taskbarInfo)); this->m_taskbarInfo.reset(new TaskbarInfo(taskbarInfo));
@ -179,26 +203,6 @@ std::unique_ptr<Response> Response::build_416(const InternalServer& server, size
return response; return response;
} }
std::unique_ptr<Response> Response::build_500(const InternalServer& server, const std::string& msg)
{
MustacheData data;
data.set("error", msg);
auto content = render_template(RESOURCE::templates::_500_html, data);
std::unique_ptr<Response> response (
new ContentResponse(
server.m_root, //root
true, //verbose
true, //raw
false, //withTaskbar
false, //withLibraryButton
false, //blockExternalLinks
content, //content
"text/html" //mimetype
));
response->set_code(MHD_HTTP_INTERNAL_SERVER_ERROR);
return response;
}
std::unique_ptr<Response> Response::build_redirect(const InternalServer& server, const std::string& redirectUrl) std::unique_ptr<Response> Response::build_redirect(const InternalServer& server, const std::string& redirectUrl)
{ {

View File

@ -50,7 +50,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<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_redirect(const InternalServer& server, const std::string& redirectUrl); static std::unique_ptr<Response> build_redirect(const InternalServer& server, const std::string& redirectUrl);
MHD_Result send(const RequestContext& request, MHD_Connection* connection); MHD_Result send(const RequestContext& request, MHD_Connection* connection);
@ -217,6 +216,17 @@ struct HTTP400HtmlResponse : HTTPErrorHtmlResponse
HTTPErrorHtmlResponse& operator+(InvalidUrlMsg /*unused*/); HTTPErrorHtmlResponse& operator+(InvalidUrlMsg /*unused*/);
}; };
struct HTTP500HtmlResponse : HTTPErrorHtmlResponse
{
HTTP500HtmlResponse(const InternalServer& server,
const RequestContext& request);
private: // overrides
// generateResponseObject() is overriden in order to produce a minimal
// response without any need for additional resources from the server
std::unique_ptr<ContentResponse> generateResponseObject() const override;
};
class ItemResponse : public Response { class ItemResponse : public Response {
public: public:
ItemResponse(bool verbose, const zim::Item& item, const std::string& mimetype, const ByteRange& byterange); ItemResponse(bool verbose, const zim::Item& item, const std::string& mimetype, const ByteRange& byterange);

View File

@ -36,7 +36,6 @@ skin/search_results.css
templates/search_result.html templates/search_result.html
templates/no_search_result.html templates/no_search_result.html
templates/error.html templates/error.html
templates/500.html
templates/index.html templates/index.html
templates/suggestion.json templates/suggestion.json
templates/head_taskbar.html templates/head_taskbar.html

View File

@ -1,16 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="content-type" />
<title>Internal Server Error</title>
</head>
<body>
<h1>Internal Server Error</h1>
<p>
An internal server error occured. We are sorry about that :/
</p>
<p>
{{ error }}
</p>
</body>
</html>