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