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