host.hpp
Go to the documentation of this file.
/*
* RESTinio
*/
#pragma once
#include <restinio/helpers/http_field_parsers/basics.hpp>
#include <restinio/helpers/http_field_parsers/details/pct_encoded_symbols.hpp>
#include <restinio/variant.hpp>
namespace restinio
{
namespace http_field_parsers
{
namespace host_details
{
namespace ep_impl = restinio::easy_parser::impl;
namespace hfp_impl = restinio::http_field_parsers::impl;
namespace hfp_details = restinio::http_field_parsers::details;
//
// unreserved_predicate_t
//
struct unreserved_predicate_t
{
RESTINIO_NODISCARD
bool
operator()( const char actual ) const noexcept
{
return hfp_impl::is_alpha(actual)
|| hfp_impl::is_digit(actual)
|| '-' == actual
|| '.' == actual
|| '_' == actual
|| '~' == actual
;
}
};
//
// unreserved_symbol_producer
//
RESTINIO_NODISCARD
inline auto
unreserved_symbol_p()
{
return ep_impl::symbol_producer_template_t< unreserved_predicate_t >{};
}
//
// sub_delims_predicate_t
//
struct sub_delims_predicate_t
{
RESTINIO_NODISCARD
bool
operator()( const char actual ) const noexcept
{
return '!' == actual
|| '$' == actual
|| '&' == actual
|| '\'' == actual
|| '(' == actual
|| ')' == actual
|| '*' == actual
|| '+' == actual
|| ',' == actual
|| ';' == actual
|| '=' == actual
;
}
};
//
// sub_delims_symbol_producer
//
RESTINIO_NODISCARD
inline auto
sub_delims_symbol_p()
{
return ep_impl::symbol_producer_template_t< sub_delims_predicate_t >{};
}
//
// ipv4_address_producer
//
RESTINIO_NODISCARD
inline auto
ipv4_address_p()
{
const auto dec_octet = produce< std::string >(
alternatives(
sequence(
symbol_p('2') >> to_container(),
symbol_p('5') >> to_container(),
symbol_from_range_p('0', '5') >> to_container()
),
sequence(
symbol_p('2') >> to_container(),
symbol_from_range_p('0', '4') >> to_container(),
digit_p() >> to_container()
),
sequence(
symbol_p('1') >> to_container(),
digit_p() >> to_container(),
digit_p() >> to_container()
),
sequence(
symbol_from_range_p('1', '9') >> to_container(),
digit_p() >> to_container()
),
digit_p() >> to_container()
)
);
return produce< std::string >(
dec_octet >> to_container(),
symbol_p('.') >> to_container(),
dec_octet >> to_container(),
symbol_p('.') >> to_container(),
dec_octet >> to_container(),
symbol_p('.') >> to_container(),
dec_octet >> to_container()
);
}
//FIXME: maybe this should be a part of easy_parser?
#if 0
struct debug_printer : public ep_impl::clause_tag
{
std::string m_tag;
debug_printer( std::string v ) noexcept : m_tag{ std::move(v) } {}
template< typename Target_Type >
RESTINIO_NODISCARD
optional_t< parse_error_t >
try_process( ep_impl::source_t & from, Target_Type & /*target*/ )
{
std::cout << "*** debug_print: " << m_tag << std::endl;
return nullopt;
}
};
#endif
//
// ipv6_address_producer
//
RESTINIO_NODISCARD
inline auto
ipv6_address_p()
{
const auto h16 = produce< std::string >(
repeat( 1u, 4u, hexdigit_p() >> to_container() )
);
const auto h16_with_colon = sequence(
h16 >> to_container(),
symbol_p(':') >> to_container(),
not_clause( symbol(':') )
);
const auto ls32 = produce< std::string >(
alternatives(
sequence(
h16 >> to_container(),
symbol_p(':') >> to_container(),
h16 >> to_container()
),
ipv4_address_p() >> as_result()
)
);
const auto double_colon =
exact_p( "::" ) >> just( std::string{ "::" } ) >> to_container()
;
return produce< std::string >(
alternatives(
sequence(
repeat( 6u, 6u, h16_with_colon ),
ls32 >> to_container()
),
sequence(
double_colon,
repeat( 5u, 5u, h16_with_colon ),
ls32 >> to_container()
),
sequence(
maybe( h16 >> to_container() ),
double_colon,
repeat( 4u, 4u, h16_with_colon ),
ls32 >> to_container()
),
sequence(
maybe(
repeat( 0u, 1u, h16_with_colon ),
h16 >> to_container()
),
double_colon,
repeat( 3u, 3u, h16_with_colon ),
ls32 >> to_container()
),
sequence(
maybe(
repeat( 0u, 2u, h16_with_colon ),
h16 >> to_container()
),
double_colon,
repeat( 2u, 2u, h16_with_colon ),
ls32 >> to_container()
),
sequence(
maybe(
repeat( 0u, 3u, h16_with_colon ),
h16 >> to_container()
),
double_colon,
h16_with_colon,
ls32 >> to_container()
),
sequence(
maybe(
repeat( 0u, 4u, h16_with_colon ),
h16 >> to_container()
),
double_colon,
ls32 >> to_container()
),
sequence(
maybe(
repeat( 0u, 5u, h16_with_colon ),
h16 >> to_container()
),
double_colon,
h16 >> to_container()
),
sequence(
maybe(
repeat( 0u, 6u, h16_with_colon ),
h16 >> to_container()
),
double_colon
)
)
);
}
//
// reg_name_producer
//
RESTINIO_NODISCARD
inline auto
reg_name_p()
{
return produce< std::string >(
repeat( 1, N,
alternatives(
unreserved_symbol_p() >> to_container(),
hfp_details::pct_encoded_symbols_p()
>> hfp_details::pct_encoded_symbols_consumer_t{},
sub_delims_symbol_p() >> to_container()
)
)
);
}
} /* namespace host_details */
//
// raw_host_value_t
//
struct raw_host_value_t
{
struct reg_name_t
{
std::string v;
reg_name_t() = default;
explicit reg_name_t( std::string val ) noexcept : v{ std::move(val) } {}
friend bool
operator==( const reg_name_t & a, const reg_name_t & b ) noexcept
{
return a.v == b.v;
}
friend bool
operator!=( const reg_name_t & a, const reg_name_t & b ) noexcept
{
return a.v != b.v;
}
friend bool
operator<( const reg_name_t & a, const reg_name_t & b ) noexcept
{
return a.v < b.v;
}
RESTINIO_NODISCARD
static reg_name_t
from_string( std::string v ) noexcept
{
return reg_name_t{ std::move(v) };
}
};
struct ipv4_address_t
{
std::string v;
ipv4_address_t() = default;
explicit ipv4_address_t( std::string val ) noexcept : v{ std::move(val) } {}
friend bool
operator==( const ipv4_address_t & a, const ipv4_address_t & b ) noexcept
{
return a.v == b.v;
}
friend bool
operator!=( const ipv4_address_t & a, const ipv4_address_t & b ) noexcept
{
return a.v != b.v;
}
friend bool
operator<( const ipv4_address_t & a, const ipv4_address_t & b ) noexcept
{
return a.v < b.v;
}
RESTINIO_NODISCARD
static ipv4_address_t
from_string( std::string v ) noexcept
{
return ipv4_address_t{ std::move(v) };
}
};
struct ipv6_address_t
{
std::string v;
ipv6_address_t() = default;
explicit ipv6_address_t( std::string val ) noexcept : v{ std::move(val) } {}
friend bool
operator==( const ipv6_address_t & a, const ipv6_address_t & b ) noexcept
{
return a.v == b.v;
}
friend bool
operator!=( const ipv6_address_t & a, const ipv6_address_t & b ) noexcept
{
return a.v != b.v;
}
friend bool
operator<( const ipv6_address_t & a, const ipv6_address_t & b ) noexcept
{
return a.v < b.v;
}
RESTINIO_NODISCARD
static ipv6_address_t
from_string( std::string v ) noexcept
{
return ipv6_address_t{ std::move(v) };
}
};
using host_value_t = variant_t< reg_name_t, ipv4_address_t, ipv6_address_t >;
host_value_t host;
optional_t<std::uint16_t> port;
RESTINIO_NODISCARD
static auto
make_parser()
{
using namespace host_details;
return produce< raw_host_value_t >(
produce< host_value_t >(
alternatives(
produce< ipv6_address_t >(
symbol('['),
ipv6_address_p()
>> to_lower()
>> convert( ipv6_address_t::from_string )
>> as_result(),
symbol(']')
) >> as_result(),
produce< ipv4_address_t >(
ipv4_address_p()
>> convert( ipv4_address_t::from_string )
>> as_result()
) >> as_result(),
produce< reg_name_t >(
reg_name_p() >> to_lower()
>> convert( reg_name_t::from_string )
>> as_result()
) >> as_result()
)
) >> &raw_host_value_t::host,
maybe(
symbol(':'),
non_negative_decimal_number_p< std::uint16_t >()
>> &raw_host_value_t::port
)
);
}
RESTINIO_NODISCARD
static expected_t< raw_host_value_t, restinio::easy_parser::parse_error_t >
try_parse( string_view_t what )
{
return restinio::easy_parser::try_parse( what, make_parser() );
}
};
inline std::ostream &
operator<<( std::ostream & to, const raw_host_value_t & rhv )
{
struct host_dumper_t
{
std::ostream & m_to;
void operator()( const raw_host_value_t::reg_name_t & n ) const
{
m_to << n.v;
}
void operator()( const raw_host_value_t::ipv4_address_t & n ) const
{
m_to << n.v;
}
void operator()( const raw_host_value_t::ipv6_address_t & n ) const
{
m_to << '[' << n.v << ']';
}
};
visit( host_dumper_t{ to }, rhv.host );
if( rhv.port )
to << ':' << *(rhv.port) << std::endl;
return to;
}
} /* namespace http_field_parsers */
} /* namespace restinio */
basics.hpp
Utilities for parsing values of http-fields.
nonstd::optional_lite::optional
class optional
Definition: optional.hpp:919
nonstd::variants::variant
Definition: variant.hpp:1301
restinio::easy_parser::impl::source_t
The class that implements "input stream".
Definition: easy_parser.hpp:740
restinio::easy_parser::impl::symbol_producer_template_t
A template for producer of charachers that satisfy some predicate.
Definition: easy_parser.hpp:2133
RESTINIO_NODISCARD
#define RESTINIO_NODISCARD
Definition: compiler_features.hpp:33
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:465
nonstd::optional_lite::nullopt
const nullopt_t nullopt((nullopt_t::init()))
nonstd::variants::visit
R visit(const Visitor &v, V1 const &arg1)
Definition: variant.hpp:2532
restinio::easy_parser::impl
Definition: easy_parser.hpp:280
restinio::easy_parser::impl::is_digit
RESTINIO_NODISCARD constexpr bool is_digit(const char ch) noexcept
Is a character a decimal digit?
Definition: easy_parser.hpp:669
restinio::easy_parser::symbol_from_range_p
RESTINIO_NODISCARD auto symbol_from_range_p(char left, char right) noexcept
A factory function to create a symbol_from_range_producer.
Definition: easy_parser.hpp:4011
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::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::digit_p
RESTINIO_NODISCARD auto digit_p() noexcept
A factory function to create a digit_producer.
Definition: easy_parser.hpp:4133
restinio::easy_parser::exact_p
RESTINIO_NODISCARD auto exact_p(string_view_t fragment)
A factory function that creates an instance of exact_fragment_producer.
Definition: easy_parser.hpp:4768
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::hexdigit_p
RESTINIO_NODISCARD auto hexdigit_p() noexcept
A factory function to create a hexdigit_producer.
Definition: easy_parser.hpp:4172
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::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::just
RESTINIO_NODISCARD auto just(T value) noexcept(noexcept(impl::just_value_transformer_t< T >{value}))
A special transformer that replaces the produced value by a value specified by a user.
Definition: easy_parser.hpp:4635
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::details
Definition: pct_encoded_symbols.hpp:23
restinio::http_field_parsers::details::pct_encoded_symbols_p
RESTINIO_NODISCARD auto pct_encoded_symbols_p()
A producer that extract a sequence of symbols represented a percent-encoded character.
Definition: pct_encoded_symbols.hpp:57
restinio::http_field_parsers::host_details::reg_name_p
RESTINIO_NODISCARD auto reg_name_p()
A factory for producer of reg-name value.
Definition: host.hpp:353
restinio::http_field_parsers::host_details::ipv6_address_p
RESTINIO_NODISCARD auto ipv6_address_p()
A factory for producer of ipv6_address value.
Definition: host.hpp:235
restinio::http_field_parsers::host_details::sub_delims_symbol_p
RESTINIO_NODISCARD auto sub_delims_symbol_p()
A factory for producer that extracts sub-delims symbols.
Definition: host.hpp:121
restinio::http_field_parsers::host_details::unreserved_symbol_p
RESTINIO_NODISCARD auto unreserved_symbol_p()
A factory for producer that extracts unreserved symbols.
Definition: host.hpp:72
restinio::http_field_parsers::host_details::ipv4_address_p
RESTINIO_NODISCARD auto ipv4_address_p()
A factory for producer of IPv4address value.
Definition: host.hpp:149
restinio::http_field_parsers::impl
Definition: basics.hpp:247
restinio::http_field_parsers::impl::is_alpha
RESTINIO_NODISCARD constexpr bool is_alpha(const char ch) noexcept
Is a character an ALPHA?
Definition: basics.hpp:263
restinio::http_field_parsers::operator<<
std::ostream & operator<<(std::ostream &to, const authorization_value_t::param_value_t &v)
Definition: authorization.hpp:249
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
pct_encoded_symbols.hpp
Stuff related to percent-encoded symbols.
restinio::easy_parser::impl::clause_tag
A special base class to be used with clauses.
Definition: easy_parser.hpp:1393
restinio::http_field_parsers::details::pct_encoded_symbols_consumer_t
A special consumer that inserts an extracted sequence of symbols into the result string.
Definition: pct_encoded_symbols.hpp:80
restinio::http_field_parsers::host_details::sub_delims_predicate_t
A preducate for symbol_producer_template that checks that a symbol is sub-delims symbol from RCF3986.
Definition: host.hpp:89
restinio::http_field_parsers::host_details::sub_delims_predicate_t::operator()
RESTINIO_NODISCARD bool operator()(const char actual) const noexcept
Definition: host.hpp:92
restinio::http_field_parsers::host_details::unreserved_predicate_t
A preducate for symbol_producer_template that checks that a symbol is unreserved symbol from RCF3986.
Definition: host.hpp:45
restinio::http_field_parsers::host_details::unreserved_predicate_t::operator()
RESTINIO_NODISCARD bool operator()(const char actual) const noexcept
Definition: host.hpp:48
restinio::http_field_parsers::raw_host_value_t::ipv4_address_t
Definition: host.hpp:423
restinio::http_field_parsers::raw_host_value_t::ipv4_address_t::operator==
friend bool operator==(const ipv4_address_t &a, const ipv4_address_t &b) noexcept
Definition: host.hpp:430
restinio::http_field_parsers::raw_host_value_t::ipv4_address_t::ipv4_address_t
ipv4_address_t()=default
restinio::http_field_parsers::raw_host_value_t::ipv4_address_t::v
std::string v
Definition: host.hpp:424
restinio::http_field_parsers::raw_host_value_t::ipv4_address_t::operator!=
friend bool operator!=(const ipv4_address_t &a, const ipv4_address_t &b) noexcept
Definition: host.hpp:436
restinio::http_field_parsers::raw_host_value_t::ipv4_address_t::from_string
static RESTINIO_NODISCARD ipv4_address_t from_string(std::string v) noexcept
Definition: host.hpp:449
restinio::http_field_parsers::raw_host_value_t::ipv4_address_t::ipv4_address_t
ipv4_address_t(std::string val) noexcept
Definition: host.hpp:427
restinio::http_field_parsers::raw_host_value_t::ipv4_address_t::operator<
friend bool operator<(const ipv4_address_t &a, const ipv4_address_t &b) noexcept
Definition: host.hpp:442
restinio::http_field_parsers::raw_host_value_t::ipv6_address_t
Definition: host.hpp:456
restinio::http_field_parsers::raw_host_value_t::ipv6_address_t::operator!=
friend bool operator!=(const ipv6_address_t &a, const ipv6_address_t &b) noexcept
Definition: host.hpp:469
restinio::http_field_parsers::raw_host_value_t::ipv6_address_t::operator==
friend bool operator==(const ipv6_address_t &a, const ipv6_address_t &b) noexcept
Definition: host.hpp:463
restinio::http_field_parsers::raw_host_value_t::ipv6_address_t::operator<
friend bool operator<(const ipv6_address_t &a, const ipv6_address_t &b) noexcept
Definition: host.hpp:475
restinio::http_field_parsers::raw_host_value_t::ipv6_address_t::ipv6_address_t
ipv6_address_t()=default
restinio::http_field_parsers::raw_host_value_t::ipv6_address_t::v
std::string v
Definition: host.hpp:457
restinio::http_field_parsers::raw_host_value_t::ipv6_address_t::ipv6_address_t
ipv6_address_t(std::string val) noexcept
Definition: host.hpp:460
restinio::http_field_parsers::raw_host_value_t::ipv6_address_t::from_string
static RESTINIO_NODISCARD ipv6_address_t from_string(std::string v) noexcept
Definition: host.hpp:482
restinio::http_field_parsers::raw_host_value_t::reg_name_t
Definition: host.hpp:390
restinio::http_field_parsers::raw_host_value_t::reg_name_t::reg_name_t
reg_name_t()=default
restinio::http_field_parsers::raw_host_value_t::reg_name_t::operator<
friend bool operator<(const reg_name_t &a, const reg_name_t &b) noexcept
Definition: host.hpp:409
restinio::http_field_parsers::raw_host_value_t::reg_name_t::from_string
static RESTINIO_NODISCARD reg_name_t from_string(std::string v) noexcept
Definition: host.hpp:416
restinio::http_field_parsers::raw_host_value_t::reg_name_t::operator!=
friend bool operator!=(const reg_name_t &a, const reg_name_t &b) noexcept
Definition: host.hpp:403
restinio::http_field_parsers::raw_host_value_t::reg_name_t::reg_name_t
reg_name_t(std::string val) noexcept
Definition: host.hpp:394
restinio::http_field_parsers::raw_host_value_t::reg_name_t::v
std::string v
Definition: host.hpp:391
restinio::http_field_parsers::raw_host_value_t::reg_name_t::operator==
friend bool operator==(const reg_name_t &a, const reg_name_t &b) noexcept
Definition: host.hpp:397
restinio::http_field_parsers::raw_host_value_t
Tools for working with the raw value of Host HTTP-field.
Definition: host.hpp:388
restinio::http_field_parsers::raw_host_value_t::try_parse
static RESTINIO_NODISCARD expected_t< raw_host_value_t, restinio::easy_parser::parse_error_t > try_parse(string_view_t what)
An attempt to parse Host HTTP-field.
Definition: host.hpp:550
restinio::http_field_parsers::raw_host_value_t::port
optional_t< std::uint16_t > port
Optional port value.
Definition: host.hpp:496
restinio::http_field_parsers::raw_host_value_t::make_parser
static RESTINIO_NODISCARD auto make_parser()
A factory function for a parser of Host value.
Definition: host.hpp:505
restinio::http_field_parsers::raw_host_value_t::host
host_value_t host
Definition: host.hpp:490
variant.hpp