https_session.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (c) 2017-2023 zhllxt
  3. *
  4. * author : zhllxt
  5. * email : 37792738@qq.com
  6. *
  7. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. */
  10. #if defined(ASIO2_ENABLE_SSL) || defined(ASIO2_USE_SSL)
  11. #ifndef __ASIO2_HTTPS_SESSION_HPP__
  12. #define __ASIO2_HTTPS_SESSION_HPP__
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. #pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <asio2/base/detail/push_options.hpp>
  17. #include <asio2/http/http_session.hpp>
  18. #include <asio2/tcp/impl/ssl_stream_cp.hpp>
  19. namespace asio2::detail
  20. {
  21. struct template_args_https_session : public template_args_http_session
  22. {
  23. using stream_t = websocket::stream<asio::ssl::stream<typename template_args_http_session::socket_t&>&>;
  24. };
  25. ASIO2_CLASS_FORWARD_DECLARE_BASE;
  26. ASIO2_CLASS_FORWARD_DECLARE_TCP_BASE;
  27. ASIO2_CLASS_FORWARD_DECLARE_TCP_SERVER;
  28. ASIO2_CLASS_FORWARD_DECLARE_TCP_SESSION;
  29. template<class derived_t, class args_t = template_args_https_session>
  30. class https_session_impl_t
  31. : public http_session_impl_t<derived_t, args_t>
  32. , public ssl_stream_cp <derived_t, args_t>
  33. {
  34. ASIO2_CLASS_FRIEND_DECLARE_BASE;
  35. ASIO2_CLASS_FRIEND_DECLARE_TCP_BASE;
  36. ASIO2_CLASS_FRIEND_DECLARE_TCP_SERVER;
  37. ASIO2_CLASS_FRIEND_DECLARE_TCP_SESSION;
  38. public:
  39. using super = http_session_impl_t <derived_t, args_t>;
  40. using self = https_session_impl_t<derived_t, args_t>;
  41. using args_type = args_t;
  42. using key_type = std::size_t;
  43. using body_type = typename args_t::body_t;
  44. using buffer_type = typename args_t::buffer_t;
  45. using ws_stream_comp = ws_stream_cp <derived_t, args_t>;
  46. using ssl_stream_comp = ssl_stream_cp<derived_t, args_t>;
  47. using super::send;
  48. using super::async_send;
  49. public:
  50. /**
  51. * @brief constructor
  52. */
  53. explicit https_session_impl_t(
  54. http_router_t<derived_t, args_t> & router,
  55. std::filesystem::path & root_directory,
  56. bool is_arg0_session,
  57. bool support_websocket,
  58. asio::ssl::context & ctx,
  59. session_mgr_t<derived_t> & sessions,
  60. listener_t & listener,
  61. std::shared_ptr<io_t> rwio,
  62. std::size_t init_buf_size,
  63. std::size_t max_buf_size
  64. )
  65. : super(router, root_directory, is_arg0_session, support_websocket,
  66. sessions, listener, std::move(rwio), init_buf_size, max_buf_size)
  67. , ssl_stream_comp(ctx, asio::ssl::stream_base::server)
  68. , ctx_(ctx)
  69. {
  70. }
  71. /**
  72. * @brief destructor
  73. */
  74. ~https_session_impl_t()
  75. {
  76. }
  77. /**
  78. * @brief destroy the content of all member variables, this is used for solve the memory leaks.
  79. * After this function is called, this class object cannot be used again.
  80. */
  81. inline void destroy()
  82. {
  83. derived_t& derive = this->derived();
  84. derive.ws_stream_.reset();
  85. derive.ssl_stream_.reset();
  86. super::destroy();
  87. }
  88. /**
  89. * @brief get the stream object reference
  90. */
  91. inline typename ssl_stream_comp::ssl_stream_type & stream() noexcept
  92. {
  93. return this->derived().ssl_stream();
  94. }
  95. /**
  96. * @brief get the stream object reference
  97. */
  98. inline typename ssl_stream_comp::ssl_stream_type const& stream() const noexcept
  99. {
  100. return this->derived().ssl_stream();
  101. }
  102. public:
  103. /**
  104. * @brief get this object hash key,used for session map
  105. */
  106. inline key_type hash_key() const noexcept
  107. {
  108. return reinterpret_cast<key_type>(this);
  109. }
  110. protected:
  111. template<typename C>
  112. inline void _do_init(std::shared_ptr<derived_t>& this_ptr, std::shared_ptr<ecs_t<C>>& ecs)
  113. {
  114. this->derived()._ssl_init(ecs, this->derived().socket(), this->ctx_);
  115. super::_do_init(this_ptr, ecs);
  116. }
  117. template<typename C, typename DeferEvent>
  118. inline void _handle_connect(
  119. const error_code& ec,
  120. std::shared_ptr<derived_t> this_ptr, std::shared_ptr<ecs_t<C>> ecs, DeferEvent chain)
  121. {
  122. detail::ignore_unused(ec);
  123. derived_t& derive = this->derived();
  124. ASIO2_ASSERT(!ec);
  125. ASIO2_ASSERT(derive.sessions_.io_->running_in_this_thread());
  126. asio::dispatch(derive.io_->context(), make_allocator(derive.wallocator(),
  127. [&derive, this_ptr = std::move(this_ptr), ecs = std::move(ecs), chain = std::move(chain)]
  128. () mutable
  129. {
  130. derive._ssl_start(this_ptr, ecs, derive.socket(), derive.ctx_);
  131. derive._post_handshake(std::move(this_ptr), std::move(ecs), std::move(chain));
  132. }));
  133. }
  134. template<typename DeferEvent>
  135. inline void _post_shutdown(const error_code& ec, std::shared_ptr<derived_t> this_ptr, DeferEvent chain)
  136. {
  137. ASIO2_LOG_DEBUG("https_session::_post_shutdown: {} {}", ec.value(), ec.message());
  138. if (this->is_http())
  139. {
  140. this->derived()._ssl_stop(this_ptr, defer_event
  141. {
  142. [this, ec, this_ptr, e = chain.move_event()](event_queue_guard<derived_t> g) mutable
  143. {
  144. super::_post_shutdown(ec, std::move(this_ptr), defer_event(std::move(e), std::move(g)));
  145. }, chain.move_guard()
  146. });
  147. }
  148. else
  149. {
  150. this->derived()._ws_stop(this_ptr, defer_event
  151. {
  152. [this, ec, this_ptr, e = chain.move_event()](event_queue_guard<derived_t> g) mutable
  153. {
  154. this->derived()._ssl_stop(this_ptr, defer_event
  155. {
  156. [this, ec, this_ptr, e = std::move(e)](event_queue_guard<derived_t> g) mutable
  157. {
  158. super::super::_post_shutdown(
  159. ec, std::move(this_ptr), defer_event(std::move(e), std::move(g)));
  160. }, std::move(g)
  161. });
  162. }, chain.move_guard()
  163. });
  164. }
  165. }
  166. inline void _fire_handshake(std::shared_ptr<derived_t>& this_ptr)
  167. {
  168. // the _fire_handshake must be executed in the thread 0.
  169. ASIO2_ASSERT(this->sessions_.io_->running_in_this_thread());
  170. this->listener_.notify(event_type::handshake, this_ptr);
  171. }
  172. protected:
  173. asio::ssl::context & ctx_;
  174. };
  175. }
  176. namespace asio2
  177. {
  178. using https_session_args = detail::template_args_https_session;
  179. template<class derived_t, class args_t>
  180. using https_session_impl_t = detail::https_session_impl_t<derived_t, args_t>;
  181. template<class derived_t>
  182. class https_session_t : public detail::https_session_impl_t<derived_t, detail::template_args_https_session>
  183. {
  184. public:
  185. using detail::https_session_impl_t<derived_t, detail::template_args_https_session>::https_session_impl_t;
  186. };
  187. class https_session : public https_session_t<https_session>
  188. {
  189. public:
  190. using https_session_t<https_session>::https_session_t;
  191. };
  192. }
  193. #if defined(ASIO2_INCLUDE_RATE_LIMIT)
  194. #include <asio2/tcp/tcp_stream.hpp>
  195. namespace asio2
  196. {
  197. struct https_rate_session_args : public https_session_args
  198. {
  199. using socket_t = asio2::tcp_stream<asio2::simple_rate_policy>;
  200. using stream_t = websocket::stream<asio::ssl::stream<socket_t&>&>;
  201. };
  202. template<class derived_t>
  203. class https_rate_session_t : public asio2::https_session_impl_t<derived_t, https_rate_session_args>
  204. {
  205. public:
  206. using asio2::https_session_impl_t<derived_t, https_rate_session_args>::https_session_impl_t;
  207. };
  208. class https_rate_session : public asio2::https_rate_session_t<https_rate_session>
  209. {
  210. public:
  211. using asio2::https_rate_session_t<https_rate_session>::https_rate_session_t;
  212. };
  213. }
  214. #endif
  215. #include <asio2/base/detail/pop_options.hpp>
  216. #endif // !__ASIO2_HTTPS_SESSION_HPP__
  217. #endif