ssl.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #ifndef BOOST_BEAST_WEBSOCKET_IMPL_SSL_HPP
  10. #define BOOST_BEAST_WEBSOCKET_IMPL_SSL_HPP
  11. #include <utility>
  12. #include <boost/beast/websocket/teardown.hpp>
  13. #include <boost/asio/compose.hpp>
  14. #include <boost/asio/coroutine.hpp>
  15. namespace boost {
  16. namespace beast {
  17. /*
  18. See
  19. http://stackoverflow.com/questions/32046034/what-is-the-proper-way-to-securely-disconnect-an-asio-ssl-socket/32054476#32054476
  20. Behavior of ssl::stream regarding close_notify
  21. If the remote host calls async_shutdown then the
  22. local host's async_read will complete with eof.
  23. If both hosts call async_shutdown then the calls
  24. to async_shutdown will complete with eof.
  25. */
  26. template<class AsyncStream>
  27. void
  28. teardown(
  29. role_type role,
  30. boost::asio::ssl::stream<AsyncStream>& stream,
  31. error_code& ec)
  32. {
  33. stream.shutdown(ec);
  34. using boost::beast::websocket::teardown;
  35. error_code ec2;
  36. teardown(role, stream.next_layer(), ec ? ec2 : ec);
  37. }
  38. namespace detail {
  39. template<class AsyncStream>
  40. struct ssl_shutdown_op
  41. : boost::asio::coroutine
  42. {
  43. ssl_shutdown_op(
  44. boost::asio::ssl::stream<AsyncStream>& s,
  45. role_type role)
  46. : s_(s)
  47. , role_(role)
  48. {
  49. }
  50. template<class Self>
  51. void
  52. operator()(Self& self, error_code ec = {}, std::size_t = 0)
  53. {
  54. BOOST_ASIO_CORO_REENTER(*this)
  55. {
  56. self.reset_cancellation_state(net::enable_total_cancellation());
  57. BOOST_ASIO_CORO_YIELD
  58. s_.async_shutdown(std::move(self));
  59. ec_ = ec;
  60. using boost::beast::websocket::async_teardown;
  61. BOOST_ASIO_CORO_YIELD
  62. async_teardown(role_, s_.next_layer(), std::move(self));
  63. if (!ec_)
  64. ec_ = ec;
  65. self.complete(ec_);
  66. }
  67. }
  68. private:
  69. boost::asio::ssl::stream<AsyncStream>& s_;
  70. role_type role_;
  71. error_code ec_;
  72. };
  73. } // detail
  74. template<
  75. class AsyncStream,
  76. class TeardownHandler>
  77. void
  78. async_teardown(
  79. role_type role,
  80. boost::asio::ssl::stream<AsyncStream>& stream,
  81. TeardownHandler&& handler)
  82. {
  83. return boost::asio::async_compose<TeardownHandler, void(error_code)>(
  84. detail::ssl_shutdown_op<AsyncStream>(stream, role),
  85. handler,
  86. stream);
  87. }
  88. } // beast
  89. } // boost
  90. #endif