Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
| 6 | * |
| 7 | * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
| 19 | */ |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 20 | // |
| 21 | // connection.cpp |
| 22 | // ~~~~~~~~~~~~~~ |
| 23 | // |
| 24 | // Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
| 25 | // |
| 26 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 27 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 28 | // |
| 29 | |
| 30 | #include "connection.hpp" |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 31 | #include "connection_manager.hpp" |
| 32 | #include "request_handler.hpp" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 33 | #include <boost/bind.hpp> |
| 34 | #include <vector> |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 35 | |
| 36 | namespace http { |
| 37 | namespace server { |
| 38 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 39 | connection::connection(boost::asio::io_service& io_service, connection_manager& manager, |
| 40 | request_handler& handler) |
| 41 | : socket_(io_service) |
| 42 | , connection_manager_(manager) |
| 43 | , request_handler_(handler) |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 47 | boost::asio::ip::tcp::socket& |
| 48 | connection::socket() |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 49 | { |
| 50 | return socket_; |
| 51 | } |
| 52 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 53 | void |
| 54 | connection::start() |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 55 | { |
| 56 | socket_.async_read_some(boost::asio::buffer(buffer_), |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 57 | boost::bind(&connection::handle_read, shared_from_this(), |
| 58 | boost::asio::placeholders::error, |
| 59 | boost::asio::placeholders::bytes_transferred)); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 62 | void |
| 63 | connection::stop() |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 64 | { |
| 65 | socket_.close(); |
| 66 | } |
| 67 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 68 | void |
| 69 | connection::handle_read(const boost::system::error_code& e, std::size_t bytes_transferred) |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 70 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 71 | if (!e) { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 72 | boost::tribool result; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 73 | boost::tie(result, boost::tuples::ignore) = |
| 74 | request_parser_.parse(request_, buffer_.data(), buffer_.data() + bytes_transferred); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 75 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 76 | if (result) { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 77 | request_handler_.handle_request(request_, reply_); |
| 78 | boost::asio::async_write(socket_, reply_.to_buffers(), |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 79 | boost::bind(&connection::handle_write, shared_from_this(), |
| 80 | boost::asio::placeholders::error)); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 81 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 82 | else if (!result) { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 83 | reply_ = reply::stock_reply(reply::bad_request); |
| 84 | boost::asio::async_write(socket_, reply_.to_buffers(), |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 85 | boost::bind(&connection::handle_write, shared_from_this(), |
| 86 | boost::asio::placeholders::error)); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 87 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 88 | else { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 89 | socket_.async_read_some(boost::asio::buffer(buffer_), |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 90 | boost::bind(&connection::handle_read, shared_from_this(), |
| 91 | boost::asio::placeholders::error, |
| 92 | boost::asio::placeholders::bytes_transferred)); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 93 | } |
| 94 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 95 | else if (e != boost::asio::error::operation_aborted) { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 96 | connection_manager_.stop(shared_from_this()); |
| 97 | } |
| 98 | } |
| 99 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 100 | void |
| 101 | connection::handle_write(const boost::system::error_code& e) |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 102 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 103 | if (!e) { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 104 | // Initiate graceful connection closure. |
| 105 | boost::system::error_code ignored_ec; |
| 106 | socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec); |
| 107 | } |
| 108 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 109 | if (e != boost::asio::error::operation_aborted) { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 110 | connection_manager_.stop(shared_from_this()); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | } // namespace server |
| 115 | } // namespace http |