RESTinio

http_server.hpp
Go to the documentation of this file.
1/*
2 restinio
3*/
4
9#pragma once
10
11#include <restinio/exception.hpp>
12#include <restinio/settings.hpp>
13#include <restinio/request_handler.hpp>
14#include <restinio/impl/acceptor.hpp>
15#include <restinio/traits.hpp>
16
17#include <memory>
18
19 namespace restinio
20{
21
22//
23// io_context_shared_ptr_t
24//
25 using io_context_shared_ptr_t = std::shared_ptr< asio_ns::io_context >;
26
27//
28// io_context_holder_t
29//
35 class io_context_holder_t
36{
37 io_context_shared_ptr_t m_context;
38public :
39 io_context_holder_t( io_context_shared_ptr_t context )
40 : m_context( std::move(context) )
41 {}
42
43 io_context_shared_ptr_t
44 giveaway_context()
45 {
46 return std::move(m_context);
47 }
48};
49
50//
51// own_io_context
52//
64 inline io_context_holder_t
65 own_io_context()
66{
67 return { std::make_shared< asio_ns::io_context >() };
68}
69
70//
71// external_io_context
72//
86 inline io_context_holder_t
87 external_io_context( asio_ns::io_context & ctx )
88{
89 return { std::shared_ptr< asio_ns::io_context >(
90 std::addressof(ctx),
91 // Empty deleter.
92 []( asio_ns::io_context * ){} )
93 };
94}
95
96//
97// http_server_t
98//
99
101
167 template < typename Traits = default_traits_t >
168 class http_server_t
169{
170 using connection_settings_t = impl::connection_settings_t< Traits >;
171 using connection_factory_t = impl::connection_factory_t< Traits >;
172 using acceptor_t = impl::acceptor_t< Traits >;
173 using timer_manager_t = typename Traits::timer_manager_t;
174 using timer_manager_handle_t = std::shared_ptr< timer_manager_t >;
175
176 public:
182 using traits_t = Traits;
183
184 // This is not Copyable nor Moveable type.
185 http_server_t( const http_server_t & ) = delete;
186 http_server_t( http_server_t && ) = delete;
187
188 template<typename D>
189 http_server_t(
190 io_context_holder_t io_context,
191 basic_server_settings_t< D, Traits > && settings )
192 : m_io_context{ io_context.giveaway_context() }
193 , m_cleanup_functor{ settings.giveaway_cleanup_func() }
194 {
195 // Since v.0.5.1 the presence of custom connection state
196 // listener should be checked before the start of HTTP server.
197 settings.ensure_valid_connection_state_listener();
198 // The presence of IP-blocker should also be checked.
199 settings.ensure_valid_ip_blocker();
200
201 // Now we can continue preparation of HTTP server.
202
203 using actual_settings_type = basic_server_settings_t<D, Traits>;
204
205 auto timer_factory = settings.timer_factory();
206 m_timer_manager = timer_factory->create( this->io_context() );
207
208 auto conn_settings =
209 std::make_shared< connection_settings_t >(
210 std::forward< actual_settings_type >(settings),
211 impl::create_parser_settings< typename Traits::http_methods_mapper_t >(),
212 m_timer_manager );
213
214 m_acceptor =
215 std::make_shared< acceptor_t >(
216 settings,
217 this->io_context(),
218 std::make_shared< connection_factory_t >(
219 conn_settings,
220 settings.socket_options_setter() ),
221 *( conn_settings->m_logger ) );
222 }
223
224 template<
225 typename Configurator,
226 // Use SFINAE.
227 // This constructor must be called only if Configurator
228 // allows to call operator() with server_settings_t& arg.
229 typename = decltype(
230 std::declval<Configurator>()(
231 *(static_cast<server_settings_t<Traits>*>(nullptr)))) >
232 http_server_t(
233 io_context_holder_t io_context,
234 Configurator && configurator )
235 : http_server_t{
236 io_context,
237 exec_configurator< Traits, Configurator >(
238 std::forward< Configurator >( configurator ) ) }
239 {}
240
242 virtual ~http_server_t()
243 {
244 // Ensure server is closed after destruction of http_server instance.
245 close_sync();
246 }
247
249 asio_ns::io_context & io_context() noexcept { return *m_io_context; }
250
252
261 template <
262 typename Server_Open_Ok_CB,
263 typename Server_Open_Error_CB >
264 void
265 open_async(
266 Server_Open_Ok_CB open_ok_cb,
267 Server_Open_Error_CB open_err_cb )
268 {
269 asio_ns::post(
270 m_acceptor->get_open_close_operations_executor(),
271 [ this,
272 ok_cb = std::move( open_ok_cb ),
273 err_cb = std::move( open_err_cb ) ]{
274 try
275 {
276 open_sync();
277 call_nothrow_cb( ok_cb );
278 }
279 catch( ... )
280 {
281 call_nothrow_cb( [&err_cb] {
282 err_cb( std::current_exception() );
283 } );
284 }
285 } );
286 }
287
289
293 void
294 open_sync()
295 {
296 if( running_state_t::not_running == m_running_state )
297 {
298 m_timer_manager->start();
299 m_acceptor->open();
300 m_running_state = running_state_t::running;
301 }
302 }
303
305
315 template <
316 typename Server_Close_Ok_CB,
317 typename Server_Close_Error_CB >
318 void
319 close_async(
320 Server_Close_Ok_CB close_ok_cb,
321 Server_Close_Error_CB close_err_cb )
322 {
323 asio_ns::post(
324 m_acceptor->get_open_close_operations_executor(),
325 [ this,
326 ok_cb = std::move( close_ok_cb ),
327 err_cb = std::move( close_err_cb ) ]{
328 try
329 {
330 close_sync();
331 call_nothrow_cb( ok_cb );
332 }
333 catch( ... )
334 {
335 call_nothrow_cb( [&err_cb] {
336 err_cb( std::current_exception() );
337 } );
338 }
339 } );
340 }
341
343
347 void
348 close_sync()
349 {
350 if( running_state_t::running == m_running_state )
351 {
352 m_timer_manager->stop();
353 m_acceptor->close();
354 call_cleanup_functor();
355 m_running_state = running_state_t::not_running;
356 }
357 }
358
359 private:
361 io_context_shared_ptr_t m_io_context;
362
364 cleanup_functor_t m_cleanup_functor;
365
367 std::shared_ptr< acceptor_t > m_acceptor;
368
370 timer_manager_handle_t m_timer_manager;
371
373 enum class running_state_t
374 {
375 not_running,
376 running,
377 };
378
380 running_state_t m_running_state{ running_state_t::not_running };
381
383
391 void
392 call_cleanup_functor() noexcept
393 {
394 if( m_cleanup_functor )
395 {
396 cleanup_functor_t fn{ std::move(m_cleanup_functor) };
397 fn();
398 }
399 }
400
402 template< typename Callback >
403 static void call_nothrow_cb( Callback && cb ) noexcept
404 {
405 cb();
406 }
407};
408
409} /* namespace restinio */
410
acceptor.hpp
restinio::basic_server_settings_t
Basic container for http_server settings.
Definition: settings.hpp:556
restinio::basic_server_settings_t::timer_factory
std::unique_ptr< timer_factory_t > timer_factory()
Definition: settings.hpp:903
restinio::http_server_t
Class for http-server.
Definition: http_server.hpp:169
restinio::http_server_t::m_io_context
io_context_shared_ptr_t m_io_context
A wrapper for asio io_context where server is running.
Definition: http_server.hpp:361
restinio::http_server_t::traits_t
Traits traits_t
An alias for Traits type.
Definition: http_server.hpp:182
restinio::http_server_t::timer_manager_t
typename Traits::timer_manager_t timer_manager_t
Definition: http_server.hpp:173
restinio::http_server_t::call_cleanup_functor
void call_cleanup_functor() noexcept
Call a cleanup functor if it is defined.
Definition: http_server.hpp:392
restinio::http_server_t::http_server_t
http_server_t(const http_server_t &)=delete
restinio::http_server_t::close_sync
void close_sync()
Stop server.
Definition: http_server.hpp:348
restinio::http_server_t::m_acceptor
std::shared_ptr< acceptor_t > m_acceptor
Acceptor for new connections.
Definition: http_server.hpp:367
restinio::http_server_t::open_async
void open_async(Server_Open_Ok_CB open_ok_cb, Server_Open_Error_CB open_err_cb)
Starts server in async way.
Definition: http_server.hpp:265
restinio::http_server_t::http_server_t
http_server_t(io_context_holder_t io_context, basic_server_settings_t< D, Traits > &&settings)
Definition: http_server.hpp:189
restinio::http_server_t::close_async
void close_async(Server_Close_Ok_CB close_ok_cb, Server_Close_Error_CB close_err_cb)
Closes server in async way.
Definition: http_server.hpp:319
restinio::http_server_t::m_timer_manager
timer_manager_handle_t m_timer_manager
Timer manager object.
Definition: http_server.hpp:370
restinio::http_server_t::running_state_t
running_state_t
State of server.
Definition: http_server.hpp:374
restinio::http_server_t::http_server_t
http_server_t(http_server_t &&)=delete
restinio::http_server_t::http_server_t
http_server_t(io_context_holder_t io_context, Configurator &&configurator)
Definition: http_server.hpp:232
restinio::http_server_t::io_context
asio_ns::io_context & io_context() noexcept
Get io_context on which server runs.
Definition: http_server.hpp:249
restinio::http_server_t::timer_manager_handle_t
std::shared_ptr< timer_manager_t > timer_manager_handle_t
Definition: http_server.hpp:174
restinio::http_server_t::m_cleanup_functor
cleanup_functor_t m_cleanup_functor
An optional user's cleanup functor.
Definition: http_server.hpp:364
restinio::http_server_t::~http_server_t
virtual ~http_server_t()
It is allowed to inherit from http_server_t.
Definition: http_server.hpp:242
restinio::http_server_t::open_sync
void open_sync()
Start server.
Definition: http_server.hpp:294
restinio::http_server_t::call_nothrow_cb
static void call_nothrow_cb(Callback &&cb) noexcept
Call callback and terminate the application if callback throws.
Definition: http_server.hpp:403
restinio::impl::acceptor_t
Context for accepting http connections.
Definition: acceptor.hpp:167
restinio::impl::connection_factory_t
Factory for connections.
Definition: connection.hpp:1709
restinio::io_context_holder_t
Helper class for holding shared pointer to io_context.
Definition: http_server.hpp:36
restinio::io_context_holder_t::giveaway_context
io_context_shared_ptr_t giveaway_context()
Definition: http_server.hpp:44
restinio::io_context_holder_t::m_context
io_context_shared_ptr_t m_context
Definition: http_server.hpp:37
restinio::io_context_holder_t::io_context_holder_t
io_context_holder_t(io_context_shared_ptr_t context)
Definition: http_server.hpp:39
restinio::server_settings_t
A fluent style interface for setting http server params.
Definition: settings.hpp:1768
exception.hpp
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:465
restinio
Definition: asio_include.hpp:21
restinio::external_io_context
io_context_holder_t external_io_context(asio_ns::io_context &ctx)
Function which tells that http_server should use external instance of io_context and should not contr...
Definition: http_server.hpp:87
restinio::cleanup_functor_t
std::function< void(void) > cleanup_functor_t
Type of holder for user's cleanup function.
Definition: settings.hpp:308
restinio::exec_configurator
auto exec_configurator(Configurator &&configurator)
Definition: settings.hpp:1777
restinio::io_context_shared_ptr_t
std::shared_ptr< asio_ns::io_context > io_context_shared_ptr_t
Definition: http_server.hpp:25
restinio::own_io_context
io_context_holder_t own_io_context()
Function which tells that http_server should create and use its own instance of io_context.
Definition: http_server.hpp:65
std
STL namespace.
request_handler.hpp
settings.hpp
restinio::impl::connection_settings_t
Parameters shared between connections.
Definition: connection_settings.hpp:114
traits.hpp