Introduce HTTP400HtmlResponse.

HTTP400HtmlResponse is build on the same design than HTTP404HtmlResponse.
This commit is contained in:
Matthieu Gautier 2022-03-28 16:23:48 +02:00
parent 574c1ad690
commit b1643e422e
4 changed files with 61 additions and 0 deletions

View File

@ -110,6 +110,7 @@ std::unique_ptr<ContentResponse> Response::build_404(const InternalServer& serve
}
extern const UrlNotFoundMsg urlNotFoundMsg;
extern const InvalidUrlMsg invalidUrlMsg;
std::unique_ptr<ContentResponse> ContentResponseBlueprint::generateResponseObject() const
{
@ -145,6 +146,36 @@ HTTP404HtmlResponse& HTTP404HtmlResponse::operator+(const std::string& msg)
return *this;
}
HTTP400HtmlResponse::HTTP400HtmlResponse(const InternalServer& server,
const RequestContext& request)
: ContentResponseBlueprint(&server,
&request,
MHD_HTTP_BAD_REQUEST,
"text/html",
RESOURCE::templates::_400_html)
{
kainjow::mustache::list emptyList;
this->m_data = kainjow::mustache::object{{"details", emptyList}};
}
HTTP400HtmlResponse& HTTP400HtmlResponse::operator+(InvalidUrlMsg /*unused*/)
{
std::string requestUrl = m_request.get_full_url();
const auto query = m_request.get_query();
if (!query.empty()) {
requestUrl += "?" + encodeDiples(query);
}
kainjow::mustache::mustache msgTmpl(R"(The requested URL "{{{url}}}" is not a valid request.)");
return *this + msgTmpl.render({"url", requestUrl});
}
HTTP400HtmlResponse& HTTP400HtmlResponse::operator+(const std::string& msg)
{
m_data["details"].push_back({"p", msg});
return *this;
}
ContentResponseBlueprint& ContentResponseBlueprint::operator+(const TaskbarInfo& taskbarInfo)
{
this->m_taskbarInfo.reset(new TaskbarInfo(taskbarInfo));

View File

@ -207,6 +207,20 @@ struct HTTP404HtmlResponse : ContentResponseBlueprint
HTTP404HtmlResponse& operator+(const std::string& errorDetails);
};
class InvalidUrlMsg {};
extern const InvalidUrlMsg invalidUrlMsg;
struct HTTP400HtmlResponse : ContentResponseBlueprint
{
HTTP400HtmlResponse(const InternalServer& server,
const RequestContext& request);
using ContentResponseBlueprint::operator+;
HTTP400HtmlResponse& operator+(InvalidUrlMsg /*unused*/);
HTTP400HtmlResponse& operator+(const std::string& errorDetails);
};
class ItemResponse : public Response {
public:
ItemResponse(bool verbose, const zim::Item& item, const std::string& mimetype, const ByteRange& byterange);

View File

@ -35,6 +35,7 @@ skin/block_external.js
skin/search_results.css
templates/search_result.html
templates/no_search_result.html
templates/400.html
templates/404.html
templates/500.html
templates/index.html

15
static/templates/400.html Normal file
View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html;charset=UTF-8" http-equiv="content-type" />
<title>Invalid request</title>
</head>
<body>
<h1>Invalid request</h1>
{{#details}}
<p>
{{{p}}}
</p>
{{/details}}
</body>
</html>