• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
teardown(role_type role,boost::asio::ssl::stream<AsyncStream> & stream,error_code & ec)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 {
ssl_shutdown_opboost::beast::detail::ssl_shutdown_op55     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
operator ()boost::beast::detail::ssl_shutdown_op65     operator()(Self& self, error_code ec = {}, std::size_t = 0)
66     {
67         BOOST_ASIO_CORO_REENTER(*this)
68         {
69             BOOST_ASIO_CORO_YIELD
70                 s_.async_shutdown(std::move(self));
71             ec_ = ec;
72 
73             using boost::beast::websocket::async_teardown;
74             BOOST_ASIO_CORO_YIELD
75                 async_teardown(role_, s_.next_layer(), std::move(self));
76             if (!ec_)
77                 ec_ = ec;
78 
79             self.complete(ec_);
80         }
81     }
82 
83 private:
84     boost::asio::ssl::stream<AsyncStream>& s_;
85     role_type role_;
86     error_code ec_;
87 };
88 
89 } // detail
90 
91 template<
92     class AsyncStream,
93     class TeardownHandler>
94 void
async_teardown(role_type role,boost::asio::ssl::stream<AsyncStream> & stream,TeardownHandler && handler)95 async_teardown(
96     role_type role,
97     boost::asio::ssl::stream<AsyncStream>& stream,
98     TeardownHandler&& handler)
99 {
100     return boost::asio::async_compose<TeardownHandler, void(error_code)>(
101         detail::ssl_shutdown_op<AsyncStream>(stream, role),
102         handler,
103         stream);
104 }
105 
106 } // beast
107 } // boost
108 
109 #endif
110