RESTinio
express.hpp
Go to the documentation of this file.
1 /*
2  restinio
3 */
4 
9 #pragma once
10 
13 
14 #include <restinio/optional.hpp>
15 
17 
20 
23 
24 #include <map>
25 #include <vector>
26 
27 namespace restinio
28 {
29 
30 namespace router
31 {
32 
33 namespace impl
34 {
35 
37 
38 } /* namespace impl */
39 
40 //
41 // route_params_t
42 //
43 
45 
57 class route_params_t final
58 {
59  public:
61  std::vector< std::pair< string_view_t, string_view_t > >;
63  std::vector< string_view_t >;
64 
65  private:
67 
68  void
70  std::unique_ptr< char[] > request_target,
71  std::shared_ptr< std::string > key_names_buffer,
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;
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 
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 
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 } :
119  }
120 
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
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 
183 
186 
189 };
190 
191 namespace impl
192 {
193 
194 //
195 // route_params_accessor_t
196 //
197 
200 {
202  static void
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_,
210  {
211  rp.match(
212  std::move( request_target ),
213  std::move( key_names_buffer ),
214  match_,
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 
241 {
242  public:
246  : m_named_parameters{ named_parameters }
247  , m_indexed_parameters{ indexed_parameters }
248  {}
249 
254 
255  void
257  {
258  m_named_parameters.emplace_back( key, value );
259  }
260 
261  void
263  {
264  m_indexed_parameters.emplace_back( value );
265  }
266 
267  private:
270 };
271 
274 
275 //
276 // route_matcher_t
277 //
278 
280 template < typename Regex_Engine = std_regex_engine_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 
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 >
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(
320  std::forward<Method_Matcher>(method_matcher) );
321  }
322 
323  route_matcher_t() = default;
325 
327  bool
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(),
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 
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 ];
364  param_appender,
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.
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
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:
406 
409 
411  std::shared_ptr< std::string > m_named_params_buffer;
412 
415 };
416 
417 } /* namespace impl */
418 
419 //
420 // express_request_handler_t
421 //
422 
425 
426 //
427 // express_route_entry_t
428 //
429 
431 
436 template < typename Regex_Engine = std_regex_engine_t>
438 {
442  Regex_Engine >;
443 
444  template< typename Method_Matcher >
446  Method_Matcher && method_matcher,
447  matcher_init_data_t matcher_data,
448  express_request_handler_t handler )
449  : m_matcher{
450  std::forward<Method_Matcher>( method_matcher ),
451  std::move( matcher_data.m_regex ),
452  std::move( matcher_data.m_named_params_buffer ),
453  std::move( matcher_data.m_param_appender_sequence ) }
454  , m_handler{ std::move( handler ) }
455  {}
456 
457  public:
460 
465 
466  template< typename Method_Matcher >
468  Method_Matcher && method_matcher,
469  string_view_t route_path,
470  const path2regex::options_t & options,
471  express_request_handler_t handler )
473  std::forward<Method_Matcher>( method_matcher ),
474  path2regex::path2regex< impl::route_params_appender_t, Regex_Engine >(
475  route_path,
476  options ),
477  std::move( handler ) }
478  {}
479 
480  template< typename Method_Matcher >
482  Method_Matcher && method_matcher,
483  string_view_t route_path,
484  express_request_handler_t handler )
486  std::forward<Method_Matcher>( method_matcher ),
487  route_path,
489  std::move( handler ) }
490  {}
491 
495  bool
497  const http_request_header_t & h,
498  impl::target_path_holder_t & target_path,
499  route_params_t & params ) const
500  {
501  return m_matcher( h, target_path, params );
502  }
503 
508  {
509  return m_handler( std::move( rh ), std::move( rp ) );
510  }
511 
512  private:
515 };
516 
517 //
518 // express_router_t
519 //
520 
522 /*
523  Express routers acts as a request handler (it means it is a function-object
524  that can be called as a restinio request handler).
525  It aggregates several endpoint-handlers and picks one or none of them to handle the request.
526  The choice of the handler to execute depends on request target and HTTP method.
527  If router finds no handler matching the request then request is considered unmatched.
528  It is possible to set a handler for unmatched requests, otherwise router rejects the request and
529  RESTinio takes care of it.
530 
531  There is a difference between ordinary restinio request handler
532  and the one that is used with experss router: express_request_handler_t.
533  The signature of a handlers that can be put in router
534  has an additional parameter -- a container with parameters extracted from URI (request target).
535 */
536 template < typename Regex_Engine = std_regex_engine_t>
538 {
539  public:
540  express_router_t() = default;
542 
546  {
547  impl::target_path_holder_t target_path{ req->header().path() };
548  route_params_t params;
549  for( const auto & entry : m_handlers )
550  {
551  if( entry.match( req->header(), target_path, params ) )
552  {
553  return entry.handle( std::move( req ), std::move( params ) );
554  }
555  }
556 
557  // Here: none of the routes matches this handler.
558 
560  {
561  // If non matched request handler is set
562  // then call it.
563  return m_non_matched_request_handler( std::move( req ) );
564  }
565 
566  return request_rejected();
567  }
568 
571  template< typename Method_Matcher >
572  void
574  Method_Matcher && method_matcher,
575  string_view_t route_path,
576  express_request_handler_t handler )
577  {
578  add_handler(
579  std::forward<Method_Matcher>(method_matcher),
580  route_path,
582  std::move( handler ) );
583  }
584 
585  template< typename Method_Matcher >
586  void
588  Method_Matcher && method_matcher,
589  string_view_t route_path,
590  const path2regex::options_t & options,
591  express_request_handler_t handler )
592  {
593  m_handlers.emplace_back(
594  std::forward<Method_Matcher>(method_matcher),
595  route_path,
596  options,
597  std::move( handler ) );
598  }
599 
600  void
602  string_view_t route_path,
603  express_request_handler_t handler )
604  {
605  add_handler(
606  http_method_delete(),
607  route_path,
608  std::move( handler ) );
609  }
610 
611  void
613  string_view_t route_path,
614  const path2regex::options_t & options,
615  express_request_handler_t handler )
616  {
617  add_handler(
618  http_method_delete(),
619  route_path,
620  options,
621  std::move( handler ) );
622  }
623 
624  void
626  string_view_t route_path,
627  express_request_handler_t handler )
628  {
629  add_handler(
630  http_method_get(),
631  route_path,
632  std::move( handler ) );
633  }
634 
635  void
637  string_view_t route_path,
638  const path2regex::options_t & options,
639  express_request_handler_t handler )
640  {
641  add_handler(
642  http_method_get(),
643  route_path,
644  options,
645  std::move( handler ) );
646  }
647 
648  void
650  string_view_t route_path,
651  express_request_handler_t handler )
652  {
653  add_handler(
654  http_method_head(),
655  route_path,
656  std::move( handler ) );
657  }
658 
659  void
661  string_view_t route_path,
662  const path2regex::options_t & options,
663  express_request_handler_t handler )
664  {
665  add_handler(
666  http_method_head(),
667  route_path,
668  options,
669  std::move( handler ) );
670  }
671 
672  void
674  string_view_t route_path,
675  express_request_handler_t handler )
676  {
677  add_handler(
678  http_method_post(),
679  route_path,
680  std::move( handler ) );
681  }
682 
683  void
685  string_view_t route_path,
686  const path2regex::options_t & options,
687  express_request_handler_t handler )
688  {
689  add_handler(
690  http_method_post(),
691  route_path,
692  options,
693  std::move( handler ) );
694  }
695 
696  void
698  string_view_t route_path,
699  express_request_handler_t handler )
700  {
701  add_handler(
702  http_method_put(),
703  route_path,
704  std::move( handler ) );
705  }
706 
707  void
709  string_view_t route_path,
710  const path2regex::options_t & options,
711  express_request_handler_t handler )
712  {
713  add_handler(
714  http_method_put(),
715  route_path,
716  options,
717  std::move( handler ) );
718  }
720 
722  void
724  {
726  }
727 
728  private:
730 
732  std::vector< route_entry_t > m_handlers;
733 
736 };
737 
738 } /* namespace router */
739 
741 template < typename Value_Type >
742 Value_Type
744 {
745  return get< Value_Type >( params[ key ] );
746 }
747 
749 template < typename Value_Type >
750 Value_Type
751 get( const router::route_params_t & params, std::size_t index )
752 {
753  return get< Value_Type >( params[ index ] );
754 }
755 
756 } /* namespace restinio */
restinio::router::impl::route_params_appender_t::route_params_appender_t
route_params_appender_t(const route_params_appender_t &)=delete
restinio::exception_t
Exception class for all exceptions thrown by RESTinio.
Definition: exception.hpp:26
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::http_method_id_t
A type for representation of HTTP method ID.
Definition: http_headers.hpp:1744
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_NODISCARD
#define RESTINIO_NODISCARD
Definition: compiler_features.hpp:33
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::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::express_router_t::http_put
void http_put(string_view_t route_path, const path2regex::options_t &options, express_request_handler_t handler)
Definition: express.hpp:708
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::router::express_router_t::http_head
void http_head(string_view_t route_path, const path2regex::options_t &options, express_request_handler_t handler)
Definition: express.hpp:660
restinio::router::express_router_t::http_delete
void http_delete(string_view_t route_path, express_request_handler_t handler)
Definition: express.hpp:601
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
restinio::path2regex::impl::route_regex_matcher_data_t::m_regex
regex_t m_regex
Definition: path2regex.hpp:760
restinio::router::route_params_t::indexed_parameters_size
auto indexed_parameters_size() const noexcept
Definition: express.hpp:134
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::impl::route_matcher_t::route_matcher_t
route_matcher_t()=default
restinio::router::route_params_t::route_params_t
route_params_t(const route_params_t &)=delete
from_string.hpp
restinio::router::express_router_t::non_matched_request_handler
void non_matched_request_handler(non_matched_request_handler_t nmrh)
Set handler for requests that don't match any route.
Definition: express.hpp:723
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::indexed_parameters_container_t
std::vector< string_view_t > indexed_parameters_container_t
Definition: express.hpp:63
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::express_route_entry_t::express_route_entry_t
express_route_entry_t(const express_route_entry_t &)=delete
restinio::path2regex::options_t
Options for matching routes.
Definition: path2regex.hpp:92
restinio::router::express_router_t::http_put
void http_put(string_view_t route_path, express_request_handler_t handler)
Definition: express.hpp:697
restinio::string_view_t
nonstd::string_view string_view_t
Definition: string_view.hpp:19
restinio::router::express_router_t::express_router_t
express_router_t(express_router_t &&)=default
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(route_params_t::named_parameters_container_t &named_parameters, route_params_t::indexed_parameters_container_t &indexed_parameters)
Definition: express.hpp:243
restinio::router::route_params_t::operator=
route_params_t & operator=(route_params_t &&)=default
restinio::router::express_route_entry_t::operator=
express_route_entry_t & operator=(const express_route_entry_t &)=delete
restinio::router::express_request_handler_t
std::function< request_handling_status_t(request_handle_t, route_params_t) > express_request_handler_t
Definition: express.hpp:424
restinio::router::impl::route_params_accessor_t
Route params private internals accessor.
Definition: express.hpp:200
restinio::router::impl::route_matcher_t< std_regex_engine_t >::match_results_t
typename Regex_Engine::match_results_t match_results_t
Definition: express.hpp:285
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::express_router_t::add_handler
void add_handler(Method_Matcher &&method_matcher, string_view_t route_path, express_request_handler_t handler)
Add handlers.
Definition: express.hpp:573
restinio::router::route_params_t::m_match
string_view_t m_match
Matched pattern.
Definition: express.hpp:182
restinio::router::express_route_entry_t::express_route_entry_t
express_route_entry_t(Method_Matcher &&method_matcher, string_view_t route_path, const path2regex::options_t &options, express_request_handler_t handler)
Definition: express.hpp:467
path2regex.hpp
restinio::path2regex::impl::route_regex_matcher_data_t::m_named_params_buffer
std::shared_ptr< std::string > m_named_params_buffer
Char buffer for holding named paramaters.
Definition: path2regex.hpp:767
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::express_route_entry_t
A single express route entry.
Definition: express.hpp:438
restinio::router::express_route_entry_t::express_route_entry_t
express_route_entry_t(express_route_entry_t &&)=default
target_path_holder.hpp
Implementation of target_path_holder helper class.
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::path2regex::impl::route_regex_matcher_data_t
Resulting regex and param extraction for a specific route.
Definition: path2regex.hpp:750
restinio::router::express_route_entry_t::express_route_entry_t
express_route_entry_t(Method_Matcher &&method_matcher, matcher_init_data_t matcher_data, express_request_handler_t handler)
Definition: express.hpp:445
restinio::router::express_route_entry_t::express_route_entry_t
express_route_entry_t(Method_Matcher &&method_matcher, string_view_t route_path, express_request_handler_t handler)
Definition: express.hpp:481
restinio::router::express_router_t::http_post
void http_post(string_view_t route_path, const path2regex::options_t &options, express_request_handler_t handler)
Definition: express.hpp:684
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::match
string_view_t match() const noexcept
Matched route.
Definition: express.hpp:93
restinio::router::impl::route_params_appender_t::add_indexed_param
void add_indexed_param(string_view_t value)
Definition: express.hpp:262
restinio::path2regex::impl::route_regex_matcher_data_t::m_param_appender_sequence
param_appender_sequence_t< Route_Param_Appender > m_param_appender_sequence
Appenders for captured values (names/indexed groups).
Definition: path2regex.hpp:770
restinio::router::route_params_t::route_params_t
route_params_t()=default
restinio::request_handling_status_t
request_handling_status_t
Request handling status.
Definition: common_types.hpp:26
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::express_router_t::operator()
RESTINIO_NODISCARD request_handling_status_t operator()(request_handle_t req) const
Definition: express.hpp:545
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::route_params_t::m_request_target
std::unique_ptr< char[] > m_request_target
A raw request target.
Definition: express.hpp:176
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_params_appender_t::m_named_parameters
route_params_t::named_parameters_container_t & m_named_parameters
Definition: express.hpp:268
nonstd::optional_lite::optional
class optional
Definition: optional.hpp:839
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_matcher_t::route_matcher_t
route_matcher_t(route_matcher_t &&)=default
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_matcher_t::m_route_regex
regex_t m_route_regex
Regex of a given route.
Definition: express.hpp:408
restinio::router::express_route_entry_t::m_matcher
impl::route_matcher_t< Regex_Engine > m_matcher
Definition: express.hpp:513
restinio::router::route_params_t::operator[]
string_view_t operator[](string_view_t key) const
Get named parameter.
Definition: express.hpp:97
restinio::router::express_router_t::http_head
void http_head(string_view_t route_path, express_request_handler_t handler)
Definition: express.hpp:649
nonstd::optional_lite::nullopt
const nullopt_t nullopt((nullopt_t::init()))
restinio::router::non_matched_request_handler_t
std::function< request_handling_status_t(request_handle_t) > non_matched_request_handler_t
Definition: non_matched_request_handler.hpp:29
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::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::express_route_entry_t::handle
RESTINIO_NODISCARD request_handling_status_t handle(request_handle_t rh, route_params_t rp) const
Calls a handler of given request with given params.
Definition: express.hpp:507
restinio::router::route_params_t::m_named_parameters
named_parameters_container_t m_named_parameters
Named params.
Definition: express.hpp:185
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< std_regex_engine_t >::regex_t
typename Regex_Engine::compiled_regex_t regex_t
Definition: express.hpp:284
method_matcher.hpp
Stuff related to method_matchers.
restinio::router::route_params_t
Parameters extracted from route.
Definition: express.hpp:58
restinio::router::express_router_t::http_get
void http_get(string_view_t route_path, const path2regex::options_t &options, express_request_handler_t handler)
Definition: express.hpp:636
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::express_router_t::m_handlers
std::vector< route_entry_t > m_handlers
A list of existing routes.
Definition: express.hpp:732
restinio
Definition: asio_include.hpp:21
restinio::router::impl::param_appender_sequence_t
path2regex::param_appender_sequence_t< route_params_appender_t > param_appender_sequence_t
Definition: express.hpp:273
std_regex_engine.hpp
restinio::router::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:496
restinio::router::express_router_t::add_handler
void add_handler(Method_Matcher &&method_matcher, string_view_t route_path, const path2regex::options_t &options, express_request_handler_t handler)
Definition: express.hpp:587
restinio::router::route_params_t::has
bool has(string_view_t key) const noexcept
Check parameter.
Definition: express.hpp:104
restinio::router::impl::route_matcher_t
A matcher for a given path.
Definition: express.hpp:282
restinio::router::impl::route_params_appender_t
Helper class for gthering parameters from route.
Definition: express.hpp:241
percent_encoding.hpp
restinio::router::express_router_t::m_non_matched_request_handler
non_matched_request_handler_t m_non_matched_request_handler
Handler that is called for requests that don't match any route.
Definition: express.hpp:735
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::express_router_t
Express.js style router.
Definition: express.hpp:538
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::express_router_t::http_get
void http_get(string_view_t route_path, express_request_handler_t handler)
Definition: express.hpp:625
restinio::http_request_header_t::method
http_method_id_t method() const noexcept
Definition: http_headers.hpp:1881
restinio::request_handle_t
std::shared_ptr< request_t > request_handle_t
Request handler, that is the type for calling request handlers.
Definition: request_handler.hpp:182
restinio::router::express_route_entry_t::express_route_entry_t
express_route_entry_t()=default
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(route_params_t &&)=default
restinio::router::express_router_t::http_post
void http_post(string_view_t route_path, express_request_handler_t handler)
Definition: express.hpp:673
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::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::http_request_header_t
Req header.
Definition: http_headers.hpp:1859
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:743
restinio::request_rejected
constexpr request_handling_status_t request_rejected() noexcept
Definition: common_types.hpp:44
restinio::router::impl::route_params_appender_t::route_params_appender_t
route_params_appender_t(route_params_appender_t &&)=delete
restinio::router::express_route_entry_t::m_handler
express_request_handler_t m_handler
Definition: express.hpp:514
restinio::router::express_router_t::express_router_t
express_router_t()=default
non_matched_request_handler.hpp
The definition of the non_matched_request_handler type.
const
#define const
Definition: zconf.h:230
restinio::router::express_router_t::http_delete
void http_delete(string_view_t route_path, const path2regex::options_t &options, express_request_handler_t handler)
Definition: express.hpp:612
optional.hpp