#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
class blocker_t
{
std::mutex m_lock;
using connections_t = std::map<
restinio::asio_ns::ip::address,
std::vector< restinio::connection_id_t > >;
connections_t m_connections;
public:
inspect(
{
std::lock_guard<std::mutex> l{ m_lock };
if( it == m_connections.end() || it->second.size() < 3u )
else
}
void state_changed(
{
std::lock_guard<std::mutex> l{ m_lock };
const auto cause = notice.
cause();
if( restinio::holds_alternative< accepted_t >( cause ) )
{
}
else if( restinio::holds_alternative< closed_t >( cause ) )
{
connections.erase(
std::find( std::begin(connections), std::end(connections),
if( connections.empty() )
}
}
};
restinio::asio_ns::io_context & ioctx,
{
if( restinio::http_method_get() == req->header().method() &&
req->header().request_target() == "/" )
{
auto timer = std::make_shared<restinio::asio_ns::steady_timer>( ioctx );
timer->expires_after( std::chrono::seconds{15} );
timer->async_wait( [timer, req](const auto & ec) {
if( !ec ) {
req->create_response()
.append_header( restinio::http_field::server, "RESTinio hello world server" )
.append_header_date_field()
.append_header( restinio::http_field::content_type, "text/plain; charset=utf-8" )
.set_body( "Hello world!")
.done();
}
} );
}
}
int main()
{
try
{
{
using connection_state_listener_t = blocker_t;
using ip_blocker_t = blocker_t;
};
restinio::asio_ns::io_context ioctx;
auto blocker = std::make_shared<blocker_t>();
ioctx,
restinio::on_thread_pool<my_traits_t>( std::thread::hardware_concurrency() )
.port( 8080 )
.address( "localhost" )
.connection_state_listener( blocker )
.ip_blocker( blocker )
.max_pipelined_requests( 4 )
.handle_request_timeout( std::chrono::seconds{20} )
.request_handler( [&ioctx](auto req) {
} )
);
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << std::endl;
return 1;
}
return 0;
}
An object with info about connection to be passed to state listener.
RESTINIO_NODISCARD cause_t cause() const noexcept
Get the cause for the notification.
RESTINIO_NODISCARD connection_id_t connection_id() const noexcept
Get the connection id.
RESTINIO_NODISCARD endpoint_t remote_endpoint() const noexcept
Get the remote endpoint for the connection.
An information about new incoming connection to be passed to IP-blocker object.
endpoint_t remote_endpoint() const noexcept
Remote endpoint of the new connection.
constexpr inspection_result_t deny() noexcept
Shorthand for inspection_result_t::deny.
inspection_result_t
Enumeration of result of inspecting new incoming connection.
constexpr inspection_result_t allow() noexcept
Shorthand for inspection_result_t::allow.
RESTINIO_NODISCARD constexpr request_handling_status_t request_rejected() noexcept
ostream_logger_t< std::mutex > shared_ostream_logger_t
void run(asio_ns::io_context &ioctx, run_on_this_thread_settings_t< Traits > &&settings)
Helper function for running http server until ctrl+c is hit.
request_handling_status_t
Request handling status.
RESTINIO_NODISCARD constexpr request_handling_status_t request_accepted() noexcept
std::shared_ptr< request_t > request_handle_t
An alias for handle for incoming request without additional extra-data.