RESTinio
transfer-encoding.hpp
Go to the documentation of this file.
1 /*
2  * RESTinio
3  */
4 
5 /*!
6  * @file
7  * @brief Stuff related to value of Transfer-Encoding HTTP-field.
8  *
9  * @since v.0.6.9
10  */
11 
12 #pragma once
13 
14 #include <restinio/helpers/http_field_parsers/basics.hpp>
15 
16 #include <variant>
17 #include <tuple>
18 
19 namespace restinio
20 {
21 
22 namespace http_field_parsers
23 {
24 
25 //
26 // transfer_encoding_value_t
27 //
28 /*!
29  * @brief Tools for working with the value of Transfer-Encoding HTTP-field.
30  *
31  * This struct represents parsed value of HTTP-field Transfer-Encoding
32  * (see https://tools.ietf.org/html/rfc7230#section-3.3.1 and
33  * https://tools.ietf.org/html/rfc7230#section-4):
34 @verbatim
35 Transfer-Encoding = 1#transfer-coding
36 transfer-coding = "chunked"
37  / "compress"
38  / "deflate"
39  / ("gzip" | "x-gzip")
40  / transfer-extension
41 transfer-extension = token *( OWS ";" OWS transfer-parameter )
42 transfer-parameter = token BWS "=" BWS ( token / quoted-string )
43 @endverbatim
44  *
45  * @since v.0.6.9
46  */
48 {
49  //! Enumeration for transfer-coding values from RFC7230.
51  {
52  chunked,
53  compress,
54  deflate,
55  gzip,
56  };
57 
58  [[nodiscard]]
59  static constexpr known_transfer_coding_t chunked() noexcept
61 
62  [[nodiscard]]
63  static constexpr known_transfer_coding_t compress() noexcept
65 
66  [[nodiscard]]
67  static constexpr known_transfer_coding_t deflate() noexcept
69 
70  [[nodiscard]]
71  static constexpr known_transfer_coding_t gzip() noexcept
73 
74  //! Description of transfer-extension.
76  {
79 
80  [[nodiscard]]
81  bool
82  operator==( const transfer_extension_t & o ) const noexcept
83  {
84  return std::tie(this->token, this->transfer_parameters) ==
85  std::tie(o.token, o.transfer_parameters);
86  }
87  };
88 
89  //! Type for one value from Transfer-Encoding HTTP-field.
90  using value_t = std::variant<
93  >;
94 
96 
98 
99  /*!
100  * @brief A factory function for a parser of Transfer-Encoding value.
101  *
102  * @since v.0.6.9
103  */
104  [[nodiscard]]
105  static auto
107  {
108  return produce< transfer_encoding_value_t >(
109  non_empty_comma_separated_list_p< value_container_t >(
110  produce< value_t >(
111  alternatives(
112  expected_caseless_token_p("chunked")
113  >> just_result( chunked() ),
114  expected_caseless_token_p("compress")
115  >> just_result( compress() ),
116  expected_caseless_token_p("deflate")
117  >> just_result( deflate() ),
118  expected_caseless_token_p("gzip")
119  >> just_result( gzip() ),
120  expected_caseless_token_p("x-gzip")
121  >> just_result( gzip() ),
122  produce< transfer_extension_t >(
123  token_p() >> to_lower() >> &transfer_extension_t::token,
124  params_with_value_p()
125  >> &transfer_extension_t::transfer_parameters
126  ) >> as_result()
127  )
128  )
129  ) >> &transfer_encoding_value_t::values
130  );
131  }
132 
133  /*!
134  * @brief An attempt to parse Transfer-Encoding HTTP-field.
135  *
136  * @since v.0.6.9
137  */
138  [[nodiscard]]
139  static expected_t<
143  {
144  return restinio::easy_parser::try_parse( what, make_parser() );
145  }
146 };
147 
148 } /* namespace http_field_parsers */
149 
150 } /* namespace restinio */
expected_t< std::optional< string_view_t >, not_found_t > find_first(const parameter_with_optional_value_container_t &where, string_view_t what)
A helper function to find the first occurence of a parameter with the specified value.
Definition: basics.hpp:1740
static constexpr known_transfer_coding_t deflate() noexcept
Tools for working with the value of Transfer-Encoding 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, std::optional< Value_Type > >::type opt_value(const Parameter_Container &params, string_view_t key)
Gets the value of a parameter specified by key wrapped in std::optional<Value_Type> if parameter exis...
Definition: value_or.hpp:64
static constexpr known_transfer_coding_t compress() noexcept
static expected_t< transfer_encoding_value_t, restinio::easy_parser::parse_error_t > try_parse(string_view_t what)
An attempt to parse Transfer-Encoding HTTP-field.
static constexpr known_transfer_coding_t chunked() noexcept
static auto make_parser()
A factory function for a parser of Transfer-Encoding value.
known_transfer_coding_t
Enumeration for transfer-coding values from RFC7230.
static constexpr known_transfer_coding_t gzip() noexcept