RESTinio
Loading...
Searching...
No Matches
user-agent.hpp
Go to the documentation of this file.
1/*
2 * RESTinio
3 */
4
5/*!
6 * @file
7 * @brief Stuff related to value of User-Agent HTTP-field.
8 *
9 * @since v.0.6.4
10 */
11
12#pragma once
13
14#include <restinio/helpers/http_field_parsers/basics.hpp>
15
16#include <variant>
17
18namespace restinio
19{
20
21namespace http_field_parsers
22{
23
24//
25// user_agent_value_t
26//
27/*!
28 * @brief Tools for working with the value of User-Agent HTTP-field.
29 *
30 * This struct represents parsed value of HTTP-field User-Agent
31 * (see https://tools.ietf.org/html/rfc7231#section-5.5.3):
32@verbatim
33 User-Agent = product *( RWS ( product / comment ) )
34
35 product = token ["/" product-version]
36 product-version = token
37@endverbatim
38 *
39 * @since v.0.6.4
40 */
42{
43 /*!
44 * @brief A type for holding an info about a product.
45 *
46 * @since v.0.6.4
47 */
48 struct product_t
49 {
50 std::string product;
51 std::optional<std::string> product_version;
52 };
53
54 /*!
55 * @brief A type for holding an info about a product or a comment.
56 *
57 * @since v.0.6.4
58 */
59 using tail_item_t = std::variant< product_t, std::string >;
60
63
64 /*!
65 * @brief A factory function for a parser of User-Agent value.
66 *
67 * @since v.0.6.4
68 */
69 [[nodiscard]]
70 static auto
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
96 /*!
97 * @brief An attempt to parse User-Agent HTTP-field.
98 *
99 * @since v.0.6.4
100 */
101 [[nodiscard]]
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 */
A type for holding an info about a product.
Tools for working with the value of User-Agent HTTP-field.
static 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.
std::variant< product_t, std::string > tail_item_t
A type for holding an info about a product or a comment.
static auto make_parser()
A factory function for a parser of User-Agent value.