RESTinio

range.hpp
Go to the documentation of this file.
1/*
2 * RESTinio
3 */
4
12#pragma once
13
14#include <restinio/helpers/http_field_parsers/basics.hpp>
15
16#include <restinio/variant.hpp>
17
18 namespace restinio
19{
20
21 namespace http_field_parsers
22{
23
24 namespace range_details
25{
26
38 template< typename T >
39 struct double_ended_range_t
40{
41 T first;
42 T last;
43};
44
56 template< typename T >
57 struct open_ended_range_t
58{
59 T first;
60};
61
73 template< typename T >
74 struct suffix_length_t
75{
76 T length;
77};
78
84 template< typename T >
85 using byte_range_spec_t = variant_t<
86 double_ended_range_t<T>,
87 open_ended_range_t<T>,
88 suffix_length_t<T> >;
89
95 template< typename T >
96 struct byte_ranges_specifier_t
97{
98 std::vector< byte_range_spec_t<T> > ranges;
99};
100
114 struct other_ranges_specifier_t
115{
116 std::string range_unit;
117 std::string range_set;
118};
119
125 template< typename T >
126 using value_t = variant_t<
127 byte_ranges_specifier_t<T>,
128 other_ranges_specifier_t >;
129
149 template< typename T >
150 RESTINIO_NODISCARD
151 auto
152 make_byte_range_spec_parser()
153{
154 return produce< byte_range_spec_t<T> >(
155 alternatives(
156 produce< double_ended_range_t<T> >(
157 non_negative_decimal_number_p<T>()
158 >> &double_ended_range_t<T>::first,
159 symbol('-'),
160 non_negative_decimal_number_p<T>()
161 >> &double_ended_range_t<T>::last
162 ) >> as_result(),
163 produce< open_ended_range_t<T> >(
164 non_negative_decimal_number_p<T>()
165 >> &open_ended_range_t<T>::first,
166 symbol('-')
167 ) >> as_result(),
168 produce< suffix_length_t<T> >(
169 symbol('-'),
170 non_negative_decimal_number_p<T>()
171 >> &suffix_length_t<T>::length
172 ) >> as_result()
173 )
174 );
175}
176
182 RESTINIO_NODISCARD
183 inline auto
184 make_bytes_prefix_parser()
185{
186 return sequence( exact( "bytes" ), symbol('=') );
187}
188
208 template< typename T >
209 RESTINIO_NODISCARD
210 auto
211 make_byte_ranges_specifier_parser()
212{
213 return produce< byte_ranges_specifier_t<T> >(
214 make_bytes_prefix_parser(),
215 force_only_this_alternative(
216 non_empty_comma_separated_list_p<
217 std::vector< byte_range_spec_t<T> > >(
218 make_byte_range_spec_parser<T>()
219 ) >> &byte_ranges_specifier_t<T>::ranges
220 )
221 );
222}
223
237 RESTINIO_NODISCARD
238 inline auto
239 make_other_ranges_specifier_parser()
240{
241 return produce< other_ranges_specifier_t >(
242 token_p() >> &other_ranges_specifier_t::range_unit,
243 symbol('='),
244 force_only_this_alternative(
245 produce< std::string >(
246 repeat( 1u, N, vchar_symbol_p() >> to_container() )
247 ) >> &other_ranges_specifier_t::range_set
248 )
249 );
250}
251
252} /* namespace range_details */
253
254//
255// range_value_t
256//
285 template< typename T >
286 struct range_value_t
287{
318 using double_ended_range_t = range_details::double_ended_range_t<T>;
319
349 using open_ended_range_t = range_details::open_ended_range_t<T>;
350
381 using suffix_length_t = range_details::suffix_length_t<T>;
382
388 using byte_range_spec_t = range_details::byte_range_spec_t<T>;
389
420 using byte_ranges_specifier_t = range_details::byte_ranges_specifier_t<T>;
421
435 using other_ranges_specifier_t = range_details::other_ranges_specifier_t;
436
442 using value_t = range_details::value_t<T>;
443
444 value_t value;
445
451 RESTINIO_NODISCARD
452 static auto
453 make_parser()
454 {
455 using namespace range_details;
456
457 return produce< range_value_t >(
458 alternatives(
459 make_byte_ranges_specifier_parser<T>() >>
460 &range_value_t::value,
461 make_other_ranges_specifier_parser() >>
462 &range_value_t::value
463 )
464 );
465 }
466
472 RESTINIO_NODISCARD
473 static expected_t< range_value_t, restinio::easy_parser::parse_error_t >
474 try_parse( string_view_t what )
475 {
476 return restinio::easy_parser::try_parse( what, make_parser() );
477 }
478};
479
480} /* namespace http_field_parsers */
481
482} /* namespace restinio */
483
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