authorization.hpp
Go to the documentation of this file.
/*
* RESTinio
*/
#pragma once
#include <restinio/helpers/http_field_parsers/basics.hpp>
#include <restinio/variant.hpp>
#include <iostream>
namespace restinio
{
namespace http_field_parsers
{
namespace authorization_details
{
namespace hfp_impl = restinio::http_field_parsers::impl;
//
// is_token68_char_predicate_t
//
struct is_token68_char_predicate_t
: protected hfp_impl::is_alphanum_predicate_t
{
using base_type_t = hfp_impl::is_alphanum_predicate_t;
RESTINIO_NODISCARD
bool
operator()( const char actual ) const noexcept
{
return base_type_t::operator()(actual)
|| '-' == actual
|| '.' == actual
|| '_' == actual
|| '~' == actual
|| '+' == actual
|| '/' == actual
;
}
};
//
// token68_symbol_p
//
RESTINIO_NODISCARD
inline auto
token68_symbol_p()
{
return restinio::easy_parser::impl::symbol_producer_template_t<
is_token68_char_predicate_t >{};
}
//
// token68_t
//
struct token68_t
{
std::string value;
};
inline std::ostream &
operator<<( std::ostream & to, const token68_t & v )
{
return (to << v.value);
}
//
// token68_p
//
RESTINIO_NODISCARD
inline auto
token68_p()
{
return produce< token68_t >(
produce< std::string >(
repeat( 1, N, token68_symbol_p() >> to_container() ),
repeat( 0, N, symbol_p('=') >> to_container() )
) >> &token68_t::value
);
}
} /* authorization_details */
//
// authorization_value_t
//
struct authorization_value_t
{
enum class value_form_t
{
token,
quoted_string
};
struct param_value_t
{
std::string value;
value_form_t form;
};
struct param_t
{
std::string name;
param_value_t value;
};
using param_container_t = std::vector< param_t >;
using token68_t = authorization_details::token68_t;
using auth_param_t = variant_t< token68_t, param_container_t >;
std::string auth_scheme;
auth_param_t auth_param;
RESTINIO_NODISCARD
static auto
make_parser()
{
using namespace authorization_details;
auto token_to_v = []( std::string v ) -> param_value_t {
return { std::move(v), value_form_t::token };
};
auto qstring_to_v = []( std::string v ) -> param_value_t {
return { std::move(v), value_form_t::quoted_string };
};
// NOTE: token68 should consume all input.
// So there should not be any symbols after the value.
auto token68_seq = sequence(
token68_p() >> as_result(),
not_clause( any_symbol_p() >> skip() ) );
// Parameters list can be empty.
auto params_seq = maybe_empty_comma_separated_list_p< param_container_t >(
produce< param_t >(
token_p() >> to_lower() >> &param_t::name,
ows(),
symbol('='),
ows(),
produce< param_value_t >(
alternatives(
token_p() >> convert( token_to_v ) >> as_result(),
quoted_string_p() >> convert( qstring_to_v )
>> as_result()
)
) >> &param_t::value
)
) >> as_result();
return produce< authorization_value_t >(
token_p() >> to_lower() >> &authorization_value_t::auth_scheme,
maybe(
repeat( 1, N, space() ),
produce< auth_param_t >(
alternatives( token68_seq, params_seq )
) >> &authorization_value_t::auth_param
)
);
}
RESTINIO_NODISCARD
static expected_t<
authorization_value_t,
restinio::easy_parser::parse_error_t >
try_parse( string_view_t what )
{
return restinio::easy_parser::try_parse( what, make_parser() );
}
};
//
// Various helpers for dumping values to std::ostream.
//
inline std::ostream &
operator<<(
std::ostream & to,
const authorization_value_t::param_value_t & v )
{
if(authorization_value_t::value_form_t::token == v.form)
to << v.value;
else
to << '"' << v.value << '"';
return to;
}
inline std::ostream &
operator<<(
std::ostream & to,
const authorization_value_t::param_t & v )
{
return (to << v.name << '=' << v.value);
}
inline std::ostream &
operator<<(
std::ostream & to,
const authorization_value_t::auth_param_t & p )
{
struct printer_t
{
std::ostream & to;
void
operator()( const authorization_value_t::token68_t & t ) const
{
to << t;
}
void
operator()( const authorization_value_t::param_container_t & c ) const
{
bool first = true;
to << '{';
for( const auto & param : c )
{
if( !first )
to << ", ";
else
first = false;
to << param;
}
to << '}';
}
};
restinio::visit( printer_t{ to }, p );
return to;
}
inline std::ostream &
operator<<(
std::ostream & to,
const authorization_value_t & v )
{
return (to << v.auth_scheme << ' ' << v.auth_param);
}
} /* namespace http_field_parsers */
} /* namespace restinio */
basics.hpp
Utilities for parsing values of http-fields.
nonstd::variants::variant
Definition: variant.hpp:1301
restinio::easy_parser::impl::symbol_producer_template_t
A template for producer of charachers that satisfy some predicate.
Definition: easy_parser.hpp:2133
restinio::easy_parser::parse_error_t
Information about parsing error.
Definition: easy_parser.hpp:93
RESTINIO_NODISCARD
#define RESTINIO_NODISCARD
Definition: compiler_features.hpp:33
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:465
nonstd::variants::visit
R visit(const Visitor &v, V1 const &arg1)
Definition: variant.hpp:2532
restinio::easy_parser::maybe
RESTINIO_NODISCARD auto maybe(Clauses &&... clauses)
A factory function to create an optional clause.
Definition: easy_parser.hpp:3634
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::easy_parser::sequence
RESTINIO_NODISCARD auto sequence(Clauses &&... clauses)
A factory function to create a sequence of subclauses.
Definition: easy_parser.hpp:3758
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::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::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::as_result
RESTINIO_NODISCARD auto as_result() noexcept
A factory function to create a as_result_consumer.
Definition: easy_parser.hpp:4470
restinio::easy_parser::N
constexpr std::size_t N
A special marker that means infinite repetitions.
Definition: easy_parser.hpp:455
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::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::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::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::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::easy_parser::alternatives
RESTINIO_NODISCARD auto alternatives(Clauses &&... clauses)
A factory function to create an alternatives clause.
Definition: easy_parser.hpp:3595
restinio::easy_parser::convert
RESTINIO_NODISCARD auto convert(Converter &&converter)
A factory function to create convert_transformer.
Definition: easy_parser.hpp:4737
restinio::http_field_parsers::authorization_details::token68_symbol_p
RESTINIO_NODISCARD auto token68_symbol_p()
Definition: authorization.hpp:65
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_details::token68_p
RESTINIO_NODISCARD auto token68_p()
Definition: authorization.hpp:98
restinio::http_field_parsers::impl
Definition: basics.hpp:247
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::ows
RESTINIO_NODISCARD auto ows() noexcept
A factory function to create an OWS clause.
Definition: basics.hpp:939
restinio::http_field_parsers::token_p
RESTINIO_NODISCARD auto token_p() noexcept
A factory function to create a token_producer.
Definition: basics.hpp:985
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
Definition: asio_include.hpp:21
restinio::string_view_t
nonstd::string_view string_view_t
Definition: string_view.hpp:19
restinio::expected_t
nonstd::expected< T, E > expected_t
Definition: expected.hpp:22
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_details::is_token68_char_predicate_t::operator()
RESTINIO_NODISCARD bool operator()(const char actual) const noexcept
Definition: authorization.hpp:47
restinio::http_field_parsers::authorization_details::token68_t
A structure for holding a value of token68 from RFC7235.
Definition: authorization.hpp:83
restinio::http_field_parsers::authorization_details::token68_t::value
std::string value
Definition: authorization.hpp:84
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::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::authorization_value_t::param_t::value
param_value_t value
The value of a parameter.
Definition: authorization.hpp:158
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_value_t::form
value_form_t form
How this value was represented: as a token, or a quoted string?
Definition: authorization.hpp:149
restinio::http_field_parsers::authorization_value_t::param_value_t::value
std::string value
The value of a parameter.
Definition: authorization.hpp:147
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::auth_param
auth_param_t auth_param
A parameter for authorization.
Definition: authorization.hpp:177
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::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::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::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::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::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::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::http_field_parsers::impl::is_alphanum_predicate_t::operator()
RESTINIO_NODISCARD bool operator()(const char actual) const noexcept
Definition: basics.hpp:301
variant.hpp