Imported Upstream version 1.64.0
[platform/upstream/boost.git] / libs / asio / example / cpp03 / 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 <vector>
13 #include <boost/bind.hpp>
14 #include "connection_manager.hpp"
15 #include "request_handler.hpp"
16
17 namespace http {
18 namespace server {
19
20 connection::connection(boost::asio::io_service& io_service,
21     connection_manager& manager, request_handler& handler)
22   : socket_(io_service),
23     connection_manager_(manager),
24     request_handler_(handler)
25 {
26 }
27
28 boost::asio::ip::tcp::socket& connection::socket()
29 {
30   return socket_;
31 }
32
33 void connection::start()
34 {
35   socket_.async_read_some(boost::asio::buffer(buffer_),
36       boost::bind(&connection::handle_read, shared_from_this(),
37         boost::asio::placeholders::error,
38         boost::asio::placeholders::bytes_transferred));
39 }
40
41 void connection::stop()
42 {
43   socket_.close();
44 }
45
46 void connection::handle_read(const boost::system::error_code& e,
47     std::size_t bytes_transferred)
48 {
49   if (!e)
50   {
51     boost::tribool result;
52     boost::tie(result, boost::tuples::ignore) = request_parser_.parse(
53         request_, buffer_.data(), buffer_.data() + bytes_transferred);
54
55     if (result)
56     {
57       request_handler_.handle_request(request_, reply_);
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 if (!result)
63     {
64       reply_ = reply::stock_reply(reply::bad_request);
65       boost::asio::async_write(socket_, reply_.to_buffers(),
66           boost::bind(&connection::handle_write, shared_from_this(),
67             boost::asio::placeholders::error));
68     }
69     else
70     {
71       socket_.async_read_some(boost::asio::buffer(buffer_),
72           boost::bind(&connection::handle_read, shared_from_this(),
73             boost::asio::placeholders::error,
74             boost::asio::placeholders::bytes_transferred));
75     }
76   }
77   else if (e != boost::asio::error::operation_aborted)
78   {
79     connection_manager_.stop(shared_from_this());
80   }
81 }
82
83 void connection::handle_write(const boost::system::error_code& e)
84 {
85   if (!e)
86   {
87     // Initiate graceful connection closure.
88     boost::system::error_code ignored_ec;
89     socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
90   }
91
92   if (e != boost::asio::error::operation_aborted)
93   {
94     connection_manager_.stop(shared_from_this());
95   }
96 }
97
98 } // namespace server
99 } // namespace http