mirror of https://github.com/kiwix/libkiwix.git
Extend ItemResponse and ContentResponse to return raw content.
This commit is contained in:
parent
78c10346f2
commit
160a74f5f8
|
@ -117,8 +117,8 @@ class InternalServer {
|
||||||
std::string m_library_id;
|
std::string m_library_id;
|
||||||
|
|
||||||
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);
|
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);
|
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);
|
friend std::unique_ptr<Response> Response::build_500(const InternalServer& server, const std::string& msg);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -117,7 +117,16 @@ std::unique_ptr<Response> Response::build_500(const InternalServer& server, cons
|
||||||
data.set("error", msg);
|
data.set("error", msg);
|
||||||
auto content = render_template(RESOURCE::templates::_500_html, data);
|
auto content = render_template(RESOURCE::templates::_500_html, data);
|
||||||
std::unique_ptr<Response> response (
|
std::unique_ptr<Response> response (
|
||||||
new ContentResponse(server.m_root, true, false, false, false, content, "text/html"));
|
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);
|
response->set_code(MHD_HTTP_INTERNAL_SERVER_ERROR);
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
@ -238,6 +247,9 @@ ContentResponse::can_compress(const RequestContext& request) const
|
||||||
bool
|
bool
|
||||||
ContentResponse::contentDecorationAllowed() const
|
ContentResponse::contentDecorationAllowed() const
|
||||||
{
|
{
|
||||||
|
if (m_raw) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return (startsWith(m_mimeType, "text/html")
|
return (startsWith(m_mimeType, "text/html")
|
||||||
&& m_mimeType.find(";raw=true") == std::string::npos);
|
&& m_mimeType.find(";raw=true") == std::string::npos);
|
||||||
}
|
}
|
||||||
|
@ -327,11 +339,12 @@ void ContentResponse::set_taskbar(const std::string& bookName, const std::string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ContentResponse::ContentResponse(const std::string& root, bool verbose, bool withTaskbar, bool withLibraryButton, bool blockExternalLinks, const std::string& content, const std::string& mimetype) :
|
ContentResponse::ContentResponse(const std::string& root, bool verbose, bool raw, bool withTaskbar, bool withLibraryButton, bool blockExternalLinks, const std::string& content, const std::string& mimetype) :
|
||||||
Response(verbose),
|
Response(verbose),
|
||||||
m_root(root),
|
m_root(root),
|
||||||
m_content(content),
|
m_content(content),
|
||||||
m_mimeType(mimetype),
|
m_mimeType(mimetype),
|
||||||
|
m_raw(raw),
|
||||||
m_withTaskbar(withTaskbar),
|
m_withTaskbar(withTaskbar),
|
||||||
m_withLibraryButton(withLibraryButton),
|
m_withLibraryButton(withLibraryButton),
|
||||||
m_blockExternalLinks(blockExternalLinks),
|
m_blockExternalLinks(blockExternalLinks),
|
||||||
|
@ -341,11 +354,17 @@ ContentResponse::ContentResponse(const std::string& root, bool verbose, bool wit
|
||||||
add_header(MHD_HTTP_HEADER_CONTENT_TYPE, m_mimeType);
|
add_header(MHD_HTTP_HEADER_CONTENT_TYPE, m_mimeType);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<ContentResponse> ContentResponse::build(const InternalServer& server, const std::string& content, const std::string& mimetype, bool isHomePage)
|
std::unique_ptr<ContentResponse> ContentResponse::build(
|
||||||
|
const InternalServer& server,
|
||||||
|
const std::string& content,
|
||||||
|
const std::string& mimetype,
|
||||||
|
bool isHomePage,
|
||||||
|
bool raw)
|
||||||
{
|
{
|
||||||
return std::unique_ptr<ContentResponse>(new ContentResponse(
|
return std::unique_ptr<ContentResponse>(new ContentResponse(
|
||||||
server.m_root,
|
server.m_root,
|
||||||
server.m_verbose.load(),
|
server.m_verbose.load(),
|
||||||
|
raw,
|
||||||
server.m_withTaskbar && !isHomePage,
|
server.m_withTaskbar && !isHomePage,
|
||||||
server.m_withLibraryButton,
|
server.m_withLibraryButton,
|
||||||
server.m_blockExternalLinks,
|
server.m_blockExternalLinks,
|
||||||
|
@ -353,7 +372,13 @@ std::unique_ptr<ContentResponse> ContentResponse::build(const InternalServer& se
|
||||||
mimetype));
|
mimetype));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<ContentResponse> ContentResponse::build(const InternalServer& server, const std::string& template_str, kainjow::mustache::data data, const std::string& mimetype, bool isHomePage) {
|
std::unique_ptr<ContentResponse> ContentResponse::build(
|
||||||
|
const InternalServer& server,
|
||||||
|
const std::string& template_str,
|
||||||
|
kainjow::mustache::data data,
|
||||||
|
const std::string& mimetype,
|
||||||
|
bool isHomePage)
|
||||||
|
{
|
||||||
auto content = render_template(template_str, data);
|
auto content = render_template(template_str, data);
|
||||||
return ContentResponse::build(server, content, mimetype, isHomePage);
|
return ContentResponse::build(server, content, mimetype, isHomePage);
|
||||||
}
|
}
|
||||||
|
@ -368,14 +393,14 @@ ItemResponse::ItemResponse(bool verbose, const zim::Item& item, const std::strin
|
||||||
add_header(MHD_HTTP_HEADER_CONTENT_TYPE, m_mimeType);
|
add_header(MHD_HTTP_HEADER_CONTENT_TYPE, m_mimeType);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Response> ItemResponse::build(const InternalServer& server, const RequestContext& request, const zim::Item& item)
|
std::unique_ptr<Response> ItemResponse::build(const InternalServer& server, const RequestContext& request, const zim::Item& item, bool raw)
|
||||||
{
|
{
|
||||||
const std::string mimetype = get_mime_type(item);
|
const std::string mimetype = get_mime_type(item);
|
||||||
auto byteRange = request.get_range().resolve(item.getSize());
|
auto byteRange = request.get_range().resolve(item.getSize());
|
||||||
const bool noRange = byteRange.kind() == ByteRange::RESOLVED_FULL_CONTENT;
|
const bool noRange = byteRange.kind() == ByteRange::RESOLVED_FULL_CONTENT;
|
||||||
if (noRange && is_compressible_mime_type(mimetype)) {
|
if (noRange && is_compressible_mime_type(mimetype)) {
|
||||||
// Return a contentResponse
|
// Return a contentResponse
|
||||||
auto response = ContentResponse::build(server, item.getData(), mimetype);
|
auto response = ContentResponse::build(server, item.getData(), mimetype, /*isHomePage=*/false, raw);
|
||||||
response->set_cacheable();
|
response->set_cacheable();
|
||||||
response->m_byteRange = byteRange;
|
response->m_byteRange = byteRange;
|
||||||
return std::move(response);
|
return std::move(response);
|
||||||
|
|
|
@ -78,9 +78,27 @@ class Response {
|
||||||
|
|
||||||
class ContentResponse : public Response {
|
class ContentResponse : public Response {
|
||||||
public:
|
public:
|
||||||
ContentResponse(const std::string& root, bool verbose, bool withTaskbar, bool withLibraryButton, bool blockExternalLinks, const std::string& content, const std::string& mimetype);
|
ContentResponse(
|
||||||
static std::unique_ptr<ContentResponse> build(const InternalServer& server, const std::string& content, const std::string& mimetype, bool isHomePage = false);
|
const std::string& root,
|
||||||
static std::unique_ptr<ContentResponse> build(const InternalServer& server, const std::string& template_str, kainjow::mustache::data data, const std::string& mimetype, bool isHomePage = false);
|
bool verbose,
|
||||||
|
bool raw,
|
||||||
|
bool withTaskbar,
|
||||||
|
bool withLibraryButton,
|
||||||
|
bool blockExternalLinks,
|
||||||
|
const std::string& content,
|
||||||
|
const std::string& mimetype);
|
||||||
|
static std::unique_ptr<ContentResponse> build(
|
||||||
|
const InternalServer& server,
|
||||||
|
const std::string& content,
|
||||||
|
const std::string& mimetype,
|
||||||
|
bool isHomePage = false,
|
||||||
|
bool raw = false);
|
||||||
|
static std::unique_ptr<ContentResponse> build(
|
||||||
|
const InternalServer& server,
|
||||||
|
const std::string& template_str,
|
||||||
|
kainjow::mustache::data data,
|
||||||
|
const std::string& mimetype,
|
||||||
|
bool isHomePage = false);
|
||||||
|
|
||||||
void set_taskbar(const std::string& bookName, const std::string& bookTitle);
|
void set_taskbar(const std::string& bookName, const std::string& bookTitle);
|
||||||
|
|
||||||
|
@ -98,6 +116,7 @@ class ContentResponse : public Response {
|
||||||
std::string m_root;
|
std::string m_root;
|
||||||
std::string m_content;
|
std::string m_content;
|
||||||
std::string m_mimeType;
|
std::string m_mimeType;
|
||||||
|
bool m_raw;
|
||||||
bool m_withTaskbar;
|
bool m_withTaskbar;
|
||||||
bool m_withLibraryButton;
|
bool m_withLibraryButton;
|
||||||
bool m_blockExternalLinks;
|
bool m_blockExternalLinks;
|
||||||
|
@ -108,7 +127,7 @@ class ContentResponse : public Response {
|
||||||
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);
|
||||||
static std::unique_ptr<Response> build(const InternalServer& server, const RequestContext& request, const zim::Item& item);
|
static std::unique_ptr<Response> build(const InternalServer& server, const RequestContext& request, const zim::Item& item, bool raw = false);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
MHD_Response* create_mhd_response(const RequestContext& request);
|
MHD_Response* create_mhd_response(const RequestContext& request);
|
||||||
|
|
Loading…
Reference in New Issue