RESTinio
Loading...
Searching...
No Matches
request_handler.hpp
Go to the documentation of this file.
1/*
2 restinio
3*/
4
9#pragma once
10
12
18
19#include <array>
20#include <functional>
21#include <iosfwd>
22
23namespace restinio
24{
25
26//
27// extra_data_buffer_t
28//
52template< typename Extra_Data >
54{
55 void * m_buffer;
56
57public:
58 extra_data_buffer_t( void * buffer ) : m_buffer{ buffer } {}
59
61 void *
62 get() const noexcept { return m_buffer; }
63};
64
65//
66// no_extra_data_factory_t
67//
77{
82 struct data_t {};
83
84 void
86 {
87 new(buffer.get()) data_t{};
88 }
89};
90
91//
92// simple_extra_data_factory_t
93//
117template< typename Extra_Data >
119{
120 using data_t = Extra_Data;
121
122 void
124 noexcept( noexcept(new(buffer.get()) data_t{}) )
125 {
126 new(buffer.get()) data_t{};
127 }
128};
129
130template< typename Extra_Data >
131class generic_request_t;
132
133namespace impl
134{
135
136template< typename Extra_Data >
138access_req_connection( generic_request_t<Extra_Data> & ) noexcept;
139
140//
141// generic_request_extra_data_holder_t
142//
153template< typename Extra_Data >
155{
156 alignas(Extra_Data) std::array<char, sizeof(Extra_Data)> m_data;
157
158public:
159 template< typename Factory >
161 Factory & factory )
162 {
163 factory.make_within( extra_data_buffer_t<Extra_Data>{ m_data.data() } );
164 }
165
167 {
168 get_ptr()->~Extra_Data();
169 }
170
172 Extra_Data *
173 get_ptr() noexcept
174 {
175 // Because the content of m_data.data() is rewritten by
176 // placement new we have to use std::launder to avoid UB.
178 reinterpret_cast<Extra_Data *>(m_data.data()) );
179 }
180
182 const Extra_Data *
183 get_ptr() const noexcept
184 {
185 // Because the content of m_data.data() is rewritten by
186 // placement new we have to use std::launder to avoid UB.
188 reinterpret_cast<const Extra_Data *>(m_data.data()) );
189 }
190};
191
192} /* namespace impl */
193
194//
195// generic_request_t
196//
197
199
206template< typename Extra_Data >
208 : public std::enable_shared_from_this< generic_request_t< Extra_Data > >
209{
210 template< typename UD >
213
214 public:
216
220 template< typename Extra_Data_Factory >
224 std::string body,
225 impl::connection_handle_t connection,
227 Extra_Data_Factory & extra_data_factory )
230 std::move( header ),
231 std::move( body ),
233 std::move( connection ),
234 std::move( remote_endpoint ),
235 extra_data_factory
236 }
237 {}
238
240
243 template< typename Extra_Data_Factory >
247 std::string body,
249 impl::connection_handle_t connection,
251 Extra_Data_Factory & extra_data_factory )
253 , m_header{ std::move( header ) }
254 , m_body{ std::move( body ) }
256 , m_connection{ std::move( connection ) }
259 , m_extra_data_holder{ extra_data_factory }
260 {}
261
264 header() const noexcept
265 {
266 return m_header;
267 }
268
270 const std::string &
271 body() const noexcept
272 {
273 return m_body;
274 }
275
276 template < typename Output = restinio_controlled_output_t >
277 auto
279 {
281
283 status_line,
284 std::move( m_connection ),
287 }
288
290 auto request_id() const noexcept { return m_request_id; }
291
294
296 const endpoint_t & remote_endpoint() const noexcept { return m_remote_endpoint; }
297
299
308 {
309 return m_chunked_input_info.get();
310 }
311
348 Extra_Data &
349 extra_data() noexcept
350 {
351 return *m_extra_data_holder.get_ptr();
352 }
353
394 const Extra_Data &
395 extra_data() const noexcept
396 {
397 return *m_extra_data_holder.get_ptr();
398 }
399
400 private:
401 void
403 {
404 if( !m_connection )
405 {
406 throw exception_t{ "connection already moved" };
407 }
408 }
409
412 const std::string m_body;
413
415
422
425
428
436};
437
438template< typename Extra_Data >
439std::ostream &
441 std::ostream & o,
443{
444 o << "{req_id: " << req.request_id() << ", "
445 "conn_id: " << req.connection_id() << ", "
446 "path: " << req.header().path() << ", "
447 "query: " << req.header().query() << "}";
448
449 return o;
450}
451
453template< typename Extra_Data >
455 std::shared_ptr< generic_request_t< Extra_Data > >;
456
458
464
466
471using request_handle_t = std::shared_ptr< request_t >;
472
473//
474// default_request_handler_t
475//
476
478 std::function< request_handling_status_t ( request_handle_t ) >;
479
480namespace impl
481{
482
483template< typename Extra_Data >
486{
487 return req.m_connection;
488}
489
490} /* namespace impl */
491
492
493} /* namespace restinio */
Information about chunked encoded body.
Exception class for all exceptions thrown by RESTinio.
Definition: exception.hpp:26
Helper for holding a pointer to a buffer where a new object of type Extra_Data should be constructed.
RESTINIO_NODISCARD void * get() const noexcept
const http_request_header_t & header() const noexcept
Get request header.
const chunked_input_info_unique_ptr_t m_chunked_input_info
Optional description for chunked-encoding.
const std::string & body() const noexcept
Get request body.
auto request_id() const noexcept
Get request id.
generic_request_t(request_id_t request_id, http_request_header_t header, std::string body, impl::connection_handle_t connection, endpoint_t remote_endpoint, Extra_Data_Factory &extra_data_factory)
Old-format initializing constructor.
generic_request_t(request_id_t request_id, http_request_header_t header, std::string body, chunked_input_info_unique_ptr_t chunked_input_info, impl::connection_handle_t connection, endpoint_t remote_endpoint, Extra_Data_Factory &extra_data_factory)
New-format initializing constructor.
const endpoint_t & remote_endpoint() const noexcept
Get the remote endpoint of the underlying connection.
impl::generic_request_extra_data_holder_t< Extra_Data > m_extra_data_holder
An instance of extra-data that is incorporated into a request object.
RESTINIO_NODISCARD Extra_Data & extra_data() noexcept
Get writeable access to extra-data object incorporated into a request object.
connection_id_t connection_id() const noexcept
Get connection id.
RESTINIO_NODISCARD const Extra_Data & extra_data() const noexcept
Get readonly access to extra-data object incorporated into a request object.
const endpoint_t m_remote_endpoint
Remote endpoint for underlying connection.
auto create_response(http_status_line_t status_line=status_ok())
nullable_pointer_t< const chunked_input_info_t > chunked_input_info() const noexcept
Get optional info about chunked input.
impl::connection_handle_t m_connection
const http_request_header_t m_header
const request_id_t m_request_id
const connection_id_t m_connection_id
HTTP response header status line.
Helper class for holding a buffer for extra-data object to be incorporated into a request object.
RESTINIO_NODISCARD Extra_Data * get_ptr() noexcept
RESTINIO_NODISCARD const Extra_Data * get_ptr() const noexcept
std::array< char, sizeof(Extra_Data)> m_data
Forbid arbitrary response_builder_t instantiations.
Detection of compiler version and absence of various features.
#define RESTINIO_STD_LAUNDER(x)
#define RESTINIO_NODISCARD
connection_handle_t & access_req_connection(generic_request_t< Extra_Data > &) noexcept
std::shared_ptr< connection_base_t > connection_handle_t
Alias for http connection handle.
T * nullable_pointer_t
Type for pointer that can be nullptr.
std::unique_ptr< chunked_input_info_t > chunked_input_info_unique_ptr_t
Alias of unique_ptr for chunked_input_info.
std::shared_ptr< generic_request_t< Extra_Data > > generic_request_handle_t
An alias for shared-pointer to incoming request.
std::ostream & operator<<(std::ostream &o, response_parts_attr_t attr)
unsigned int request_id_t
Request id in scope of single connection.
http_status_line_t status_ok()
request_handling_status_t
Request handling status.
std::function< request_handling_status_t(request_handle_t) > default_request_handler_t
asio_ns::ip::tcp::endpoint endpoint_t
An alias for endpoint type from Asio.
std::shared_ptr< request_t > request_handle_t
An alias for handle for incoming request without additional extra-data.
std::uint64_t connection_id_t
Type for ID of connection.
STL namespace.
bool should_keep_alive() const noexcept
string_view_t path() const noexcept
Request URL-structure.
string_view_t query() const noexcept
Get the query part of the request URL.
A type of extra-data to be incorporated into a request object by the default.
The default extra-data-factory to be used in server's traits if a user doesn't specify own one.
void make_within(extra_data_buffer_t< data_t > buffer) noexcept
A helper template class for cases when extra-data-factory is just a simple stateless object.
void make_within(extra_data_buffer_t< data_t > buffer) noexcept(noexcept(new(buffer.get()) data_t{}))
#define const
Definition: zconf.h:230