]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp11/http/server/connection.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / asio / example / cpp11 / http / server / connection.cpp
1 //
2 // connection.cpp
3 // ~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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 #include "connection.hpp"
12 #include <utility>
13 #include <vector>
14 #include "connection_manager.hpp"
15 #include "request_handler.hpp"
16
17 namespace http {
18 namespace server {
19
20 connection::connection(boost::asio::ip::tcp::socket socket,
21 connection_manager& manager, request_handler& handler)
22 : socket_(std::move(socket)),
23 connection_manager_(manager),
24 request_handler_(handler)
25 {
26 }
27
28 void connection::start()
29 {
30 do_read();
31 }
32
33 void connection::stop()
34 {
35 socket_.close();
36 }
37
38 void connection::do_read()
39 {
40 auto self(shared_from_this());
41 socket_.async_read_some(boost::asio::buffer(buffer_),
42 [this, self](boost::system::error_code ec, std::size_t bytes_transferred)
43 {
44 if (!ec)
45 {
46 request_parser::result_type result;
47 std::tie(result, std::ignore) = request_parser_.parse(
48 request_, buffer_.data(), buffer_.data() + bytes_transferred);
49
50 if (result == request_parser::good)
51 {
52 request_handler_.handle_request(request_, reply_);
53 do_write();
54 }
55 else if (result == request_parser::bad)
56 {
57 reply_ = reply::stock_reply(reply::bad_request);
58 do_write();
59 }
60 else
61 {
62 do_read();
63 }
64 }
65 else if (ec != boost::asio::error::operation_aborted)
66 {
67 connection_manager_.stop(shared_from_this());
68 }
69 });
70 }
71
72 void connection::do_write()
73 {
74 auto self(shared_from_this());
75 boost::asio::async_write(socket_, reply_.to_buffers(),
76 [this, self](boost::system::error_code ec, std::size_t)
77 {
78 if (!ec)
79 {
80 // Initiate graceful connection closure.
81 boost::system::error_code ignored_ec;
82 socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both,
83 ignored_ec);
84 }
85
86 if (ec != boost::asio::error::operation_aborted)
87 {
88 connection_manager_.stop(shared_from_this());
89 }
90 });
91 }
92
93 } // namespace server
94 } // namespace http