1 //
2 // connection.hpp
3 // ~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef HTTP_CONNECTION_HPP
12 #define HTTP_CONNECTION_HPP
13 
14 #include <array>
15 #include <memory>
16 #include <asio.hpp>
17 #include "reply.hpp"
18 #include "request.hpp"
19 #include "request_handler.hpp"
20 #include "request_parser.hpp"
21 
22 namespace http {
23 namespace server {
24 
25 class connection_manager;
26 
27 /// Represents a single connection from a client.
28 class connection
29   : public std::enable_shared_from_this<connection>
30 {
31 public:
32   connection(const connection&) = delete;
33   connection& operator=(const connection&) = delete;
34 
35   /// Construct a connection with the given socket.
36   explicit connection(asio::ip::tcp::socket socket,
37       connection_manager& manager, request_handler& handler);
38 
39   /// Start the first asynchronous operation for the connection.
40   void start();
41 
42   /// Stop all asynchronous operations associated with the connection.
43   void stop();
44 
45 private:
46   /// Perform an asynchronous read operation.
47   void do_read();
48 
49   /// Perform an asynchronous write operation.
50   void do_write();
51 
52   /// Socket for the connection.
53   asio::ip::tcp::socket socket_;
54 
55   /// The manager for this connection.
56   connection_manager& connection_manager_;
57 
58   /// The handler used to process the incoming request.
59   request_handler& request_handler_;
60 
61   /// Buffer for incoming data.
62   std::array<char, 8192> buffer_;
63 
64   /// The incoming request.
65   request request_;
66 
67   /// The parser for the incoming request.
68   request_parser request_parser_;
69 
70   /// The reply to be sent back to the client.
71   reply reply_;
72 };
73 
74 typedef std::shared_ptr<connection> connection_ptr;
75 
76 } // namespace server
77 } // namespace http
78 
79 #endif // HTTP_CONNECTION_HPP
80