Skip to content

Commit

Permalink
Reintroduced server construction with plain ssl context.
Browse files Browse the repository at this point in the history
  • Loading branch information
5cript committed Nov 11, 2023
1 parent b144dd8 commit 82b136b
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 13 deletions.
3 changes: 2 additions & 1 deletion include/roar/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <unordered_map>
#include <functional>
#include <iterator>
#include <variant>

namespace Roar
{
Expand All @@ -38,7 +39,7 @@ namespace Roar
boost::asio::any_io_executor executor;

/// Supply for SSL support.
std::optional<SslServerContext> sslContext;
std::optional<std::variant<SslServerContext, boost::asio::ssl::context>> sslContext;

/// Called when an error occurs in an asynchronous routine.
std::function<void(Error&&)> onError = [](auto&&) {};
Expand Down
5 changes: 4 additions & 1 deletion include/roar/session/factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <memory>
#include <optional>
#include <functional>
#include <variant>
#include <chrono>

namespace Roar
Expand All @@ -24,7 +25,9 @@ namespace Roar
constexpr static std::chrono::seconds sslDetectionTimeout{10};

public:
Factory(std::optional<SslServerContext>& sslContext, std::function<void(Error&&)> onError);
Factory(
std::optional<std::variant<SslServerContext, boost::asio::ssl::context>>& sslContext,
std::function<void(Error&&)> onError);
ROAR_PIMPL_SPECIAL_FUNCTIONS(Factory);

/**
Expand Down
2 changes: 1 addition & 1 deletion include/roar/session/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace Roar
Session(
boost::asio::basic_stream_socket<boost::asio::ip::tcp>&& socket,
boost::beast::basic_flat_buffer<std::allocator<char>>&& buffer,
std::optional<SslServerContext>& sslContext,
std::optional<std::variant<SslServerContext, boost::asio::ssl::context>>& sslContext,
bool isSecure,
std::function<void(Error&&)> onError,
std::weak_ptr<Router> router,
Expand Down
2 changes: 2 additions & 0 deletions include/roar/ssl/make_ssl_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ namespace Roar
* @return boost::asio::ssl::context
*/
void initializeServerSslContext(SslServerContext& ctx);

boost::asio::ssl::context makeSslContext(const std::string& certificate, const std::string& privateKey);
}
6 changes: 3 additions & 3 deletions src/roar/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Roar
struct Server::Implementation : public std::enable_shared_from_this<Server::Implementation>
{
boost::asio::ip::tcp::acceptor acceptor;
std::optional<SslServerContext> sslContext;
std::optional<std::variant<SslServerContext, boost::asio::ssl::context>> sslContext;
boost::asio::ip::tcp::endpoint bindEndpoint;
boost::asio::ip::tcp::endpoint resolvedEndpoint;
std::shared_mutex acceptorStopGuard;
Expand All @@ -32,7 +32,7 @@ namespace Roar

Implementation(
boost::asio::any_io_executor& executor,
std::optional<SslServerContext> sslContext,
std::optional<std::variant<SslServerContext, boost::asio::ssl::context>> sslContext,
std::function<void(Error&&)> onError,
std::function<void(boost::system::error_code)> onAcceptAbort,
std::unique_ptr<StandardResponseProvider> standardResponseProvider);
Expand All @@ -42,7 +42,7 @@ namespace Roar
//------------------------------------------------------------------------------------------------------------------
Server::Implementation::Implementation(
boost::asio::any_io_executor& executor,
std::optional<SslServerContext> sslContext,
std::optional<std::variant<SslServerContext, boost::asio::ssl::context>> sslContext,
std::function<void(Error&&)> onError,
std::function<void(boost::system::error_code)> onAcceptAbort,
std::unique_ptr<StandardResponseProvider> standardResponseProvider)
Expand Down
6 changes: 3 additions & 3 deletions src/roar/session/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ namespace Roar
// ##################################################################################################################
struct Factory::Implementation
{
std::optional<SslServerContext>& sslContext;
std::optional<std::variant<SslServerContext, boost::asio::ssl::context>>& sslContext;
std::function<void(Error&&)> onError;

Implementation(std::optional<SslServerContext>& sslContext, std::function<void(Error&&)> onError)
Implementation(std::optional<std::variant<SslServerContext, boost::asio::ssl::context>>& sslContext, std::function<void(Error&&)> onError)
: sslContext{sslContext}
, onError{std::move(onError)}
{}
};
// ##################################################################################################################
Factory::Factory(std::optional<SslServerContext>& sslContext, std::function<void(Error&&)> onError)
Factory::Factory(std::optional<std::variant<SslServerContext, boost::asio::ssl::context>>& sslContext, std::function<void(Error&&)> onError)
: impl_{std::make_unique<Implementation>(sslContext, std::move(onError))}
{}
//------------------------------------------------------------------------------------------------------------------
Expand Down
24 changes: 20 additions & 4 deletions src/roar/session/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <roar/session/session.hpp>
#include <roar/request.hpp>
#include <roar/routing/router.hpp>
#include <roar/utility/visit_overloaded.hpp>

#include <boost/beast/core/flat_buffer.hpp>
#include <boost/asio/dispatch.hpp>
Expand Down Expand Up @@ -33,14 +34,29 @@ namespace Roar
Implementation(
boost::asio::ip::tcp::socket&& socket,
boost::beast::flat_buffer&& buffer,
std::optional<SslServerContext>& sslContext,
std::optional<std::variant<SslServerContext, boost::asio::ssl::context>>& sslContext,
bool isSecure,
std::function<void(Error&&)> onError,
std::weak_ptr<Router> router,
std::shared_ptr<const StandardResponseProvider> standardResponseProvider)
: stream{[&socket, &sslContext, isSecure]() -> decltype(stream) {
: stream{[&socket, &sslContext, isSecure]() mutable -> decltype(stream) {
if (isSecure)
return boost::beast::ssl_stream<Detail::StreamType>{std::move(socket), sslContext->ctx};
{
if (!sslContext)
throw std::runtime_error{"No SSL context available."};

return boost::beast::ssl_stream<Detail::StreamType>{
std::move(socket), [&sslContext]() -> boost::asio::ssl::context& {
return std::visit(
[](auto& ctx) -> boost::asio::ssl::context& {
if constexpr (std::is_same_v<std::decay_t<decltype(ctx)>, SslServerContext>)
return ctx.ctx;
else
return ctx;
},
*sslContext);
}()};
}
return Detail::StreamType{std::move(socket)};
}()}
, buffer{std::move(buffer)}
Expand All @@ -62,7 +78,7 @@ namespace Roar
Session::Session(
boost::asio::ip::tcp::socket&& socket,
boost::beast::flat_buffer&& buffer,
std::optional<SslServerContext>& sslContext,
std::optional<std::variant<SslServerContext, boost::asio::ssl::context>>& sslContext,
bool isSecure,
std::function<void(Error&&)> onError,
std::weak_ptr<Router> router,
Expand Down
9 changes: 9 additions & 0 deletions src/roar/ssl/make_ssl_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,13 @@ namespace Roar
boost::asio::buffer(ctx.diffieHellmanParameters.data(), ctx.diffieHellmanParameters.size()));
}
}

boost::asio::ssl::context makeSslContext(const std::string& certificate, const std::string& privateKey)
{
SslServerContext ctx;
ctx.certificate = certificate;
ctx.privateKey = privateKey;
initializeServerSslContext(ctx);
return std::move(ctx.ctx);
}
}

0 comments on commit 82b136b

Please sign in to comment.