RESTinio

user-agent.hpp
Go to the documentation of this file.
1/*
2 * RESTinio
3 */
4
12#pragma once
13
14#include <restinio/helpers/http_field_parsers/basics.hpp>
15
16#include <restinio/variant.hpp>
17
18 namespace restinio
19{
20
21 namespace http_field_parsers
22{
23
24//
25// user_agent_value_t
26//
41 struct user_agent_value_t
42{
48 struct product_t
49 {
50 std::string product;
51 restinio::optional_t<std::string> product_version;
52 };
53
59 using tail_item_t = restinio::variant_t< product_t, std::string >;
60
61 product_t product;
62 std::vector< tail_item_t > tail;
63
69 RESTINIO_NODISCARD
70 static auto
71 make_parser()
72 {
73 auto product_producer = produce< product_t >(
74 token_p() >> &product_t::product,
75 maybe(
76 symbol('/'),
77 token_p() >> &product_t::product_version
78 )
79 );
80
81 return produce< user_agent_value_t >(
82 product_producer >> &user_agent_value_t::product,
83 produce< std::vector< tail_item_t > >(
84 repeat( 0, N,
85 space(),
86 ows(),
87 alternatives(
88 product_producer >> to_container(),
89 comment_p() >> to_container()
90 )
91 )
92 ) >> &user_agent_value_t::tail
93 );
94 }
95
101 RESTINIO_NODISCARD
102 static expected_t< user_agent_value_t, restinio::easy_parser::parse_error_t >
103 try_parse( string_view_t what )
104 {
105 return restinio::easy_parser::try_parse( what, make_parser() );
106 }
107};
108
109} /* namespace http_field_parsers */
110
111} /* namespace restinio */
112
basics.hpp
Utilities for parsing values of http-fields.
nonstd::optional_lite::optional
class optional
Definition: optional.hpp:919
nonstd::variants::variant
Definition: variant.hpp:1301
RESTINIO_NODISCARD
#define RESTINIO_NODISCARD
Definition: compiler_features.hpp:33
restinio::easy_parser::maybe
RESTINIO_NODISCARD auto maybe(Clauses &&... clauses)
A factory function to create an optional clause.
Definition: easy_parser.hpp:3634
restinio::easy_parser::symbol
RESTINIO_NODISCARD auto symbol(char expected) noexcept
A factory function to create a clause that expects the speficied symbol, extracts it and then skips i...
Definition: easy_parser.hpp:4032
restinio::easy_parser::produce
RESTINIO_NODISCARD auto produce(Clauses &&... clauses)
A factory function to create a producer that creates an instance of the target type by using specifie...
Definition: easy_parser.hpp:3554
restinio::easy_parser::space
RESTINIO_NODISCARD auto space() noexcept
A factory function to create a clause that expects a space, extracts it and then skips it.
Definition: easy_parser.hpp:4115
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::easy_parser::repeat
RESTINIO_NODISCARD auto repeat(std::size_t min_occurences, std::size_t max_occurences, Clauses &&... clauses)
A factory function to create repetitor of subclauses.
Definition: easy_parser.hpp:3876
restinio::easy_parser::N
constexpr std::size_t N
A special marker that means infinite repetitions.
Definition: easy_parser.hpp:455
restinio::easy_parser::to_container
RESTINIO_NODISCARD auto to_container()
A factory function to create a to_container_consumer.
Definition: easy_parser.hpp:4583
restinio::easy_parser::alternatives
RESTINIO_NODISCARD auto alternatives(Clauses &&... clauses)
A factory function to create an alternatives clause.
Definition: easy_parser.hpp:3595
restinio::http_field_parsers::comment_p
RESTINIO_NODISCARD auto comment_p()
A factory for producer of comment token.
Definition: basics.hpp:880
restinio::http_field_parsers::ows
RESTINIO_NODISCARD auto ows() noexcept
A factory function to create an OWS clause.
Definition: basics.hpp:939
restinio::http_field_parsers::token_p
RESTINIO_NODISCARD auto token_p() noexcept
A factory function to create a token_producer.
Definition: basics.hpp:985
restinio
Definition: asio_include.hpp:21
restinio::string_view_t
nonstd::string_view string_view_t
Definition: string_view.hpp:19
restinio::expected_t
nonstd::expected< T, E > expected_t
Definition: expected.hpp:22
restinio::http_field_parsers::user_agent_value_t::product_t
A type for holding an info about a product.
Definition: user-agent.hpp:49
restinio::http_field_parsers::user_agent_value_t::product_t::product
std::string product
Definition: user-agent.hpp:50
restinio::http_field_parsers::user_agent_value_t::product_t::product_version
restinio::optional_t< std::string > product_version
Definition: user-agent.hpp:51
restinio::http_field_parsers::user_agent_value_t
Tools for working with the value of User-Agent HTTP-field.
Definition: user-agent.hpp:42
restinio::http_field_parsers::user_agent_value_t::try_parse
static RESTINIO_NODISCARD expected_t< user_agent_value_t, restinio::easy_parser::parse_error_t > try_parse(string_view_t what)
An attempt to parse User-Agent HTTP-field.
Definition: user-agent.hpp:103
restinio::http_field_parsers::user_agent_value_t::tail
std::vector< tail_item_t > tail
Definition: user-agent.hpp:62
restinio::http_field_parsers::user_agent_value_t::make_parser
static RESTINIO_NODISCARD auto make_parser()
A factory function for a parser of User-Agent value.
Definition: user-agent.hpp:71
restinio::http_field_parsers::user_agent_value_t::product
product_t product
Definition: user-agent.hpp:61
variant.hpp