RESTinio

try_parse_field.hpp
Go to the documentation of this file.
1/*
2 * RESTinio
3 */
4
12#pragma once
13
14#include <restinio/helpers/easy_parser.hpp>
15
16#include <restinio/http_headers.hpp>
17#include <restinio/request_handler.hpp>
18#include <restinio/variant.hpp>
19
20#include <iostream>
21
22 namespace restinio
23{
24
25 namespace http_field_parsers
26{
27
28//
29// field_not_found_t
30//
37 struct field_not_found_t {};
38
39 namespace try_extract_field_details
40{
41
42//
43// result_variant_t
44//
51 template< typename Parsed_Field_Type >
52 using result_variant_t = variant_t<
53 Parsed_Field_Type,
54 field_not_found_t,
55 restinio::easy_parser::parse_error_t >;
56
57//
58// valid_field_type
59//
60 template< typename, typename = restinio::utils::metaprogramming::void_t<> >
61 struct valid_field_type : public std::false_type {};
62
63 template< typename T >
64 struct valid_field_type<
65 T,
66 restinio::utils::metaprogramming::void_t<
67 std::enable_if_t<
68 std::is_same<
69 expected_t< T, restinio::easy_parser::parse_error_t >,
70 decltype(T::try_parse(std::declval<string_view_t>()))
71 >::value,
72 bool
73 >
74 >
75 > : public std::true_type
76{};
77
78//
79// try_extract_field_value_from
80//
81 template< typename Parsed_Field_Type >
82 RESTINIO_NODISCARD
83 result_variant_t< Parsed_Field_Type >
84 try_extract_field_value_from(
85 optional_t< string_view_t > opt_value,
86 string_view_t default_value )
87{
88 static_assert( valid_field_type<Parsed_Field_Type>::value,
89 "Parsed_Field_Type should have static try_parse method that "
90 "accepts string_view_t and returns "
91 "expected_t<Parsed_Field_Type, parse_error_t>" );
92
93 if( !opt_value && default_value.empty() )
94 return { field_not_found_t{} };
95
96 string_view_t content = opt_value ? *opt_value : default_value;
97
98 auto parse_result = Parsed_Field_Type::try_parse( content );
99 if( parse_result )
100 return { std::move(*parse_result) };
101 else
102 return { parse_result.error() };
103}
104
105} /* namespace try_extract_field_details */
106
107//
108// try_parse_field
109//
148 template< typename Parsed_Field_Type, typename Extra_Data >
149 RESTINIO_NODISCARD
150 auto
151 try_parse_field(
153 const generic_request_t< Extra_Data > & req,
155 string_view_t field_name,
159 string_view_t default_value = string_view_t{} )
160{
161 using namespace try_extract_field_details;
162
163 return try_extract_field_value_from< Parsed_Field_Type >(
164 req.header().opt_value_of( field_name ),
165 default_value );
166}
167
206 template< typename Parsed_Field_Type, typename Extra_Data >
207 RESTINIO_NODISCARD
208 auto
209 try_parse_field(
211 const generic_request_t< Extra_Data > & req,
213 http_field_t field_id,
217 string_view_t default_value = string_view_t{} )
218{
219 using namespace try_extract_field_details;
220
221 return try_extract_field_value_from< Parsed_Field_Type >(
222 req.header().opt_value_of( field_id ),
223 default_value );
224}
225
226} /* namespace http_field_parsers */
227
228} /* namespace restinio */
229
nonstd::optional_lite::optional
class optional
Definition: optional.hpp:919
nonstd::variants::variant
Definition: variant.hpp:1301
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:201
restinio::generic_request_t::header
const http_request_header_t & header() const noexcept
Get request header.
Definition: request_handler.hpp:256
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:1300
RESTINIO_NODISCARD
#define RESTINIO_NODISCARD
Definition: compiler_features.hpp:33
easy_parser.hpp
An very small, simple and somewhat limited implementation of recursive-descent parser.
http_headers.hpp
nonstd::optional_lite::std11::false_type
bool_constant< false > false_type
Definition: optional.hpp:460
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:465
nonstd::optional_lite::std11::true_type
bool_constant< true > true_type
Definition: optional.hpp:459
restinio::easy_parser::try_parse
RESTINIO_NODISCARD expected_t< typename Producer::result_type, parse_error_t > try_parse(string_view_t from, Producer producer)
Perform the parsing of the specified content by using specified value producer.
Definition: easy_parser.hpp:5042
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