diff options
Diffstat (limited to 'http/http_server.hpp')
-rw-r--r-- | http/http_server.hpp | 52 |
1 files changed, 21 insertions, 31 deletions
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 <typename Handler, typename Adaptor = boost::asio::ip::tcp::socket> class Server { public: - Server(Handler* handlerIn, - std::unique_ptr<boost::asio::ip::tcp::acceptor>&& acceptorIn, + Server(Handler* handlerIn, boost::asio::ip::tcp::acceptor&& acceptorIn, std::shared_ptr<boost::asio::ssl::context> adaptorCtxIn, - std::shared_ptr<boost::asio::io_context> io = - std::make_shared<boost::asio::io_context>()) : + std::shared_ptr<boost::asio::io_context> 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<boost::asio::ssl::context>& adaptorCtxIn, - const std::shared_ptr<boost::asio::io_context>& io = - std::make_shared<boost::asio::io_context>()) : - Server(handlerIn, - std::make_unique<boost::asio::ip::tcp::acceptor>( - *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<boost::asio::ssl::context>& adaptorCtxIn, - const std::shared_ptr<boost::asio::io_context>& io = - std::make_shared<boost::asio::io_context>()) : - Server(handlerIn, - std::make_unique<boost::asio::ip::tcp::acceptor>( - *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<Adaptor, Handler>> connection; if constexpr (std::is_same<Adaptor, boost::beast::ssl_stream< boost::asio::ip::tcp::socket>>::value) { + if (adaptorCtx == nullptr) + { + BMCWEB_LOG_CRITICAL( + "Asked to lauch TLS socket but no context available"); + return; + } connection = std::make_shared<Connection<Adaptor, Handler>>( 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<boost::asio::io_context> ioService; std::function<std::string()> getCachedDateStr; - std::unique_ptr<boost::asio::ip::tcp::acceptor> acceptor; + boost::asio::ip::tcp::acceptor acceptor; boost::asio::signal_set signals; std::string dateStr; |