RESTinio
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  */
42 {
44 
46 
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  */
65  static auto
67  {
68  return produce< media_type_value_t >(
70  symbol('/'),
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  */
92  static auto
94  {
95  return produce< media_type_value_t >(
97  symbol('/'),
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,
111  )
112  ) >> to_container()
113  )
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  */
129  {
131  }
132 };
133 
134 } /* namespace http_field_parsers */
135 
136 } /* namespace restinio */
static RESTINIO_NODISCARD 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_NODISCARD auto try_parse_field(const generic_request_t< Extra_Data > &req, http_field_t field_id, 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, optional_t< Value_Type > >::type opt_value(const Parameter_Container &params, string_view_t key)
Gets the value of a parameter specified by key wrapped in optional_t<Value_Type> if parameter exists ...
Definition: value_or.hpp:64