RESTinio
request_handler.hpp
Go to the documentation of this file.
1/*
2 restinio
3*/
4
9#pragma once
10
16
17#include <array>
18#include <functional>
19#include <iosfwd>
20
21namespace restinio
22{
23
24//
25// extra_data_buffer_t
26//
50template< typename Extra_Data >
52{
53 void * m_buffer;
54
55public:
56 extra_data_buffer_t( void * buffer ) : m_buffer{ buffer } {}
57
59 void *
60 get() const noexcept { return m_buffer; }
61};
62
63//
64// no_extra_data_factory_t
65//
75{
80 struct data_t {};
81
82 void
84 {
85 new(buffer.get()) data_t{};
86 }
87};
88
89//
90// simple_extra_data_factory_t
91//
115template< typename Extra_Data >
117{
118 using data_t = Extra_Data;
119
120 void
122 noexcept( noexcept(new(buffer.get()) data_t{}) )
123 {
124 new(buffer.get()) data_t{};
125 }
126};
127
128template< typename Extra_Data >
129class generic_request_t;
130
131namespace impl
132{
133
134template< typename Extra_Data >
136access_req_connection( generic_request_t<Extra_Data> & ) noexcept;
137
138//
139// generic_request_extra_data_holder_t
140//
151template< typename Extra_Data >
153{
154 alignas(Extra_Data) std::array<char, sizeof(Extra_Data)> m_data;
155
156public:
157 template< typename Factory >
159 Factory & factory )
160 {
161 factory.make_within( extra_data_buffer_t<Extra_Data>{ m_data.data() } );
162 }
163
165 {
166 get_ptr()->~Extra_Data();
167 }
168
170 Extra_Data *
171 get_ptr() noexcept
172 {
173 return reinterpret_cast<Extra_Data *>(m_data.data());
174 }
175
177 const Extra_Data *
178 get_ptr() const noexcept
179 {
180 return reinterpret_cast<const Extra_Data *>(m_data.data());
181 }
182};
183
184} /* namespace impl */
185
186//
187// generic_request_t
188//
189
191
198template< typename Extra_Data >
200 : public std::enable_shared_from_this< generic_request_t< Extra_Data > >
201{
202 template< typename UD >
205
206 public:
208
212 template< typename Extra_Data_Factory >
216 std::string body,
217 impl::connection_handle_t connection,
219 Extra_Data_Factory & extra_data_factory )
222 std::move( header ),
223 std::move( body ),
225 std::move( connection ),
227 extra_data_factory
228 }
229 {}
230
232
235 template< typename Extra_Data_Factory >
239 std::string body,
241 impl::connection_handle_t connection,
243 Extra_Data_Factory & extra_data_factory )
245 , m_header{ std::move( header ) }
246 , m_body{ std::move( body ) }
248 , m_connection{ std::move( connection ) }
251 , m_extra_data_holder{ extra_data_factory }
252 {}
253
256 header() const noexcept
257 {
258 return m_header;
259 }
260
262 const std::string &
263 body() const noexcept
264 {
265 return m_body;
266 }
267
268 template < typename Output = restinio_controlled_output_t >
269 auto
271 {
273
275 status_line,
279 }
280
282 auto request_id() const noexcept { return m_request_id; }
283
286
288 const endpoint_t & remote_endpoint() const noexcept { return m_remote_endpoint; }
289
291
300 {
301 return m_chunked_input_info.get();
302 }
303
340 Extra_Data &
341 extra_data() noexcept
342 {
343 return *m_extra_data_holder.get_ptr();
344 }
345
386 const Extra_Data &
387 extra_data() const noexcept
388 {
389 return *m_extra_data_holder.get_ptr();
390 }
391
392 private:
393 void
395 {
396 if( !m_connection )
397 {
398 throw exception_t{ "connection already moved" };
399 }
400 }
401
404 const std::string m_body;
405
407
414
417
420
428};
429
430template< typename Extra_Data >
431std::ostream &
433 std::ostream & o,
435{
436 o << "{req_id: " << req.request_id() << ", "
437 "conn_id: " << req.connection_id() << ", "
438 "path: " << req.header().path() << ", "
439 "query: " << req.header().query() << "}";
440
441 return o;
442}
443
445template< typename Extra_Data >
447 std::shared_ptr< generic_request_t< Extra_Data > >;
448
450
456
458
463using request_handle_t = std::shared_ptr< request_t >;
464
465//
466// default_request_handler_t
467//
468
470 std::function< request_handling_status_t ( request_handle_t ) >;
471
472namespace impl
473{
474
475template< typename Extra_Data >
478{
479 return req.m_connection;
480}
481
482} /* namespace impl */
483
484
485} /* 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.
#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