| 1 | // |
| 2 | // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | // |
| 7 | // Official repository: https://github.com/boostorg/beast |
| 8 | // |
| 9 | |
| 10 | #ifndef BOOST_BEAST_WEBSOCKET_IMPL_SSL_HPP |
| 11 | #define BOOST_BEAST_WEBSOCKET_IMPL_SSL_HPP |
| 12 | |
| 13 | #include <utility> |
| 14 | #include <boost/beast/websocket/teardown.hpp> |
| 15 | #include <boost/asio/compose.hpp> |
| 16 | #include <boost/asio/coroutine.hpp> |
| 17 | |
| 18 | namespace boost { |
| 19 | namespace beast { |
| 20 | |
| 21 | /* |
| 22 | |
| 23 | See |
| 24 | http://stackoverflow.com/questions/32046034/what-is-the-proper-way-to-securely-disconnect-an-asio-ssl-socket/32054476#32054476 |
| 25 | |
| 26 | Behavior of ssl::stream regarding close_notify |
| 27 | |
| 28 | If the remote host calls async_shutdown then the |
| 29 | local host's async_read will complete with eof. |
| 30 | |
| 31 | If both hosts call async_shutdown then the calls |
| 32 | to async_shutdown will complete with eof. |
| 33 | |
| 34 | */ |
| 35 | |
| 36 | template<class AsyncStream> |
| 37 | void |
| 38 | teardown( |
| 39 | role_type role, |
| 40 | boost::asio::ssl::stream<AsyncStream>& stream, |
| 41 | error_code& ec) |
| 42 | { |
| 43 | stream.shutdown(ec); |
| 44 | using boost::beast::websocket::teardown; |
| 45 | error_code ec2; |
| 46 | teardown(role, stream.next_layer(), ec ? ec2 : ec); |
| 47 | } |
| 48 | |
| 49 | namespace detail { |
| 50 | |
| 51 | template<class AsyncStream> |
| 52 | struct ssl_shutdown_op |
| 53 | : boost::asio::coroutine |
| 54 | { |
| 55 | ssl_shutdown_op( |
| 56 | boost::asio::ssl::stream<AsyncStream>& s, |
| 57 | role_type role) |
| 58 | : s_(s) |
| 59 | , role_(role) |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | template<class Self> |
| 64 | void |
| 65 | operator()(Self& self, error_code ec = {}, std::size_t = 0) |
| 66 | { |
| 67 | BOOST_ASIO_CORO_REENTER(*this) |
| 68 | { |
| 69 | self.reset_cancellation_state(net::enable_total_cancellation()); |
| 70 | |
| 71 | BOOST_ASIO_CORO_YIELD |
| 72 | s_.async_shutdown(std::move(self)); |
| 73 | ec_ = ec; |
| 74 | |
| 75 | using boost::beast::websocket::async_teardown; |
| 76 | BOOST_ASIO_CORO_YIELD |
| 77 | async_teardown(role_, s_.next_layer(), std::move(self)); |
| 78 | if (!ec_) |
| 79 | ec_ = ec; |
| 80 | |
| 81 | self.complete(ec_); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | boost::asio::ssl::stream<AsyncStream>& s_; |
| 87 | role_type role_; |
| 88 | error_code ec_; |
| 89 | }; |
| 90 | |
| 91 | } // detail |
| 92 | |
| 93 | template< |
| 94 | class AsyncStream, |
| 95 | class TeardownHandler> |
| 96 | void |
| 97 | async_teardown( |
| 98 | role_type role, |
| 99 | boost::asio::ssl::stream<AsyncStream>& stream, |
| 100 | TeardownHandler&& handler) |
| 101 | { |
| 102 | return boost::asio::async_compose<TeardownHandler, void(error_code)>( |
| 103 | detail::ssl_shutdown_op<AsyncStream>(stream, role), |
| 104 | handler, |
| 105 | stream); |
| 106 | } |
| 107 | |
| 108 | } // beast |
| 109 | } // boost |
| 110 | |
| 111 | #endif |
| 112 | |