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 <ed@tanous.net>
diff --git a/http/app.hpp b/http/app.hpp
index eeb331e..d7863f6 100644
--- a/http/app.hpp
+++ b/http/app.hpp
@@ -8,6 +8,8 @@
#include "routing.hpp"
#include "utility.hpp"
+#include <systemd/sd-daemon.h>
+
#include <boost/asio/io_context.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/context.hpp>
@@ -31,9 +33,11 @@
{
public:
using ssl_socket_t = boost::beast::ssl_stream<boost::asio::ip::tcp::socket>;
- using ssl_server_t = Server<App, ssl_socket_t>;
- using socket_t = boost::asio::ip::tcp::socket;
- using server_t = Server<App, socket_t>;
+ using raw_socket_t = boost::asio::ip::tcp::socket;
+
+ using socket_type =
+ std::conditional_t<bmcwebEnableTLS, ssl_socket_t, raw_socket_t>;
+ using server_type = Server<App, socket_type>;
explicit App(std::shared_ptr<boost::asio::io_context> ioIn =
std::make_shared<boost::asio::io_context>()) :
@@ -74,52 +78,53 @@
return router.newRuleTagged<Tag>(std::move(rule));
}
- App& socket(int existingSocket)
- {
- socketFd = existingSocket;
- return *this;
- }
-
- App& port(std::uint16_t port)
- {
- portUint = port;
- return *this;
- }
-
void validate()
{
router.validate();
}
+ std::optional<boost::asio::ip::tcp::acceptor> setupSocket()
+ {
+ if (io == nullptr)
+ {
+ BMCWEB_LOG_CRITICAL("IO was nullptr?");
+ return std::nullopt;
+ }
+ constexpr int defaultPort = 18080;
+ int listenFd = sd_listen_fds(0);
+ if (listenFd == 1)
+ {
+ BMCWEB_LOG_INFO("attempting systemd socket activation");
+ if (sd_is_socket_inet(SD_LISTEN_FDS_START, AF_UNSPEC, SOCK_STREAM,
+ 1, 0) != 0)
+ {
+ BMCWEB_LOG_INFO("Starting webserver on socket handle {}",
+ SD_LISTEN_FDS_START);
+ return boost::asio::ip::tcp::acceptor(
+ *io, boost::asio::ip::tcp::v6(), SD_LISTEN_FDS_START);
+ }
+ BMCWEB_LOG_ERROR(
+ "bad incoming socket, starting webserver on port {}",
+ defaultPort);
+ }
+ BMCWEB_LOG_INFO("Starting webserver on port {}", defaultPort);
+ return boost::asio::ip::tcp::acceptor(
+ *io, boost::asio::ip::tcp::endpoint(
+ boost::asio::ip::make_address("0.0.0.0"), defaultPort));
+ }
+
void run()
{
validate();
-#ifdef BMCWEB_ENABLE_SSL
- if (-1 == socketFd)
- {
- sslServer = std::make_unique<ssl_server_t>(this, portUint,
- sslContext, io);
- }
- else
- {
- sslServer = std::make_unique<ssl_server_t>(this, socketFd,
- sslContext, io);
- }
- sslServer->run();
-#else
-
- if (-1 == socketFd)
+ std::optional<boost::asio::ip::tcp::acceptor> acceptor = setupSocket();
+ if (!acceptor)
{
- server = std::make_unique<server_t>(this, portUint, nullptr, io);
+ BMCWEB_LOG_CRITICAL("Couldn't start server");
+ return;
}
- else
- {
- server = std::make_unique<server_t>(this, socketFd, nullptr, io);
- }
+ server.emplace(this, std::move(*acceptor), sslContext, io);
server->run();
-
-#endif
}
void stop()
@@ -160,19 +165,10 @@
private:
std::shared_ptr<boost::asio::io_context> io;
-#ifdef BMCWEB_ENABLE_SSL
- uint16_t portUint = 443;
-#else
- uint16_t portUint = 80;
-#endif
- int socketFd = -1;
- Router router;
-#ifdef BMCWEB_ENABLE_SSL
- std::unique_ptr<ssl_server_t> sslServer;
-#else
- std::unique_ptr<server_t> server;
-#endif
+ std::optional<server_type> server;
+
+ Router router;
};
} // namespace crow
using App = crow::App;