2
3
6
7
11#include <restinio/router/impl/target_path_holder.hpp>
12#include <restinio/router/non_matched_request_handler.hpp>
14#include <restinio/path2regex/path2regex.hpp>
16#include <restinio/router/std_regex_engine.hpp>
17#include <restinio/router/method_matcher.hpp>
19#include <restinio/utils/from_string.hpp>
20#include <restinio/utils/percent_encoding.hpp>
45
46
47
48
49
50
51
52
53
54
55
69 std::unique_ptr<
char[] > request_target,
70 std::shared_ptr< std::string > key_names_buffer,
72 named_parameters_container_t named_parameters,
73 indexed_parameters_container_t indexed_parameters )
75 m_request_target = std::move( request_target );
76 m_key_names_buffer = std::move( key_names_buffer );
78 m_named_parameters = std::move( named_parameters );
79 m_indexed_parameters = std::move( indexed_parameters );
98 return find_named_parameter_with_check( key ).second;
103 has( string_view_t key )
const noexcept
105 return m_named_parameters.end() != find_named_parameter( key );
113 const auto it = find_named_parameter( key );
115 return m_named_parameters.end() != it ?
116 std::optional< string_view_t >{ it->second } :
117 std::optional< string_view_t >{ std::nullopt };
124 if( i >= m_indexed_parameters.size() )
131 return m_indexed_parameters.at( i );
146 m_named_parameters.begin(),
147 m_named_parameters.end(),
149 return key == p.first;
156 auto it = find_named_parameter( key );
158 if( m_named_parameters.end() == it )
162 std::string{ key.data(), key.size() } ) };
169
170
171
172
173
174
175
176
177
178
208 std::unique_ptr<
char[] > request_target,
209 std::shared_ptr< std::string > key_names_buffer,
210 string_view_t match_,
211 route_params_t::named_parameters_container_t named_parameters,
212 route_params_t::indexed_parameters_container_t indexed_parameters )
215 std::move( request_target ),
216 std::move( key_names_buffer ),
218 std::move( named_parameters ),
219 std::move( indexed_parameters ) );
227 return rp.m_named_parameters;
233 return rp.m_indexed_parameters;
247 route_params_t::named_parameters_container_t & named_parameters,
248 route_params_t::indexed_parameters_container_t & indexed_parameters )
261 m_named_parameters.emplace_back( key, value );
267 m_indexed_parameters.emplace_back( value );
294 std::shared_ptr< std::string > named_params_buffer,
295 param_appender_sequence_t param_appender_sequence )
300 assign( m_method_matcher, std::move(method) );
304
305
306
307
308
309
310
311 template<
typename Method_Matcher >
313 Method_Matcher && method_matcher,
315 std::shared_ptr< std::string > named_params_buffer,
316 param_appender_sequence_t param_appender_sequence )
323 std::forward<Method_Matcher>(method_matcher) );
336 if( Regex_Engine::try_match(
341 assert( m_param_appender_sequence.size() + 1 >= matches.size() );
345 auto captured_params = target_path.giveout_data();
347 const string_view_t match{
348 captured_params.get() + Regex_Engine::submatch_begin_pos( matches[0] ),
349 Regex_Engine::submatch_end_pos( matches[0] ) -
350 Regex_Engine::submatch_begin_pos( matches[0] ) } ;
352 route_params_t::named_parameters_container_t named_parameters;
353 route_params_t::indexed_parameters_container_t indexed_parameters;
363 for( ; i < matches.size(); ++i )
365 const auto & m = matches[ i ];
366 m_param_appender_sequence[ i - 1](
369 captured_params.get() + Regex_Engine::submatch_begin_pos( m ),
370 Regex_Engine::submatch_end_pos( m ) -
371 Regex_Engine::submatch_begin_pos( m ) } );
374 for( ; i < m_param_appender_sequence.size() + 1; ++i )
376 m_param_appender_sequence[ i - 1 ](
378 string_view_t{ captured_params.get(), 0 } );
382 route_params_accessor_t::match(
384 std::move( captured_params ),
385 m_named_params_buffer,
387 std::move( named_parameters ),
388 std::move( indexed_parameters ) );
398 const http_request_header_t & h,
402 return m_method_matcher->match( h.method() ) &&
403 match_route( target_path, parameters );
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441template<
typename Extra_Data >
452
453
454
455
456
457
458
468
469
470
471
473 typename Regex_Engine,
474 typename Extra_Data_Factory >
491 template<
typename Method_Matcher >
493 Method_Matcher && method_matcher,
495 actual_request_handler_t handler )
497 std::forward<Method_Matcher>( method_matcher ),
498 std::move( matcher_data.m_regex ),
499 std::move( matcher_data.m_named_params_buffer ),
500 std::move( matcher_data.m_param_appender_sequence ) }
517 template<
typename Method_Matcher >
519 Method_Matcher && method_matcher,
520 string_view_t route_path,
522 actual_request_handler_t handler )
524 std::forward<Method_Matcher>( method_matcher ),
528 std::move( handler ) }
531 template<
typename Method_Matcher >
533 Method_Matcher && method_matcher,
534 string_view_t route_path,
535 actual_request_handler_t handler )
537 std::forward<Method_Matcher>( method_matcher ),
540 std::move( handler ) }
548 const http_request_header_t & h,
560 return m_handler( std::move( rh ), std::move( rp ) );
572
573
574
575
576
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
612 typename Regex_Engine,
613 typename Extra_Data_Factory >
638 for(
const auto & entry : m_handlers )
640 if( entry.match( req->header(), target_path, params ) )
642 return entry.handle( std::move( req ), std::move( params ) );
648 if( m_non_matched_request_handler )
652 return m_non_matched_request_handler( std::move( req ) );
660 template<
typename Method_Matcher >
663 Method_Matcher && method_matcher,
664 string_view_t route_path,
668 std::forward<Method_Matcher>(method_matcher),
671 std::move( handler ) );
674 template<
typename Method_Matcher >
677 Method_Matcher && method_matcher,
678 string_view_t route_path,
682 m_handlers.emplace_back(
683 std::forward<Method_Matcher>(method_matcher),
686 std::move( handler ) );
691 string_view_t route_path,
695 http_method_delete(),
697 std::move( handler ) );
702 string_view_t route_path,
707 http_method_delete(),
710 std::move( handler ) );
715 string_view_t route_path,
721 std::move( handler ) );
726 string_view_t route_path,
734 std::move( handler ) );
739 string_view_t route_path,
745 std::move( handler ) );
750 string_view_t route_path,
758 std::move( handler ) );
763 string_view_t route_path,
769 std::move( handler ) );
774 string_view_t route_path,
782 std::move( handler ) );
787 string_view_t route_path,
793 std::move( handler ) );
798 string_view_t route_path,
806 std::move( handler ) );
814 m_non_matched_request_handler = std::move( nmrh );
834
835
836
837
838
839
840
841
851template <
typename Value_Type >
855 return std::get< Value_Type >( params[ key ] );
859template <
typename Value_Type >
863 return std::get< Value_Type >( params
[ index
] );
Exception class for all exceptions thrown by RESTinio.
A type for representation of HTTP method ID.
Options for matching routes.
A single generic express route entry.
generic_express_route_entry_t(Method_Matcher &&method_matcher, matcher_init_data_t matcher_data, actual_request_handler_t handler)
generic_express_route_entry_t & operator=(const generic_express_route_entry_t &)=delete
generic_express_route_entry_t(Method_Matcher &&method_matcher, string_view_t route_path, const path2regex::options_t &options, actual_request_handler_t handler)
generic_express_route_entry_t(generic_express_route_entry_t &&)=default
bool match(const http_request_header_t &h, impl::target_path_holder_t &target_path, route_params_t ¶ms) const
Checks if request header matches entry, and if so, set route params.
generic_express_route_entry_t(Method_Matcher &&method_matcher, string_view_t route_path, actual_request_handler_t handler)
generic_express_route_entry_t & operator=(generic_express_route_entry_t &&)=default
path2regex::impl::route_regex_matcher_data_t< impl::route_params_appender_t, Regex_Engine > matcher_init_data_t
actual_request_handler_t m_handler
generic_express_route_entry_t()=default
request_handling_status_t handle(actual_request_handle_t rh, route_params_t rp) const
Calls a handler of given request with given params.
generic_express_route_entry_t(const generic_express_route_entry_t &)=delete
impl::route_matcher_t< Regex_Engine > m_matcher
Generic Express.js style router.
void http_post(string_view_t route_path, const path2regex::options_t &options, actual_request_handler_t handler)
void http_get(string_view_t route_path, actual_request_handler_t handler)
request_handling_status_t operator()(actual_request_handle_t req) const
void http_put(string_view_t route_path, const path2regex::options_t &options, actual_request_handler_t handler)
generic_express_route_entry_t< Regex_Engine, Extra_Data_Factory > route_entry_t
void non_matched_request_handler(non_matched_handler_t nmrh)
Set handler for requests that don't match any route.
non_matched_handler_t m_non_matched_request_handler
Handler that is called for requests that don't match any route.
void http_head(string_view_t route_path, const path2regex::options_t &options, actual_request_handler_t handler)
generic_express_router_t()=default
typename generic_express_route_entry_t< Regex_Engine, Extra_Data_Factory >::actual_request_handler_t actual_request_handler_t
void http_delete(string_view_t route_path, const path2regex::options_t &options, actual_request_handler_t handler)
void http_delete(string_view_t route_path, actual_request_handler_t handler)
std::vector< route_entry_t > m_handlers
A list of existing routes.
void add_handler(Method_Matcher &&method_matcher, string_view_t route_path, const path2regex::options_t &options, actual_request_handler_t handler)
void http_put(string_view_t route_path, actual_request_handler_t handler)
void http_head(string_view_t route_path, actual_request_handler_t handler)
void http_post(string_view_t route_path, actual_request_handler_t handler)
void http_get(string_view_t route_path, const path2regex::options_t &options, actual_request_handler_t handler)
void add_handler(Method_Matcher &&method_matcher, string_view_t route_path, actual_request_handler_t handler)
Add handlers.
generic_express_router_t(generic_express_router_t &&)=default
A special class that allows to hold a copy of small-size method_matchers or a pointer to dynamically ...
A matcher for a given path.
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)
buffered_matcher_holder_t m_method_matcher
HTTP method to match.
regex_t m_route_regex
Regex of a given route.
param_appender_sequence_t m_param_appender_sequence
Parameters values.
bool match_route(target_path_holder_t &target_path, route_params_t ¶meters) const
Try to match a given request target with this route.
typename Regex_Engine::match_results_t match_results_t
bool operator()(const http_request_header_t &h, target_path_holder_t &target_path, route_params_t ¶meters) const
route_matcher_t()=default
std::shared_ptr< std::string > m_named_params_buffer
Buffer for named parameters names string views.
typename Regex_Engine::compiled_regex_t regex_t
route_matcher_t(route_matcher_t &&)=default
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.
Helper class for gthering parameters from route.
route_params_t::indexed_parameters_container_t & m_indexed_parameters
void add_indexed_param(string_view_t value)
void add_named_param(string_view_t key, string_view_t value)
route_params_t::named_parameters_container_t & m_named_parameters
route_params_appender_t & operator=(route_params_appender_t &&)=delete
route_params_appender_t(route_params_appender_t &&)=delete
route_params_appender_t & operator=(const route_params_appender_t &)=delete
route_params_appender_t(const route_params_appender_t &)=delete
route_params_appender_t(route_params_t::named_parameters_container_t &named_parameters, route_params_t::indexed_parameters_container_t &indexed_parameters)
Helper class for holding a unique instance of char array with target_path value.
named_parameters_container_t::const_reference find_named_parameter_with_check(string_view_t key) const
auto indexed_parameters_size() const noexcept
bool has(string_view_t key) const noexcept
Check parameter.
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)
string_view_t operator[](std::size_t i) const
Get indexed parameter.
route_params_t(const route_params_t &)=delete
std::unique_ptr< char[] > m_request_target
A raw request target.
string_view_t match() const noexcept
Matched route.
route_params_t & operator=(const route_params_t &)=delete
named_parameters_container_t m_named_parameters
Named params.
route_params_t & operator=(route_params_t &&)=default
auto named_parameters_size() const noexcept
Get number of parameters.
indexed_parameters_container_t m_indexed_parameters
Indexed params.
route_params_t(route_params_t &&)=default
string_view_t operator[](string_view_t key) const
Get named parameter.
std::optional< string_view_t > get_param(string_view_t key) const noexcept
Get the value of a parameter if it exists.
std::vector< std::pair< string_view_t, string_view_t > > named_parameters_container_t
named_parameters_container_t::const_iterator find_named_parameter(string_view_t key) const noexcept
string_view_t m_match
Matched pattern.
std::shared_ptr< std::string > m_key_names_buffer
Shared buffer for string_view of named parameterts names.
#define RESTINIO_FMT_FORMAT_STRING(s)
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.
generic_express_router_t< Regex_Engine, no_extra_data_factory_t > express_router_t
A type of express-like router for the case when the default extra-data-factory is specified in the se...
generic_express_route_entry_t< Regex_Engine, no_extra_data_factory_t > express_route_entry_t
An alias for a single route entry in the case when the default extra-data-factory is used in server's...
Value_Type get(const router::route_params_t ¶ms, std::size_t index)
Cast indexed parameter value to a given type.
constexpr request_handling_status_t request_not_handled() noexcept
request_handling_status_t
Request handling status.
Value_Type get(const router::route_params_t ¶ms, string_view_t key)
Cast named parameter value to a given type.
Resulting regex and param extraction for a specific route.
Route params private internals accessor.
static const auto & named_parameters(const route_params_t &rp) noexcept
Get values containers for all parameters (used in unit tests).
static const auto & indexed_parameters(const route_params_t &rp) noexcept
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.
Regex engine implementation for using with standard regex implementation.