Loading...
Searching...
No Matches
sample/query_string_params/main.cpp
#include <iostream>
#include <restinio/all.hpp>
#include <iterator>
// Create request handler.
restinio::request_handling_status_t handler( const restinio::request_handle_t& req )
{
if( restinio::http_method_get() == req->header().method() )
{
fmt::basic_memory_buffer< char, 1u > response_body;
auto response_body_inserter = std::back_inserter( response_body );
fmt::format_to( response_body_inserter, "GET request to '{}'\n",
req->header().request_target() );
// Request header fields.
fmt::format_to( response_body_inserter, "HTTP-fields ({}):\n",
req->header().fields_count() );
for( const auto & f : req->header() )
{
fmt::format_to( response_body_inserter, "{}: {}\n",
f.name(), f.value() );
}
// Query params.
const auto qp = restinio::parse_query( req->header().query() );
if( qp.empty() )
{
fmt::format_to( response_body_inserter, "No query parameters." );
}
else
{
fmt::format_to( response_body_inserter,
"Query params ({}):\n", qp.size() );
for( const auto p : qp )
{
fmt::format_to( response_body_inserter, "'{}' => '{}'\n",
p.first, p.second );
}
}
if( qp.has( "debug" ) && qp[ "debug" ] == "true" )
{
std::cout.write( response_body.data(), response_body.size() );
std::cout << std::flush;
}
req->create_response()
.append_header( restinio::http_field::server, "RESTinio query string params server" )
.append_header_date_field()
.append_header( restinio::http_field::content_type, "text/plain; charset=utf-8" )
.set_body( std::move(response_body) )
.done();
return restinio::request_accepted();
}
return restinio::request_rejected();
}
int main()
{
try
{
restinio::run(
restinio::on_thread_pool( std::thread::hardware_concurrency() )
.port( 8080 )
.address( "localhost" )
.request_handler( handler ) );
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << std::endl;
return 1;
}
return 0;
}
all.hpp
restinio::on_thread_pool
run_on_thread_pool_settings_t< Traits > on_thread_pool(std::size_t pool_size)
A special marker for the case when http_server must be run on an thread pool.
Definition http_server_run.hpp:183
restinio::request_rejected
RESTINIO_NODISCARD constexpr request_handling_status_t request_rejected() noexcept
Definition common_types.hpp:53
restinio::on_this_thread
run_on_this_thread_settings_t< Traits > on_this_thread()
A special marker for the case when http_server must be run on the context of the current thread.
Definition http_server_run.hpp:123
restinio::parse_query
RESTINIO_NODISCARD query_string_params_t parse_query(string_view_t original_query_string)
Parse query key-value parts.
Definition uri_helpers.hpp:518
restinio::run
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.
Definition http_server_run.hpp:216
restinio::request_handling_status_t
request_handling_status_t
Request handling status.
Definition common_types.hpp:26
restinio::request_accepted
RESTINIO_NODISCARD constexpr request_handling_status_t request_accepted() noexcept
Definition common_types.hpp:46
restinio::request_handle_t
std::shared_ptr< request_t > request_handle_t
An alias for handle for incoming request without additional extra-data.
Definition request_handler.hpp:463