RESTinio
authorization.hpp
Go to the documentation of this file.
1 /*
2  * RESTinio
3  */
4 
12 #pragma once
13 
15 
16 #include <restinio/variant.hpp>
17 
18 #include <iostream>
19 
20 namespace restinio
21 {
22 
23 namespace http_field_parsers
24 {
25 
26 namespace authorization_details
27 {
28 
30 
31 //
32 // is_token68_char_predicate_t
33 //
42 {
44 
46  bool
47  operator()( const char actual ) const noexcept
48  {
49  return base_type_t::operator()(actual)
50  || '-' == actual
51  || '.' == actual
52  || '_' == actual
53  || '~' == actual
54  || '+' == actual
55  || '/' == actual
56  ;
57  }
58 };
59 
60 //
61 // token68_symbol_p
62 //
64 inline auto
66 {
69 }
70 
71 //
72 // token68_t
73 //
82 struct token68_t
83 {
84  std::string value;
85 };
86 
87 inline std::ostream &
88 operator<<( std::ostream & to, const token68_t & v )
89 {
90  return (to << v.value);
91 }
92 
93 //
94 // token68_p
95 //
97 inline auto
99 {
100  return produce< token68_t >(
101  produce< std::string >(
102  repeat( 1, N, token68_symbol_p() >> to_container() ),
103  repeat( 0, N, symbol_p('=') >> to_container() )
104  ) >> &token68_t::value
105  );
106 }
107 
108 } /* authorization_details */
109 
110 //
111 // authorization_value_t
112 //
133 {
135  enum class value_form_t
136  {
138  token,
141  };
142 
145  {
147  std::string value;
150  };
151 
153  struct param_t
154  {
156  std::string name;
159  };
160 
162  using param_container_t = std::vector< param_t >;
163 
166 
169 
171  std::string auth_scheme;
173 
178 
185  static auto
187  {
188  using namespace authorization_details;
189 
190  auto token_to_v = []( std::string v ) -> param_value_t {
191  return { std::move(v), value_form_t::token };
192  };
193  auto qstring_to_v = []( std::string v ) -> param_value_t {
194  return { std::move(v), value_form_t::quoted_string };
195  };
196 
197  // NOTE: token68 should consume all input.
198  // So there should not be any symbols after the value.
199  auto token68_seq = sequence(
200  token68_p() >> as_result(),
201  not_clause( any_symbol_p() >> skip() ) );
202  // Parameters list can be empty.
203  auto params_seq = maybe_empty_comma_separated_list_p< param_container_t >(
204  produce< param_t >(
205  token_p() >> to_lower() >> &param_t::name,
206  ows(),
207  symbol('='),
208  ows(),
209  produce< param_value_t >(
210  alternatives(
211  token_p() >> convert( token_to_v ) >> as_result(),
212  quoted_string_p() >> convert( qstring_to_v )
213  >> as_result()
214  )
215  ) >> &param_t::value
216  )
217  ) >> as_result();
218 
219  return produce< authorization_value_t >(
221  maybe(
222  repeat( 1, N, space() ),
223  produce< auth_param_t >(
224  alternatives( token68_seq, params_seq )
226  )
227  );
228  }
229 
236  static expected_t<
240  {
242  }
243 };
244 
245 //
246 // Various helpers for dumping values to std::ostream.
247 //
248 inline std::ostream &
249 operator<<(
250  std::ostream & to,
252 {
254  to << v.value;
255  else
256  to << '"' << v.value << '"';
257  return to;
258 }
259 
260 inline std::ostream &
261 operator<<(
262  std::ostream & to,
264 {
265  return (to << v.name << '=' << v.value);
266 }
267 
268 inline std::ostream &
269 operator<<(
270  std::ostream & to,
272 {
273  struct printer_t
274  {
275  std::ostream & to;
276 
277  void
278  operator()( const authorization_value_t::token68_t & t ) const
279  {
280  to << t;
281  }
282 
283  void
284  operator()( const authorization_value_t::param_container_t & c ) const
285  {
286  bool first = true;
287  to << '{';
288  for( const auto & param : c )
289  {
290  if( !first )
291  to << ", ";
292  else
293  first = false;
294 
295  to << param;
296  }
297  to << '}';
298  }
299  };
300 
301  restinio::visit( printer_t{ to }, p );
302 
303  return to;
304 }
305 
306 inline std::ostream &
307 operator<<(
308  std::ostream & to,
309  const authorization_value_t & v )
310 {
311  return (to << v.auth_scheme << ' ' << v.auth_param);
312 }
313 
314 } /* namespace http_field_parsers */
315 
316 } /* namespace restinio */
317 
restinio::http_field_parsers::quoted_string_p
RESTINIO_NODISCARD auto quoted_string_p() noexcept
A factory function to create a quoted_string_producer.
Definition: basics.hpp:1012
RESTINIO_NODISCARD
#define RESTINIO_NODISCARD
Definition: compiler_features.hpp:33
restinio::http_field_parsers::authorization_details::token68_t
A structure for holding a value of token68 from RFC7235.
Definition: authorization.hpp:83
restinio::easy_parser::symbol
RESTINIO_NODISCARD auto symbol(char expected) noexcept
A factory function to create a clause that expects the speficied symbol, extracts it and then skips i...
Definition: easy_parser.hpp:4032
restinio::http_field_parsers::authorization_value_t::param_container_t
std::vector< param_t > param_container_t
Type of container for holding parameters.
Definition: authorization.hpp:162
restinio::easy_parser::impl::symbol_producer_template_t
A template for producer of charachers that satisfy some predicate.
Definition: easy_parser.hpp:2133
restinio::http_field_parsers::token_p
RESTINIO_NODISCARD auto token_p() noexcept
A factory function to create a token_producer.
Definition: basics.hpp:985
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
restinio::http_field_parsers::authorization_value_t::value_form_t
value_form_t
An indicator of the source form of the value of a parameter.
Definition: authorization.hpp:136
restinio::http_field_parsers::authorization_value_t::param_value_t
A storage for the value of a parameter.
Definition: authorization.hpp:145
restinio::http_field_parsers::authorization_value_t::param_t::value
param_value_t value
The value of a parameter.
Definition: authorization.hpp:158
restinio::easy_parser::N
constexpr std::size_t N
A special marker that means infinite repetitions.
Definition: easy_parser.hpp:455
restinio::http_field_parsers::authorization_value_t::make_parser
static RESTINIO_NODISCARD auto make_parser()
A factory function for a parser of Authorization value.
Definition: authorization.hpp:186
restinio::http_field_parsers::authorization_details::is_token68_char_predicate_t
A preducate for symbol_producer_template that checks that a symbol can be used inside token68 from RF...
Definition: authorization.hpp:42
restinio::http_field_parsers::authorization_value_t::param_value_t::form
value_form_t form
How this value was represented: as a token, or a quoted string?
Definition: authorization.hpp:149
restinio::easy_parser::not_clause
RESTINIO_NODISCARD auto not_clause(Clauses &&... clauses)
A factory function to create a not_clause.
Definition: easy_parser.hpp:3676
restinio::easy_parser::parse_error_t
Information about parsing error.
Definition: easy_parser.hpp:93
restinio::string_view_t
nonstd::string_view string_view_t
Definition: string_view.hpp:19
restinio::easy_parser::any_symbol_p
RESTINIO_NODISCARD auto any_symbol_p() noexcept
A factory function to create an any_symbol_producer.
Definition: easy_parser.hpp:3937
restinio::http_field_parsers::authorization_details::token68_symbol_p
RESTINIO_NODISCARD auto token68_symbol_p()
Definition: authorization.hpp:65
restinio::http_field_parsers::impl::is_alphanum_predicate_t::operator()
RESTINIO_NODISCARD bool operator()(const char actual) const noexcept
Definition: basics.hpp:301
restinio::http_field_parsers::authorization_value_t::auth_scheme
std::string auth_scheme
A value of auth-scheme.
Definition: authorization.hpp:171
restinio::http_field_parsers::authorization_value_t::param_t
A storage for a parameter with a name and a value.
Definition: authorization.hpp:154
restinio::easy_parser::as_result
RESTINIO_NODISCARD auto as_result() noexcept
A factory function to create a as_result_consumer.
Definition: easy_parser.hpp:4470
restinio::http_field_parsers::ows
RESTINIO_NODISCARD auto ows() noexcept
A factory function to create an OWS clause.
Definition: basics.hpp:939
restinio::easy_parser::symbol_p
RESTINIO_NODISCARD auto symbol_p(char expected) noexcept
A factory function to create a symbol_producer.
Definition: easy_parser.hpp:3955
restinio::http_field_parsers::authorization_details::operator<<
std::ostream & operator<<(std::ostream &to, const token68_t &v)
Definition: authorization.hpp:88
restinio::http_field_parsers::authorization_value_t::auth_param
auth_param_t auth_param
A parameter for authorization.
Definition: authorization.hpp:177
nonstd::variants::visit
R visit(const Visitor &v, V1 const &arg1)
Definition: variant.hpp:2194
restinio::http_field_parsers::authorization_details::is_token68_char_predicate_t::operator()
RESTINIO_NODISCARD bool operator()(const char actual) const noexcept
Definition: authorization.hpp:47
restinio::http_field_parsers::impl
Definition: basics.hpp:247
restinio::easy_parser::try_parse
RESTINIO_NODISCARD expected_t< typename Producer::result_type, parse_error_t > try_parse(string_view_t from, Producer producer)
Perform the parsing of the specified content by using specified value producer.
Definition: easy_parser.hpp:5042
restinio::expected_t
nonstd::expected< T, E > expected_t
Definition: expected.hpp:22
basics.hpp
Utilities for parsing values of http-fields.
restinio::http_field_parsers::authorization_value_t
Tools for working with the value of Authorization HTTP-field.
Definition: authorization.hpp:133
restinio::http_field_parsers::authorization_value_t::param_t::name
std::string name
The name of a parameter.
Definition: authorization.hpp:156
restinio::http_field_parsers::impl::is_alphanum_predicate_t
A preducate for symbol_producer_template that checks that a symbol is an alpha or numeric.
Definition: basics.hpp:298
restinio::easy_parser::to_container
RESTINIO_NODISCARD auto to_container()
A factory function to create a to_container_consumer.
Definition: easy_parser.hpp:4583
restinio
Definition: asio_include.hpp:21
restinio::http_field_parsers::authorization_value_t::param_value_t::value
std::string value
The value of a parameter.
Definition: authorization.hpp:147
restinio::easy_parser::space
RESTINIO_NODISCARD auto space() noexcept
A factory function to create a clause that expects a space, extracts it and then skips it.
Definition: easy_parser.hpp:4115
restinio::easy_parser::convert
RESTINIO_NODISCARD auto convert(Converter &&converter)
A factory function to create convert_transformer.
Definition: easy_parser.hpp:4737
restinio::easy_parser::to_lower
RESTINIO_NODISCARD auto to_lower() noexcept
A factory function to create a to_lower_transformer.
Definition: easy_parser.hpp:4610
restinio::http_field_parsers::authorization_details::token68_p
RESTINIO_NODISCARD auto token68_p()
Definition: authorization.hpp:98
restinio::http_field_parsers::authorization_value_t::try_parse
static RESTINIO_NODISCARD expected_t< authorization_value_t, restinio::easy_parser::parse_error_t > try_parse(string_view_t what)
An attempt to parse Authorization HTTP-field.
Definition: authorization.hpp:239
restinio::http_field_parsers::authorization_value_t::value_form_t::token
@ token
The value of a parameter was specified as token.
restinio::http_field_parsers::authorization_value_t::value_form_t::quoted_string
@ quoted_string
The value of a parameter was specified as quoted_string.
restinio::easy_parser::alternatives
RESTINIO_NODISCARD auto alternatives(Clauses &&... clauses)
A factory function to create an alternatives clause.
Definition: easy_parser.hpp:3595
variant.hpp
nonstd::variants::variant
Definition: variant.hpp:1209
restinio::easy_parser::sequence
RESTINIO_NODISCARD auto sequence(Clauses &&... clauses)
A factory function to create a sequence of subclauses.
Definition: easy_parser.hpp:3758
restinio::http_field_parsers::operator<<
std::ostream & operator<<(std::ostream &to, const authorization_value_t::param_value_t &v)
Definition: authorization.hpp:249
restinio::http_field_parsers::authorization_details::token68_t::value
std::string value
Definition: authorization.hpp:84
restinio::easy_parser::repeat
RESTINIO_NODISCARD auto repeat(std::size_t min_occurences, std::size_t max_occurences, Clauses &&... clauses)
A factory function to create repetitor of subclauses.
Definition: easy_parser.hpp:3876
restinio::easy_parser::skip
RESTINIO_NODISCARD auto skip() noexcept
A factory function to create a skip_consumer.
Definition: easy_parser.hpp:3922
restinio::easy_parser::maybe
RESTINIO_NODISCARD auto maybe(Clauses &&... clauses)
A factory function to create an optional clause.
Definition: easy_parser.hpp:3634