Loading...
Searching...
No Matches
sample/try_parse_query_string/main.cpp
#include <iostream>
#include <string>
#include <iterator>
#include <restinio/all.hpp>
using namespace std::string_literals;
// Type of query-string parser.
using query_string_parser_type =
restinio::expected_t<
restinio::query_string_params_t,
restinio::parse_query_failure_t >
(*)( restinio::string_view_t );
// Create an appropriate query-string parser.
query_string_parser_type create_parser( int argc, char ** argv )
{
if( argc < 2 || "restinio_defaults"s == argv[ 1 ] )
return []( restinio::string_view_t what ) {
return restinio::try_parse_query<
restinio::parse_query_traits::restinio_defaults >( what );
};
if( 2 == argc )
{
if( "javascript_compatible"s == argv[ 1 ] )
return []( restinio::string_view_t what ) {
return restinio::try_parse_query<
restinio::parse_query_traits::javascript_compatible >( what );
};
if( "x_www_form_urlencoded"s == argv[ 1 ] )
return []( restinio::string_view_t what ) {
return restinio::try_parse_query<
restinio::parse_query_traits::x_www_form_urlencoded >( what );
};
if( "relaxed"s == argv[ 1 ] ) {
return []( restinio::string_view_t what ) {
return restinio::try_parse_query<
restinio::parse_query_traits::relaxed >( what );
};
}
}
throw std::runtime_error{
"one optional argument expected: "
"restinio_defaults, javascript_compatible, x_www_form_urlencoded or "
"relaxed"
};
}
// Create request handler.
restinio::request_handling_status_t handler(
const restinio::request_handle_t& req,
query_string_parser_type parser )
{
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,
RESTINIO_FMT_FORMAT_STRING( "GET request to '{}'\n" ),
req->header().request_target() );
// Request header fields.
fmt::format_to( response_body_inserter,
RESTINIO_FMT_FORMAT_STRING( "HTTP-fields ({}):\n" ),
req->header().fields_count() );
for( const auto & f : req->header() )
{
fmt::format_to( response_body_inserter,
RESTINIO_FMT_FORMAT_STRING( "{}: {}\n" ),
f.name(), f.value() );
}
// An attempt to parse query-string.
const auto qs_parse_result = parser( req->header().query() );
if( !qs_parse_result )
{
// Return a negative response.
req->create_response( restinio::status_bad_request() )
.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( "Unable to parse query-string: " +
qs_parse_result.error().description() )
.done();
return restinio::request_accepted();
}
// Handle query-string params.
const auto & qp = *qs_parse_result;
if( qp.empty() )
{
fmt::format_to( response_body_inserter,
RESTINIO_FMT_FORMAT_STRING( "No query parameters." ) );
}
else
{
fmt::format_to( response_body_inserter,
RESTINIO_FMT_FORMAT_STRING( "Query params ({}):\n" ),
qp.size() );
for( const auto & p : qp )
{
fmt::format_to( response_body_inserter,
RESTINIO_FMT_FORMAT_STRING( "'{}' => '{}'\n" ),
restinio::fmtlib_tools::streamed( p.first ),
restinio::fmtlib_tools::streamed( 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( int argc, char ** argv )
{
try
{
restinio::run(
restinio::on_thread_pool( std::thread::hardware_concurrency() )
.port( 8080 )
.address( "localhost" )
.request_handler(
[parser = create_parser( argc, argv )]( const auto & req ) {
return handler( req, parser );
} )
);
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << std::endl;
return 1;
}
return 0;
}
all.hpp
restinio::parse_query_failure_t
Type that indicates a failure of an attempt of query-string parsing.
Definition: uri_helpers.hpp:339
restinio::query_string_params_t
Parameters container for query strings parameters.
Definition: uri_helpers.hpp:43
RESTINIO_FMT_FORMAT_STRING
#define RESTINIO_FMT_FORMAT_STRING(s)
Definition: include_fmtlib.hpp:92
restinio::fmtlib_tools::streamed
decltype(auto) streamed(T &&v) noexcept
Definition: include_fmtlib.hpp:63
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::string_view_t
nonstd::string_view string_view_t
Definition: string_view.hpp:19
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::try_parse_query
RESTINIO_NODISCARD expected_t< query_string_params_t, parse_query_failure_t > try_parse_query(string_view_t original_query_string)
Helper function for parsing query string.
Definition: uri_helpers.hpp:399
restinio::request_accepted
RESTINIO_NODISCARD constexpr request_handling_status_t request_accepted() noexcept
Definition: common_types.hpp:46
restinio::expected_t
nonstd::expected< T, E > expected_t
Definition: expected.hpp:22
restinio::status_bad_request
http_status_line_t status_bad_request()
Definition: http_headers.hpp:2218
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:471
restinio::parse_query_traits::javascript_compatible
Traits for parsing a query string in JavaScript-compatible mode.
Definition: uri_helpers.hpp:266
restinio::parse_query_traits::relaxed
Traits for parsing a query string in a very relaxed mode.
Definition: uri_helpers.hpp:329
restinio::parse_query_traits::restinio_defaults
Traits for the default RESTinio parser for query string.
Definition: uri_helpers.hpp:248
restinio::parse_query_traits::x_www_form_urlencoded
Traits for parsing a query string in application/x-www-form-urlencoded mode.
Definition: uri_helpers.hpp:293