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> 56 class route_params_t
final 59 using named_parameters_container_t =
60 std::vector< std::pair< string_view_t, string_view_t > >;
61 using indexed_parameters_container_t =
62 std::vector< string_view_t >;
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 );
86 route_params_t &
operator = ( route_params_t && ) =
default;
89 route_params_t &
operator = (
const route_params_t & ) =
delete;
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() )
127 RESTINIO_FMT_FORMAT_STRING(
"invalid parameter index: {}" ),
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 )
161 RESTINIO_FMT_FORMAT_STRING(
"invalid parameter name: {}" ),
162 std::string{ key.data(), key.size() } ) };
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 );
287 using regex_t =
typename Regex_Engine::compiled_regex_t;
288 using match_results_t =
typename Regex_Engine::match_results_t;
292 http_method_id_t method,
294 std::shared_ptr< std::string > named_params_buffer,
295 param_appender_sequence_t param_appender_sequence )
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 )
332 target_path_holder_t & target_path,
333 route_params_t & parameters )
const 398 const http_request_header_t & h,
399 target_path_holder_t & target_path,
400 route_params_t & parameters )
const 473 typename Regex_Engine,
474 typename Extra_Data_Factory >
491 template<
typename Method_Matcher >
493 Method_Matcher && method_matcher,
494 matcher_init_data_t matcher_data,
495 actual_request_handler_t handler )
517 template<
typename Method_Matcher >
519 Method_Matcher && method_matcher,
520 string_view_t route_path,
521 const path2regex::options_t & options,
522 actual_request_handler_t handler )
531 template<
typename Method_Matcher >
533 Method_Matcher && method_matcher,
534 string_view_t route_path,
535 actual_request_handler_t handler )
548 const http_request_header_t & h,
549 impl::target_path_holder_t & target_path,
550 route_params_t & params )
const 612 typename Regex_Engine,
613 typename Extra_Data_Factory >
619 using actual_request_handler_t =
623 >::actual_request_handler_t;
660 template<
typename Method_Matcher >
663 Method_Matcher && method_matcher,
664 string_view_t route_path,
665 actual_request_handler_t handler )
674 template<
typename Method_Matcher >
677 Method_Matcher && method_matcher,
678 string_view_t route_path,
679 const path2regex::options_t & options,
680 actual_request_handler_t handler )
691 string_view_t route_path,
692 actual_request_handler_t handler )
702 string_view_t route_path,
703 const path2regex::options_t & options,
704 actual_request_handler_t handler )
715 string_view_t route_path,
716 actual_request_handler_t handler )
726 string_view_t route_path,
727 const path2regex::options_t & options,
728 actual_request_handler_t handler )
739 string_view_t route_path,
740 actual_request_handler_t handler )
750 string_view_t route_path,
751 const path2regex::options_t & options,
752 actual_request_handler_t handler )
763 string_view_t route_path,
764 actual_request_handler_t handler )
774 string_view_t route_path,
775 const path2regex::options_t & options,
776 actual_request_handler_t handler )
787 string_view_t route_path,
788 actual_request_handler_t handler )
798 string_view_t route_path,
799 const path2regex::options_t & options,
800 actual_request_handler_t handler )
851 template <
typename Value_Type >
853 get(
const router::route_params_t & params, string_view_t key )
859 template <
typename Value_Type >
861 get(
const router::route_params_t & params, std::size_t index )
route_matcher_t()=default
static const auto & indexed_parameters(const route_params_t &rp) noexcept
generic_express_route_entry_t(generic_express_route_entry_t &&)=default
route_params_appender_t(route_params_t::named_parameters_container_t &named_parameters, route_params_t::indexed_parameters_container_t &indexed_parameters)
string_view_t operator[](std::size_t i) const
Get indexed parameter.
generic_express_route_entry_t & operator=(const generic_express_route_entry_t &)=delete
void http_head(string_view_t route_path, actual_request_handler_t handler)
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.
void non_matched_request_handler(non_matched_handler_t nmrh)
Set handler for requests that don't match any route.
void add_indexed_param(string_view_t value)
void http_head(string_view_t route_path, const path2regex::options_t &options, actual_request_handler_t handler)
route_matcher_t(route_matcher_t &&)=default
generic_express_route_entry_t()=default
Route params private internals accessor.
request_handling_status_t operator()(actual_request_handle_t req) const
route_params_t & operator=(route_params_t &&)=default
void add_named_param(string_view_t key, string_view_t value)
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.
regex_t m_route_regex
Regex of a given route.
void http_put(string_view_t route_path, actual_request_handler_t handler)
auto indexed_parameters_size() const noexcept
actual_request_handler_t m_handler
std::shared_ptr< std::string > m_named_params_buffer
Buffer for named parameters names string views.
std::enable_if< std::is_same< Parameter_Container, query_string_params_t >::value||std::is_same< Parameter_Container, router::route_params_t >::value, std::optional< Value_Type > >::type opt_value(const Parameter_Container ¶ms, string_view_t key)
Gets the value of a parameter specified by key wrapped in std::optional<Value_Type> if parameter exis...
auto named_parameters_size() const noexcept
Get number of parameters.
bool match_route(target_path_holder_t &target_path, route_params_t ¶meters) const
Try to match a given request target with this route.
std::optional< string_view_t > get_param(string_view_t key) const noexcept
Get the value of a parameter if it exists.
std::shared_ptr< std::string > m_key_names_buffer
Shared buffer for string_view of named parameterts names.
A single generic express route entry.
std::unique_ptr< char[] > m_request_target
A raw request target.
generic_express_route_entry_t(Method_Matcher &&method_matcher, string_view_t route_path, const path2regex::options_t &options, actual_request_handler_t handler)
static const auto & named_parameters(const route_params_t &rp) noexcept
Get values containers for all parameters (used in unit tests).
named_parameters_container_t::const_reference find_named_parameter_with_check(string_view_t key) const
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.
indexed_parameters_container_t m_indexed_parameters
Indexed params.
generic_express_route_entry_t & operator=(generic_express_route_entry_t &&)=default
string_view_t m_match
Matched pattern.
void http_get(string_view_t route_path, actual_request_handler_t handler)
void http_put(string_view_t route_path, const path2regex::options_t &options, actual_request_handler_t handler)
A matcher for a given path.
impl::route_matcher_t< Regex_Engine > m_matcher
void add_handler(Method_Matcher &&method_matcher, string_view_t route_path, const path2regex::options_t &options, actual_request_handler_t handler)
route_params_t(route_params_t &&)=default
named_parameters_container_t m_named_parameters
Named params.
generic_express_route_entry_t(const generic_express_route_entry_t &)=delete
generic_express_router_t(generic_express_router_t &&)=default
Value_Type get(const router::route_params_t ¶ms, std::size_t index)
Cast indexed parameter value to a given type.
string_view_t match() const noexcept
Matched route.
route_params_t & operator=(const route_params_t &)=delete
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)
route_params_appender_t & operator=(const route_params_appender_t &)=delete
route_params_t::named_parameters_container_t & m_named_parameters
route_params_appender_t & operator=(route_params_appender_t &&)=delete
route_params_appender_t(const route_params_appender_t &)=delete
param_appender_sequence_t m_param_appender_sequence
Parameters values.
bool operator()(const http_request_header_t &h, target_path_holder_t &target_path, route_params_t ¶meters) const
std::vector< route_entry_t > m_handlers
A list of existing routes.
bool has(string_view_t key) const noexcept
Check parameter.
void add_handler(Method_Matcher &&method_matcher, string_view_t route_path, actual_request_handler_t handler)
Add handlers.
named_parameters_container_t::const_iterator find_named_parameter(string_view_t key) const noexcept
Generic Express.js style router.
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)
Helper class for gthering parameters from route.
request_handling_status_t handle(actual_request_handle_t rh, route_params_t rp) const
Calls a handler of given request with given params.
non_matched_handler_t m_non_matched_request_handler
Handler that is called for requests that don't match any route.
route_params_appender_t(route_params_appender_t &&)=delete
void http_delete(string_view_t route_path, const path2regex::options_t &options, actual_request_handler_t handler)
generic_express_route_entry_t(Method_Matcher &&method_matcher, string_view_t route_path, actual_request_handler_t handler)
void http_post(string_view_t route_path, const path2regex::options_t &options, actual_request_handler_t handler)
generic_express_router_t()=default
void http_delete(string_view_t route_path, actual_request_handler_t handler)
route_params_t(const route_params_t &)=delete
void http_post(string_view_t route_path, actual_request_handler_t handler)
route_params_t::indexed_parameters_container_t & m_indexed_parameters
buffered_matcher_holder_t m_method_matcher
HTTP method to match.
void http_get(string_view_t route_path, const path2regex::options_t &options, actual_request_handler_t handler)