From 8db83747b6ea72de30ac83f19578ecc37489b13d Mon Sep 17 00:00:00 2001 From: Ed Tanous Date: Sat, 13 Apr 2024 09:11:15 -0700 Subject: Clean up BMCWEB_ENABLE_SSL This macro came originally from CROW_ENABLE_SSL, and was used as a macro to optionally compile without openssl being required. OpenSSL has been pulled into many other dependencies, and has been functionally required to be included for a long time, so there's no reason to hold onto this macro. Remove most uses of the macro, and for the couple functional places the macro is used, transition to a constexpr if to enable the TLS paths. This allows a large simplification of code in some places. Tested: Redfish service validator passes. Change-Id: Iebd46a68e5e417b6031479e24be3c21bef782f4c Signed-off-by: Ed Tanous --- http/http_server.hpp | 52 +++++++++++++++++++++------------------------------- 1 file changed, 21 insertions(+), 31 deletions(-) (limited to 'http/http_server.hpp') diff --git a/http/http_server.hpp b/http/http_server.hpp index 2a6bd9f4aa..da73b107db 100644 --- a/http/http_server.hpp +++ b/http/http_server.hpp @@ -27,38 +27,15 @@ template class Server { public: - Server(Handler* handlerIn, - std::unique_ptr&& acceptorIn, + Server(Handler* handlerIn, boost::asio::ip::tcp::acceptor&& acceptorIn, std::shared_ptr adaptorCtxIn, - std::shared_ptr io = - std::make_shared()) : + std::shared_ptr io) : ioService(std::move(io)), acceptor(std::move(acceptorIn)), signals(*ioService, SIGINT, SIGTERM, SIGHUP), handler(handlerIn), adaptorCtx(std::move(adaptorCtxIn)) {} - Server(Handler* handlerIn, uint16_t port, - const std::shared_ptr& adaptorCtxIn, - const std::shared_ptr& io = - std::make_shared()) : - Server(handlerIn, - std::make_unique( - *io, boost::asio::ip::tcp::endpoint( - boost::asio::ip::make_address("0.0.0.0"), port)), - adaptorCtxIn, io) - {} - - Server(Handler* handlerIn, int existingSocket, - const std::shared_ptr& adaptorCtxIn, - const std::shared_ptr& io = - std::make_shared()) : - Server(handlerIn, - std::make_unique( - *io, boost::asio::ip::tcp::v6(), existingSocket), - adaptorCtxIn, io) - {} - void updateDateStr() { time_t lastTimeT = time(nullptr); @@ -90,14 +67,17 @@ class Server }; BMCWEB_LOG_INFO("bmcweb server is running, local endpoint {}", - acceptor->local_endpoint().address().to_string()); + acceptor.local_endpoint().address().to_string()); startAsyncWaitForSignal(); doAccept(); } void loadCertificate() { -#ifdef BMCWEB_ENABLE_SSL + if constexpr (!bmcwebEnableTLS) + { + return; + } namespace fs = std::filesystem; // Cleanup older certificate file existing in the system fs::path oldCert = "/home/root/server.pem"; @@ -121,7 +101,6 @@ class Server ensuressl::getSslContext(sslPemFile); adaptorCtx = sslContext; handler->ssl(std::move(sslContext)); -#endif } void startAsyncWaitForSignal() @@ -139,7 +118,7 @@ class Server BMCWEB_LOG_INFO("Receivied reload signal"); loadCertificate(); boost::system::error_code ec2; - acceptor->cancel(ec2); + acceptor.cancel(ec2); if (ec2) { BMCWEB_LOG_ERROR( @@ -163,12 +142,23 @@ class Server void doAccept() { + if (ioService == nullptr) + { + BMCWEB_LOG_CRITICAL("IoService was null"); + return; + } boost::asio::steady_timer timer(*ioService); std::shared_ptr> connection; if constexpr (std::is_same>::value) { + if (adaptorCtx == nullptr) + { + BMCWEB_LOG_CRITICAL( + "Asked to lauch TLS socket but no context available"); + return; + } connection = std::make_shared>( handler, std::move(timer), getCachedDateStr, Adaptor(*ioService, *adaptorCtx)); @@ -179,7 +169,7 @@ class Server handler, std::move(timer), getCachedDateStr, Adaptor(*ioService)); } - acceptor->async_accept( + acceptor.async_accept( boost::beast::get_lowest_layer(connection->socket()), [this, connection](const boost::system::error_code& ec) { if (!ec) @@ -194,7 +184,7 @@ class Server private: std::shared_ptr ioService; std::function getCachedDateStr; - std::unique_ptr acceptor; + boost::asio::ip::tcp::acceptor acceptor; boost::asio::signal_set signals; std::string dateStr; -- cgit v1.2.3