tls.hpp
Go to the documentation of this file.
/*
restinio
*/
#pragma once
#include <restinio/traits.hpp>
#include <restinio/impl/tls_socket.hpp>
namespace restinio
{
namespace connection_state
{
class tls_accessor_t
{
tls_socket_t & m_tls_socket;
public:
tls_accessor_t( tls_socket_t & tls_socket ) : m_tls_socket{tls_socket} {}
RESTINIO_NODISCARD
auto native_handle() const noexcept
{
return m_tls_socket.asio_ssl_stream().native_handle();
}
};
//
// The implementation of TLS-related part of notice_t.
//
template< typename Lambda >
void
accepted_t::try_inspect_tls( Lambda && lambda ) const
{
if( m_tls_socket )
lambda( tls_accessor_t{*m_tls_socket} );
}
template< typename Lambda >
decltype(auto)
accepted_t::inspect_tls_or_throw( Lambda && lambda ) const
{
if( !m_tls_socket )
throw exception_t{ "an attempt to call inspect_tls for "
"non-TLS-connection" };
return lambda( tls_accessor_t{*m_tls_socket} );
}
template< typename Lambda, typename T >
T
accepted_t::inspect_tls_or_default( Lambda && lambda, T && default_value ) const
{
if( m_tls_socket )
return lambda( tls_accessor_t{*m_tls_socket} );
return default_value;
}
} /* namespace connection_state */
//
// tls_traits_t
//
template <
typename Timer_Factory,
typename Logger,
typename Request_Handler = default_request_handler_t,
typename Strand = asio_ns::strand< default_asio_executor > >
using tls_traits_t = traits_t< Timer_Factory, Logger, Request_Handler, Strand, tls_socket_t >;
//
// single_thread_traits_t
//
template <
typename Timer_Factory,
typename Logger,
typename Request_Handler = default_request_handler_t >
using single_thread_tls_traits_t =
tls_traits_t< Timer_Factory, Logger, Request_Handler, noop_strand_t >;
using default_tls_traits_t = tls_traits_t< asio_timer_manager_t, null_logger_t >;
//
// prepare_connection_and_start_read()
//
template < typename Connection, typename Start_Read_CB, typename Failed_CB >
void
prepare_connection_and_start_read(
tls_socket_t & socket,
Connection & con,
Start_Read_CB start_read_cb,
Failed_CB failed_cb )
{
socket.async_handshake(
asio_ns::ssl::stream_base::server,
[ start_read_cb = std::move( start_read_cb ),
failed_cb = std::move( failed_cb ),
con = con.shared_from_this() ]( const asio_ns::error_code & ec ){
if( !ec )
start_read_cb();
else
failed_cb( ec );
} );
}
//
// socket_type_dependent_settings_t
//
template < typename Settings >
class socket_type_dependent_settings_t< Settings, tls_socket_t >
{
protected:
~socket_type_dependent_settings_t() = default;
public:
socket_type_dependent_settings_t() = default;
socket_type_dependent_settings_t(
socket_type_dependent_settings_t && ) = default;
Settings &
tls_context(
asio_ns::ssl::context context ) &
{
m_tls_context = std::make_shared< asio_ns::ssl::context >(
std::move( context ) );
return upcast_reference();
}
Settings &&
tls_context(
asio_ns::ssl::context context ) &&
{
return std::move( this->tls_context( std::move( context ) ) );
}
Settings &
tls_context(
std::shared_ptr< asio_ns::ssl::context > shared_context ) &
{
m_tls_context = std::move( shared_context );
return upcast_reference();
}
Settings &&
tls_context(
std::shared_ptr< asio_ns::ssl::context > shared_context ) &&
{
return std::move( this->tls_context( std::move(shared_context) ) );
}
//FIXME: should be removed in v.0.7.
[[deprecated]]
asio_ns::ssl::context
tls_context()
{
asio_ns::ssl::context result{ std::move( *m_tls_context ) };
m_tls_context.reset();
return result;
}
std::shared_ptr< asio_ns::ssl::context >
giveaway_tls_context()
{
return std::move(m_tls_context);
}
private:
Settings &
upcast_reference()
{
return static_cast< Settings & >( *this );
}
std::shared_ptr< asio_ns::ssl::context > m_tls_context{
std::make_shared< asio_ns::ssl::context >(
asio_ns::ssl::context::sslv23 )
};
};
namespace impl
{
// An overload for the case of non-TLS-connection.
inline tls_socket_t *
make_tls_socket_pointer_for_state_listener(
tls_socket_t & socket ) noexcept
{
return &socket;
}
//
// socket_supplier_t
//
template <>
class socket_supplier_t< tls_socket_t >
{
protected:
template < typename Settings >
socket_supplier_t(
Settings & settings,
asio_ns::io_context & io_context )
: m_tls_context{ settings.giveaway_tls_context() }
, m_io_context{ io_context }
{
m_sockets.reserve( settings.concurrent_accepts_count() );
while( m_sockets.size() < settings.concurrent_accepts_count() )
{
m_sockets.emplace_back( m_io_context, m_tls_context );
}
}
virtual ~socket_supplier_t() = default;
tls_socket_t &
socket(
std::size_t idx )
{
return m_sockets.at( idx );
}
auto
move_socket(
std::size_t idx )
{
tls_socket_t res{ m_io_context, m_tls_context };
std::swap( res, m_sockets.at( idx ) );
return res;
}
auto
concurrent_accept_sockets_count() const
{
return m_sockets.size();
}
private:
std::shared_ptr< asio_ns::ssl::context > m_tls_context;
asio_ns::io_context & m_io_context;
std::vector< tls_socket_t > m_sockets;
};
} /* namespace impl */
} /* namespace restinio */
restinio::connection_state::accepted_t::inspect_tls_or_throw
decltype(auto) inspect_tls_or_throw(Lambda &&lambda) const
Calls the specified lambda-function if the accepted connection is a TLS-connection.
Definition: tls.hpp:109
restinio::connection_state::accepted_t::inspect_tls_or_default
T inspect_tls_or_default(Lambda &&lambda, T &&default_value) const
Calls the specified lambda-function if the accepted connection is a TLS-connection.
Definition: tls.hpp:120
restinio::connection_state::accepted_t::m_tls_socket
tls_socket_t * m_tls_socket
An optional pointer to TLS-related connection.
Definition: connection_state_listener.hpp:45
restinio::connection_state::accepted_t::try_inspect_tls
void try_inspect_tls(Lambda &&lambda) const
Calls the specified lambda-function if the accepted connection is a TLS-connection.
Definition: tls.hpp:101
restinio::connection_state::tls_accessor_t
Accessor to TLS-specific information related to a connection.
Definition: tls.hpp:31
restinio::connection_state::tls_accessor_t::native_handle
RESTINIO_NODISCARD auto native_handle() const noexcept
Get the access to native handle behind Asio's ssl_stream.
Definition: tls.hpp:89
restinio::connection_state::tls_accessor_t::tls_accessor_t
tls_accessor_t(tls_socket_t &tls_socket)
Definition: tls.hpp:35
restinio::connection_state::tls_accessor_t::m_tls_socket
tls_socket_t & m_tls_socket
Definition: tls.hpp:32
restinio::exception_t
Exception class for all exceptions thrown by RESTinio.
Definition: exception.hpp:26
restinio::impl::socket_supplier_t< tls_socket_t >::m_io_context
asio_ns::io_context & m_io_context
Definition: tls.hpp:401
restinio::impl::socket_supplier_t< tls_socket_t >::socket
tls_socket_t & socket(std::size_t idx)
Definition: tls.hpp:374
restinio::impl::socket_supplier_t< tls_socket_t >::~socket_supplier_t
virtual ~socket_supplier_t()=default
restinio::impl::socket_supplier_t< tls_socket_t >::concurrent_accept_sockets_count
auto concurrent_accept_sockets_count() const
The number of sockets that can be used for cuncurrent accept operations.
Definition: tls.hpp:394
restinio::impl::socket_supplier_t< tls_socket_t >::m_tls_context
std::shared_ptr< asio_ns::ssl::context > m_tls_context
Definition: tls.hpp:400
restinio::impl::socket_supplier_t< tls_socket_t >::m_sockets
std::vector< tls_socket_t > m_sockets
Definition: tls.hpp:402
restinio::impl::socket_supplier_t< tls_socket_t >::move_socket
auto move_socket(std::size_t idx)
Definition: tls.hpp:382
restinio::impl::socket_supplier_t< tls_socket_t >::socket_supplier_t
socket_supplier_t(Settings &settings, asio_ns::io_context &io_context)
Definition: tls.hpp:357
restinio::impl::socket_supplier_t
Definition: acceptor.hpp:42
restinio::impl::socket_supplier_t::m_sockets
std::vector< Socket > m_sockets
A temporary socket for receiving new connections.
Definition: acceptor.hpp:96
restinio::impl::socket_supplier_t::m_io_context
asio_ns::io_context & m_io_context
io_context for sockets to run on.
Definition: acceptor.hpp:92
restinio::impl::tls_socket_t
Socket adapter for asio::ssl::stream< asio::ip::tcp::socket >.
Definition: tls_socket.hpp:37
restinio::impl::tls_socket_t::async_handshake
auto async_handshake(Args &&... args)
Definition: tls_socket.hpp:162
restinio::impl::tls_socket_t::asio_ssl_stream
socket_t & asio_ssl_stream()
Get an access to underlying Asio's socket.
Definition: tls_socket.hpp:87
restinio::socket_type_dependent_settings_t< Settings, tls_socket_t >::tls_context
Settings & tls_context(std::shared_ptr< asio_ns::ssl::context > shared_context) &
Setup a shared TLS-context for server's settings.
Definition: tls.hpp:247
restinio::socket_type_dependent_settings_t< Settings, tls_socket_t >::socket_type_dependent_settings_t
socket_type_dependent_settings_t()=default
restinio::socket_type_dependent_settings_t< Settings, tls_socket_t >::tls_context
Settings && tls_context(std::shared_ptr< asio_ns::ssl::context > shared_context) &&
Setup a shared TLS-context for server's settings.
Definition: tls.hpp:288
restinio::socket_type_dependent_settings_t< Settings, tls_socket_t >::upcast_reference
Settings & upcast_reference()
Definition: tls.hpp:325
restinio::socket_type_dependent_settings_t< Settings, tls_socket_t >::tls_context
asio_ns::ssl::context tls_context()
Definition: tls.hpp:302
restinio::socket_type_dependent_settings_t< Settings, tls_socket_t >::tls_context
Settings && tls_context(asio_ns::ssl::context context) &&
Setup an exclusive TLS-context for server's settings.
Definition: tls.hpp:211
restinio::socket_type_dependent_settings_t< Settings, tls_socket_t >::~socket_type_dependent_settings_t
~socket_type_dependent_settings_t()=default
restinio::socket_type_dependent_settings_t< Settings, tls_socket_t >::socket_type_dependent_settings_t
socket_type_dependent_settings_t(socket_type_dependent_settings_t &&)=default
restinio::socket_type_dependent_settings_t< Settings, tls_socket_t >::tls_context
Settings & tls_context(asio_ns::ssl::context context) &
Setup an exclusive TLS-context for server's settings.
Definition: tls.hpp:201
restinio::socket_type_dependent_settings_t< Settings, tls_socket_t >::giveaway_tls_context
std::shared_ptr< asio_ns::ssl::context > giveaway_tls_context()
Get away the TLS-context from settings.
Definition: tls.hpp:318
restinio::socket_type_dependent_settings_t
Extra settings needed for working with socket.
Definition: settings.hpp:155
RESTINIO_NODISCARD
#define RESTINIO_NODISCARD
Definition: compiler_features.hpp:33
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:465
nonstd::optional_lite::swap
void swap(optional< T > &x, optional< T > &y)
Definition: optional.hpp:1705
restinio::impl::make_tls_socket_pointer_for_state_listener
tls_socket_t * make_tls_socket_pointer_for_state_listener(asio_ns::ip::tcp::socket &) noexcept
Definition: connection.hpp:277
restinio
Definition: asio_include.hpp:21
restinio::tls_socket_t
impl::tls_socket_t tls_socket_t
A public alias for the actual implementation of TLS-socket.
Definition: tls_fwd.hpp:30
restinio::default_request_handler_t
std::function< request_handling_status_t(request_handle_t) > default_request_handler_t
Definition: request_handler.hpp:470
restinio::prepare_connection_and_start_read
void prepare_connection_and_start_read(tls_socket_t &socket, Connection &con, Start_Read_CB start_read_cb, Failed_CB failed_cb)
Customizes connection init routine with an additional step: perform handshake and only then start rea...
Definition: tls.hpp:162
restinio::traits_t
Definition: traits.hpp:147
tls_socket.hpp
traits.hpp
const
#define const
Definition: zconf.h:230