NetFlex  0.0.0
C++11 HTTP Server Library.
client.hpp
1 // The MIT License (MIT)
2 //
3 // Copyright (c) 2015-2017 Simon Ninon <simon.ninon@gmail.com>
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #pragma once
24 
25 #include <memory>
26 
27 #include <tacopie/tacopie>
28 
29 #include <netflex/http/request.hpp>
30 #include <netflex/http/response.hpp>
31 #include <netflex/parsing/request_parser.hpp>
32 
33 namespace netflex {
34 
35 namespace http {
36 
41 class client {
42 public:
48  explicit client(const std::shared_ptr<tacopie::tcp_client>& tcp_client);
49 
51  ~client(void) = default;
52 
54  client(const client&) = delete;
56  client& operator=(const client&) = delete;
57 
58 public:
62  const std::string& get_host(void) const;
63 
67  std::uint32_t get_port(void) const;
68 
69 public:
74  typedef std::function<void(bool, request&)> request_handler_t;
75 
79  typedef tacopie::tcp_client::disconnection_handler_t disconnection_handler_t;
80 
86  void set_request_handler(const request_handler_t& cb);
87 
93  void set_disconnection_handler(const disconnection_handler_t& cb);
94 
95 public:
100  void send_response(const response& response);
101 
102 private:
106  void call_request_received_callback(bool success, request& request);
107 
108 public:
114  void on_async_read_result(tacopie::tcp_client::read_result& res);
115 
116 private:
120  void async_read(void);
121 
122 private:
126  std::shared_ptr<tacopie::tcp_client> m_tcp_client;
127 
131  request_handler_t m_request_received_callback;
132 
136  parsing::request_parser m_parser;
137 };
138 
139 } // namespace http
140 
141 } // namespace netflex
const std::string & get_host(void) const
std::uint32_t get_port(void) const
void set_request_handler(const request_handler_t &cb)
~client(void)=default
default dtor
void send_response(const response &response)
client(const std::shared_ptr< tacopie::tcp_client > &tcp_client)
void on_async_read_result(tacopie::tcp_client::read_result &res)
Definition: client.hpp:41
tacopie::tcp_client::disconnection_handler_t disconnection_handler_t
Definition: client.hpp:79
Definition: client.hpp:33
std::function< void(bool, request &)> request_handler_t
Definition: client.hpp:74
Definition: request_parser.hpp:43
client & operator=(const client &)=delete
assignment operator
Definition: response.hpp:37
void set_disconnection_handler(const disconnection_handler_t &cb)
Definition: request.hpp:39