mirror of https://github.com/kiwix/libkiwix.git
Move the redirection response in its own class.
The redirection is the easiest to move, let's start with this one.
This commit is contained in:
parent
9e351b279e
commit
1011d1ff0b
|
@ -557,9 +557,8 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
|
|||
|
||||
/* If article found then redirect directly to it */
|
||||
if (!patternCorrespondingUrl.empty()) {
|
||||
auto response = Response::build(*this);
|
||||
response->set_redirection(m_root + "/" + bookName + "/" + patternCorrespondingUrl);
|
||||
return response;
|
||||
auto redirectUrl = m_root + "/" + bookName + "/" + patternCorrespondingUrl;
|
||||
return RedirectionResponse::build(*this, redirectUrl);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -798,10 +797,8 @@ InternalServer::get_reader(const std::string& bookName) const
|
|||
std::unique_ptr<Response>
|
||||
InternalServer::build_redirect(const std::string& bookName, const kiwix::Entry& entry) const
|
||||
{
|
||||
auto response = Response::build(*this);
|
||||
response->set_redirection(m_root + "/" + bookName + "/" +
|
||||
kiwix::urlEncode(entry.getPath()));
|
||||
return response;
|
||||
auto redirectUrl = m_root + "/" + bookName + "/" + kiwix::urlEncode(entry.getPath());
|
||||
return RedirectionResponse::build(*this, redirectUrl);
|
||||
}
|
||||
|
||||
std::unique_ptr<Response> InternalServer::handle_content(const RequestContext& request)
|
||||
|
|
|
@ -106,6 +106,7 @@ class InternalServer {
|
|||
std::string m_server_id;
|
||||
|
||||
friend std::unique_ptr<Response> Response::build(const InternalServer& server);
|
||||
friend std::unique_ptr<Response> RedirectionResponse::build(const InternalServer& server, const std::string& redirectionUrl);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -252,13 +252,6 @@ Response::create_raw_content_mhd_response(const RequestContext& request)
|
|||
return response;
|
||||
}
|
||||
|
||||
MHD_Response*
|
||||
Response::create_redirection_mhd_response() const
|
||||
{
|
||||
MHD_Response* response = MHD_create_response_from_buffer(0, nullptr, MHD_RESPMEM_MUST_COPY);
|
||||
MHD_add_response_header(response, MHD_HTTP_HEADER_LOCATION, m_content.c_str());
|
||||
return response;
|
||||
}
|
||||
|
||||
MHD_Response*
|
||||
Response::create_entry_mhd_response() const
|
||||
|
@ -296,9 +289,6 @@ Response::create_mhd_response(const RequestContext& request)
|
|||
case ResponseMode::RAW_CONTENT :
|
||||
return create_raw_content_mhd_response(request);
|
||||
|
||||
case ResponseMode::REDIRECTION :
|
||||
return create_redirection_mhd_response();
|
||||
|
||||
case ResponseMode::ENTRY :
|
||||
return create_entry_mhd_response();
|
||||
}
|
||||
|
@ -338,12 +328,6 @@ void Response::set_content(const std::string& content) {
|
|||
m_mode = ResponseMode::RAW_CONTENT;
|
||||
}
|
||||
|
||||
void Response::set_redirection(const std::string& url) {
|
||||
m_content = url;
|
||||
m_mode = ResponseMode::REDIRECTION;
|
||||
m_returnCode = MHD_HTTP_FOUND;
|
||||
}
|
||||
|
||||
void Response::set_entry(const Entry& entry, const RequestContext& request) {
|
||||
m_entry = entry;
|
||||
m_mode = ResponseMode::ENTRY;
|
||||
|
@ -374,4 +358,31 @@ void Response::set_taskbar(const std::string& bookName, const std::string& bookT
|
|||
}
|
||||
|
||||
|
||||
RedirectionResponse::RedirectionResponse(const std::string& root, bool verbose, bool withTaskbar, bool withLibraryButton, bool blockExternalLinks, const std::string& redirectionUrl) :
|
||||
Response(root, verbose, withTaskbar, withLibraryButton, blockExternalLinks),
|
||||
m_redirectionUrl(redirectionUrl)
|
||||
{
|
||||
m_returnCode = MHD_HTTP_FOUND;
|
||||
}
|
||||
|
||||
std::unique_ptr<Response> RedirectionResponse::build(const InternalServer& server, const std::string& redirectionUrl)
|
||||
{
|
||||
return std::unique_ptr<Response>(new RedirectionResponse(
|
||||
server.m_root,
|
||||
server.m_verbose.load(),
|
||||
server.m_withTaskbar,
|
||||
server.m_withLibraryButton,
|
||||
server.m_blockExternalLinks,
|
||||
redirectionUrl));
|
||||
}
|
||||
|
||||
MHD_Response* RedirectionResponse::create_mhd_response(const RequestContext& request)
|
||||
{
|
||||
MHD_Response* response = MHD_create_response_from_buffer(0, nullptr, MHD_RESPMEM_MUST_COPY);
|
||||
MHD_add_response_header(response, MHD_HTTP_HEADER_LOCATION, m_redirectionUrl.c_str());
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -37,7 +37,6 @@ namespace kiwix {
|
|||
enum class ResponseMode {
|
||||
ERROR_RESPONSE,
|
||||
RAW_CONTENT,
|
||||
REDIRECTION,
|
||||
ENTRY
|
||||
};
|
||||
|
||||
|
@ -47,7 +46,7 @@ class RequestContext;
|
|||
class Response {
|
||||
public:
|
||||
Response(const std::string& root, bool verbose, bool withTaskbar, bool withLibraryButton, bool blockExternalLinks);
|
||||
~Response() = default;
|
||||
virtual ~Response() = default;
|
||||
|
||||
static std::unique_ptr<Response> build(const InternalServer& server);
|
||||
|
||||
|
@ -55,7 +54,6 @@ class Response {
|
|||
|
||||
void set_template(const std::string& template_str, kainjow::mustache::data data);
|
||||
void set_content(const std::string& content);
|
||||
void set_redirection(const std::string& url);
|
||||
void set_entry(const Entry& entry, const RequestContext& request);
|
||||
|
||||
|
||||
|
@ -77,13 +75,12 @@ class Response {
|
|||
bool contentDecorationAllowed() const;
|
||||
|
||||
private: // functions
|
||||
MHD_Response* create_mhd_response(const RequestContext& request);
|
||||
virtual MHD_Response* create_mhd_response(const RequestContext& request);
|
||||
MHD_Response* create_error_response(const RequestContext& request) const;
|
||||
MHD_Response* create_raw_content_mhd_response(const RequestContext& request);
|
||||
MHD_Response* create_redirection_mhd_response() const;
|
||||
MHD_Response* create_entry_mhd_response() const;
|
||||
|
||||
private: // data
|
||||
protected: // data
|
||||
bool m_verbose;
|
||||
ResponseMode m_mode;
|
||||
std::string m_root;
|
||||
|
@ -101,6 +98,20 @@ class Response {
|
|||
ETag m_etag;
|
||||
};
|
||||
|
||||
|
||||
class RedirectionResponse : public Response {
|
||||
public:
|
||||
RedirectionResponse(const std::string& root, bool verbose, bool withTaskbar, bool withLibraryButton, bool blockExternalLinks, const std::string& redirectionUrl);
|
||||
|
||||
static std::unique_ptr<Response> build(const InternalServer& server, const std::string& redirectionUrl);
|
||||
|
||||
|
||||
private:
|
||||
MHD_Response* create_mhd_response(const RequestContext& request);
|
||||
|
||||
std::string m_redirectionUrl;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //KIWIXLIB_SERVER_RESPONSE_H
|
||||
|
|
Loading…
Reference in New Issue