RESTinio
Toggle main menu visibility
Loading...
Searching...
No Matches
restinio
helpers
http_field_parsers
media-type.hpp
Go to the documentation of this file.
1
/*
2
* RESTinio
3
*/
4
5
/*!
6
* @file
7
* @brief Stuff related to Media-Type value in HTTP-fields.
8
*
9
* @since v.0.6.1
10
*/
11
12
#
pragma
once
13
14
#
include
<
restinio
/
helpers
/
http_field_parsers
/
basics
.
hpp
>
15
16
namespace
restinio
17
{
18
19
namespace
http_field_parsers
20
{
21
22
//
23
// media_type_value_t
24
//
25
/*!
26
* @brief Tools for working with media-type in HTTP-fields.
27
*
28
* This struct represents parsed value of media-type.
29
* Media-type is present in different HTTP-fields and has the following
30
* format (see https://tools.ietf.org/html/rfc7231 section
31
* `3.1.1.1. Media Type`):
32
@verbatim
33
media-type = type "/" subtype *( OWS ";" OWS parameter )
34
type = token
35
subtype = token
36
parameter = token "=" ( token / quoted-string )
37
@endverbatim
38
*
39
* @since v.0.6.1
40
*/
41
struct
media_type_value_t
42
{
43
using
parameter_t
=
parameter_with_mandatory_value_t
;
44
45
using
parameter_container_t
=
parameter_with_mandatory_value_container_t
;
46
47
std::string
type
;
48
std::string
subtype
;
49
parameter_container_t
parameters
;
50
51
/*!
52
* @brief Make a default parser that doesn't handles weight parameter
53
* a special way.
54
*
55
* This parser handles the following rules:
56
@verbatim
57
media-type = type "/" subtype *( OWS ";" OWS parameter )
58
type = token
59
subtype = token
60
parameter = token "=" ( token / quoted-string )
61
@endverbatim
62
* @since v.0.6.1
63
*/
64
[[nodiscard]]
65
static
auto
66
make_default_parser
()
67
{
68
return
produce<
media_type_value_t
>(
69
token_p() >> to_lower() >> &media_type_value_t::type,
70
symbol(
'/'
),
71
token_p() >> to_lower() >> &media_type_value_t::subtype,
72
params_with_value_p
(
)
>> &media_type_value_t::parameters
73
);
74
}
75
76
/*!
77
* @brief Make a special parser that stops when weight parameter is found.
78
*
79
* This parser handles the following rules:
80
@verbatim
81
media-type = type "/" subtype *( ![weight] OWS ";" OWS parameter )
82
type = token
83
subtype = token
84
parameter = token "=" ( token / quoted-string )
85
weight = OWS ";" OWS "q=" qvalue
86
qvalue = ( "0" [ "." 0*3DIGIT ] )
87
/ ( "1" [ "." 0*3("0") ] )
88
@endverbatim
89
* @since v.0.6.1
90
*/
91
[[nodiscard]]
92
static
auto
93
make_weight_aware_parser
()
94
{
95
return
produce<
media_type_value_t
>(
96
token_p() >> to_lower() >> &media_type_value_t::type,
97
symbol(
'/'
),
98
token_p() >> to_lower() >> &media_type_value_t::subtype,
99
produce< parameter_container_t >(
100
repeat( 0, N,
101
produce< parameter_t >(
102
not_clause( weight_p() >> skip() ),
103
ows(),
104
symbol(
';'
),
105
ows(),
106
token_p() >> to_lower() >> ¶meter_t::first,
107
symbol(
'='
),
108
alternatives(
109
token_p() >> ¶meter_t::second,
110
quoted_string_p() >> ¶meter_t::second
111
)
112
) >> to_container()
113
)
114
) >> &media_type_value_t::parameters
115
);
116
}
117
118
/*!
119
* @brief An attempt to parse media-type value.
120
*
121
* @note
122
* This method uses make_default_parser() for actual parser.
123
*
124
* @since v.0.6.1
125
*/
126
[[nodiscard]]
127
static
expected_t
<
media_type_value_t
,
restinio
::
easy_parser
::
parse_error_t
>
128
try_parse
( string_view_t what )
129
{
130
return
restinio
::
easy_parser
::try_parse( what, make_default_parser() );
131
}
132
};
133
134
}
/* namespace http_field_parsers */
135
136
}
/* namespace restinio */
restinio::easy_parser
Definition
easy_parser.hpp:42
restinio::http_field_parsers
Definition
accept-charset.hpp:20
restinio::http_field_parsers::params_with_value_p
impl::params_with_value_producer_t params_with_value_p()
A factory of producer of parameter_with_mandatory_value_container.
Definition
basics.hpp:1687
restinio::http_field_parsers::parameter_with_mandatory_value_t
std::pair< std::string, std::string > parameter_with_mandatory_value_t
A type that describes a parameter with mandatory value.
Definition
basics.hpp:1523
restinio
Definition
sendfile_operation_default.ipp:12
restinio::http_field_parsers::media_type_value_t
Tools for working with media-type in HTTP-fields.
Definition
media-type.hpp:42
restinio::http_field_parsers::media_type_value_t::make_default_parser
static auto make_default_parser()
Definition
media-type.hpp:66
restinio::http_field_parsers::media_type_value_t::type
std::string type
Definition
media-type.hpp:47
restinio::http_field_parsers::media_type_value_t::make_weight_aware_parser
static auto make_weight_aware_parser()
Definition
media-type.hpp:93
restinio::http_field_parsers::media_type_value_t::parameters
parameter_container_t parameters
Definition
media-type.hpp:49
restinio::http_field_parsers::media_type_value_t::try_parse
static expected_t< media_type_value_t, restinio::easy_parser::parse_error_t > try_parse(string_view_t what)
An attempt to parse media-type value.
Definition
media-type.hpp:128
restinio::http_field_parsers::media_type_value_t::parameter_t
parameter_with_mandatory_value_t parameter_t
Definition
media-type.hpp:43
restinio::http_field_parsers::media_type_value_t::subtype
std::string subtype
Definition
media-type.hpp:48
Generated by
1.17.0