Loading...
Searching...
No Matches
http_server.hpp
Go to the documentation of this file.
/*
restinio
*/
#pragma once
#include <restinio/exception.hpp>
#include <restinio/settings.hpp>
#include <restinio/request_handler.hpp>
#include <restinio/impl/acceptor.hpp>
#include <restinio/traits.hpp>
#include <memory>
namespace restinio
{
//
// io_context_shared_ptr_t
//
using io_context_shared_ptr_t = std::shared_ptr< asio_ns::io_context >;
//
// io_context_holder_t
//
class io_context_holder_t
{
io_context_shared_ptr_t m_context;
public :
io_context_holder_t( io_context_shared_ptr_t context )
: m_context( std::move(context) )
{}
io_context_shared_ptr_t
giveaway_context()
{
return std::move(m_context);
}
};
//
// own_io_context
//
inline io_context_holder_t
own_io_context()
{
return { std::make_shared< asio_ns::io_context >() };
}
//
// external_io_context
//
inline io_context_holder_t
external_io_context( asio_ns::io_context & ctx )
{
return { std::shared_ptr< asio_ns::io_context >(
std::addressof(ctx),
// Empty deleter.
[]( asio_ns::io_context * ){} )
};
}
//
// http_server_t
//
template < typename Traits = default_traits_t >
class http_server_t
{
using connection_settings_t = impl::connection_settings_t< Traits >;
using connection_factory_t = impl::connection_factory_t< Traits >;
using acceptor_t = impl::acceptor_t< Traits >;
using timer_manager_t = typename Traits::timer_manager_t;
using timer_manager_handle_t = std::shared_ptr< timer_manager_t >;
public:
using traits_t = Traits;
// This is not Copyable nor Moveable type.
http_server_t( const http_server_t & ) = delete;
http_server_t( http_server_t && ) = delete;
template<typename D>
http_server_t(
io_context_holder_t io_context,
basic_server_settings_t< D, Traits > && settings )
: m_io_context{ io_context.giveaway_context() }
, m_cleanup_functor{ settings.giveaway_cleanup_func() }
{
// Since v.0.5.1 the presence of custom connection state
// listener should be checked before the start of HTTP server.
settings.ensure_valid_connection_state_listener();
// The presence of IP-blocker should also be checked.
settings.ensure_valid_ip_blocker();
// Now we can continue preparation of HTTP server.
using actual_settings_type = basic_server_settings_t<D, Traits>;
auto timer_factory = settings.timer_factory();
m_timer_manager = timer_factory->create( this->io_context() );
auto conn_settings =
std::make_shared< connection_settings_t >(
std::forward< actual_settings_type >(settings),
impl::create_parser_settings< typename Traits::http_methods_mapper_t >(),
m_timer_manager );
m_acceptor =
std::make_shared< acceptor_t >(
settings,
this->io_context(),
std::make_shared< connection_factory_t >(
conn_settings,
settings.socket_options_setter() ),
*( conn_settings->m_logger ) );
}
template<
typename Configurator,
// Use SFINAE.
// This constructor must be called only if Configurator
// allows to call operator() with server_settings_t& arg.
typename = decltype(
std::declval<Configurator>()(
*(static_cast<server_settings_t<Traits>*>(nullptr)))) >
http_server_t(
io_context_holder_t io_context,
Configurator && configurator )
: http_server_t{
io_context,
exec_configurator< Traits, Configurator >(
std::forward< Configurator >( configurator ) ) }
{}
virtual ~http_server_t()
{
// Ensure server is closed after destruction of http_server instance.
close_sync();
}
asio_ns::io_context & io_context() noexcept { return *m_io_context; }
template <
typename Server_Open_Ok_CB,
typename Server_Open_Error_CB >
void
open_async(
Server_Open_Ok_CB open_ok_cb,
Server_Open_Error_CB open_err_cb )
{
asio_ns::post(
m_acceptor->get_open_close_operations_executor(),
[ this,
ok_cb = std::move( open_ok_cb ),
err_cb = std::move( open_err_cb ) ]{
try
{
open_sync();
call_nothrow_cb( ok_cb );
}
catch( ... )
{
call_nothrow_cb( [&err_cb] {
err_cb( std::current_exception() );
} );
}
} );
}
void
open_sync()
{
if( running_state_t::not_running == m_running_state )
{
m_timer_manager->start();
m_acceptor->open();
m_running_state = running_state_t::running;
}
}
template <
typename Server_Close_Ok_CB,
typename Server_Close_Error_CB >
void
close_async(
Server_Close_Ok_CB close_ok_cb,
Server_Close_Error_CB close_err_cb )
{
asio_ns::post(
m_acceptor->get_open_close_operations_executor(),
[ this,
ok_cb = std::move( close_ok_cb ),
err_cb = std::move( close_err_cb ) ]{
try
{
close_sync();
call_nothrow_cb( ok_cb );
}
catch( ... )
{
call_nothrow_cb( [&err_cb] {
err_cb( std::current_exception() );
} );
}
} );
}
void
close_sync()
{
if( running_state_t::running == m_running_state )
{
m_timer_manager->stop();
m_acceptor->close();
call_cleanup_functor();
m_running_state = running_state_t::not_running;
}
}
private:
io_context_shared_ptr_t m_io_context;
cleanup_functor_t m_cleanup_functor;
std::shared_ptr< acceptor_t > m_acceptor;
timer_manager_handle_t m_timer_manager;
enum class running_state_t
{
not_running,
running,
};
running_state_t m_running_state{ running_state_t::not_running };
void
call_cleanup_functor() noexcept
{
if( m_cleanup_functor )
{
cleanup_functor_t fn{ std::move(m_cleanup_functor) };
fn();
}
}
template< typename Callback >
static void call_nothrow_cb( Callback && cb ) noexcept
{
cb();
}
};
} /* namespace restinio */
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:1754
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:1769
exception.hpp
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:1778
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