RESTinio
traits.hpp
Go to the documentation of this file.
1 /*
2  restinio
3 */
4 
5 /*!
6  HTTP server traits.
7 */
8 
9 #pragma once
10 
11 #include <restinio/request_handler.hpp>
12 #include <restinio/asio_timer_manager.hpp>
13 #include <restinio/null_logger.hpp>
14 #include <restinio/connection_state_listener.hpp>
15 #include <restinio/ip_blocker.hpp>
16 
17 namespace restinio
18 {
19 
20 //
21 // traits_t
22 //
23 
24 template <
25  typename Timer_Manager,
26  typename Logger,
27  typename Request_Handler = default_request_handler_t,
28  typename Strand = asio_ns::strand< asio_ns::executor >,
29  typename Socket = asio_ns::ip::tcp::socket >
30 struct traits_t
31 {
32  /*!
33  * @brief A type for HTTP methods mapping.
34  *
35  * If RESTinio is used with vanila version of http_parser then
36  * the default value of http_methods_mapper_t is enough. But if a user
37  * uses modified version of http_parser with support of additional,
38  * not-standard HTTP methods then the user should provide its
39  * http_methods_mapper. For example:
40  * \code
41  * // Definition for non-standard HTTP methods.
42  * // Note: values of HTTP_ENCODE and HTTP_DECODE are from modified
43  * // http_parser version.
44  * constexpr const restinio::http_method_id_t http_encode(HTTP_ENCODE, "ENCODE");
45  * constexpr const restinio::http_method_id_t http_decode(HTTP_DECODE, "DECODE");
46  *
47  * // Definition of non-standard http_method_mapper.
48  * struct my_http_method_mapper {
49  * inline constexpr restinio::http_method_id_t
50  * from_nodejs(int value) noexcept {
51  * switch(value) {
52  * case HTTP_ENCODE: return http_encode;
53  * case HTTP_DECODE: return http_decode;
54  * default: return restinio::default_http_methods_t::from_nodejs(value);
55  * }
56  * }
57  * };
58  * ...
59  * // Make a custom traits with non-standard http_method_mapper.
60  * struct my_server_traits : public restinio::default_traits_t {
61  * using http_methods_mapper_t = my_http_method_mapper;
62  * };
63  * \endcode
64  *
65  * @since v.0.5.0
66  */
68 
69  /*!
70  * @brief A type for connection state listener.
71  *
72  * By default RESTinio doesn't inform about changes with connection state.
73  * But if a user specify its type of connection state listener then
74  * RESTinio will call this listener object when the state of connection
75  * changes.
76  *
77  * An example:
78  * @code
79  * // Definition of user's state listener.
80  * class my_state_listener {
81  * ...
82  * public:
83  * ...
84  * void state_changed(const restinio::connection_state::notice_t & notice) noexcept {
85  * ... // some reaction to state change.
86  * }
87  * };
88  *
89  * // Definition of custom traits for HTTP server.
90  * struct my_server_traits : public restinio::default_traits_t {
91  * using connection_state_listener_t = my_state_listener;
92  * };
93  * @endcode
94  *
95  * @since v.0.5.1
96  */
98 
99  /*!
100  * @brief A type for IP-blocker.
101  *
102  * By default RESTinio's accepts all incoming connections.
103  * But since v.0.5.1 a user can specify IP-blocker object that
104  * will be called for every new connection. This IP-blocker can
105  * deny or allow a new connection.
106  *
107  * Type of that IP-blocker object is specified by typedef
108  * ip_blocker_t.
109  *
110  * An example:
111  * @code
112  * // Definition of user's IP-blocker.
113  * class my_ip_blocker {
114  * ...
115  * public:
116  * ...
117  * restinio::ip_blocker::inspection_result_t
118  * state_changed(const restinio::ip_blocker::incoming_info_t & info) noexcept {
119  * ... // some checking for new connection.
120  * }
121  * };
122  *
123  * // Definition of custom traits for HTTP server.
124  * struct my_server_traits : public restinio::default_traits_t {
125  * using ip_blocker_t = my_ip_blocker;
126  * };
127  * @endcode
128  *
129  * @since v.0.5.1
130  */
132 
133  using timer_manager_t = Timer_Manager;
134  using logger_t = Logger;
135  using request_handler_t = Request_Handler;
136  using strand_t = Strand;
137  using stream_socket_t = Socket;
138 };
139 
141 
142 //
143 // single_thread_traits_t
144 //
145 
146 template <
147  typename Timer_Manager,
148  typename Logger,
152 
153 //
154 // default_traits_t
155 //
156 
158 
159 /*!
160  * \brief Default traits for single-threaded HTTP-server.
161  *
162  * Uses default timer manager. And null logger.
163  *
164  * Usage example:
165  * \code
166  * struct my_traits : public restinio::default_single_thread_traits_t {
167  * using logger_t = my_special_single_threaded_logger_type;
168  * };
169  * \endcode
170  *
171  * \since
172  * v.0.4.0
173  */
177 
178 } /* namespace restinio */
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