RESTinio
Loading...
Searching...
No Matches
try_parse_field.hpp
Go to the documentation of this file.
1/*
2 * RESTinio
3 */
4
12#pragma once
13
15
18
19#include <iostream>
20#include <variant>
21
22namespace restinio
23{
24
25namespace http_field_parsers
26{
27
28//
29// field_not_found_t
30//
38
39namespace try_extract_field_details
40{
41
42//
43// result_variant_t
44//
51template< typename Parsed_Field_Type >
52using result_variant_t = std::variant<
56
57//
58// valid_field_type
59//
60template< typename, typename = restinio::utils::metaprogramming::void_t<> >
61struct valid_field_type : public std::false_type {};
62
63template< typename T >
65 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//
81template< typename Parsed_Field_Type >
82[[nodiscard]]
85 std::optional< string_view_t > opt_value,
87{
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
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//
148template< typename Parsed_Field_Type, typename Extra_Data >
149[[nodiscard]]
150auto
160{
161 using namespace try_extract_field_details;
162
164 req.header().opt_value_of( field_name ),
166}
167
206template< typename Parsed_Field_Type, typename Extra_Data >
207[[nodiscard]]
208auto
213 http_field_t field_id,
218{
219 using namespace try_extract_field_details;
220
222 req.header().opt_value_of( field_id ),
224}
225
226} /* namespace http_field_parsers */
227
228} /* namespace restinio */
229
Information about parsing error.
An very small, simple and somewhat limited implementation of recursive-descent parser.
result_variant_t< Parsed_Field_Type > try_extract_field_value_from(std::optional< string_view_t > opt_value, string_view_t default_value)
std::variant< Parsed_Field_Type, field_not_found_t, restinio::easy_parser::parse_error_t > result_variant_t
Type of a variant to be returned as the result of attempt to parse HTTP-field.
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.
std::enable_if< std::is_same< Parameter_Container, query_string_params_t >::value||std::is_same< Parameter_Container, router::route_params_t >::value, std::optional< Value_Type > >::type opt_value(const Parameter_Container &params, string_view_t key)
Gets the value of a parameter specified by key wrapped in std::optional<Value_Type> if parameter exis...
Definition value_or.hpp:64
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.
std::string_view string_view_t
http_field_t
C++ enum that repeats nodejs c-style enum.
A special type to be returned in the case if HTTP-field isn't found in a request.