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
20namespace restinio
21{
22
23namespace http_field_parsers
24{
25
26namespace 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//
64inline auto
66{
69}
70
71//
72// token68_t
73//
83{
84 std::string value;
85};
86
87inline std::ostream &
88operator<<( std::ostream & to, const token68_t & v )
89{
90 return (to << v.value);
91}
92
93//
94// token68_p
95//
97inline auto
99{
100 return produce< token68_t >(
101 produce< std::string >(
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,
140 quoted_string
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 {
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 >(
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//
248inline std::ostream &
250 std::ostream & to,
252{
254 to << v.value;
255 else
256 to << '"' << v.value << '"';
257 return to;
258}
259
260inline std::ostream &
262 std::ostream & to,
264{
265 return (to << v.name << '=' << v.value);
266}
267
268inline std::ostream &
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
306inline std::ostream &
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
Utilities for parsing values of http-fields.
A template for producer of charachers that satisfy some predicate.
Information about parsing error.
Definition: easy_parser.hpp:93
#define RESTINIO_NODISCARD
R visit(const Visitor &v, V1 const &arg1)
Definition: variant.hpp:2532
RESTINIO_NODISCARD auto maybe(Clauses &&... clauses)
A factory function to create an optional clause.
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...
RESTINIO_NODISCARD auto sequence(Clauses &&... clauses)
A factory function to create a sequence of subclauses.
RESTINIO_NODISCARD auto space() noexcept
A factory function to create a clause that expects a space, extracts it and then skips it.
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.
RESTINIO_NODISCARD auto repeat(std::size_t min_occurences, std::size_t max_occurences, Clauses &&... clauses)
A factory function to create repetitor of subclauses.
RESTINIO_NODISCARD auto skip() noexcept
A factory function to create a skip_consumer.
RESTINIO_NODISCARD auto as_result() noexcept
A factory function to create a as_result_consumer.
constexpr std::size_t N
A special marker that means infinite repetitions.
RESTINIO_NODISCARD auto to_container()
A factory function to create a to_container_consumer.
RESTINIO_NODISCARD auto symbol_p(char expected) noexcept
A factory function to create a symbol_producer.
RESTINIO_NODISCARD auto not_clause(Clauses &&... clauses)
A factory function to create a not_clause.
RESTINIO_NODISCARD auto any_symbol_p() noexcept
A factory function to create an any_symbol_producer.
RESTINIO_NODISCARD auto to_lower() noexcept
A factory function to create a to_lower_transformer.
RESTINIO_NODISCARD auto alternatives(Clauses &&... clauses)
A factory function to create an alternatives clause.
RESTINIO_NODISCARD auto convert(Converter &&converter)
A factory function to create convert_transformer.
std::ostream & operator<<(std::ostream &to, const token68_t &v)
std::ostream & operator<<(std::ostream &to, const authorization_value_t::param_value_t &v)
RESTINIO_NODISCARD auto ows() noexcept
A factory function to create an OWS clause.
Definition: basics.hpp:939
RESTINIO_NODISCARD auto token_p() noexcept
A factory function to create a token_producer.
Definition: basics.hpp:985
RESTINIO_NODISCARD auto quoted_string_p() noexcept
A factory function to create a quoted_string_producer.
Definition: basics.hpp:1012
nonstd::string_view string_view_t
Definition: string_view.hpp:19
nonstd::expected< T, E > expected_t
Definition: expected.hpp:22
A preducate for symbol_producer_template that checks that a symbol can be used inside token68 from RF...
RESTINIO_NODISCARD bool operator()(const char actual) const noexcept
A structure for holding a value of token68 from RFC7235.
A storage for a parameter with a name and a value.
value_form_t form
How this value was represented: as a token, or a quoted string?
Tools for working with the value of Authorization HTTP-field.
auth_param_t auth_param
A parameter for authorization.
std::string auth_scheme
A value of auth-scheme.
value_form_t
An indicator of the source form of the value of a parameter.
@ token
The value of a parameter was specified as token.
@ quoted_string
The value of a parameter was specified as quoted_string.
std::vector< param_t > param_container_t
Type of container for holding parameters.
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.
static RESTINIO_NODISCARD auto make_parser()
A factory function for a parser of Authorization value.
A preducate for symbol_producer_template that checks that a symbol is an alpha or numeric.
Definition: basics.hpp:298
RESTINIO_NODISCARD bool operator()(const char actual) const noexcept
Definition: basics.hpp:301