Loading...
Searching...
No Matches
try_parse_field.hpp
Go to the documentation of this file.
/*
* RESTinio
*/
#pragma once
#include <restinio/helpers/easy_parser.hpp>
#include <restinio/http_headers.hpp>
#include <restinio/request_handler.hpp>
#include <restinio/variant.hpp>
#include <iostream>
namespace restinio
{
namespace http_field_parsers
{
//
// field_not_found_t
//
struct field_not_found_t {};
namespace try_extract_field_details
{
//
// result_variant_t
//
template< typename Parsed_Field_Type >
using result_variant_t = variant_t<
Parsed_Field_Type,
field_not_found_t,
restinio::easy_parser::parse_error_t >;
//
// valid_field_type
//
template< typename, typename = restinio::utils::metaprogramming::void_t<> >
struct valid_field_type : public std::false_type {};
template< typename T >
struct valid_field_type<
T,
restinio::utils::metaprogramming::void_t<
std::enable_if_t<
std::is_same<
expected_t< T, restinio::easy_parser::parse_error_t >,
decltype(T::try_parse(std::declval<string_view_t>()))
>::value,
bool
>
>
> : public std::true_type
{};
//
// try_extract_field_value_from
//
template< typename Parsed_Field_Type >
RESTINIO_NODISCARD
result_variant_t< Parsed_Field_Type >
try_extract_field_value_from(
optional_t< string_view_t > opt_value,
string_view_t default_value )
{
static_assert( valid_field_type<Parsed_Field_Type>::value,
"Parsed_Field_Type should have static try_parse method that "
"accepts string_view_t and returns "
"expected_t<Parsed_Field_Type, parse_error_t>" );
if( !opt_value && default_value.empty() )
return { field_not_found_t{} };
string_view_t content = opt_value ? *opt_value : default_value;
auto parse_result = Parsed_Field_Type::try_parse( content );
if( parse_result )
return { std::move(*parse_result) };
else
return { parse_result.error() };
}
} /* namespace try_extract_field_details */
//
// try_parse_field
//
template< typename Parsed_Field_Type, typename Extra_Data >
RESTINIO_NODISCARD
auto
try_parse_field(
const generic_request_t< Extra_Data > & req,
string_view_t field_name,
string_view_t default_value = string_view_t{} )
{
using namespace try_extract_field_details;
return try_extract_field_value_from< Parsed_Field_Type >(
req.header().opt_value_of( field_name ),
default_value );
}
template< typename Parsed_Field_Type, typename Extra_Data >
RESTINIO_NODISCARD
auto
try_parse_field(
const generic_request_t< Extra_Data > & req,
http_field_t field_id,
string_view_t default_value = string_view_t{} )
{
using namespace try_extract_field_details;
return try_extract_field_value_from< Parsed_Field_Type >(
req.header().opt_value_of( field_id ),
default_value );
}
} /* namespace http_field_parsers */
} /* namespace restinio */
nonstd::optional_lite::optional
class optional
Definition: optional.hpp:932
nonstd::variants::variant
Definition: variant.hpp:1304
restinio::easy_parser::parse_error_t
Information about parsing error.
Definition: easy_parser.hpp:93
restinio::generic_request_t
HTTP Request data.
Definition: request_handler.hpp:209
restinio::generic_request_t::header
const http_request_header_t & header() const noexcept
Get request header.
Definition: request_handler.hpp:264
restinio::http_header_fields_t::opt_value_of
optional_t< string_view_t > opt_value_of(string_view_t name) const noexcept
Get optional value of a field.
Definition: http_headers.hpp:1301
RESTINIO_NODISCARD
#define RESTINIO_NODISCARD
Definition: compiler_features.hpp:36
easy_parser.hpp
An very small, simple and somewhat limited implementation of recursive-descent parser.
http_headers.hpp
restinio::http_field_parsers::try_extract_field_details::try_extract_field_value_from
RESTINIO_NODISCARD result_variant_t< Parsed_Field_Type > try_extract_field_value_from(optional_t< string_view_t > opt_value, string_view_t default_value)
Definition: try_parse_field.hpp:84
restinio::http_field_parsers::try_parse_field
RESTINIO_NODISCARD auto try_parse_field(const generic_request_t< Extra_Data > &req, string_view_t field_name, string_view_t default_value=string_view_t{})
A helper function for extraction and parsing a value of HTTP-field.
Definition: try_parse_field.hpp:151
restinio::utils::metaprogramming::void_t
typename make_void< Ts... >::type void_t
Definition: metaprogramming.hpp:28
restinio
Definition: asio_include.hpp:21
restinio::string_view_t
nonstd::string_view string_view_t
Definition: string_view.hpp:19
restinio::opt_value
std::enable_if< std::is_same< Parameter_Container, query_string_params_t >::value||std::is_same< Parameter_Container, router::route_params_t >::value, optional_t< Value_Type > >::type opt_value(const Parameter_Container &params, string_view_t key)
Gets the value of a parameter specified by key wrapped in optional_t<Value_Type> if parameter exists ...
Definition: value_or.hpp:64
restinio::http_field_t
http_field_t
C++ enum that repeats nodejs c-style enum.
Definition: http_headers.hpp:233
request_handler.hpp
restinio::http_field_parsers::field_not_found_t
A special type to be returned in the case if HTTP-field isn't found in a request.
Definition: try_parse_field.hpp:37
restinio::http_field_parsers::try_extract_field_details::valid_field_type
Definition: try_parse_field.hpp:61
variant.hpp