bmcweb: make SSL enabling more effective
When specializing code, we know at compile time whether or not SSL will
be used, so we don't need to instantiate both types of
socket/server/app, when we only really care about one at runtime.
The size savings here aren't huge, but are worth doing, just for compile
times.
Change-Id: If4e9934a72eb0f12ce3755c6350e91b8ba485af1
Signed-off-by: Ed Tanous <ed.tanous@intel.com>
diff --git a/crow/include/crow/app.h b/crow/include/crow/app.h
index c919245..b935bf1 100644
--- a/crow/include/crow/app.h
+++ b/crow/include/crow/app.h
@@ -27,9 +27,14 @@
{
public:
using self_t = Crow;
- using server_t = Server<Crow, SocketAdaptor, Middlewares...>;
+
#ifdef BMCWEB_ENABLE_SSL
- using ssl_server_t = Server<Crow, SSLAdaptor, Middlewares...>;
+ using ssl_socket_t = SocketAdaptor;
+ using ssl_server_t = Server<Crow, ssl_socket_t, Middlewares...>;
+#else
+ using socket_t = SSLAdaptor;
+ using server_t = Server<Crow, socket_t, Middlewares...>;
+
#endif
explicit Crow(std::shared_ptr<boost::asio::io_service> io =
std::make_shared<boost::asio::io_service>()) :
@@ -89,38 +94,35 @@
{
validate();
#ifdef BMCWEB_ENABLE_SSL
- if (useSsl)
+ if (-1 == socketFd)
{
- if (-1 == socketFd)
- {
- sslServer = std::move(std::make_unique<ssl_server_t>(
- this, bindaddrStr, portUint, &middlewares, &sslContext,
- io));
- }
- else
- {
- sslServer = std::move(std::make_unique<ssl_server_t>(
- this, socketFd, &middlewares, &sslContext, io));
- }
- sslServer->setTickFunction(tickInterval, tickFunction);
- sslServer->run();
+ sslServer = std::move(std::make_unique<ssl_server_t>(
+ this, bindaddrStr, portUint, &middlewares, &sslContext, io));
}
else
-#endif
{
- if (-1 == socketFd)
- {
- server = std::move(std::make_unique<server_t>(
- this, bindaddrStr, portUint, &middlewares, nullptr, io));
- }
- else
- {
- server = std::move(std::make_unique<server_t>(
- this, socketFd, &middlewares, nullptr, io));
- }
- server->setTickFunction(tickInterval, tickFunction);
- server->run();
+ sslServer = std::move(std::make_unique<ssl_server_t>(
+ this, socketFd, &middlewares, &sslContext, io));
}
+ sslServer->setTickFunction(tickInterval, tickFunction);
+ sslServer->run();
+
+#else
+
+ if (-1 == socketFd)
+ {
+ server = std::move(std::make_unique<server_t>(
+ this, bindaddrStr, portUint, &middlewares, nullptr, io));
+ }
+ else
+ {
+ server = std::move(std::make_unique<server_t>(
+ this, socketFd, &middlewares, nullptr, io));
+ }
+ server->setTickFunction(tickInterval, tickFunction);
+ server->run();
+
+#endif
}
void stop()
@@ -149,7 +151,6 @@
self_t& sslFile(const std::string& crt_filename,
const std::string& key_filename)
{
- useSsl = true;
sslContext.set_verify_mode(boost::asio::ssl::verify_peer);
sslContext.use_certificate_file(crt_filename, ssl_context_t::pem);
sslContext.use_private_key_file(key_filename, ssl_context_t::pem);
@@ -161,7 +162,6 @@
self_t& sslFile(const std::string& pem_filename)
{
- useSsl = true;
sslContext.set_verify_mode(boost::asio::ssl::verify_peer);
sslContext.load_verify_file(pem_filename);
sslContext.set_options(boost::asio::ssl::context::default_workarounds |
@@ -172,12 +172,10 @@
self_t& ssl(boost::asio::ssl::context&& ctx)
{
- useSsl = true;
sslContext = std::move(ctx);
return *this;
}
- bool useSsl{false};
ssl_context_t sslContext{boost::asio::ssl::context::sslv23};
#else
@@ -228,7 +226,11 @@
private:
std::shared_ptr<asio::io_service> io;
+#ifdef BMCWEB_ENABLE_SSL
+ uint16_t portUint = 443;
+#else
uint16_t portUint = 80;
+#endif
std::string bindaddrStr = "::";
int socketFd = -1;
Router router;
@@ -240,8 +242,9 @@
#ifdef BMCWEB_ENABLE_SSL
std::unique_ptr<ssl_server_t> sslServer;
-#endif
+#else
std::unique_ptr<server_t> server;
+#endif
};
template <typename... Middlewares> using App = Crow<Middlewares...>;
using SimpleApp = Crow<>;