RESTinio
settings.hpp
Go to the documentation of this file.
1 /*
2  restinio
3 */
4 
5 /*!
6  HTTP-Server configuration.
7 */
8 
9 #pragma once
10 
11 #include <chrono>
12 #include <tuple>
13 #include <utility>
14 
15 #include <restinio/asio_include.hpp>
16 
17 #include <restinio/exception.hpp>
18 #include <restinio/request_handler.hpp>
19 #include <restinio/traits.hpp>
20 
21 namespace restinio
22 {
23 
24 namespace details
25 {
26 
27 //! Default instantiation for a specific type.
28 template < typename Object >
29 inline auto
31 {
32  return std::unique_ptr< Object >{};
33 }
34 
35 template < typename Object >
36 inline auto
38 {
39  return std::make_unique< Object >();
40 }
41 
42 //! Default instantiation for a specific type.
43 template < typename Object >
44 inline auto
46 {
47  return std::shared_ptr< Object >{};
48 }
49 
50 template < typename Object >
51 inline auto
53 {
54  return std::make_shared< Object >();
55 }
56 
57 } /* namespace details */
58 
59 //
60 // create_default_unique_object_instance
61 //
62 
63 //! Default instantiation for a specific type.
64 template < typename Object>
65 inline auto
67 {
68  typename std::is_default_constructible< Object >::type tag;
69  return details::create_default_unique_object_instance< Object >( tag );
70 }
71 
72 //! Default instantiation for default_request_handler_t.
73 template <>
74 inline auto
76 {
78  std::false_type{} );
79 }
80 
81 //
82 // create_default_shared_object_instance
83 //
84 
85 //! Default instantiation for a specific type.
86 template < typename Object>
87 inline auto
89 {
92 }
93 
94 //! Default instantiation for default_request_handler_t.
95 template <>
96 inline auto
98 {
100  std::false_type{} );
101 }
102 
103 //
104 // ensure_created()
105 //
106 
107 //! Ensure that object was created.
108 template < typename Object >
109 auto
113 {
114  if( !mb_created_one )
116 
117  if( !mb_created_one )
118  throw exception_t{ fail_description };
119 
120  return mb_created_one;
121 }
122 
123 //
124 // unsure_created()
125 //
126 
127 //! Ensure that object was created.
128 template < typename Object >
129 auto
133 {
134  if( !mb_created_one )
136 
137  if( !mb_created_one )
138  throw exception_t{ fail_description };
139 
140  return mb_created_one;
141 }
142 
143 
144 //
145 // socket_type_dependent_settings_t
146 //
147 
148 //! Extra settings needed for working with socket.
149 template < typename Settings, typename Socket >
151 {
152 protected :
154 
155 public :
157 
160 
162  operator=(const socket_type_dependent_settings_t &) = default;
163 
166 
167  // No extra settings by default.
168 };
169 
170 //
171 // acceptor_options_t
172 //
173 
174 //! An adapter for setting acceptor options before running server.
175 /*!
176  Class hides an acceptor object and opens only set/get options API.
177  It is used as an argument for a user defined function-object
178  that can set custom options for acceptor.
179 */
181 {
182  public:
183  acceptor_options_t( asio_ns::ip::tcp::acceptor & acceptor )
184  : m_acceptor{ acceptor }
185  {}
186 
187  //! API for setting/getting options.
188  //! \{
189  template< typename Option >
190  void
191  set_option( const Option & option )
192  {
194  }
195 
196  template< typename Option >
197  void
198  set_option( const Option & option, asio_ns::error_code & ec )
199  {
201  }
202 
203  template< typename Option >
204  void
205  get_option( Option & option )
206  {
208  }
209 
210  template< typename Option >
211  void
212  get_option( Option & option, asio_ns::error_code & ec )
213  {
215  }
216  //! \}
217 
218  private:
220 };
221 
223 
224 template <>
225 inline auto
227 {
229  []( acceptor_options_t & options ){
231  } );
232 }
233 
234 //
235 // socket_options_t
236 //
237 
238 //! An adapter for setting acceptor options before running server.
239 /*!
240  Class hides a socket object and opens only set/get options API.
241  It is used as an argument for a user defined function-object
242  that can set custom options for socket.
243 */
245 {
246  public:
248  //! A reference on the most base class with interface of setting options.
250  : m_socket{ socket }
251  {}
252 
253  //! API for setting/getting options.
254  //! \{
255  template< typename Option >
256  void
258  {
260  }
261 
262  template< typename Option >
263  void
265  {
267  }
268 
269  template< typename Option >
270  void
272  {
274  }
275 
276  template< typename Option >
277  void
279  {
281  }
282  //! \}
283 
284  private:
285  //! A reference on the most base class with interface of setting options.
287 };
288 
290 
291 template <>
292 inline auto
294 {
295  return std::make_unique< socket_options_setter_t >( []( auto & ){} );
296 }
297 
298 //
299 // cleanup_functor_t
300 //
301 /*!
302  * \brief Type of holder for user's cleanup function.
303  */
304 using cleanup_functor_t = std::function< void(void) >;
305 
306 //
307 // connection_state_listener_holder_t
308 //
309 /*!
310  * @brief A special class for holding actual connection state listener.
311  *
312  * This class holds shared pointer to actual connection state listener
313  * and provides an actual implementation of
314  * check_valid_connection_state_listener_pointer() method.
315  *
316  * @since v.0.5.1
317  */
318 template< typename Listener >
320 {
321  static_assert(
322  noexcept( std::declval<Listener>().state_changed(
324  "Listener::state_changed() method should be noexcept" );
325 
327 
328  static constexpr bool has_actual_connection_state_listener = true;
329 
330  //! Checks that pointer to state listener is not null.
331  /*!
332  * Throws an exception if m_connection_state_listener is nullptr.
333  */
334  void
336  {
338  throw exception_t{ "connection state listener is not specified" };
339  }
340 };
341 
342 /*!
343  * @brief A special class for case when no-op state listener is used.
344  *
345  * Doesn't hold anything and contains empty
346  * check_valid_connection_state_listener_pointer() method.
347  *
348  * @since v.0.5.1
349  */
350 template<>
352 {
353  static constexpr bool has_actual_connection_state_listener = false;
354 
355  void
357  {
358  // Nothing to do.
359  }
360 };
361 
362 //
363 // ip_blocker_holder_t
364 //
365 /*!
366  * @brief A special class for holding actual IP-blocker object.
367  *
368  * This class holds shared pointer to actual IP-blocker
369  * and provides an actual implementation of
370  * check_valid_ip_blocker_pointer() method.
371  *
372  * @since v.0.5.1
373  */
374 template< typename Ip_Blocker >
376 {
377  static_assert(
378  noexcept( std::declval<Ip_Blocker>().inspect(
380  "Ip_Blocker::inspect() method should be noexcept" );
381 
382  static_assert(
383  std::is_same<
385  decltype(std::declval<Ip_Blocker>().inspect(
387  "Ip_Blocker::inspect() should return "
388  "restinio::ip_blocker::inspection_result_t" );
389 
391 
392  static constexpr bool has_actual_ip_blocker = true;
393 
394  //! Checks that pointer to IP-blocker is not null.
395  /*!
396  * Throws an exception if m_ip_blocker is nullptr.
397  */
398  void
400  {
401  if( !m_ip_blocker )
402  throw exception_t{ "IP-blocker is not specified" };
403  }
404 };
405 
406 /*!
407  * @brief A special class for case when no-op IP-blocker is used.
408  *
409  * Doesn't hold anything and contains empty
410  * check_valid_ip_blocker_pointer() method.
411  *
412  * @since v.0.5.1
413  */
414 template<>
416 {
417  static constexpr bool has_actual_ip_blocker = false;
418 
419  void
421  {
422  // Nothing to do.
423  }
424 };
425 
426 //
427 // basic_server_settings_t
428 //
429 
430 //! Basic container for http_server settings.
431 /*!
432  * It exists to provide ablity to create various derived classes
433  * like server_settings_t, run_on_this_thread_settings_t,
434  * run_on_this_thread_settings_t and so on.
435  *
436  * \tparam Derived A drived type. Reference to this derived type
437  * will be returned by setters.
438  *
439  * \tparam Traits A type with traits for http_server.
440  */
441 template<typename Derived, typename Traits>
445  typename Traits::connection_state_listener_t >
446  , protected ip_blocker_holder_t< typename Traits::ip_blocker_t >
447 {
450 
451  static constexpr bool has_actual_connection_state_listener =
453  typename Traits::connection_state_listener_t
454  >::has_actual_connection_state_listener;
455 
456  static constexpr bool has_actual_ip_blocker =
457  ip_blocker_holder_t< typename Traits::ip_blocker_t >::
458  has_actual_ip_blocker;
459 
460  public:
462  std::uint16_t port = 8080,
463  asio_ns::ip::tcp protocol = asio_ns::ip::tcp::v4() )
464  : base_type_t{}
465  , m_port{ port }
466  , m_protocol{ protocol }
467  {}
468 
469  //! Server endpoint.
470  //! \{
471  Derived &
472  port( std::uint16_t p ) &
473  {
474  m_port = p;
475  return reference_to_derived();
476  }
477 
478  Derived &&
479  port( std::uint16_t p ) &&
480  {
481  return std::move( this->port( p ) );
482  }
483 
484  std::uint16_t
485  port() const
486  {
487  return m_port;
488  }
489 
490  Derived &
491  protocol( asio_ns::ip::tcp p ) &
492  {
493  m_protocol = p;
494  return reference_to_derived();
495  }
496 
497  Derived &&
498  protocol( asio_ns::ip::tcp p ) &&
499  {
500  return std::move( this->protocol( p ) );
501  }
502 
503  asio_ns::ip::tcp
504  protocol() const
505  {
506  return m_protocol;
507  }
508 
509  Derived &
510  address( std::string addr ) &
511  {
512  m_address = std::move(addr);
513  return reference_to_derived();
514  }
515 
516  Derived &&
517  address( std::string addr ) &&
518  {
519  return std::move( this->address( std::move( addr ) ) );
520  }
521 
522  const std::string &
523  address() const
524  {
525  return m_address;
526  }
527  //! \}
528 
529  //! Size of buffer for io operations.
530  /*!
531  It limits a size of chunk that can be read from socket in a single
532  read operattion (async read).
533  */
534  //! {
535  Derived &
536  buffer_size( std::size_t s ) &
537  {
538  m_buffer_size = s;
539  return reference_to_derived();
540  }
541 
542  Derived &&
543  buffer_size( std::size_t s ) &&
544  {
545  return std::move( this->buffer_size( s ) );
546  }
547 
548  std::size_t
549  buffer_size() const
550  {
551  return m_buffer_size;
552  }
553  //! }
554 
555  //! A period for holding connection before completely receiving
556  //! new http-request. Starts counting since connection is establised
557  //! or a previous request was responsed.
558  /*!
559  Generaly it defines timeout for keep-alive connections.
560  */
561  //! \{
562  Derived &
563  read_next_http_message_timelimit( std::chrono::steady_clock::duration d ) &
564  {
566  return reference_to_derived();
567  }
568 
569  Derived &&
570  read_next_http_message_timelimit( std::chrono::steady_clock::duration d ) &&
571  {
572  return std::move( this->read_next_http_message_timelimit( std::move( d ) ) );
573  }
574 
577  {
579  }
580  //! \}
581 
582  //! A period of time wait for response to be written to socket.
583  //! \{
584  Derived &
585  write_http_response_timelimit( std::chrono::steady_clock::duration d ) &
586  {
588  return reference_to_derived();
589  }
590 
591  Derived &&
592  write_http_response_timelimit( std::chrono::steady_clock::duration d ) &&
593  {
594  return std::move( this->write_http_response_timelimit( std::move( d ) ) );
595  }
596 
599  {
601  }
602  //! \}
603 
604  //! A period of time that is given for a handler to create response.
605  //! \{
606  Derived &
607  handle_request_timeout( std::chrono::steady_clock::duration d ) &
608  {
610  return reference_to_derived();
611  }
612 
613  Derived &&
614  handle_request_timeout( std::chrono::steady_clock::duration d ) &&
615  {
616  return std::move( this->handle_request_timeout( std::move( d ) ) );
617  }
618 
621  {
623  }
624  //! \}
625 
626  //! Max pipelined requests able to receive on single connection.
627  //! \{
628  Derived &
629  max_pipelined_requests( std::size_t mpr ) &
630  {
632  return reference_to_derived();
633  }
634 
635  Derived &&
636  max_pipelined_requests( std::size_t mpr ) &&
637  {
638  return std::move( this->max_pipelined_requests( mpr ) );
639  }
640 
641  std::size_t
643  {
645  }
646  //! \}
647 
648 
649  //! Request handler.
650  //! \{
651  using request_handler_t = typename Traits::request_handler_t;
652 
653  Derived &
654  request_handler( std::unique_ptr< request_handler_t > handler ) &
655  {
657  return reference_to_derived();
658  }
659 
660  template< typename... Params >
661  Derived &
662  request_handler( Params &&... params ) &
663  {
664  return set_unique_instance(
666  std::forward< Params >( params )... );
667  }
668 
669 
670  template< typename... Params >
671  Derived &&
672  request_handler( Params &&... params ) &&
673  {
674  return std::move( this->request_handler( std::forward< Params >( params )... ) );
675  }
676 
679  {
680  return ensure_created(
682  "request handler must be set" );
683  }
684  //! \}
685 
686 
687  //! Timers manager.
688  //! \{
689  using timer_manager_t = typename Traits::timer_manager_t;
690  using timer_factory_t = typename timer_manager_t::factory_t;
691 
692  template< typename... Params >
693  Derived &
694  timer_manager( Params &&... params ) &
695  {
696  return set_unique_instance(
698  std::forward< Params >( params )... );
699  }
700 
701  template< typename... Params >
702  Derived &&
703  timer_manager( Params &&... params ) &&
704  {
705  return std::move( this->timer_manager( std::forward< Params >( params )... ) );
706  }
707 
710  {
711  return ensure_created(
713  "timer manager is not set" );
714  }
715  //! \}
716 
717  //! Logger.
718  //! \{
719  using logger_t = typename Traits::logger_t;
720 
721  template< typename... Params >
722  Derived &
723  logger( Params &&... params ) &
724  {
725  return set_unique_instance(
726  m_logger,
727  std::forward< Params >( params )... );
728  }
729 
730  template< typename... Params >
731  Derived &&
732  logger( Params &&... params ) &&
733  {
734  return std::move( this->logger( std::forward< Params >( params )... ) );
735  }
736 
739  {
740  return ensure_created(
741  std::move( m_logger ),
742  "logger must be set" );
743  }
744  //! \}
745 
746  //! Acceptor options setter.
747  //! \{
748  Derived &
749  acceptor_options_setter( acceptor_options_setter_t aos ) &
750  {
751  if( !aos )
752  throw exception_t{ "acceptor options setter cannot be empty" };
753 
754  return set_unique_instance(
756  std::move( aos ) );
757  }
758 
759  Derived &&
760  acceptor_options_setter( acceptor_options_setter_t aos ) &&
761  {
762  return std::move( this->acceptor_options_setter( std::move( aos ) ) );
763  }
764 
767  {
768  return ensure_created(
770  "acceptor options setter must be set" );
771  }
772  //! \}
773 
774  //! Socket options setter.
775  //! \{
776  Derived &
777  socket_options_setter( socket_options_setter_t sos ) &
778  {
779  if( !sos )
780  throw exception_t{ "socket options setter cannot be empty" };
781 
782  return set_unique_instance(
784  std::move( sos ) );
785  }
786 
787  Derived &&
788  socket_options_setter( socket_options_setter_t sos ) &&
789  {
790  return std::move( this->socket_options_setter( std::move( sos ) ) );
791  }
792 
795  {
796  return ensure_created(
798  "socket options setter must be set" );
799  }
800  //! \}
801 
802  //! Max number of running concurrent accepts.
803  /*!
804  When running server on N threads
805  then up to N accepts can be handled concurrently.
806  */
807  //! \{
808  Derived &
809  concurrent_accepts_count( std::size_t n ) &
810  {
811  if( 0 == n || 1024 < n )
812  throw exception_t{
813  fmt::format(
814  "invalid value for number of cuncurrent connects: {}",
815  n ) };
816 
818  return reference_to_derived();
819  }
820 
821  Derived &&
822  concurrent_accepts_count( std::size_t n ) &&
823  {
824  return std::move( this->concurrent_accepts_count( n ) );
825  }
826 
827  std::size_t
829  {
831  }
832  //! \}
833 
834  //! Do separate an accept operation and connection instantiation.
835  /*!
836  For the cases when a lot of connection can be fired by clients
837  in a short time interval, it is vital to accept connections
838  and initiate new accept operations as quick as possible.
839  So creating connection instance that involves allocations
840  and initialization can be done in a context that
841  is independent to acceptors one.
842  */
843  //! \{
844  Derived &
846  {
848  return reference_to_derived();
849  }
850 
851  Derived &&
852  separate_accept_and_create_connect( bool do_separate ) &&
853  {
855  }
856 
857  bool
859  {
861  }
862  //! \}
863 
864  //! Cleanup function.
865  //! \{
866  template< typename Func >
867  Derived &
868  cleanup_func( Func && func ) &
869  {
871  return reference_to_derived();
872  }
873 
874  template< typename Func >
875  Derived &&
876  cleanup_func( Func && func ) &&
877  {
878  return std::move(this->cleanup_func( std::forward<Func>(func) ));
879  }
880 
883  {
884  return std::move(m_cleanup_functor);
885  }
886  //! \}
887 
888  /*!
889  * @brief Setter for connection state listener.
890  *
891  * @note connection_state_listener() method should be called if
892  * user specify its type for connection_state_listener_t traits.
893  * For example:
894  * @code
895  * class my_state_listener_t {
896  * ...
897  * public:
898  * ...
899  * void state_changed(const restinio::connection_state::notice_t & notice) noexcept {
900  * ...
901  * }
902  * };
903  *
904  * struct my_traits_t : public restinio::default_traits_t {
905  * using connection_state_listener_t = my_state_listener_t;
906  * };
907  *
908  * restinio::server_setting_t<my_traits_t> settings;
909  * setting.connection_state_listener( std::make_shared<my_state_listener_t>(...) );
910  * ...
911  * @endcode
912  *
913  * @attention This method can't be called if the default no-op
914  * state listener is used in server traits.
915  *
916  * @since v.0.5.1
917  */
918  Derived &
920  std::shared_ptr< typename Traits::connection_state_listener_t > listener ) &
921  {
922  static_assert(
924  "connection_state_listener(listener) can't be used "
925  "for the default connection_state::noop_listener_t" );
926 
928  return reference_to_derived();
929  }
930 
931  /*!
932  * @brief Setter for connection state listener.
933  *
934  * @note connection_state_listener() method should be called if
935  * user specify its type for connection_state_listener_t traits.
936  * For example:
937  * @code
938  * class my_state_listener_t {
939  * ...
940  * public:
941  * ...
942  * void state_changed(const restinio::connection_state::notice_t & notice) noexcept {
943  * ...
944  * }
945  * };
946  *
947  * struct my_traits_t : public restinio::default_traits_t {
948  * using connection_state_listener_t = my_state_listener_t;
949  * };
950  *
951  * restinio::run( restinio::on_this_thread<my_traits_t>()
952  * .connection_state_listener( std::make_shared<my_state_listener_t>(...) )
953  * .port(...)
954  * ...);
955  * @endcode
956  *
957  * @attention This method can't be called if the default no-op
958  * state listener is used in server traits.
959  *
960  * @since v.0.5.1
961  */
962  Derived &&
964  std::shared_ptr< typename Traits::connection_state_listener_t > listener ) &&
965  {
966  return std::move(this->connection_state_listener(
967  std::move(listener)));
968  }
969 
970  /*!
971  * @brief Get reference to connection state listener.
972  *
973  * @attention This method can't be called if the default no-op
974  * state listener is used in server traits.
975  *
976  * @since v.0.5.1
977  */
978  const std::shared_ptr< typename Traits::connection_state_listener_t > &
979  connection_state_listener() const noexcept
980  {
981  static_assert(
983  "connection_state_listener() can't be used "
984  "for the default connection_state::noop_listener_t" );
985 
986  return this->m_connection_state_listener;
987  }
988 
989  /*!
990  * @brief Internal method for checking presence of state listener
991  * object.
992  *
993  * If a user specifies custom state listener type but doesn't
994  * set a pointer to listener object that method throws an exception.
995  *
996  * @since v.0.5.1
997  */
998  void
1000  {
1002  }
1003 
1004  /*!
1005  * @brief Setter for IP-blocker.
1006  *
1007  * @note ip_blocker() method should be called if
1008  * user specify its type for ip_blocker_t traits.
1009  * For example:
1010  * @code
1011  * class my_ip_blocker_t {
1012  * ...
1013  * public:
1014  * ...
1015  * restinio::ip_blocker::inspection_result_t
1016  * inspect(const restinio::ip_blocker::incoming_info_t & info) noexcept {
1017  * ...
1018  * }
1019  * };
1020  *
1021  * struct my_traits_t : public restinio::default_traits_t {
1022  * using ip_blocker_t = my_ip_blocker_t;
1023  * };
1024  *
1025  * restinio::server_setting_t<my_traits_t> settings;
1026  * setting.ip_blocker( std::make_shared<my_ip_blocker_t>(...) );
1027  * ...
1028  * @endcode
1029  *
1030  * @attention This method can't be called if the default no-op
1031  * IP-blocker is used in server traits.
1032  *
1033  * @since v.0.5.1
1034  */
1035  Derived &
1037  std::shared_ptr< typename Traits::ip_blocker_t > blocker ) &
1038  {
1039  static_assert(
1041  "ip_blocker(blocker) can't be used "
1042  "for the default ip_blocker::noop_ip_blocker_t" );
1043 
1044  this->m_ip_blocker = std::move(blocker);
1045  return reference_to_derived();
1046  }
1047 
1048  /*!
1049  * @brief Setter for IP-blocker.
1050  *
1051  * @note ip_blocker() method should be called if
1052  * user specify its type for ip_blocker_t traits.
1053  * For example:
1054  * @code
1055  * class my_ip_blocker_t {
1056  * ...
1057  * public:
1058  * ...
1059  * restinio::ip_blocker::inspection_result_t
1060  * inspect(const restinio::ip_blocker::incoming_info_t & info) noexcept {
1061  * ...
1062  * }
1063  * };
1064  *
1065  * struct my_traits_t : public restinio::default_traits_t {
1066  * using ip_blocker_t = my_ip_blocker_t;
1067  * };
1068  *
1069  * restinio::run( restinio::on_this_thread<my_traits_t>()
1070  * .ip_blocker( std::make_shared<my_ip_blocker_t>(...) )
1071  * .port(...)
1072  * ...);
1073  * @endcode
1074  *
1075  * @attention This method can't be called if the default no-op
1076  * state listener is used in server traits.
1077  *
1078  * @since v.0.5.1
1079  */
1080  Derived &&
1082  std::shared_ptr< typename Traits::ip_blocker_t > blocker ) &&
1083  {
1084  return std::move(this->ip_blocker(std::move(blocker)));
1085  }
1086 
1087  /*!
1088  * @brief Get reference to IP-blocker.
1089  *
1090  * @attention This method can't be called if the default no-op
1091  * IP-blocker is used in server traits.
1092  *
1093  * @since v.0.5.1
1094  */
1095  const std::shared_ptr< typename Traits::ip_blocker_t > &
1096  ip_blocker() const noexcept
1097  {
1098  static_assert(
1100  "ip_blocker() can't be used "
1101  "for the default ip_blocker::noop_ip_blocker_t" );
1102 
1103  return this->m_ip_blocker;
1104  }
1105 
1106  /*!
1107  * @brief Internal method for checking presence of IP-blocker object.
1108  *
1109  * If a user specifies custom IP-blocker type but doesn't
1110  * set a pointer to blocker object that method throws an exception.
1111  *
1112  * @since v.0.5.1
1113  */
1114  void
1116  {
1118  }
1119 
1120  private:
1121  Derived &
1123  {
1124  return static_cast<Derived &>(*this);
1125  }
1126 
1127  template< typename Target, typename... Params >
1128  Derived &
1129  set_unique_instance( std::unique_ptr< Target > & t, Params &&... params )
1130  {
1131  t =
1132  std::make_unique< Target >(
1133  std::forward< Params >( params )... );
1134 
1135  return reference_to_derived();
1136  }
1137 
1138  template< typename Target, typename... Params >
1139  Derived &
1140  set_shared_instance( std::shared_ptr< Target > & t, Params &&... params )
1141  {
1142  t =
1143  std::make_shared< Target >(
1144  std::forward< Params >( params )... );
1145 
1146  return reference_to_derived();
1147  }
1148 
1149  //! Server endpoint.
1150  //! \{
1154  //! \}
1155 
1156  //! Size of buffer for io operations.
1157  std::size_t m_buffer_size{ 4 * 1024 };
1158 
1159  //! Operations timeouts.
1160  //! \{
1163 
1166 
1169  //! \}
1170 
1171  //! Max pipelined requests to receive on single connection.
1173 
1174  //! Request handler.
1176 
1177  //! Timers factory.
1179 
1180  //! Logger.
1182 
1183  //! Acceptor options setter.
1185 
1186  //! Socket options setter.
1188 
1190 
1191  //! Do separate an accept operation and connection instantiation.
1193 
1194  //! Optional cleanup functor.
1196 };
1197 
1198 //
1199 // server_settings_t
1200 //
1201 
1202 //! A fluent style interface for setting http server params.
1203 template<typename Traits = default_traits_t>
1205  : public basic_server_settings_t< server_settings_t<Traits>, Traits >
1206 {
1207  using base_type_t = basic_server_settings_t<
1208  server_settings_t<Traits>, Traits>;
1209 public:
1210  using base_type_t::base_type_t;
1211 };
1212 
1213 template < typename Traits, typename Configurator >
1214 auto
1215 exec_configurator( Configurator && configurator )
1216 {
1217  server_settings_t< Traits > result;
1218 
1219  configurator( result );
1220 
1221  return result;
1222 }
1223 
1224 } /* namespace restinio */
std::unique_ptr< logger_t > logger()
Definition: settings.hpp:738
Derived && timer_manager(Params &&... params) &&
Definition: settings.hpp:703
Derived & buffer_size(std::size_t s) &
Size of buffer for io operations.
Definition: settings.hpp:536
static constexpr bool has_actual_ip_blocker
Definition: settings.hpp:392
Basic container for http_server settings.
Definition: settings.hpp:442
auto create_default_unique_object_instance(std::true_type)
Definition: settings.hpp:37
Derived & write_http_response_timelimit(std::chrono::steady_clock::duration d) &
A period of time wait for response to be written to socket.
Definition: settings.hpp:585
std::size_t concurrent_accepts_count() const
Definition: settings.hpp:828
Derived && write_http_response_timelimit(std::chrono::steady_clock::duration d) &&
Definition: settings.hpp:592
std::unique_ptr< logger_t > m_logger
Logger.
Definition: settings.hpp:1181
std::unique_ptr< request_handler_t > m_request_handler
Request handler.
Definition: settings.hpp:1175
static constexpr bool has_actual_connection_state_listener
Definition: settings.hpp:451
void get_option(Option &option, asio_ns::error_code &ec)
Definition: settings.hpp:212
std::unique_ptr< acceptor_options_setter_t > m_acceptor_options_setter
Acceptor options setter.
Definition: settings.hpp:1184
Derived & ip_blocker(std::shared_ptr< typename Traits::ip_blocker_t > blocker) &
Setter for IP-blocker.
Definition: settings.hpp:1036
Derived && concurrent_accepts_count(std::size_t n) &&
Definition: settings.hpp:822
void set_option(const Option &option, asio_ns::error_code &ec)
Definition: settings.hpp:198
void check_valid_ip_blocker_pointer() const
Checks that pointer to IP-blocker is not null.
Definition: settings.hpp:399
void check_valid_connection_state_listener_pointer() const
Checks that pointer to state listener is not null.
Definition: settings.hpp:335
Derived & request_handler(std::unique_ptr< request_handler_t > handler) &
Definition: settings.hpp:654
Derived && separate_accept_and_create_connect(bool do_separate) &&
Definition: settings.hpp:852
Derived && address(std::string addr) &&
Definition: settings.hpp:517
Derived & logger(Params &&... params) &
Definition: settings.hpp:723
Derived & concurrent_accepts_count(std::size_t n) &
Max number of running concurrent accepts.
Definition: settings.hpp:809
acceptor_options_t(asio_ns::ip::tcp::acceptor &acceptor)
Definition: settings.hpp:183
static constexpr bool has_actual_connection_state_listener
Definition: settings.hpp:328
Derived && cleanup_func(Func &&func) &&
Definition: settings.hpp:876
std::size_t max_pipelined_requests() const
Definition: settings.hpp:642
std::unique_ptr< request_handler_t > request_handler()
Definition: settings.hpp:678
std::unique_ptr< timer_factory_t > timer_factory()
Definition: settings.hpp:709
auto create_default_unique_object_instance()
Default instantiation for a specific type.
Definition: settings.hpp:66
std::shared_ptr< Ip_Blocker > m_ip_blocker
Definition: settings.hpp:380
Derived && acceptor_options_setter(acceptor_options_setter_t aos) &&
Definition: settings.hpp:760
std::size_t buffer_size() const
Definition: settings.hpp:549
std::uint16_t m_port
Server endpoint.
Definition: settings.hpp:1151
Derived & cleanup_func(Func &&func) &
Cleanup function.
Definition: settings.hpp:868
Derived & acceptor_options_setter(acceptor_options_setter_t aos) &
Acceptor options setter.
Definition: settings.hpp:749
std::size_t m_buffer_size
Size of buffer for io operations.
Definition: settings.hpp:1157
auto create_default_shared_object_instance(std::true_type)
Definition: settings.hpp:52
Derived && connection_state_listener(std::shared_ptr< typename Traits::connection_state_listener_t > listener) &&
Setter for connection state listener.
Definition: settings.hpp:963
std::unique_ptr< socket_options_setter_t > m_socket_options_setter
Socket options setter.
Definition: settings.hpp:1187
Derived & set_shared_instance(std::shared_ptr< Target > &t, Params &&... params)
Definition: settings.hpp:1140
Derived && read_next_http_message_timelimit(std::chrono::steady_clock::duration d) &&
Definition: settings.hpp:570
A fluent style interface for setting http server params.
Definition: settings.hpp:1204
std::chrono::steady_clock::duration m_write_http_response_timelimit
Definition: settings.hpp:1165
Derived & separate_accept_and_create_connect(bool do_separate) &
Do separate an accept operation and connection instantiation.
Definition: settings.hpp:845
bool separate_accept_and_create_connect() const
Definition: settings.hpp:858
const std::string & address() const
Definition: settings.hpp:523
Derived & protocol(asio_ns::ip::tcp p) &
Definition: settings.hpp:491
const std::shared_ptr< typename Traits::ip_blocker_t > & ip_blocker() const noexcept
Get reference to IP-blocker.
Definition: settings.hpp:1096
Derived & timer_manager(Params &&... params) &
Definition: settings.hpp:694
Derived && max_pipelined_requests(std::size_t mpr) &&
Definition: settings.hpp:636
void ensure_valid_connection_state_listener()
Internal method for checking presence of state listener object.
Definition: settings.hpp:999
Derived && protocol(asio_ns::ip::tcp p) &&
Definition: settings.hpp:498
void ensure_valid_ip_blocker()
Internal method for checking presence of IP-blocker object.
Definition: settings.hpp:1115
Derived & set_unique_instance(std::unique_ptr< Target > &t, Params &&... params)
Definition: settings.hpp:1129
Derived & socket_options_setter(socket_options_setter_t sos) &
Socket options setter.
Definition: settings.hpp:777
std::chrono::steady_clock::duration write_http_response_timelimit() const
Definition: settings.hpp:598
basic_server_settings_t(std::uint16_t port=8080, asio_ns::ip::tcp protocol=asio_ns::ip::tcp::v4())
Definition: settings.hpp:461
Derived && logger(Params &&... params) &&
Definition: settings.hpp:732
cleanup_functor_t giveaway_cleanup_func()
Definition: settings.hpp:882
void set_option(const Option &option)
API for setting/getting options.
Definition: settings.hpp:191
A special class for holding actual connection state listener.
Definition: settings.hpp:319
std::uint16_t port() const
Definition: settings.hpp:485
Derived & read_next_http_message_timelimit(std::chrono::steady_clock::duration d) &
}
Definition: settings.hpp:563
std::chrono::steady_clock::duration m_read_next_http_message_timelimit
Operations timeouts.
Definition: settings.hpp:1162
auto exec_configurator(Configurator &&configurator)
Definition: settings.hpp:1215
A special class for holding actual IP-blocker object.
Definition: settings.hpp:375
std::size_t m_max_pipelined_requests
Max pipelined requests to receive on single connection.
Definition: settings.hpp:1172
Derived & handle_request_timeout(std::chrono::steady_clock::duration d) &
A period of time that is given for a handler to create response.
Definition: settings.hpp:607
An adapter for setting acceptor options before running server.
Definition: settings.hpp:180
std::unique_ptr< socket_options_setter_t > socket_options_setter()
Definition: settings.hpp:794
Derived && socket_options_setter(socket_options_setter_t sos) &&
Definition: settings.hpp:788
const std::shared_ptr< typename Traits::connection_state_listener_t > & connection_state_listener() const noexcept
Get reference to connection state listener.
Definition: settings.hpp:979
Derived && buffer_size(std::size_t s) &&
Definition: settings.hpp:543
std::chrono::steady_clock::duration m_handle_request_timeout
Definition: settings.hpp:1168
Derived & port(std::uint16_t p) &
Server endpoint.
Definition: settings.hpp:472
cleanup_functor_t m_cleanup_functor
Optional cleanup functor.
Definition: settings.hpp:1195
Derived & request_handler(Params &&... params) &
Definition: settings.hpp:662
Derived && request_handler(Params &&... params) &&
Definition: settings.hpp:672
static constexpr bool has_actual_ip_blocker
Definition: settings.hpp:456
Derived && port(std::uint16_t p) &&
Definition: settings.hpp:479
bool m_separate_accept_and_create_connect
Do separate an accept operation and connection instantiation.
Definition: settings.hpp:1192
Derived && ip_blocker(std::shared_ptr< typename Traits::ip_blocker_t > blocker) &&
Setter for IP-blocker.
Definition: settings.hpp:1081
Derived & max_pipelined_requests(std::size_t mpr) &
Max pipelined requests able to receive on single connection.
Definition: settings.hpp:629
std::unique_ptr< acceptor_options_setter_t > acceptor_options_setter()
Definition: settings.hpp:766
Derived && handle_request_timeout(std::chrono::steady_clock::duration d) &&
Definition: settings.hpp:614
asio_ns::ip::tcp protocol() const
Definition: settings.hpp:504
Derived & address(std::string addr) &
Definition: settings.hpp:510
asio_ns::ip::tcp::acceptor & m_acceptor
Definition: settings.hpp:219
Derived & connection_state_listener(std::shared_ptr< typename Traits::connection_state_listener_t > listener) &
Setter for connection state listener.
Definition: settings.hpp:919
std::shared_ptr< Listener > m_connection_state_listener
Definition: settings.hpp:324
std::chrono::steady_clock::duration read_next_http_message_timelimit() const
Definition: settings.hpp:576
std::unique_ptr< timer_factory_t > m_timer_factory
Timers factory.
Definition: settings.hpp:1178
void get_option(Option &option)
Definition: settings.hpp:205
std::enable_if< std::is_same< Parameter_Container, query_string_params_t >::value||std::is_same< Parameter_Container, router::route_params_t >::value, optional_t< Value_Type > >::type opt_value(const Parameter_Container &params, string_view_t key)
Gets the value of a parameter specified by key wrapped in optional_t<Value_Type> if parameter exists ...
Definition: value_or.hpp:64
std::chrono::steady_clock::duration handle_request_timeout() const
Definition: settings.hpp:620