From 320ebaa5d2a7a68b91ff4dc9cfd1173178bddad7 Mon Sep 17 00:00:00 2001 From: cyan317 <120398799+cindyyan317@users.noreply.github.com> Date: Thu, 2 Nov 2023 10:17:32 +0000 Subject: [PATCH] Move AdminVerificationStrategy to Server (#965) --- src/web/HttpSession.h | 4 ++-- src/web/Server.h | 24 +++++++++++----------- src/web/SslHttpSession.h | 4 ++-- src/web/SslWsSession.h | 8 ++++---- src/web/impl/AdminVerificationStrategy.cpp | 6 +++--- src/web/impl/AdminVerificationStrategy.h | 2 +- src/web/impl/HttpBase.h | 10 ++++----- 7 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/web/HttpSession.h b/src/web/HttpSession.h index 4f5f9ebae..b257df391 100644 --- a/src/web/HttpSession.h +++ b/src/web/HttpSession.h @@ -55,7 +55,7 @@ class HttpSession : public detail::HttpBase, explicit HttpSession( tcp::socket&& socket, std::string const& ip, - std::optional adminPassword, + std::shared_ptr const& adminVerification, std::reference_wrapper tagFactory, std::reference_wrapper dosGuard, std::shared_ptr const& handler, @@ -64,7 +64,7 @@ class HttpSession : public detail::HttpBase, : detail::HttpBase( ip, tagFactory, - std::move(adminPassword), + adminVerification, dosGuard, handler, std::move(buffer) diff --git a/src/web/Server.h b/src/web/Server.h index 282e85403..843ddb46a 100644 --- a/src/web/Server.h +++ b/src/web/Server.h @@ -57,7 +57,7 @@ class Detector : public std::enable_shared_from_this const dosGuard_; std::shared_ptr const handler_; boost::beast::flat_buffer buffer_; - std::optional adminPassword_; + std::shared_ptr const adminVerification_; public: /** @@ -75,15 +75,15 @@ class Detector : public std::enable_shared_from_this> ctx, std::reference_wrapper tagFactory, std::reference_wrapper dosGuard, - std::shared_ptr const& handler, - std::optional adminPassword + std::shared_ptr handler, + std::shared_ptr adminVerification ) : stream_(std::move(socket)) , ctx_(ctx) , tagFactory_(std::cref(tagFactory)) , dosGuard_(dosGuard) - , handler_(handler) - , adminPassword_(std::move(adminPassword)) + , handler_(std::move(handler)) + , adminVerification_(std::move(adminVerification)) { } @@ -136,7 +136,7 @@ class Detector : public std::enable_shared_from_this>( stream_.release_socket(), ip, - adminPassword_, + adminVerification_, *ctx_, tagFactory_, dosGuard_, @@ -148,7 +148,7 @@ class Detector : public std::enable_shared_from_this>( - stream_.release_socket(), ip, adminPassword_, tagFactory_, dosGuard_, handler_, std::move(buffer_) + stream_.release_socket(), ip, adminVerification_, tagFactory_, dosGuard_, handler_, std::move(buffer_) ) ->run(); } @@ -174,7 +174,7 @@ class Server : public std::enable_shared_from_this dosGuard_; std::shared_ptr handler_; tcp::acceptor acceptor_; - std::optional adminPassword_; + std::shared_ptr adminVerification_; public: /** @@ -194,16 +194,16 @@ class Server : public std::enable_shared_from_this const& handler, + std::shared_ptr handler, std::optional adminPassword ) : ioc_(std::ref(ioc)) , ctx_(ctx) , tagFactory_(tagFactory) , dosGuard_(std::ref(dosGuard)) - , handler_(handler) + , handler_(std::move(handler)) , acceptor_(boost::asio::make_strand(ioc)) - , adminPassword_(std::move(adminPassword)) + , adminVerification_(detail::make_AdminVerificationStrategy(std::move(adminPassword))) { boost::beast::error_code ec; @@ -257,7 +257,7 @@ class Server : public std::enable_shared_from_this>{ctx_.value()} : std::nullopt; std::make_shared>( - std::move(socket), ctxRef, std::cref(tagFactory_), dosGuard_, handler_, adminPassword_ + std::move(socket), ctxRef, std::cref(tagFactory_), dosGuard_, handler_, adminVerification_ ) ->run(); } diff --git a/src/web/SslHttpSession.h b/src/web/SslHttpSession.h index cd6a1fadc..7c3bc5ca7 100644 --- a/src/web/SslHttpSession.h +++ b/src/web/SslHttpSession.h @@ -56,7 +56,7 @@ class SslHttpSession : public detail::HttpBase, explicit SslHttpSession( tcp::socket&& socket, std::string const& ip, - std::optional adminPassword, + std::shared_ptr const& adminVerification, boost::asio::ssl::context& ctx, std::reference_wrapper tagFactory, std::reference_wrapper dosGuard, @@ -66,7 +66,7 @@ class SslHttpSession : public detail::HttpBase, : detail::HttpBase( ip, tagFactory, - std::move(adminPassword), + adminVerification, dosGuard, handler, std::move(buffer) diff --git a/src/web/SslWsSession.h b/src/web/SslWsSession.h index e8a2233b3..9289f216a 100644 --- a/src/web/SslWsSession.h +++ b/src/web/SslWsSession.h @@ -109,19 +109,19 @@ class SslWsUpgrader : public std::enable_shared_from_this tagFactory, std::reference_wrapper dosGuard, - std::shared_ptr const& handler, + std::shared_ptr handler, boost::beast::flat_buffer&& buffer, http::request request, - bool isAdmiin + bool isAdmin ) : https_(std::move(stream)) , buffer_(std::move(buffer)) , ip_(std::move(ip)) , tagFactory_(tagFactory) , dosGuard_(dosGuard) - , handler_(handler) + , handler_(std::move(handler)) , req_(std::move(request)) - , isAdmin_(isAdmiin) + , isAdmin_(isAdmin) { } diff --git a/src/web/impl/AdminVerificationStrategy.cpp b/src/web/impl/AdminVerificationStrategy.cpp index b26e6786b..512ff2569 100644 --- a/src/web/impl/AdminVerificationStrategy.cpp +++ b/src/web/impl/AdminVerificationStrategy.cpp @@ -54,13 +54,13 @@ PasswordAdminVerificationStrategy::isAdmin(RequestType const& request, std::stri return passwordSha256_ == userAuth; } -std::unique_ptr +std::shared_ptr make_AdminVerificationStrategy(std::optional password) { if (password.has_value()) { - return std::make_unique(std::move(*password)); + return std::make_shared(std::move(*password)); } - return std::make_unique(); + return std::make_shared(); } } // namespace web::detail diff --git a/src/web/impl/AdminVerificationStrategy.h b/src/web/impl/AdminVerificationStrategy.h index 156e61030..44e49d1d8 100644 --- a/src/web/impl/AdminVerificationStrategy.h +++ b/src/web/impl/AdminVerificationStrategy.h @@ -73,7 +73,7 @@ class PasswordAdminVerificationStrategy : public AdminVerificationStrategy { isAdmin(RequestType const& request, std::string_view) const override; }; -std::unique_ptr +std::shared_ptr make_AdminVerificationStrategy(std::optional password); } // namespace web::detail diff --git a/src/web/impl/HttpBase.h b/src/web/impl/HttpBase.h index 9c07bad9d..e52f5ea7b 100644 --- a/src/web/impl/HttpBase.h +++ b/src/web/impl/HttpBase.h @@ -85,7 +85,7 @@ class HttpBase : public ConnectionBase { std::shared_ptr res_; SendLambda sender_; - std::unique_ptr adminVerification_; + std::shared_ptr adminVerification_; protected: boost::beast::flat_buffer buffer_; @@ -129,17 +129,17 @@ class HttpBase : public ConnectionBase { HttpBase( std::string const& ip, std::reference_wrapper tagFactory, - std::optional adminPassword, + std::shared_ptr adminVerification, std::reference_wrapper dosGuard, - std::shared_ptr const& handler, + std::shared_ptr handler, boost::beast::flat_buffer buffer ) : ConnectionBase(tagFactory, ip) , sender_(*this) - , adminVerification_(make_AdminVerificationStrategy(std::move(adminPassword))) + , adminVerification_(std::move(adminVerification)) , buffer_(std::move(buffer)) , dosGuard_(dosGuard) - , handler_(handler) + , handler_(std::move(handler)) { LOG(perfLog_.debug()) << tag() << "http session created"; dosGuard_.get().increment(ip);