range.hpp
Go to the documentation of this file.
/*
* RESTinio
*/
#pragma once
#include <restinio/helpers/http_field_parsers/basics.hpp>
#include <restinio/variant.hpp>
namespace restinio
{
namespace http_field_parsers
{
namespace range_details
{
template< typename T >
struct double_ended_range_t
{
T first;
T last;
};
template< typename T >
struct open_ended_range_t
{
T first;
};
template< typename T >
struct suffix_length_t
{
T length;
};
template< typename T >
using byte_range_spec_t = variant_t<
double_ended_range_t<T>,
open_ended_range_t<T>,
suffix_length_t<T> >;
template< typename T >
struct byte_ranges_specifier_t
{
std::vector< byte_range_spec_t<T> > ranges;
};
struct other_ranges_specifier_t
{
std::string range_unit;
std::string range_set;
};
template< typename T >
using value_t = variant_t<
byte_ranges_specifier_t<T>,
other_ranges_specifier_t >;
template< typename T >
RESTINIO_NODISCARD
auto
make_byte_range_spec_parser()
{
return produce< byte_range_spec_t<T> >(
alternatives(
produce< double_ended_range_t<T> >(
non_negative_decimal_number_p<T>()
>> &double_ended_range_t<T>::first,
symbol('-'),
non_negative_decimal_number_p<T>()
>> &double_ended_range_t<T>::last
) >> as_result(),
produce< open_ended_range_t<T> >(
non_negative_decimal_number_p<T>()
>> &open_ended_range_t<T>::first,
symbol('-')
) >> as_result(),
produce< suffix_length_t<T> >(
symbol('-'),
non_negative_decimal_number_p<T>()
>> &suffix_length_t<T>::length
) >> as_result()
)
);
}
RESTINIO_NODISCARD
inline auto
make_bytes_prefix_parser()
{
return sequence( exact( "bytes" ), symbol('=') );
}
template< typename T >
RESTINIO_NODISCARD
auto
make_byte_ranges_specifier_parser()
{
return produce< byte_ranges_specifier_t<T> >(
make_bytes_prefix_parser(),
force_only_this_alternative(
non_empty_comma_separated_list_p<
std::vector< byte_range_spec_t<T> > >(
make_byte_range_spec_parser<T>()
) >> &byte_ranges_specifier_t<T>::ranges
)
);
}
RESTINIO_NODISCARD
inline auto
make_other_ranges_specifier_parser()
{
return produce< other_ranges_specifier_t >(
token_p() >> &other_ranges_specifier_t::range_unit,
symbol('='),
force_only_this_alternative(
produce< std::string >(
repeat( 1u, N, vchar_symbol_p() >> to_container() )
) >> &other_ranges_specifier_t::range_set
)
);
}
} /* namespace range_details */
//
// range_value_t
//
template< typename T >
struct range_value_t
{
using double_ended_range_t = range_details::double_ended_range_t<T>;
using open_ended_range_t = range_details::open_ended_range_t<T>;
using suffix_length_t = range_details::suffix_length_t<T>;
using byte_range_spec_t = range_details::byte_range_spec_t<T>;
using byte_ranges_specifier_t = range_details::byte_ranges_specifier_t<T>;
using other_ranges_specifier_t = range_details::other_ranges_specifier_t;
using value_t = range_details::value_t<T>;
value_t value;
RESTINIO_NODISCARD
static auto
make_parser()
{
using namespace range_details;
return produce< range_value_t >(
alternatives(
make_byte_ranges_specifier_parser<T>() >>
&range_value_t::value,
make_other_ranges_specifier_parser() >>
&range_value_t::value
)
);
}
RESTINIO_NODISCARD
static expected_t< range_value_t, restinio::easy_parser::parse_error_t >
try_parse( string_view_t what )
{
return restinio::easy_parser::try_parse( what, make_parser() );
}
};
} /* namespace http_field_parsers */
} /* namespace restinio */
basics.hpp
Utilities for parsing values of http-fields.
nonstd::variants::variant
Definition: variant.hpp:1301
RESTINIO_NODISCARD
#define RESTINIO_NODISCARD
Definition: compiler_features.hpp:33
restinio::easy_parser::force_only_this_alternative
RESTINIO_NODISCARD auto force_only_this_alternative(Clauses &&... clauses)
An alternative that should be parsed correctly or the parsing of the whole alternatives clause should...
Definition: easy_parser.hpp:3821
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::produce
RESTINIO_NODISCARD auto produce(Clauses &&... clauses)
A factory function to create a producer that creates an instance of the target type by using specifie...
Definition: easy_parser.hpp:3554
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::exact
RESTINIO_NODISCARD auto exact(string_view_t fragment)
A factory function that creates an instance of exact_fragment clause.
Definition: easy_parser.hpp:4833
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::alternatives
RESTINIO_NODISCARD auto alternatives(Clauses &&... clauses)
A factory function to create an alternatives clause.
Definition: easy_parser.hpp:3595
restinio::http_field_parsers::range_details::make_byte_range_spec_parser
RESTINIO_NODISCARD auto make_byte_range_spec_parser()
Factory for creation of a parser for byte_range_spec values.
Definition: range.hpp:152
restinio::http_field_parsers::range_details::make_byte_ranges_specifier_parser
RESTINIO_NODISCARD auto make_byte_ranges_specifier_parser()
Factory for creation of a parser for byte_ranges_specifier values.
Definition: range.hpp:211
restinio::http_field_parsers::range_details::make_other_ranges_specifier_parser
RESTINIO_NODISCARD auto make_other_ranges_specifier_parser()
Factory for creation of a parser for other_ranges_specifier values.
Definition: range.hpp:239
restinio::http_field_parsers::range_details::make_bytes_prefix_parser
RESTINIO_NODISCARD auto make_bytes_prefix_parser()
Factory for a parser of 'bytes=' prefix.
Definition: range.hpp:184
restinio::http_field_parsers::non_empty_comma_separated_list_p
RESTINIO_NODISCARD auto non_empty_comma_separated_list_p(Element_Producer element)
A factory for a producer that handles non-empty list of comma-separated values.
Definition: basics.hpp:1457
restinio::http_field_parsers::vchar_symbol_p
RESTINIO_NODISCARD auto vchar_symbol_p()
A factory for producer of VCHAR symbols.
Definition: basics.hpp:832
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
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::range_details::byte_ranges_specifier_t
A struct that holds a container of byte_range_specs.
Definition: range.hpp:97
restinio::http_field_parsers::range_details::byte_ranges_specifier_t::ranges
std::vector< byte_range_spec_t< T > > ranges
Definition: range.hpp:98
restinio::http_field_parsers::range_details::double_ended_range_t
Value of range for the case where both ends of the range are defined.
Definition: range.hpp:40
restinio::http_field_parsers::range_details::double_ended_range_t::last
T last
Definition: range.hpp:42
restinio::http_field_parsers::range_details::double_ended_range_t::first
T first
Definition: range.hpp:41
restinio::http_field_parsers::range_details::open_ended_range_t
Value of range for the case where only left border of the range is defined.
Definition: range.hpp:58
restinio::http_field_parsers::range_details::open_ended_range_t::first
T first
Definition: range.hpp:59
restinio::http_field_parsers::range_details::other_ranges_specifier_t
A description of a range value of units those are not "bytes".
Definition: range.hpp:115
restinio::http_field_parsers::range_details::other_ranges_specifier_t::range_set
std::string range_set
Definition: range.hpp:117
restinio::http_field_parsers::range_details::other_ranges_specifier_t::range_unit
std::string range_unit
Definition: range.hpp:116
restinio::http_field_parsers::range_details::suffix_length_t
Value of range for the case where only length of range's suffix is defined.
Definition: range.hpp:75
restinio::http_field_parsers::range_details::suffix_length_t::length
T length
Definition: range.hpp:76
restinio::http_field_parsers::range_value_t
Tools for working with the value of Range HTTP-field.
Definition: range.hpp:287
restinio::http_field_parsers::range_value_t::value
value_t value
Definition: range.hpp:444
restinio::http_field_parsers::range_value_t::try_parse
static RESTINIO_NODISCARD expected_t< range_value_t, restinio::easy_parser::parse_error_t > try_parse(string_view_t what)
An attempt to parse Range HTTP-field.
Definition: range.hpp:474
restinio::http_field_parsers::range_value_t::make_parser
static RESTINIO_NODISCARD auto make_parser()
A factory function for a parser of Range value.
Definition: range.hpp:453
variant.hpp