From 3352c95314b231b9b6cfb502e68f0a639d84b494 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Thu, 30 Jul 2020 15:59:59 +0200 Subject: [PATCH] Remove the `RedirectResponse` and use a basic `Response` with header. --- src/server/internalServer.cpp | 4 ++-- src/server/internalServer.h | 1 - src/server/response.cpp | 32 ++++++++++---------------------- src/server/response.h | 13 +------------ 4 files changed, 13 insertions(+), 37 deletions(-) diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index a202d5670..6961cf770 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -511,7 +511,7 @@ std::unique_ptr InternalServer::handle_search(const RequestContext& re /* If article found then redirect directly to it */ if (!patternCorrespondingUrl.empty()) { auto redirectUrl = m_root + "/" + bookName + "/" + patternCorrespondingUrl; - return RedirectionResponse::build(*this, redirectUrl); + return Response::build_redirect(*this, redirectUrl); } } @@ -743,7 +743,7 @@ std::unique_ptr InternalServer::build_redirect(const std::string& bookName, const kiwix::Entry& entry) const { auto redirectUrl = m_root + "/" + bookName + "/" + kiwix::urlEncode(entry.getPath()); - return RedirectionResponse::build(*this, redirectUrl); + return Response::build_redirect(*this, redirectUrl); } std::unique_ptr InternalServer::handle_content(const RequestContext& request) diff --git a/src/server/internalServer.h b/src/server/internalServer.h index 1e2ed994c..24b91ecb5 100644 --- a/src/server/internalServer.h +++ b/src/server/internalServer.h @@ -103,7 +103,6 @@ class InternalServer { std::string m_server_id; friend std::unique_ptr Response::build(const InternalServer& server); - friend std::unique_ptr RedirectionResponse::build(const InternalServer& server, const std::string& redirectionUrl); friend std::unique_ptr ContentResponse::build(const InternalServer& server, const std::string& content, const std::string& mimetype); friend std::unique_ptr EntryResponse::build(const InternalServer& server, const RequestContext& request, const Entry& entry); friend std::unique_ptr Response::build_500(const InternalServer& server, const std::string& msg); diff --git a/src/server/response.cpp b/src/server/response.cpp index 8cf21a740..0649fe356 100644 --- a/src/server/response.cpp +++ b/src/server/response.cpp @@ -111,6 +111,15 @@ std::unique_ptr Response::build_500(const InternalServer& server, cons } +std::unique_ptr Response::build_redirect(const InternalServer& server, const std::string& redirectUrl) +{ + auto response = Response::build(server); + response->m_returnCode = MHD_HTTP_FOUND; + response->add_header(MHD_HTTP_HEADER_LOCATION, redirectUrl); + return response; +} + + static MHD_Result print_key_value (void *cls, enum MHD_ValueKind kind, const char *key, const char *value) @@ -221,7 +230,7 @@ ContentResponse::contentDecorationAllowed() const MHD_Response* Response::create_mhd_response(const RequestContext& request) { - MHD_Response* response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT); + MHD_Response* response = MHD_create_response_from_buffer(0, nullptr, MHD_RESPMEM_PERSISTENT); return response; } @@ -303,27 +312,6 @@ void ContentResponse::set_taskbar(const std::string& bookName, const std::string } -RedirectionResponse::RedirectionResponse(bool verbose, const std::string& redirectionUrl): - Response(verbose), - m_redirectionUrl(redirectionUrl) -{ - m_returnCode = MHD_HTTP_FOUND; -} - -std::unique_ptr RedirectionResponse::build(const InternalServer& server, const std::string& redirectionUrl) -{ - return std::unique_ptr(new RedirectionResponse( - server.m_verbose.load(), - 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; -} - ContentResponse::ContentResponse(const std::string& root, bool verbose, bool withTaskbar, bool withLibraryButton, bool blockExternalLinks, const std::string& content, const std::string& mimetype) : Response(verbose), m_root(root), diff --git a/src/server/response.h b/src/server/response.h index 0dbb40813..ff21cc8e2 100644 --- a/src/server/response.h +++ b/src/server/response.h @@ -50,6 +50,7 @@ class Response { static std::unique_ptr build_404(const InternalServer& server, const RequestContext& request, const std::string& bookName); static std::unique_ptr build_416(const InternalServer& server, size_t resourceLength); static std::unique_ptr build_500(const InternalServer& server, const std::string& msg); + static std::unique_ptr build_redirect(const InternalServer& server, const std::string& redirectUrl); MHD_Result send(const RequestContext& request, MHD_Connection* connection); @@ -75,18 +76,6 @@ class Response { }; -class RedirectionResponse : public Response { - public: - RedirectionResponse(bool verbose, const std::string& redirectionUrl); - static std::unique_ptr build(const InternalServer& server, const std::string& redirectionUrl); - - - private: - MHD_Response* create_mhd_response(const RequestContext& request); - - std::string m_redirectionUrl; -}; - class ContentResponse : public Response { public: ContentResponse(const std::string& root, bool verbose, bool withTaskbar, bool withLibraryButton, bool blockExternalLinks, const std::string& content, const std::string& mimetype);