RESTinio

express.hpp
Go to the documentation of this file.
1/*
2 restinio
3*/
4
9#pragma once
10
11#include <restinio/router/impl/target_path_holder.hpp>
12#include <restinio/router/non_matched_request_handler.hpp>
13
14#include <restinio/optional.hpp>
15
16#include <restinio/path2regex/path2regex.hpp>
17
18#include <restinio/router/std_regex_engine.hpp>
19#include <restinio/router/method_matcher.hpp>
20
21#include <restinio/utils/from_string.hpp>
22#include <restinio/utils/percent_encoding.hpp>
23
24#include <map>
25#include <vector>
26
27 namespace restinio
28{
29
30 namespace router
31{
32
33 namespace impl
34{
35
36 struct route_params_accessor_t;
37
38} /* namespace impl */
39
40//
41// route_params_t
42//
43
45
57 class route_params_t final
58{
59 public:
60 using named_parameters_container_t =
61 std::vector< std::pair< string_view_t, string_view_t > >;
62 using indexed_parameters_container_t =
63 std::vector< string_view_t >;
64
65 private:
66 friend struct impl::route_params_accessor_t;
67
68 void
69 match(
70 std::unique_ptr< char[] > request_target,
71 std::shared_ptr< std::string > key_names_buffer,
72 string_view_t match,
73 named_parameters_container_t named_parameters,
74 indexed_parameters_container_t indexed_parameters )
75 {
76 m_request_target = std::move( request_target );
77 m_key_names_buffer = std::move( key_names_buffer );
78 m_match = match;
79 m_named_parameters = std::move( named_parameters );
80 m_indexed_parameters = std::move( indexed_parameters );
81 }
82
83 public:
84 route_params_t() = default;
85
86 route_params_t( route_params_t && ) = default;
87 route_params_t & operator = ( route_params_t && ) = default;
88
89 route_params_t( const route_params_t & ) = delete;
90 route_params_t & operator = ( const route_params_t & ) = delete;
91
93 string_view_t match() const noexcept { return m_match; }
94
96 string_view_t
97 operator [] ( string_view_t key ) const
98 {
99 return find_named_parameter_with_check( key ).second;
100 }
101
103 bool
104 has( string_view_t key ) const noexcept
105 {
106 return m_named_parameters.end() != find_named_parameter( key );
107 }
108
111 optional_t< string_view_t >
112 get_param( string_view_t key ) const noexcept
113 {
114 const auto it = find_named_parameter( key );
115
116 return m_named_parameters.end() != it ?
117 optional_t< string_view_t >{ it->second } :
118 optional_t< string_view_t >{ nullopt };
119 }
120
122 string_view_t
123 operator [] ( std::size_t i ) const
124 {
125 if( i >= m_indexed_parameters.size() )
126 throw exception_t{ fmt::format( "invalid parameter index: {}", i ) };
127
128 return m_indexed_parameters.at( i );
129 }
130
133 auto named_parameters_size() const noexcept { return m_named_parameters.size(); }
134 auto indexed_parameters_size() const noexcept { return m_indexed_parameters.size(); }
136
137 private:
138 named_parameters_container_t::const_iterator
139 find_named_parameter( string_view_t key ) const noexcept
140 {
141 return
142 std::find_if(
143 m_named_parameters.begin(),
144 m_named_parameters.end(),
145 [&]( const auto p ){
146 return key == p.first;
147 } );
148 }
149
150 named_parameters_container_t::const_reference
151 find_named_parameter_with_check( string_view_t key ) const
152 {
153 auto it = find_named_parameter( key );
154
155 if( m_named_parameters.end() == it )
156 throw exception_t{
157 fmt::format(
158 "invalid parameter name: {}",
159 std::string{ key.data(), key.size() } ) };
160
161 return *it;
162 }
163
165
176 std::unique_ptr< char[] > m_request_target;
177
179 std::shared_ptr< std::string > m_key_names_buffer;
180
182 string_view_t m_match;
183
185 named_parameters_container_t m_named_parameters;
186
188 indexed_parameters_container_t m_indexed_parameters;
189};
190
191 namespace impl
192{
193
194//
195// route_params_accessor_t
196//
197
199 struct route_params_accessor_t
200{
202 static void
203 match(
204 route_params_t & rp,
205 std::unique_ptr< char[] > request_target,
206 std::shared_ptr< std::string > key_names_buffer,
207 string_view_t match_,
208 route_params_t::named_parameters_container_t named_parameters,
209 route_params_t::indexed_parameters_container_t indexed_parameters )
210 {
211 rp.match(
212 std::move( request_target ),
213 std::move( key_names_buffer ),
214 match_,
215 std::move( named_parameters ),
216 std::move( indexed_parameters ) );
217 }
218
221 static const auto &
222 named_parameters( const route_params_t & rp ) noexcept
223 {
224 return rp.m_named_parameters;
225 }
226
227 static const auto &
228 indexed_parameters( const route_params_t & rp ) noexcept
229 {
230 return rp.m_indexed_parameters;
231 }
233};
234
235//
236// route_params_appender_t
237//
238
240 class route_params_appender_t
241{
242 public:
243 route_params_appender_t(
244 route_params_t::named_parameters_container_t & named_parameters,
245 route_params_t::indexed_parameters_container_t & indexed_parameters )
246 : m_named_parameters{ named_parameters }
247 , m_indexed_parameters{ indexed_parameters }
248 {}
249
250 route_params_appender_t( const route_params_appender_t & ) = delete;
251 route_params_appender_t( route_params_appender_t && ) = delete;
252 route_params_appender_t & operator = ( const route_params_appender_t & ) = delete;
253 route_params_appender_t & operator = ( route_params_appender_t && ) = delete;
254
255 void
256 add_named_param( string_view_t key, string_view_t value )
257 {
258 m_named_parameters.emplace_back( key, value );
259 }
260
261 void
262 add_indexed_param( string_view_t value )
263 {
264 m_indexed_parameters.emplace_back( value );
265 }
266
267 private:
268 route_params_t::named_parameters_container_t & m_named_parameters;
269 route_params_t::indexed_parameters_container_t & m_indexed_parameters;
270};
271
272 using param_appender_sequence_t =
273 path2regex::param_appender_sequence_t< route_params_appender_t >;
274
275//
276// route_matcher_t
277//
278
280 template < typename Regex_Engine = std_regex_engine_t >
281 class route_matcher_t
282{
283 public:
284 using regex_t = typename Regex_Engine::compiled_regex_t;
285 using match_results_t = typename Regex_Engine::match_results_t;
286
288 route_matcher_t(
289 http_method_id_t method,
290 regex_t route_regex,
291 std::shared_ptr< std::string > named_params_buffer,
292 param_appender_sequence_t param_appender_sequence )
293 : m_route_regex{ std::move( route_regex ) }
294 , m_named_params_buffer{ std::move( named_params_buffer ) }
295 , m_param_appender_sequence{ std::move( param_appender_sequence ) }
296 {
297 assign( m_method_matcher, std::move(method) );
298 }
299
308 template< typename Method_Matcher >
309 route_matcher_t(
310 Method_Matcher && method_matcher,
311 regex_t route_regex,
312 std::shared_ptr< std::string > named_params_buffer,
313 param_appender_sequence_t param_appender_sequence )
314 : m_route_regex{ std::move( route_regex ) }
315 , m_named_params_buffer{ std::move( named_params_buffer ) }
316 , m_param_appender_sequence{ std::move( param_appender_sequence ) }
317 {
318 assign(
319 m_method_matcher,
320 std::forward<Method_Matcher>(method_matcher) );
321 }
322
323 route_matcher_t() = default;
324 route_matcher_t( route_matcher_t && ) = default;
325
327 bool
328 match_route(
329 target_path_holder_t & target_path,
330 route_params_t & parameters ) const
331 {
332 match_results_t matches;
333 if( Regex_Engine::try_match(
334 target_path.view(),
335 m_route_regex,
336 matches ) )
337 {
338 assert( m_param_appender_sequence.size() + 1 >= matches.size() );
339
340 // Data for route_params_t initialization.
341
342 auto captured_params = target_path.giveout_data();
343
344 const string_view_t match{
345 captured_params.get() + Regex_Engine::submatch_begin_pos( matches[0] ),
346 Regex_Engine::submatch_end_pos( matches[0] ) -
347 Regex_Engine::submatch_begin_pos( matches[0] ) } ;
348
349 route_params_t::named_parameters_container_t named_parameters;
350 route_params_t::indexed_parameters_container_t indexed_parameters;
351
352 route_params_appender_t param_appender{ named_parameters, indexed_parameters };
353
354 // Std regex and pcre engines handle
355 // trailing groups with empty values differently.
356 // Std despite they are empty includes them in the list of match results;
357 // Pcre on the other hand does not.
358 // So the second for is for pushing empty values
359 std::size_t i = 1;
360 for( ; i < matches.size(); ++i )
361 {
362 const auto & m = matches[ i ];
363 m_param_appender_sequence[ i - 1](
364 param_appender,
365 string_view_t{
366 captured_params.get() + Regex_Engine::submatch_begin_pos( m ),
367 Regex_Engine::submatch_end_pos( m ) -
368 Regex_Engine::submatch_begin_pos( m ) } );
369 }
370
371 for( ; i < m_param_appender_sequence.size() + 1; ++i )
372 {
373 m_param_appender_sequence[ i - 1 ](
374 param_appender,
375 string_view_t{ captured_params.get(), 0 } );
376 }
377
378 // Init route parameters.
379 route_params_accessor_t::match(
380 parameters,
381 std::move( captured_params ),
382 m_named_params_buffer, // Do not move (it is used on each match).
383 std::move( match ),
384 std::move( named_parameters ),
385 std::move( indexed_parameters ) );
386
387 return true;
388 }
389
390 return false;
391 }
392
393 inline bool
394 operator () (
395 const http_request_header_t & h,
396 target_path_holder_t & target_path,
397 route_params_t & parameters ) const
398 {
399 return m_method_matcher->match( h.method() ) &&
400 match_route( target_path, parameters );
401 }
402
403 private:
405 buffered_matcher_holder_t m_method_matcher;
406
408 regex_t m_route_regex;
409
411 std::shared_ptr< std::string > m_named_params_buffer;
412
414 param_appender_sequence_t m_param_appender_sequence;
415};
416
417} /* namespace impl */
418
419//
420// generic_express_request_handler_t
421//
438 template< typename Extra_Data >
439 using generic_express_request_handler_t = std::function<
440 request_handling_status_t(
441 generic_request_handle_t<Extra_Data>,
442 route_params_t )
443 >;
444
445//
446// express_request_handler_t
447//
456 using express_request_handler_t =
457 generic_express_request_handler_t< no_extra_data_factory_t::data_t >;
458
459//
460// generic_express_route_entry_t
461//
462
464
469 template<
470 typename Regex_Engine,
471 typename Extra_Data_Factory >
472 class generic_express_route_entry_t
473{
474 public:
475 using actual_request_handler_t = generic_express_request_handler_t<
476 typename Extra_Data_Factory::data_t
477 >;
478 using actual_request_handle_t = generic_request_handle_t<
479 typename Extra_Data_Factory::data_t
480 >;
481
482 private:
483 using matcher_init_data_t =
484 path2regex::impl::route_regex_matcher_data_t<
485 impl::route_params_appender_t,
486 Regex_Engine >;
487
488 template< typename Method_Matcher >
489 generic_express_route_entry_t(
490 Method_Matcher && method_matcher,
491 matcher_init_data_t matcher_data,
492 actual_request_handler_t handler )
493 : m_matcher{
494 std::forward<Method_Matcher>( method_matcher ),
495 std::move( matcher_data.m_regex ),
496 std::move( matcher_data.m_named_params_buffer ),
497 std::move( matcher_data.m_param_appender_sequence ) }
498 , m_handler{ std::move( handler ) }
499 {}
500
501 public:
502 generic_express_route_entry_t(
503 const generic_express_route_entry_t & ) = delete;
504 generic_express_route_entry_t & operator=(
505 const generic_express_route_entry_t & ) = delete;
506
507 generic_express_route_entry_t() = default;
508 generic_express_route_entry_t(
509 generic_express_route_entry_t && ) = default;
510
511 generic_express_route_entry_t &
512 operator=( generic_express_route_entry_t && ) = default;
513
514 template< typename Method_Matcher >
515 generic_express_route_entry_t(
516 Method_Matcher && method_matcher,
517 string_view_t route_path,
518 const path2regex::options_t & options,
519 actual_request_handler_t handler )
520 : generic_express_route_entry_t{
521 std::forward<Method_Matcher>( method_matcher ),
522 path2regex::path2regex< impl::route_params_appender_t, Regex_Engine >(
523 route_path,
524 options ),
525 std::move( handler ) }
526 {}
527
528 template< typename Method_Matcher >
529 generic_express_route_entry_t(
530 Method_Matcher && method_matcher,
531 string_view_t route_path,
532 actual_request_handler_t handler )
533 : generic_express_route_entry_t{
534 std::forward<Method_Matcher>( method_matcher ),
535 route_path,
536 path2regex::options_t{},
537 std::move( handler ) }
538 {}
539
542 RESTINIO_NODISCARD
543 bool
544 match(
545 const http_request_header_t & h,
546 impl::target_path_holder_t & target_path,
547 route_params_t & params ) const
548 {
549 return m_matcher( h, target_path, params );
550 }
551
553 RESTINIO_NODISCARD
554 request_handling_status_t
555 handle( actual_request_handle_t rh, route_params_t rp ) const
556 {
557 return m_handler( std::move( rh ), std::move( rp ) );
558 }
559
560 private:
561 impl::route_matcher_t< Regex_Engine > m_matcher;
562 actual_request_handler_t m_handler;
563};
564
565//
566// express_route_entry_t
567//
574 template<
575 typename Regex_Engine = std_regex_engine_t >
576 using express_route_entry_t = generic_express_route_entry_t<
577 Regex_Engine,
578 no_extra_data_factory_t >;
579
580//
581// generic_express_router_t
582//
583
585
608 template<
609 typename Regex_Engine,
610 typename Extra_Data_Factory >
611 class generic_express_router_t
612{
613 public:
614 using actual_request_handle_t =
615 generic_request_handle_t< typename Extra_Data_Factory::data_t >;
616 using actual_request_handler_t =
617 typename generic_express_route_entry_t<
618 Regex_Engine,
619 Extra_Data_Factory
620 >::actual_request_handler_t;
621 using non_matched_handler_t =
622 generic_non_matched_request_handler_t<
623 typename Extra_Data_Factory::data_t
624 >;
625
626 generic_express_router_t() = default;
627 generic_express_router_t( generic_express_router_t && ) = default;
628
629 RESTINIO_NODISCARD
630 request_handling_status_t
631 operator()( actual_request_handle_t req ) const
632 {
633 impl::target_path_holder_t target_path{ req->header().path() };
634 route_params_t params;
635 for( const auto & entry : m_handlers )
636 {
637 if( entry.match( req->header(), target_path, params ) )
638 {
639 return entry.handle( std::move( req ), std::move( params ) );
640 }
641 }
642
643 // Here: none of the routes matches this handler.
644
645 if( m_non_matched_request_handler )
646 {
647 // If non matched request handler is set
648 // then call it.
649 return m_non_matched_request_handler( std::move( req ) );
650 }
651
652 return request_not_handled();
653 }
654
657 template< typename Method_Matcher >
658 void
659 add_handler(
660 Method_Matcher && method_matcher,
661 string_view_t route_path,
662 actual_request_handler_t handler )
663 {
664 add_handler(
665 std::forward<Method_Matcher>(method_matcher),
666 route_path,
667 path2regex::options_t{},
668 std::move( handler ) );
669 }
670
671 template< typename Method_Matcher >
672 void
673 add_handler(
674 Method_Matcher && method_matcher,
675 string_view_t route_path,
676 const path2regex::options_t & options,
677 actual_request_handler_t handler )
678 {
679 m_handlers.emplace_back(
680 std::forward<Method_Matcher>(method_matcher),
681 route_path,
682 options,
683 std::move( handler ) );
684 }
685
686 void
687 http_delete(
688 string_view_t route_path,
689 actual_request_handler_t handler )
690 {
691 add_handler(
692 http_method_delete(),
693 route_path,
694 std::move( handler ) );
695 }
696
697 void
698 http_delete(
699 string_view_t route_path,
700 const path2regex::options_t & options,
701 actual_request_handler_t handler )
702 {
703 add_handler(
704 http_method_delete(),
705 route_path,
706 options,
707 std::move( handler ) );
708 }
709
710 void
711 http_get(
712 string_view_t route_path,
713 actual_request_handler_t handler )
714 {
715 add_handler(
716 http_method_get(),
717 route_path,
718 std::move( handler ) );
719 }
720
721 void
722 http_get(
723 string_view_t route_path,
724 const path2regex::options_t & options,
725 actual_request_handler_t handler )
726 {
727 add_handler(
728 http_method_get(),
729 route_path,
730 options,
731 std::move( handler ) );
732 }
733
734 void
735 http_head(
736 string_view_t route_path,
737 actual_request_handler_t handler )
738 {
739 add_handler(
740 http_method_head(),
741 route_path,
742 std::move( handler ) );
743 }
744
745 void
746 http_head(
747 string_view_t route_path,
748 const path2regex::options_t & options,
749 actual_request_handler_t handler )
750 {
751 add_handler(
752 http_method_head(),
753 route_path,
754 options,
755 std::move( handler ) );
756 }
757
758 void
759 http_post(
760 string_view_t route_path,
761 actual_request_handler_t handler )
762 {
763 add_handler(
764 http_method_post(),
765 route_path,
766 std::move( handler ) );
767 }
768
769 void
770 http_post(
771 string_view_t route_path,
772 const path2regex::options_t & options,
773 actual_request_handler_t handler )
774 {
775 add_handler(
776 http_method_post(),
777 route_path,
778 options,
779 std::move( handler ) );
780 }
781
782 void
783 http_put(
784 string_view_t route_path,
785 actual_request_handler_t handler )
786 {
787 add_handler(
788 http_method_put(),
789 route_path,
790 std::move( handler ) );
791 }
792
793 void
794 http_put(
795 string_view_t route_path,
796 const path2regex::options_t & options,
797 actual_request_handler_t handler )
798 {
799 add_handler(
800 http_method_put(),
801 route_path,
802 options,
803 std::move( handler ) );
804 }
806
808 void
809 non_matched_request_handler( non_matched_handler_t nmrh )
810 {
811 m_non_matched_request_handler = std::move( nmrh );
812 }
813
814 private:
815 using route_entry_t = generic_express_route_entry_t<
816 Regex_Engine,
817 Extra_Data_Factory
818 >;
819
821 std::vector< route_entry_t > m_handlers;
822
824 non_matched_handler_t m_non_matched_request_handler;
825};
826
827//
828// express_router_t
829//
839 template<
840 typename Regex_Engine = std_regex_engine_t >
841 using express_router_t = generic_express_router_t<
842 Regex_Engine,
843 no_extra_data_factory_t >;
844
845} /* namespace router */
846
848 template < typename Value_Type >
849Value_Type
850 get( const router::route_params_t & params, string_view_t key )
851{
852 return get< Value_Type >( params[ key ] );
853}
854
856 template < typename Value_Type >
857Value_Type
858 get( const router::route_params_t & params, std::size_t index )
859{
860 return get< Value_Type >( params[ index ] );
861}
862
863} /* namespace restinio */
nonstd::optional_lite::optional
class optional
Definition: optional.hpp:919
restinio::exception_t
Exception class for all exceptions thrown by RESTinio.
Definition: exception.hpp:26
restinio::http_method_id_t
A type for representation of HTTP method ID.
Definition: http_headers.hpp:1744
restinio::path2regex::options_t
Options for matching routes.
Definition: path2regex.hpp:92
restinio::router::generic_express_route_entry_t
A single generic express route entry.
Definition: express.hpp:473
restinio::router::generic_express_route_entry_t::match
RESTINIO_NODISCARD bool match(const http_request_header_t &h, impl::target_path_holder_t &target_path, route_params_t &params) const
Checks if request header matches entry, and if so, set route params.
Definition: express.hpp:544
restinio::router::generic_express_route_entry_t::generic_express_route_entry_t
generic_express_route_entry_t(Method_Matcher &&method_matcher, matcher_init_data_t matcher_data, actual_request_handler_t handler)
Definition: express.hpp:489
restinio::router::generic_express_route_entry_t::actual_request_handler_t
generic_express_request_handler_t< typename Extra_Data_Factory::data_t > actual_request_handler_t
Definition: express.hpp:477
restinio::router::generic_express_route_entry_t::operator=
generic_express_route_entry_t & operator=(const generic_express_route_entry_t &)=delete
restinio::router::generic_express_route_entry_t::handle
RESTINIO_NODISCARD request_handling_status_t handle(actual_request_handle_t rh, route_params_t rp) const
Calls a handler of given request with given params.
Definition: express.hpp:555
restinio::router::generic_express_route_entry_t::generic_express_route_entry_t
generic_express_route_entry_t(Method_Matcher &&method_matcher, string_view_t route_path, const path2regex::options_t &options, actual_request_handler_t handler)
Definition: express.hpp:515
restinio::router::generic_express_route_entry_t::generic_express_route_entry_t
generic_express_route_entry_t(generic_express_route_entry_t &&)=default
restinio::router::generic_express_route_entry_t::actual_request_handle_t
generic_request_handle_t< typename Extra_Data_Factory::data_t > actual_request_handle_t
Definition: express.hpp:480
restinio::router::generic_express_route_entry_t::generic_express_route_entry_t
generic_express_route_entry_t(Method_Matcher &&method_matcher, string_view_t route_path, actual_request_handler_t handler)
Definition: express.hpp:529
restinio::router::generic_express_route_entry_t::operator=
generic_express_route_entry_t & operator=(generic_express_route_entry_t &&)=default
restinio::router::generic_express_route_entry_t::m_handler
actual_request_handler_t m_handler
Definition: express.hpp:562
restinio::router::generic_express_route_entry_t::generic_express_route_entry_t
generic_express_route_entry_t()=default
restinio::router::generic_express_route_entry_t::generic_express_route_entry_t
generic_express_route_entry_t(const generic_express_route_entry_t &)=delete
restinio::router::generic_express_route_entry_t::m_matcher
impl::route_matcher_t< Regex_Engine > m_matcher
Definition: express.hpp:561
restinio::router::generic_express_router_t
Generic Express.js style router.
Definition: express.hpp:612
restinio::router::generic_express_router_t::http_post
void http_post(string_view_t route_path, const path2regex::options_t &options, actual_request_handler_t handler)
Definition: express.hpp:770
restinio::router::generic_express_router_t::http_get
void http_get(string_view_t route_path, actual_request_handler_t handler)
Definition: express.hpp:711
restinio::router::generic_express_router_t::http_put
void http_put(string_view_t route_path, const path2regex::options_t &options, actual_request_handler_t handler)
Definition: express.hpp:794
restinio::router::generic_express_router_t::non_matched_handler_t
generic_non_matched_request_handler_t< typename Extra_Data_Factory::data_t > non_matched_handler_t
Definition: express.hpp:624
restinio::router::generic_express_router_t::non_matched_request_handler
void non_matched_request_handler(non_matched_handler_t nmrh)
Set handler for requests that don't match any route.
Definition: express.hpp:809
restinio::router::generic_express_router_t::operator()
RESTINIO_NODISCARD request_handling_status_t operator()(actual_request_handle_t req) const
Definition: express.hpp:631
restinio::router::generic_express_router_t::m_non_matched_request_handler
non_matched_handler_t m_non_matched_request_handler
Handler that is called for requests that don't match any route.
Definition: express.hpp:824
restinio::router::generic_express_router_t::http_head
void http_head(string_view_t route_path, const path2regex::options_t &options, actual_request_handler_t handler)
Definition: express.hpp:746
restinio::router::generic_express_router_t::generic_express_router_t
generic_express_router_t()=default
restinio::router::generic_express_router_t::http_delete
void http_delete(string_view_t route_path, const path2regex::options_t &options, actual_request_handler_t handler)
Definition: express.hpp:698
restinio::router::generic_express_router_t::actual_request_handle_t
generic_request_handle_t< typename Extra_Data_Factory::data_t > actual_request_handle_t
Definition: express.hpp:615
restinio::router::generic_express_router_t::http_delete
void http_delete(string_view_t route_path, actual_request_handler_t handler)
Definition: express.hpp:687
restinio::router::generic_express_router_t::m_handlers
std::vector< route_entry_t > m_handlers
A list of existing routes.
Definition: express.hpp:821
restinio::router::generic_express_router_t::add_handler
void add_handler(Method_Matcher &&method_matcher, string_view_t route_path, const path2regex::options_t &options, actual_request_handler_t handler)
Definition: express.hpp:673
restinio::router::generic_express_router_t::http_put
void http_put(string_view_t route_path, actual_request_handler_t handler)
Definition: express.hpp:783
restinio::router::generic_express_router_t::http_head
void http_head(string_view_t route_path, actual_request_handler_t handler)
Definition: express.hpp:735
restinio::router::generic_express_router_t::http_post
void http_post(string_view_t route_path, actual_request_handler_t handler)
Definition: express.hpp:759
restinio::router::generic_express_router_t::http_get
void http_get(string_view_t route_path, const path2regex::options_t &options, actual_request_handler_t handler)
Definition: express.hpp:722
restinio::router::generic_express_router_t::add_handler
void add_handler(Method_Matcher &&method_matcher, string_view_t route_path, actual_request_handler_t handler)
Add handlers.
Definition: express.hpp:659
restinio::router::generic_express_router_t::generic_express_router_t
generic_express_router_t(generic_express_router_t &&)=default
restinio::router::generic_express_router_t::actual_request_handler_t
typename generic_express_route_entry_t< Regex_Engine, Extra_Data_Factory >::actual_request_handler_t actual_request_handler_t
Definition: express.hpp:620
restinio::router::impl::buffered_matcher_holder_t
A special class that allows to hold a copy of small-size method_matchers or a pointer to dynamically ...
Definition: method_matcher.hpp:225
restinio::router::impl::route_matcher_t
A matcher for a given path.
Definition: express.hpp:282
restinio::router::impl::route_matcher_t::route_matcher_t
route_matcher_t(Method_Matcher &&method_matcher, regex_t route_regex, std::shared_ptr< std::string > named_params_buffer, param_appender_sequence_t param_appender_sequence)
Definition: express.hpp:309
restinio::router::impl::route_matcher_t::m_method_matcher
buffered_matcher_holder_t m_method_matcher
HTTP method to match.
Definition: express.hpp:405
restinio::router::impl::route_matcher_t::m_route_regex
regex_t m_route_regex
Regex of a given route.
Definition: express.hpp:408
restinio::router::impl::route_matcher_t::m_param_appender_sequence
param_appender_sequence_t m_param_appender_sequence
Parameters values.
Definition: express.hpp:414
restinio::router::impl::route_matcher_t::match_route
bool match_route(target_path_holder_t &target_path, route_params_t &parameters) const
Try to match a given request target with this route.
Definition: express.hpp:328
restinio::router::impl::route_matcher_t::match_results_t
typename Regex_Engine::match_results_t match_results_t
Definition: express.hpp:285
restinio::router::impl::route_matcher_t::operator()
bool operator()(const http_request_header_t &h, target_path_holder_t &target_path, route_params_t &parameters) const
Definition: express.hpp:394
restinio::router::impl::route_matcher_t::route_matcher_t
route_matcher_t()=default
restinio::router::impl::route_matcher_t::m_named_params_buffer
std::shared_ptr< std::string > m_named_params_buffer
Buffer for named parameters names string views.
Definition: express.hpp:411
restinio::router::impl::route_matcher_t::regex_t
typename Regex_Engine::compiled_regex_t regex_t
Definition: express.hpp:284
restinio::router::impl::route_matcher_t::route_matcher_t
route_matcher_t(route_matcher_t &&)=default
restinio::router::impl::route_matcher_t::route_matcher_t
route_matcher_t(http_method_id_t method, regex_t route_regex, std::shared_ptr< std::string > named_params_buffer, param_appender_sequence_t param_appender_sequence)
Creates matcher with a given parameters.
Definition: express.hpp:288
restinio::router::impl::route_params_appender_t
Helper class for gthering parameters from route.
Definition: express.hpp:241
restinio::router::impl::route_params_appender_t::m_indexed_parameters
route_params_t::indexed_parameters_container_t & m_indexed_parameters
Definition: express.hpp:269
restinio::router::impl::route_params_appender_t::add_indexed_param
void add_indexed_param(string_view_t value)
Definition: express.hpp:262
restinio::router::impl::route_params_appender_t::add_named_param
void add_named_param(string_view_t key, string_view_t value)
Definition: express.hpp:256
restinio::router::impl::route_params_appender_t::m_named_parameters
route_params_t::named_parameters_container_t & m_named_parameters
Definition: express.hpp:268
restinio::router::impl::route_params_appender_t::route_params_appender_t
route_params_appender_t(route_params_appender_t &&)=delete
restinio::router::impl::route_params_appender_t::operator=
route_params_appender_t & operator=(const route_params_appender_t &)=delete
restinio::router::impl::route_params_appender_t::route_params_appender_t
route_params_appender_t(const route_params_appender_t &)=delete
restinio::router::impl::route_params_appender_t::route_params_appender_t
route_params_appender_t(route_params_t::named_parameters_container_t &named_parameters, route_params_t::indexed_parameters_container_t &indexed_parameters)
Definition: express.hpp:243
restinio::router::impl::target_path_holder_t
Helper class for holding a unique instance of char array with target_path value.
Definition: target_path_holder.hpp:46
restinio::router::impl::target_path_holder_t::view
RESTINIO_NODISCARD string_view_t view() const noexcept
Get access to the value of target_path.
Definition: target_path_holder.hpp:86
restinio::router::impl::target_path_holder_t::giveout_data
RESTINIO_NODISCARD data_t giveout_data() noexcept
Give out the value from holder.
Definition: target_path_holder.hpp:99
restinio::router::route_params_t
Parameters extracted from route.
Definition: express.hpp:58
restinio::router::route_params_t::find_named_parameter_with_check
named_parameters_container_t::const_reference find_named_parameter_with_check(string_view_t key) const
Definition: express.hpp:151
restinio::router::route_params_t::indexed_parameters_size
auto indexed_parameters_size() const noexcept
Definition: express.hpp:134
restinio::router::route_params_t::get_param
optional_t< string_view_t > get_param(string_view_t key) const noexcept
Get the value of a parameter if it exists.
Definition: express.hpp:112
restinio::router::route_params_t::has
bool has(string_view_t key) const noexcept
Check parameter.
Definition: express.hpp:104
restinio::router::route_params_t::indexed_parameters_container_t
std::vector< string_view_t > indexed_parameters_container_t
Definition: express.hpp:63
restinio::router::route_params_t::match
void match(std::unique_ptr< char[] > request_target, std::shared_ptr< std::string > key_names_buffer, string_view_t match, named_parameters_container_t named_parameters, indexed_parameters_container_t indexed_parameters)
Definition: express.hpp:69
restinio::router::route_params_t::route_params_t
route_params_t(const route_params_t &)=delete
restinio::router::route_params_t::m_request_target
std::unique_ptr< char[] > m_request_target
A raw request target.
Definition: express.hpp:176
restinio::router::route_params_t::named_parameters_container_t
std::vector< std::pair< string_view_t, string_view_t > > named_parameters_container_t
Definition: express.hpp:61
restinio::router::route_params_t::match
string_view_t match() const noexcept
Matched route.
Definition: express.hpp:93
restinio::router::route_params_t::route_params_t
route_params_t()=default
restinio::router::route_params_t::m_named_parameters
named_parameters_container_t m_named_parameters
Named params.
Definition: express.hpp:185
restinio::router::route_params_t::operator=
route_params_t & operator=(route_params_t &&)=default
restinio::router::route_params_t::named_parameters_size
auto named_parameters_size() const noexcept
Get number of parameters.
Definition: express.hpp:133
restinio::router::route_params_t::m_indexed_parameters
indexed_parameters_container_t m_indexed_parameters
Indexed params.
Definition: express.hpp:188
restinio::router::route_params_t::route_params_t
route_params_t(route_params_t &&)=default
restinio::router::route_params_t::operator[]
string_view_t operator[](string_view_t key) const
Get named parameter.
Definition: express.hpp:97
restinio::router::route_params_t::find_named_parameter
named_parameters_container_t::const_iterator find_named_parameter(string_view_t key) const noexcept
Definition: express.hpp:139
restinio::router::route_params_t::m_match
string_view_t m_match
Matched pattern.
Definition: express.hpp:182
restinio::router::route_params_t::m_key_names_buffer
std::shared_ptr< std::string > m_key_names_buffer
Shared buffer for string_view of named parameterts names.
Definition: express.hpp:179
RESTINIO_NODISCARD
#define RESTINIO_NODISCARD
Definition: compiler_features.hpp:33
from_string.hpp
method_matcher.hpp
Stuff related to method_matchers.
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:465
nonstd::optional_lite::nullopt
const nullopt_t nullopt((nullopt_t::init()))
restinio::path2regex::path2regex
auto path2regex(string_view_t path, const options_t &options)
The main path matching regexp.
Definition: path2regex.hpp:863
restinio::path2regex::param_appender_sequence_t
std::vector< param_appender_t< Route_Param_Appender > > param_appender_sequence_t
A sequence of appenders for submatches.
Definition: path2regex.hpp:272
restinio::router::impl::param_appender_sequence_t
path2regex::param_appender_sequence_t< route_params_appender_t > param_appender_sequence_t
Definition: express.hpp:273
restinio::router::generic_non_matched_request_handler_t
std::function< request_handling_status_t(generic_request_handle_t< Extra_Data >) > generic_non_matched_request_handler_t
A generic type of handler for non-matched requests.
Definition: non_matched_request_handler.hpp:45
restinio::router::generic_express_request_handler_t
std::function< request_handling_status_t(generic_request_handle_t< Extra_Data >, route_params_t) > generic_express_request_handler_t
Type of generic handler for one route.
Definition: express.hpp:443
restinio::router::express_request_handler_t
generic_express_request_handler_t< no_extra_data_factory_t::data_t > express_request_handler_t
Type of a handler for one route in the case when there is no extra-data in request object.
Definition: express.hpp:457
restinio
Definition: asio_include.hpp:21
restinio::request_not_handled
RESTINIO_NODISCARD constexpr request_handling_status_t request_not_handled() noexcept
Definition: common_types.hpp:63
restinio::string_view_t
nonstd::string_view string_view_t
Definition: string_view.hpp:19
restinio::generic_request_handle_t
std::shared_ptr< generic_request_t< Extra_Data > > generic_request_handle_t
An alias for shared-pointer to incoming request.
Definition: request_handler.hpp:447
restinio::request_handling_status_t
request_handling_status_t
Request handling status.
Definition: common_types.hpp:26
restinio::get
Value_Type get(const router::route_params_t &params, string_view_t key)
Cast named parameter value to a given type.
Definition: express.hpp:850
std
STL namespace.
non_matched_request_handler.hpp
The definition of the non_matched_request_handler type.
optional.hpp
path2regex.hpp
percent_encoding.hpp
std_regex_engine.hpp
restinio::http_request_header_t
Req header.
Definition: http_headers.hpp:1859
restinio::http_request_header_t::method
http_method_id_t method() const noexcept
Definition: http_headers.hpp:1881
restinio::no_extra_data_factory_t
The default extra-data-factory to be used in server's traits if a user doesn't specify own one.
Definition: request_handler.hpp:75
restinio::path2regex::impl::route_regex_matcher_data_t
Resulting regex and param extraction for a specific route.
Definition: path2regex.hpp:750
restinio::router::impl::route_params_accessor_t
Route params private internals accessor.
Definition: express.hpp:200
restinio::router::impl::route_params_accessor_t::named_parameters
static const auto & named_parameters(const route_params_t &rp) noexcept
Get values containers for all parameters (used in unit tests).
Definition: express.hpp:222
restinio::router::impl::route_params_accessor_t::indexed_parameters
static const auto & indexed_parameters(const route_params_t &rp) noexcept
Definition: express.hpp:228
restinio::router::impl::route_params_accessor_t::match
static void match(route_params_t &rp, std::unique_ptr< char[] > request_target, std::shared_ptr< std::string > key_names_buffer, string_view_t match_, route_params_t::named_parameters_container_t named_parameters, route_params_t::indexed_parameters_container_t indexed_parameters)
Init parameters with a matched route params.
Definition: express.hpp:203
restinio::router::method_matcher_t::match
virtual RESTINIO_NODISCARD bool match(const http_method_id_t &method) const noexcept=0
Is the specified method can be applied to a route?
restinio::router::std_regex_engine_t
Regex engine implementation for using with standard regex implementation.
Definition: std_regex_engine.hpp:25
target_path_holder.hpp
Implementation of target_path_holder helper class.
const
#define const
Definition: zconf.h:230