RESTinio

host.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/helpers/http_field_parsers/details/pct_encoded_symbols.hpp>
17
18#include <restinio/variant.hpp>
19
20 namespace restinio
21{
22
23 namespace http_field_parsers
24{
25
26 namespace host_details
27{
28
29 namespace ep_impl = restinio::easy_parser::impl;
30 namespace hfp_impl = restinio::http_field_parsers::impl;
31 namespace hfp_details = restinio::http_field_parsers::details;
32
33//
34// unreserved_predicate_t
35//
44 struct unreserved_predicate_t
45{
46 RESTINIO_NODISCARD
47 bool
48 operator()( const char actual ) const noexcept
49 {
50 return hfp_impl::is_alpha(actual)
51 || hfp_impl::is_digit(actual)
52 || '-' == actual
53 || '.' == actual
54 || '_' == actual
55 || '~' == actual
56 ;
57 }
58};
59
60//
61// unreserved_symbol_producer
62//
70 RESTINIO_NODISCARD
71 inline auto
72 unreserved_symbol_p()
73{
74 return ep_impl::symbol_producer_template_t< unreserved_predicate_t >{};
75}
76
77//
78// sub_delims_predicate_t
79//
88 struct sub_delims_predicate_t
89{
90 RESTINIO_NODISCARD
91 bool
92 operator()( const char actual ) const noexcept
93 {
94 return '!' == actual
95 || '$' == actual
96 || '&' == actual
97 || '\'' == actual
98 || '(' == actual
99 || ')' == actual
100 || '*' == actual
101 || '+' == actual
102 || ',' == actual
103 || ';' == actual
104 || '=' == actual
105 ;
106 }
107};
108
109//
110// sub_delims_symbol_producer
111//
119 RESTINIO_NODISCARD
120 inline auto
121 sub_delims_symbol_p()
122{
123 return ep_impl::symbol_producer_template_t< sub_delims_predicate_t >{};
124}
125
126//
127// ipv4_address_producer
128//
147 RESTINIO_NODISCARD
148 inline auto
149 ipv4_address_p()
150{
151 const auto dec_octet = produce< std::string >(
152 alternatives(
153 sequence(
154 symbol_p('2') >> to_container(),
155 symbol_p('5') >> to_container(),
156 symbol_from_range_p('0', '5') >> to_container()
157 ),
158 sequence(
159 symbol_p('2') >> to_container(),
160 symbol_from_range_p('0', '4') >> to_container(),
161 digit_p() >> to_container()
162 ),
163 sequence(
164 symbol_p('1') >> to_container(),
165 digit_p() >> to_container(),
166 digit_p() >> to_container()
167 ),
168 sequence(
169 symbol_from_range_p('1', '9') >> to_container(),
170 digit_p() >> to_container()
171 ),
172 digit_p() >> to_container()
173 )
174 );
175
176 return produce< std::string >(
177 dec_octet >> to_container(),
178 symbol_p('.') >> to_container(),
179 dec_octet >> to_container(),
180 symbol_p('.') >> to_container(),
181 dec_octet >> to_container(),
182 symbol_p('.') >> to_container(),
183 dec_octet >> to_container()
184 );
185}
186
187//FIXME: maybe this should be a part of easy_parser?
188#if 0
189 struct debug_printer : public ep_impl::clause_tag
190{
191 std::string m_tag;
192
193 debug_printer( std::string v ) noexcept : m_tag{ std::move(v) } {}
194
195 template< typename Target_Type >
196 RESTINIO_NODISCARD
197 optional_t< parse_error_t >
198 try_process( ep_impl::source_t & from, Target_Type & /*target*/ )
199 {
200 std::cout << "*** debug_print: " << m_tag << std::endl;
201
202 return nullopt;
203 }
204};
205#endif
206
207//
208// ipv6_address_producer
209//
233 RESTINIO_NODISCARD
234 inline auto
235 ipv6_address_p()
236{
237 const auto h16 = produce< std::string >(
238 repeat( 1u, 4u, hexdigit_p() >> to_container() )
239 );
240 const auto h16_with_colon = sequence(
241 h16 >> to_container(),
242 symbol_p(':') >> to_container(),
243 not_clause( symbol(':') )
244 );
245 const auto ls32 = produce< std::string >(
246 alternatives(
247 sequence(
248 h16 >> to_container(),
249 symbol_p(':') >> to_container(),
250 h16 >> to_container()
251 ),
252 ipv4_address_p() >> as_result()
253 )
254 );
255 const auto double_colon =
256 exact_p( "::" ) >> just( std::string{ "::" } ) >> to_container()
257 ;
258
259 return produce< std::string >(
260 alternatives(
261 sequence(
262 repeat( 6u, 6u, h16_with_colon ),
263 ls32 >> to_container()
264 ),
265 sequence(
266 double_colon,
267 repeat( 5u, 5u, h16_with_colon ),
268 ls32 >> to_container()
269 ),
270 sequence(
271 maybe( h16 >> to_container() ),
272 double_colon,
273 repeat( 4u, 4u, h16_with_colon ),
274 ls32 >> to_container()
275 ),
276 sequence(
277 maybe(
278 repeat( 0u, 1u, h16_with_colon ),
279 h16 >> to_container()
280 ),
281 double_colon,
282 repeat( 3u, 3u, h16_with_colon ),
283 ls32 >> to_container()
284 ),
285 sequence(
286 maybe(
287 repeat( 0u, 2u, h16_with_colon ),
288 h16 >> to_container()
289 ),
290 double_colon,
291 repeat( 2u, 2u, h16_with_colon ),
292 ls32 >> to_container()
293 ),
294 sequence(
295 maybe(
296 repeat( 0u, 3u, h16_with_colon ),
297 h16 >> to_container()
298 ),
299 double_colon,
300 h16_with_colon,
301 ls32 >> to_container()
302 ),
303 sequence(
304 maybe(
305 repeat( 0u, 4u, h16_with_colon ),
306 h16 >> to_container()
307 ),
308 double_colon,
309 ls32 >> to_container()
310 ),
311 sequence(
312 maybe(
313 repeat( 0u, 5u, h16_with_colon ),
314 h16 >> to_container()
315 ),
316 double_colon,
317 h16 >> to_container()
318 ),
319 sequence(
320 maybe(
321 repeat( 0u, 6u, h16_with_colon ),
322 h16 >> to_container()
323 ),
324 double_colon
325 )
326 )
327 );
328}
329
330//
331// reg_name_producer
332//
351 RESTINIO_NODISCARD
352 inline auto
353 reg_name_p()
354{
355 return produce< std::string >(
356 repeat( 1, N,
357 alternatives(
358 unreserved_symbol_p() >> to_container(),
359 hfp_details::pct_encoded_symbols_p()
360 >> hfp_details::pct_encoded_symbols_consumer_t{},
361 sub_delims_symbol_p() >> to_container()
362 )
363 )
364 );
365}
366
367} /* namespace host_details */
368
369//
370// raw_host_value_t
371//
387 struct raw_host_value_t
388{
389 struct reg_name_t
390 {
391 std::string v;
392
393 reg_name_t() = default;
394 explicit reg_name_t( std::string val ) noexcept : v{ std::move(val) } {}
395
396 friend bool
397 operator==( const reg_name_t & a, const reg_name_t & b ) noexcept
398 {
399 return a.v == b.v;
400 }
401
402 friend bool
403 operator!=( const reg_name_t & a, const reg_name_t & b ) noexcept
404 {
405 return a.v != b.v;
406 }
407
408 friend bool
409 operator<( const reg_name_t & a, const reg_name_t & b ) noexcept
410 {
411 return a.v < b.v;
412 }
413
414 RESTINIO_NODISCARD
415 static reg_name_t
416 from_string( std::string v ) noexcept
417 {
418 return reg_name_t{ std::move(v) };
419 }
420 };
421
422 struct ipv4_address_t
423 {
424 std::string v;
425
426 ipv4_address_t() = default;
427 explicit ipv4_address_t( std::string val ) noexcept : v{ std::move(val) } {}
428
429 friend bool
430 operator==( const ipv4_address_t & a, const ipv4_address_t & b ) noexcept
431 {
432 return a.v == b.v;
433 }
434
435 friend bool
436 operator!=( const ipv4_address_t & a, const ipv4_address_t & b ) noexcept
437 {
438 return a.v != b.v;
439 }
440
441 friend bool
442 operator<( const ipv4_address_t & a, const ipv4_address_t & b ) noexcept
443 {
444 return a.v < b.v;
445 }
446
447 RESTINIO_NODISCARD
448 static ipv4_address_t
449 from_string( std::string v ) noexcept
450 {
451 return ipv4_address_t{ std::move(v) };
452 }
453 };
454
455 struct ipv6_address_t
456 {
457 std::string v;
458
459 ipv6_address_t() = default;
460 explicit ipv6_address_t( std::string val ) noexcept : v{ std::move(val) } {}
461
462 friend bool
463 operator==( const ipv6_address_t & a, const ipv6_address_t & b ) noexcept
464 {
465 return a.v == b.v;
466 }
467
468 friend bool
469 operator!=( const ipv6_address_t & a, const ipv6_address_t & b ) noexcept
470 {
471 return a.v != b.v;
472 }
473
474 friend bool
475 operator<( const ipv6_address_t & a, const ipv6_address_t & b ) noexcept
476 {
477 return a.v < b.v;
478 }
479
480 RESTINIO_NODISCARD
481 static ipv6_address_t
482 from_string( std::string v ) noexcept
483 {
484 return ipv6_address_t{ std::move(v) };
485 }
486 };
487
488 using host_value_t = variant_t< reg_name_t, ipv4_address_t, ipv6_address_t >;
489
490 host_value_t host;
491
493
496 optional_t<std::uint16_t> port;
497
503 RESTINIO_NODISCARD
504 static auto
505 make_parser()
506 {
507 using namespace host_details;
508
509 return produce< raw_host_value_t >(
510 produce< host_value_t >(
511 alternatives(
512
513 produce< ipv6_address_t >(
514 symbol('['),
515 ipv6_address_p()
516 >> to_lower()
517 >> convert( ipv6_address_t::from_string )
518 >> as_result(),
519 symbol(']')
520 ) >> as_result(),
521
522 produce< ipv4_address_t >(
523 ipv4_address_p()
524 >> convert( ipv4_address_t::from_string )
525 >> as_result()
526 ) >> as_result(),
527
528 produce< reg_name_t >(
529 reg_name_p() >> to_lower()
530 >> convert( reg_name_t::from_string )
531 >> as_result()
532 ) >> as_result()
533 )
534 ) >> &raw_host_value_t::host,
535 maybe(
536 symbol(':'),
537 non_negative_decimal_number_p< std::uint16_t >()
538 >> &raw_host_value_t::port
539 )
540 );
541 }
542
548 RESTINIO_NODISCARD
549 static expected_t< raw_host_value_t, restinio::easy_parser::parse_error_t >
550 try_parse( string_view_t what )
551 {
552 return restinio::easy_parser::try_parse( what, make_parser() );
553 }
554};
555
556 inline std::ostream &
557 operator<<( std::ostream & to, const raw_host_value_t & rhv )
558{
559 struct host_dumper_t
560 {
561 std::ostream & m_to;
562
563 void operator()( const raw_host_value_t::reg_name_t & n ) const
564 {
565 m_to << n.v;
566 }
567
568 void operator()( const raw_host_value_t::ipv4_address_t & n ) const
569 {
570 m_to << n.v;
571 }
572
573 void operator()( const raw_host_value_t::ipv6_address_t & n ) const
574 {
575 m_to << '[' << n.v << ']';
576 }
577 };
578
579 visit( host_dumper_t{ to }, rhv.host );
580
581 if( rhv.port )
582 to << ':' << *(rhv.port) << std::endl;
583
584 return to;
585}
586
587} /* namespace http_field_parsers */
588
589} /* namespace restinio */
590
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