]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // connection.cpp | |
3 | // ~~~~~~~~~~~~~~ | |
4 | // | |
1e59de90 | 5 | // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
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 | #include "connection.hpp" | |
12 | #include <vector> | |
f67539c2 | 13 | #include <boost/bind/bind.hpp> |
7c673cae FG |
14 | #include "request_handler.hpp" |
15 | ||
16 | namespace http { | |
17 | namespace server2 { | |
18 | ||
b32b8144 | 19 | connection::connection(boost::asio::io_context& io_context, |
7c673cae | 20 | request_handler& handler) |
b32b8144 | 21 | : socket_(io_context), |
7c673cae FG |
22 | request_handler_(handler) |
23 | { | |
24 | } | |
25 | ||
26 | boost::asio::ip::tcp::socket& connection::socket() | |
27 | { | |
28 | return socket_; | |
29 | } | |
30 | ||
31 | void connection::start() | |
32 | { | |
33 | socket_.async_read_some(boost::asio::buffer(buffer_), | |
34 | boost::bind(&connection::handle_read, shared_from_this(), | |
35 | boost::asio::placeholders::error, | |
36 | boost::asio::placeholders::bytes_transferred)); | |
37 | } | |
38 | ||
39 | void connection::handle_read(const boost::system::error_code& e, | |
40 | std::size_t bytes_transferred) | |
41 | { | |
42 | if (!e) | |
43 | { | |
44 | boost::tribool result; | |
45 | boost::tie(result, boost::tuples::ignore) = request_parser_.parse( | |
46 | request_, buffer_.data(), buffer_.data() + bytes_transferred); | |
47 | ||
48 | if (result) | |
49 | { | |
50 | request_handler_.handle_request(request_, reply_); | |
51 | boost::asio::async_write(socket_, reply_.to_buffers(), | |
52 | boost::bind(&connection::handle_write, shared_from_this(), | |
53 | boost::asio::placeholders::error)); | |
54 | } | |
55 | else if (!result) | |
56 | { | |
57 | reply_ = reply::stock_reply(reply::bad_request); | |
58 | boost::asio::async_write(socket_, reply_.to_buffers(), | |
59 | boost::bind(&connection::handle_write, shared_from_this(), | |
60 | boost::asio::placeholders::error)); | |
61 | } | |
62 | else | |
63 | { | |
64 | socket_.async_read_some(boost::asio::buffer(buffer_), | |
65 | boost::bind(&connection::handle_read, shared_from_this(), | |
66 | boost::asio::placeholders::error, | |
67 | boost::asio::placeholders::bytes_transferred)); | |
68 | } | |
69 | } | |
70 | ||
71 | // If an error occurs then no new asynchronous operations are started. This | |
72 | // means that all shared_ptr references to the connection object will | |
73 | // disappear and the object will be destroyed automatically after this | |
74 | // handler returns. The connection class's destructor closes the socket. | |
75 | } | |
76 | ||
77 | void connection::handle_write(const boost::system::error_code& e) | |
78 | { | |
79 | if (!e) | |
80 | { | |
81 | // Initiate graceful connection closure. | |
82 | boost::system::error_code ignored_ec; | |
83 | socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec); | |
84 | } | |
85 | ||
86 | // No new asynchronous operations are started. This means that all shared_ptr | |
87 | // references to the connection object will disappear and the object will be | |
88 | // destroyed automatically after this handler returns. The connection class's | |
89 | // destructor closes the socket. | |
90 | } | |
91 | ||
92 | } // namespace server2 | |
93 | } // namespace http |