ssl.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. BOOST_ASIO_CORO_YIELD
  57. s_.async_shutdown(std::move(self));
  58. ec_ = ec;
  59. using boost::beast::websocket::async_teardown;
  60. BOOST_ASIO_CORO_YIELD
  61. async_teardown(role_, s_.next_layer(), std::move(self));
  62. if (!ec_)
  63. ec_ = ec;
  64. self.complete(ec_);
  65. }
  66. }
  67. private:
  68. boost::asio::ssl::stream<AsyncStream>& s_;
  69. role_type role_;
  70. error_code ec_;
  71. };
  72. } // detail
  73. template<
  74. class AsyncStream,
  75. class TeardownHandler>
  76. void
  77. async_teardown(
  78. role_type role,
  79. boost::asio::ssl::stream<AsyncStream>& stream,
  80. TeardownHandler&& handler)
  81. {
  82. return boost::asio::async_compose<TeardownHandler, void(error_code)>(
  83. detail::ssl_shutdown_op<AsyncStream>(stream, role),
  84. handler,
  85. stream);
  86. }
  87. } // beast
  88. } // boost
  89. #endif