RESTinio
tls.hpp
Go to the documentation of this file.
1/*
2 restinio
3*/
4
9#pragma once
10
11#include <restinio/traits.hpp>
13
14namespace restinio
15{
16
17namespace connection_state
18{
19
31{
33
34public:
35 tls_accessor_t( tls_socket_t & tls_socket ) : m_tls_socket{tls_socket} {}
36
89 auto native_handle() const noexcept
90 {
91 return m_tls_socket.asio_ssl_stream().native_handle();
92 }
93};
94
95//
96// The implementation of TLS-related part of notice_t.
97//
98
99template< typename Lambda >
100void
101accepted_t::try_inspect_tls( Lambda && lambda ) const
102{
103 if( m_tls_socket )
104 lambda( tls_accessor_t{*m_tls_socket} );
105}
106
107template< typename Lambda >
108decltype(auto)
109accepted_t::inspect_tls_or_throw( Lambda && lambda ) const
110{
111 if( !m_tls_socket )
112 throw exception_t{ "an attempt to call inspect_tls for "
113 "non-TLS-connection" };
114
115 return lambda( tls_accessor_t{*m_tls_socket} );
116}
117
118template< typename Lambda, typename T >
119T
120accepted_t::inspect_tls_or_default( Lambda && lambda, T && default_value ) const
121{
122 if( m_tls_socket )
123 return lambda( tls_accessor_t{*m_tls_socket} );
124
125 return default_value;
126}
127
128} /* namespace connection_state */
129
130//
131// tls_traits_t
132//
133
134template <
135 typename Timer_Factory,
136 typename Logger,
137 typename Request_Handler = default_request_handler_t,
138 typename Strand = asio_ns::strand< default_asio_executor > >
140
141//
142// single_thread_traits_t
143//
144
145template <
146 typename Timer_Factory,
147 typename Logger,
148 typename Request_Handler = default_request_handler_t >
151
153
154//
155// prepare_connection_and_start_read()
156//
157
160template < typename Connection, typename Start_Read_CB, typename Failed_CB >
161void
163 tls_socket_t & socket,
164 Connection & con,
165 Start_Read_CB start_read_cb,
166 Failed_CB failed_cb )
167{
168 socket.async_handshake(
169 asio_ns::ssl::stream_base::server,
170 [ start_read_cb = std::move( start_read_cb ),
171 failed_cb = std::move( failed_cb ),
172 con = con.shared_from_this() ]( const asio_ns::error_code & ec ){
173 if( !ec )
174 start_read_cb();
175 else
176 failed_cb( ec );
177 } );
178}
179
180//
181// socket_type_dependent_settings_t
182//
183
185
188template < typename Settings >
190{
191protected:
193
194public:
198
200 Settings &
202 asio_ns::ssl::context context ) &
203 {
204 m_tls_context = std::make_shared< asio_ns::ssl::context >(
205 std::move( context ) );
206 return upcast_reference();
207 }
208
210 Settings &&
212 asio_ns::ssl::context context ) &&
213 {
214 return std::move( this->tls_context( std::move( context ) ) );
215 }
216
218
246 Settings &
248 std::shared_ptr< asio_ns::ssl::context > shared_context ) &
249 {
250 m_tls_context = std::move( shared_context );
251 return upcast_reference();
252 }
253
255
287 Settings &&
289 std::shared_ptr< asio_ns::ssl::context > shared_context ) &&
290 {
291 return std::move( this->tls_context( std::move(shared_context) ) );
292 }
293
294 //FIXME: should be removed in v.0.7.
300 [[deprecated]]
301 asio_ns::ssl::context
303 {
304 asio_ns::ssl::context result{ std::move( *m_tls_context ) };
305 m_tls_context.reset();
306
307 return result;
308 }
309
311
317 std::shared_ptr< asio_ns::ssl::context >
319 {
320 return std::move(m_tls_context);
321 }
322
323 private:
324 Settings &
326 {
327 return static_cast< Settings & >( *this );
328 }
329
330 std::shared_ptr< asio_ns::ssl::context > m_tls_context{
331 std::make_shared< asio_ns::ssl::context >(
332 asio_ns::ssl::context::sslv23 )
333 };
334};
335
336namespace impl
337{
338
339// An overload for the case of non-TLS-connection.
340inline tls_socket_t *
342 tls_socket_t & socket ) noexcept
343{
344 return &socket;
345}
346
347//
348// socket_supplier_t
349//
350
352template <>
354{
355 protected:
356 template < typename Settings >
358 Settings & settings,
359 asio_ns::io_context & io_context )
360 : m_tls_context{ settings.giveaway_tls_context() }
361 , m_io_context{ io_context }
362 {
363 m_sockets.reserve( settings.concurrent_accepts_count() );
364
365 while( m_sockets.size() < settings.concurrent_accepts_count() )
366 {
367 m_sockets.emplace_back( m_io_context, m_tls_context );
368 }
369 }
370
371 virtual ~socket_supplier_t() = default;
372
376 std::size_t idx )
377 {
378 return m_sockets.at( idx );
379 }
380
381 auto
384 std::size_t idx )
385 {
386 tls_socket_t res{ m_io_context, m_tls_context };
387 std::swap( res, m_sockets.at( idx ) );
388 return res;
389 }
390
393 auto
395 {
396 return m_sockets.size();
397 }
398
399 private:
400 std::shared_ptr< asio_ns::ssl::context > m_tls_context;
401 asio_ns::io_context & m_io_context;
402 std::vector< tls_socket_t > m_sockets;
403};
404
405} /* namespace impl */
406
407} /* namespace restinio */
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
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
tls_socket_t * m_tls_socket
An optional pointer to TLS-related connection.
void try_inspect_tls(Lambda &&lambda) const
Calls the specified lambda-function if the accepted connection is a TLS-connection.
Definition: tls.hpp:101
Accessor to TLS-specific information related to a connection.
Definition: tls.hpp:31
RESTINIO_NODISCARD auto native_handle() const noexcept
Get the access to native handle behind Asio's ssl_stream.
Definition: tls.hpp:89
tls_accessor_t(tls_socket_t &tls_socket)
Definition: tls.hpp:35
Exception class for all exceptions thrown by RESTinio.
Definition: exception.hpp:26
tls_socket_t & socket(std::size_t idx)
Definition: tls.hpp:374
auto concurrent_accept_sockets_count() const
The number of sockets that can be used for cuncurrent accept operations.
Definition: tls.hpp:394
std::shared_ptr< asio_ns::ssl::context > m_tls_context
Definition: tls.hpp:400
std::vector< tls_socket_t > m_sockets
Definition: tls.hpp:402
socket_supplier_t(Settings &settings, asio_ns::io_context &io_context)
Definition: tls.hpp:357
std::vector< Socket > m_sockets
A temporary socket for receiving new connections.
Definition: acceptor.hpp:96
asio_ns::io_context & m_io_context
io_context for sockets to run on.
Definition: acceptor.hpp:92
Socket adapter for asio::ssl::stream< asio::ip::tcp::socket >.
Definition: tls_socket.hpp:37
auto async_handshake(Args &&... args)
Definition: tls_socket.hpp:162
socket_t & asio_ssl_stream()
Get an access to underlying Asio's socket.
Definition: tls_socket.hpp:87
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
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
Settings && tls_context(asio_ns::ssl::context context) &&
Setup an exclusive TLS-context for server's settings.
Definition: tls.hpp:211
socket_type_dependent_settings_t(socket_type_dependent_settings_t &&)=default
Settings & tls_context(asio_ns::ssl::context context) &
Setup an exclusive TLS-context for server's settings.
Definition: tls.hpp:201
std::shared_ptr< asio_ns::ssl::context > giveaway_tls_context()
Get away the TLS-context from settings.
Definition: tls.hpp:318
Extra settings needed for working with socket.
Definition: settings.hpp:155
#define RESTINIO_NODISCARD
void swap(optional< T > &x, optional< T > &y)
Definition: optional.hpp:1705
tls_socket_t * make_tls_socket_pointer_for_state_listener(asio_ns::ip::tcp::socket &) noexcept
Definition: connection.hpp:277
impl::tls_socket_t tls_socket_t
A public alias for the actual implementation of TLS-socket.
Definition: tls_fwd.hpp:30
std::function< request_handling_status_t(request_handle_t) > default_request_handler_t
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
#define const
Definition: zconf.h:230