#include <iostream>
#include <string>
using namespace std::string_literals;
using query_string_parser_type =
query_string_parser_type create_parser( int argc, char ** argv )
{
if( argc < 2 || "restinio_defaults"s == argv[ 1 ] )
};
if( 2 == argc )
{
if( "javascript_compatible"s == argv[ 1 ] )
};
if( "x_www_form_urlencoded"s == argv[ 1 ] )
};
if( "relaxed"s == argv[ 1 ] ) {
};
}
}
throw std::runtime_error{
"one optional argument expected: "
"restinio_defaults, javascript_compatible, x_www_form_urlencoded or "
"relaxed"
};
}
query_string_parser_type parser )
{
if( restinio::http_method_get() == req->header().method() )
{
fmt::basic_memory_buffer< char, 1u > response_body;
fmt::format_to( response_body, "GET request to '{}'\n",
req->header().request_target() );
fmt::format_to( response_body, "HTTP-fields ({}):\n",
req->header().fields_count() );
for( const auto & f : req->header() )
{
fmt::format_to( response_body, "{}: {}\n",
f.name(), f.value() );
}
const auto qs_parse_result = parser( req->header().query() );
if( !qs_parse_result )
{
.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();
}
const auto & qp = *qs_parse_result;
if( qp.empty() )
{
fmt::format_to( response_body, "No query parameters." );
}
else
{
fmt::format_to( response_body, "Query params ({}):\n", qp.size() );
for( const auto p : qp )
{
fmt::format_to( response_body, "'{}' => '{}'\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" )
.done();
}
}
int main( int argc, char ** argv )
{
try
{
.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;
}