RESTinio
request_handler.hpp
Go to the documentation of this file.
1 /*
2  restinio
3 */
4 
5 /*!
6  HTTP-request handlers routine.
7 */
8 
9 #pragma once
10 
11 #include <functional>
12 #include <iosfwd>
13 
14 #include <restinio/exception.hpp>
15 #include <restinio/http_headers.hpp>
16 #include <restinio/message_builders.hpp>
17 #include <restinio/impl/connection_base.hpp>
18 
19 namespace restinio
20 {
21 
22 class request_t;
23 
24 namespace impl
25 {
26 
28 access_req_connection( request_t & ) noexcept;
29 
30 } /* namespace impl */
31 
32 //
33 // request_t
34 //
35 
36 //! HTTP Request data.
37 /*!
38  Provides acces to header and body, and creates response builder
39  for a given request.
40 */
41 class request_t final
42  : public std::enable_shared_from_this< request_t >
43 {
44  friend impl::connection_handle_t &
45  impl::access_req_connection( request_t & ) noexcept;
46 
47  public:
49  request_id_t request_id,
50  http_request_header_t header,
51  std::string body,
52  impl::connection_handle_t connection,
53  endpoint_t remote_endpoint )
55  , m_header{ std::move( header ) }
56  , m_body{ std::move( body ) }
60  {}
61 
62  //! Get request header.
63  const http_request_header_t &
64  header() const noexcept
65  {
66  return m_header;
67  }
68 
69  //! Get request body.
70  const std::string &
71  body() const noexcept
72  {
73  return m_body;
74  }
75 
76  template < typename Output = restinio_controlled_output_t >
77  auto
78  create_response( http_status_line_t status_line = status_ok() )
79  {
81 
82  return response_builder_t< Output >{
83  status_line,
84  std::move( m_connection ),
85  m_request_id,
86  m_header.should_keep_alive() };
87  }
88 
89  //! Get request id.
90  auto request_id() const noexcept { return m_request_id; }
91 
92  //! Get connection id.
93  connection_id_t connection_id() const noexcept { return m_connection_id; }
94 
95  //! Get the remote endpoint of the underlying connection.
96  const endpoint_t & remote_endpoint() const noexcept { return m_remote_endpoint; }
97 
98  private:
99  void
101  {
102  if( !m_connection )
103  {
104  throw exception_t{ "connection already moved" };
105  }
106  }
107 
110  const std::string m_body;
111 
114 
115  //! Remote endpoint for underlying connection.
117 };
118 
119 inline std::ostream &
120 operator << ( std::ostream & o, const request_t & req )
121 {
122  o << "{req_id: " << req.request_id() << ", "
123  "conn_id: " << req.connection_id() << ", "
124  "path: " << req.header().path() << ", "
125  "query: " << req.header().query() << "}";
126 
127  return o;
128 }
129 
130 //! Request handler, that is the type for calling request handlers.
132 
133 //
134 // default_request_handler_t
135 //
136 
139 
140 namespace impl
141 {
142 
143 inline connection_handle_t &
145 {
146  return req.m_connection;
147 }
148 
149 } /* namespace impl */
150 
151 
152 } /* namespace restinio */
request_t(request_id_t request_id, http_request_header_t header, std::string body, impl::connection_handle_t connection, endpoint_t remote_endpoint)
friend impl::connection_handle_t & impl::access_req_connection(request_t &) noexcept
const http_request_header_t m_header
std::ostream & operator<<(std::ostream &o, const request_t &req)
auto create_response(http_status_line_t status_line=status_ok())
const endpoint_t & remote_endpoint() const noexcept
Get the remote endpoint of the underlying connection.
const request_id_t m_request_id
const http_request_header_t & header() const noexcept
Get request header.
auto request_id() const noexcept
Get request id.
impl::connection_handle_t m_connection
connection_id_t connection_id() const noexcept
Get connection id.
const connection_id_t m_connection_id
const std::string m_body
const std::string & body() const noexcept
Get request body.
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
const endpoint_t m_remote_endpoint
Remote endpoint for underlying connection.