RESTinio
tls.hpp
Go to the documentation of this file.
1 /*
2  restinio
3 */
4 
5 /*!
6  Support for https.
7 */
8 
9 #pragma once
10 
11 #include <restinio/traits.hpp>
12 #include <restinio/impl/tls_socket.hpp>
13 
14 namespace restinio
15 {
16 
18 
19 //
20 // tls_traits_t
21 //
22 
23 template <
24  typename Timer_Factory,
25  typename Logger,
27  typename Strand = asio_ns::strand< asio_ns::executor > >
29 
30 //
31 // single_thread_traits_t
32 //
33 
34 template <
35  typename Timer_Factory,
36  typename Logger,
40 
42 
43 //
44 // prepare_connection_and_start_read()
45 //
46 
47 //! Customizes connection init routine with an additional step:
48 //! perform handshake and only then start reading.
49 template < typename Connection, typename Start_Read_CB, typename Failed_CB >
50 void
52  tls_socket_t & socket,
53  Connection & con,
54  Start_Read_CB start_read_cb,
55  Failed_CB failed_cb )
56 {
61  con = con.shared_from_this() ]( const asio_ns::error_code & ec ){
62  if( !ec )
63  start_read_cb();
64  else
65  failed_cb( ec );
66  } );
67 }
68 
69 //
70 // socket_type_dependent_settings_t
71 //
72 
73 //! Customizes extra settings needed for working with socket.
74 /*!
75  Adds tls context setting.
76 */
77 template < typename Settings >
78 class socket_type_dependent_settings_t< Settings, tls_socket_t >
79 {
80 protected:
82 
83 public:
86  socket_type_dependent_settings_t && ) = default;
87 
88  Settings &
90  asio_ns::ssl::context context ) &
91  {
92  m_tls_context = std::move( context );
93  return upcast_reference();
94  }
95 
96  Settings &&
98  asio_ns::ssl::context context ) &&
99  {
100  return std::move( this->tls_context( std::move( context ) ) );
101  }
102 
105  {
106  return asio_ns::ssl::context{ std::move( m_tls_context ) };
107  }
108 
109  private:
110  Settings &
112  {
113  return static_cast< Settings & >( *this );
114  }
115 
116  asio_ns::ssl::context m_tls_context{ asio_ns::ssl::context::sslv23 };
117 };
118 
119 namespace impl
120 {
121 
122 //
123 // socket_supplier_t
124 //
125 
126 //! A custom socket storage for tls_socket_t.
127 template <>
128 class socket_supplier_t< tls_socket_t >
129 {
130  protected:
131  template < typename Settings >
133  Settings & settings,
134  asio_ns::io_context & io_context )
135  : m_tls_context{ std::make_shared< asio_ns::ssl::context >( settings.tls_context() ) }
136  , m_io_context{ io_context }
137  {
138  m_sockets.reserve( settings.concurrent_accepts_count() );
139 
140  while( m_sockets.size() < settings.concurrent_accepts_count() )
141  {
142  m_sockets.emplace_back( m_io_context, m_tls_context );
143  }
144  }
145 
146  virtual ~socket_supplier_t() = default;
147 
148  tls_socket_t &
150  //! Index of a socket in the pool.
151  std::size_t idx )
152  {
153  return m_sockets.at( idx );
154  }
155 
156  auto
158  //! Index of a socket in the pool.
159  std::size_t idx )
160  {
161  tls_socket_t res{ m_io_context, m_tls_context };
162  std::swap( res, m_sockets.at( idx ) );
163  return res;
164  }
165 
166  //! The number of sockets that can be used for
167  //! cuncurrent accept operations.
168  auto
170  {
171  return m_sockets.size();
172  }
173 
174  private:
178 };
179 
180 } /* namespace impl */
181 
182 } /* namespace restinio */
tls_socket_t & socket(std::size_t idx)
Definition: tls.hpp:149
Settings & tls_context(asio_ns::ssl::context context) &
Definition: tls.hpp:89
socket_type_dependent_settings_t(socket_type_dependent_settings_t &&)=default
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:51
auto cuncurrent_accept_sockets_count() const
The number of sockets that can be used for cuncurrent accept operations.
Definition: tls.hpp:169
socket_supplier_t(Settings &settings, asio_ns::io_context &io_context)
Definition: tls.hpp:132
std::vector< tls_socket_t > m_sockets
Definition: tls.hpp:177
Settings && tls_context(asio_ns::ssl::context context) &&
Definition: tls.hpp:97
std::shared_ptr< asio_ns::ssl::context > m_tls_context
Definition: tls.hpp:175
std::enable_if< std::is_same< Parameter_Container, query_string_params_t >::value||std::is_same< Parameter_Container, router::route_params_t >::value, optional_t< Value_Type > >::type opt_value(const Parameter_Container &params, string_view_t key)
Gets the value of a parameter specified by key wrapped in optional_t<Value_Type> if parameter exists ...
Definition: value_or.hpp:64