RESTinio
sample/chained_handlers/main.cpp
#include <iostream>
#include <restinio/all.hpp>
template < typename RESP >
RESP
init_resp( RESP resp )
{
resp.append_header( restinio::http_field::server, "RESTinio sample server /v.0.6" );
resp.append_header_date_field();
return resp;
}
auto create_auth_handler()
{
auto auth_checker = []( const auto & req, const auto & ) {
// Try to parse Authorization header.
const auto result = try_extract_params( *req,
restinio::http_field::authorization );
if( result )
{
// Authorization header is present and contains some value.
if( result->username == "user" &&
result->password == "1234" )
{
// User authentificated. The further processing can be
// delegated to the next handler in the chain.
}
}
// Otherwise we ask for credentials.
init_resp( req->create_response( restinio::status_unauthorized() ) )
.append_header( restinio::http_field::content_type, "text/plain; charset=utf-8" )
.append_header( restinio::http_field::www_authenticate,
R"(Basic realm="Username/Password required", charset="utf-8")" )
.set_body( "Unauthorized access forbidden")
.done();
// Mark the request as processed.
};
auto router = std::make_shared< express_router_t >();
router->http_get( "/stats", auth_checker );
router->http_get( "/admin", auth_checker );
return [handler = std::move(router)]( const auto & req ) {
return (*handler)( req );
};
}
auto create_request_handler()
{
auto router = std::make_shared< express_router_t >();
router->http_get(
"/",
[]( const auto & req, const auto & ) {
init_resp( req->create_response() )
.append_header( restinio::http_field::content_type, "text/plain; charset=utf-8" )
.set_body( "Hello world!")
.done();
} );
router->http_get(
"/json",
[]( const auto & req, const auto & ) {
init_resp( req->create_response() )
.append_header( restinio::http_field::content_type, "application/json" )
.set_body( R"-({"message" : "Hello world!"})-")
.done();
} );
router->http_get(
"/html",
[]( const auto & req, const auto & ) {
init_resp( req->create_response() )
.append_header( restinio::http_field::content_type, "text/html; charset=utf-8" )
.set_body(
"<html>\r\n"
" <head><title>Hello from RESTinio!</title></head>\r\n"
" <body>\r\n"
" <center><h1>Hello world</h1></center>\r\n"
" </body>\r\n"
"</html>\r\n" )
.done();
} );
router->http_get(
"/stats",
[]( const auto & req, const auto & ) {
init_resp( req->create_response() )
.append_header( restinio::http_field::content_type, "text/plain; charset=utf-8" )
.set_body( "Statistics for that site is not available now")
.done();
} );
router->http_get(
"/admin",
[]( const auto & req, const auto & ) {
init_resp( req->create_response() )
.append_header( restinio::http_field::content_type, "text/html; charset=utf-8" )
.set_body(
"<html>\r\n"
" <head><title>Admin panel</title></head>\r\n"
" <body>\r\n"
" <center><h1>NOT IMPLEMENTED YET</h1></center>\r\n"
" </body>\r\n"
"</html>\r\n" )
.done();
} );
return [handler = std::move(router)]( const auto & req ) {
return (*handler)( req );
};
}
int main()
{
using namespace std::chrono;
try
{
using traits_t =
restinio::on_this_thread<traits_t>()
.port( 8080 )
.address( "localhost" )
.request_handler(
create_auth_handler(),
create_request_handler() ) );
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << std::endl;
return 1;
}
return 0;
}
Helpers for dealing with basic authentification.
Timer factory implementation using asio timers.
Generic Express.js style router.
Definition: express.hpp:612
A holder of fixed-size chain of synchronous handlers.
Definition: fixed_size.hpp:164
Stuff related to fixed-size chain of request-headers.
RESTINIO_NODISCARD expected_t< params_t, extraction_error_t > try_extract_params(const authorization_value_t &http_field)
Helper function for getting parameters of basic authentification from an already parsed HTTP-field.
Definition: basic_auth.hpp:183
generic_express_router_t< Regex_Engine, no_extra_data_factory_t > express_router_t
A type of express-like router for the case when the default extra-data-factory is specified in the se...
Definition: express.hpp:843
RESTINIO_NODISCARD constexpr request_handling_status_t request_not_handled() noexcept
http_status_line_t status_unauthorized()
ostream_logger_t< null_mutex_t > single_threaded_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.
RESTINIO_NODISCARD constexpr request_handling_status_t request_accepted() noexcept
Helper functions for parsing values of HTTP-fields.