RESTinio
host.hpp
Go to the documentation of this file.
1/*
2 * RESTinio
3 */
4
12#pragma once
13
15
17
18#include <restinio/variant.hpp>
19
20namespace restinio
21{
22
23namespace http_field_parsers
24{
25
26namespace host_details
27{
28
32
33//
34// unreserved_predicate_t
35//
45{
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//
71inline auto
73{
75}
76
77//
78// sub_delims_predicate_t
79//
89{
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//
120inline auto
122{
124}
125
126//
127// ipv4_address_producer
128//
148inline auto
150{
151 const auto dec_octet = produce< std::string >(
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
189struct 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 >
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//
234inline auto
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 >(
247 sequence(
248 h16 >> to_container(),
249 symbol_p(':') >> to_container(),
250 h16 >> to_container()
251 ),
253 )
254 );
255 const auto double_colon =
256 exact_p( "::" ) >> just( std::string{ "::" } ) >> to_container()
257 ;
258
259 return produce< std::string >(
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//
352inline auto
354{
355 return produce< std::string >(
356 repeat( 1, N,
362 )
363 )
364 );
365}
366
367} /* namespace host_details */
368
369//
370// raw_host_value_t
371//
388{
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
415 static reg_name_t
416 from_string( std::string v ) noexcept
417 {
418 return reg_name_t{ std::move(v) };
419 }
420 };
421
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
448 static ipv4_address_t
449 from_string( std::string v ) noexcept
450 {
451 return ipv4_address_t{ std::move(v) };
452 }
453 };
454
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
481 static ipv6_address_t
482 from_string( std::string v ) noexcept
483 {
484 return ipv6_address_t{ std::move(v) };
485 }
486 };
487
489
491
493
497
504 static auto
506 {
507 using namespace host_details;
508
509 return produce< raw_host_value_t >(
510 produce< host_value_t >(
512
513 produce< ipv6_address_t >(
514 symbol('['),
516 >> to_lower()
518 >> as_result(),
519 symbol(']')
520 ) >> as_result(),
521
522 produce< ipv4_address_t >(
525 >> as_result()
526 ) >> as_result(),
527
528 produce< reg_name_t >(
529 reg_name_p() >> to_lower()
531 >> as_result()
532 ) >> as_result()
533 )
535 maybe(
536 symbol(':'),
537 non_negative_decimal_number_p< std::uint16_t >()
539 )
540 );
541 }
542
551 {
553 }
554};
555
556inline std::ostream &
557operator<<( 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
Utilities for parsing values of http-fields.
The class that implements "input stream".
A template for producer of charachers that satisfy some predicate.
#define RESTINIO_NODISCARD
const nullopt_t nullopt((nullopt_t::init()))
R visit(const Visitor &v, V1 const &arg1)
Definition: variant.hpp:2532
RESTINIO_NODISCARD constexpr bool is_digit(const char ch) noexcept
Is a character a decimal digit?
RESTINIO_NODISCARD auto symbol_from_range_p(char left, char right) noexcept
A factory function to create a symbol_from_range_producer.
RESTINIO_NODISCARD auto maybe(Clauses &&... clauses)
A factory function to create an optional clause.
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...
RESTINIO_NODISCARD auto sequence(Clauses &&... clauses)
A factory function to create a sequence of subclauses.
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.
RESTINIO_NODISCARD auto repeat(std::size_t min_occurences, std::size_t max_occurences, Clauses &&... clauses)
A factory function to create repetitor of subclauses.
RESTINIO_NODISCARD auto digit_p() noexcept
A factory function to create a digit_producer.
RESTINIO_NODISCARD auto exact_p(string_view_t fragment)
A factory function that creates an instance of exact_fragment_producer.
RESTINIO_NODISCARD auto as_result() noexcept
A factory function to create a as_result_consumer.
constexpr std::size_t N
A special marker that means infinite repetitions.
RESTINIO_NODISCARD auto hexdigit_p() noexcept
A factory function to create a hexdigit_producer.
RESTINIO_NODISCARD auto to_container()
A factory function to create a to_container_consumer.
RESTINIO_NODISCARD auto symbol_p(char expected) noexcept
A factory function to create a symbol_producer.
RESTINIO_NODISCARD auto not_clause(Clauses &&... clauses)
A factory function to create a not_clause.
RESTINIO_NODISCARD auto to_lower() noexcept
A factory function to create a to_lower_transformer.
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.
RESTINIO_NODISCARD auto alternatives(Clauses &&... clauses)
A factory function to create an alternatives clause.
RESTINIO_NODISCARD auto convert(Converter &&converter)
A factory function to create convert_transformer.
RESTINIO_NODISCARD auto pct_encoded_symbols_p()
A producer that extract a sequence of symbols represented a percent-encoded character.
RESTINIO_NODISCARD auto reg_name_p()
A factory for producer of reg-name value.
Definition: host.hpp:353
RESTINIO_NODISCARD auto ipv6_address_p()
A factory for producer of ipv6_address value.
Definition: host.hpp:235
RESTINIO_NODISCARD auto sub_delims_symbol_p()
A factory for producer that extracts sub-delims symbols.
Definition: host.hpp:121
RESTINIO_NODISCARD auto unreserved_symbol_p()
A factory for producer that extracts unreserved symbols.
Definition: host.hpp:72
RESTINIO_NODISCARD auto ipv4_address_p()
A factory for producer of IPv4address value.
Definition: host.hpp:149
RESTINIO_NODISCARD constexpr bool is_alpha(const char ch) noexcept
Is a character an ALPHA?
Definition: basics.hpp:263
std::ostream & operator<<(std::ostream &to, const authorization_value_t::param_value_t &v)
nonstd::string_view string_view_t
Definition: string_view.hpp:19
nonstd::expected< T, E > expected_t
Definition: expected.hpp:22
Stuff related to percent-encoded symbols.
A special base class to be used with clauses.
A special consumer that inserts an extracted sequence of symbols into the result string.
A preducate for symbol_producer_template that checks that a symbol is sub-delims symbol from RCF3986.
Definition: host.hpp:89
RESTINIO_NODISCARD bool operator()(const char actual) const noexcept
Definition: host.hpp:92
A preducate for symbol_producer_template that checks that a symbol is unreserved symbol from RCF3986.
Definition: host.hpp:45
RESTINIO_NODISCARD bool operator()(const char actual) const noexcept
Definition: host.hpp:48
friend bool operator==(const ipv4_address_t &a, const ipv4_address_t &b) noexcept
Definition: host.hpp:430
friend bool operator!=(const ipv4_address_t &a, const ipv4_address_t &b) noexcept
Definition: host.hpp:436
static RESTINIO_NODISCARD ipv4_address_t from_string(std::string v) noexcept
Definition: host.hpp:449
friend bool operator<(const ipv4_address_t &a, const ipv4_address_t &b) noexcept
Definition: host.hpp:442
friend bool operator!=(const ipv6_address_t &a, const ipv6_address_t &b) noexcept
Definition: host.hpp:469
friend bool operator==(const ipv6_address_t &a, const ipv6_address_t &b) noexcept
Definition: host.hpp:463
friend bool operator<(const ipv6_address_t &a, const ipv6_address_t &b) noexcept
Definition: host.hpp:475
static RESTINIO_NODISCARD ipv6_address_t from_string(std::string v) noexcept
Definition: host.hpp:482
friend bool operator<(const reg_name_t &a, const reg_name_t &b) noexcept
Definition: host.hpp:409
static RESTINIO_NODISCARD reg_name_t from_string(std::string v) noexcept
Definition: host.hpp:416
friend bool operator!=(const reg_name_t &a, const reg_name_t &b) noexcept
Definition: host.hpp:403
friend bool operator==(const reg_name_t &a, const reg_name_t &b) noexcept
Definition: host.hpp:397
Tools for working with the raw value of Host HTTP-field.
Definition: host.hpp:388
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
optional_t< std::uint16_t > port
Optional port value.
Definition: host.hpp:496
static RESTINIO_NODISCARD auto make_parser()
A factory function for a parser of Host value.
Definition: host.hpp:505