RESTinio
Loading...
Searching...
No Matches
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
16namespace restinio
17{
18
19namespace 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 */
42{
44
46
47 std::string type;
48 std::string subtype;
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
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
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() >> &parameter_t::first,
107 symbol('='),
108 alternatives(
109 token_p() >> &parameter_t::second,
110 quoted_string_p() >> &parameter_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]]
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 */
std::pair< std::string, std::string > parameter_with_mandatory_value_t
A type that describes a parameter with mandatory value.
Definition basics.hpp:1523
Tools for working with media-type in HTTP-fields.
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.
parameter_with_mandatory_value_t parameter_t