https_server.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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_SERVER_HPP__
  12. #define __ASIO2_HTTPS_SERVER_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/tcp/tcps_server.hpp>
  18. #include <asio2/http/https_session.hpp>
  19. namespace asio2::detail
  20. {
  21. ASIO2_CLASS_FORWARD_DECLARE_BASE;
  22. ASIO2_CLASS_FORWARD_DECLARE_TCP_BASE;
  23. ASIO2_CLASS_FORWARD_DECLARE_TCP_SERVER;
  24. template<class derived_t, class session_t>
  25. class https_server_impl_t
  26. : public tcps_server_impl_t<derived_t, session_t>
  27. , public http_router_t <session_t, typename session_t::args_type>
  28. {
  29. ASIO2_CLASS_FRIEND_DECLARE_BASE;
  30. ASIO2_CLASS_FRIEND_DECLARE_TCP_BASE;
  31. ASIO2_CLASS_FRIEND_DECLARE_TCP_SERVER;
  32. public:
  33. using super = tcps_server_impl_t <derived_t, session_t>;
  34. using self = https_server_impl_t<derived_t, session_t>;
  35. using session_type = session_t;
  36. public:
  37. /**
  38. * @brief constructor
  39. */
  40. template<class... Args>
  41. explicit https_server_impl_t(
  42. asio::ssl::context::method method,
  43. Args&&... args
  44. )
  45. : super(method, std::forward<Args>(args)...)
  46. {
  47. }
  48. /**
  49. * @brief constructor
  50. */
  51. template<class... Args>
  52. explicit https_server_impl_t(Args&&... args)
  53. : super(ASIO2_DEFAULT_SSL_METHOD, std::forward<Args>(args)...)
  54. {
  55. }
  56. /**
  57. * @brief constructor
  58. */
  59. explicit https_server_impl_t()
  60. : super(ASIO2_DEFAULT_SSL_METHOD)
  61. {
  62. }
  63. /**
  64. * @brief destructor
  65. */
  66. ~https_server_impl_t()
  67. {
  68. this->stop();
  69. }
  70. /**
  71. * @brief start the server
  72. * @param host - A string identifying a location. May be a descriptive name or
  73. * a numeric address string.
  74. * @param service - A string identifying the requested service. This may be a
  75. * descriptive name or a numeric string corresponding to a port number.
  76. */
  77. template<typename String, typename StrOrInt, typename... Args>
  78. inline bool start(String&& host, StrOrInt&& service, Args&&... args)
  79. {
  80. return this->derived()._do_start(std::forward<String>(host), std::forward<StrOrInt>(service),
  81. ecs_helper::make_ecs('0', std::forward<Args>(args)...));
  82. }
  83. public:
  84. /**
  85. * @brief bind recv listener
  86. * @param fun - a user defined callback function.
  87. * @li Function signature : void(std::shared_ptr<asio2::https_session>& session_ptr,
  88. * http::web_request& req, http::web_response& rep)
  89. * or : void(http::web_request& req, http::web_response& rep)
  90. */
  91. template<class F, class ...C>
  92. inline derived_t & bind_recv(F&& fun, C&&... obj)
  93. {
  94. if constexpr (detail::is_template_callable_v<F,
  95. std::shared_ptr<session_t>&, http::web_request&, http::web_response&>)
  96. {
  97. this->is_arg0_session_ = true;
  98. this->listener_.bind(event_type::recv, observer_t<std::shared_ptr<session_t>&,
  99. http::web_request&, http::web_response&>(std::forward<F>(fun), std::forward<C>(obj)...));
  100. }
  101. else
  102. {
  103. this->is_arg0_session_ = false;
  104. this->listener_.bind(event_type::recv, observer_t<
  105. http::web_request&, http::web_response&>(std::forward<F>(fun), std::forward<C>(obj)...));
  106. }
  107. return (this->derived());
  108. }
  109. /**
  110. * @brief bind websocket upgrade listener
  111. * @param fun - a user defined callback function.
  112. * @li Function signature : void(std::shared_ptr<asio2::http_session>& session_ptr)
  113. */
  114. template<class F, class ...C>
  115. inline derived_t & bind_upgrade(F&& fun, C&&... obj)
  116. {
  117. this->listener_.bind(event_type::upgrade, observer_t<std::shared_ptr<session_t>&>
  118. (std::forward<F>(fun), std::forward<C>(obj)...));
  119. return (this->derived());
  120. }
  121. protected:
  122. template<typename... Args>
  123. inline std::shared_ptr<session_t> _make_session(Args&&... args)
  124. {
  125. return super::_make_session(std::forward<Args>(args)..., *this,
  126. this->root_directory_, this->is_arg0_session_, this->support_websocket_);
  127. }
  128. inline void _handle_stop(const error_code& ec, std::shared_ptr<derived_t> this_ptr)
  129. {
  130. // can not use std::move(this_ptr), beacuse after handle stop with std::move(this_ptr),
  131. // this object maybe destroyed, then call "this" will crash.
  132. super::_handle_stop(ec, this_ptr);
  133. this->derived().dispatch([this]() mutable
  134. {
  135. // clear the http cache
  136. this->http_cache_.clear();
  137. });
  138. }
  139. protected:
  140. bool is_arg0_session_ = false;
  141. };
  142. }
  143. namespace asio2
  144. {
  145. template<class derived_t, class session_t>
  146. using https_server_impl_t = detail::https_server_impl_t<derived_t, session_t>;
  147. template<class session_t>
  148. class https_server_t : public detail::https_server_impl_t<https_server_t<session_t>, session_t>
  149. {
  150. public:
  151. using detail::https_server_impl_t<https_server_t<session_t>, session_t>::https_server_impl_t;
  152. };
  153. using https_server = https_server_t<https_session>;
  154. }
  155. #if defined(ASIO2_INCLUDE_RATE_LIMIT)
  156. #include <asio2/tcp/tcp_stream.hpp>
  157. namespace asio2
  158. {
  159. template<class session_t>
  160. class https_rate_server_t : public asio2::https_server_impl_t<https_rate_server_t<session_t>, session_t>
  161. {
  162. public:
  163. using asio2::https_server_impl_t<https_rate_server_t<session_t>, session_t>::https_server_impl_t;
  164. };
  165. using https_rate_server = https_rate_server_t<https_rate_session>;
  166. }
  167. #endif
  168. #include <asio2/base/detail/pop_options.hpp>
  169. #endif // !__ASIO2_HTTPS_SERVER_HPP__
  170. #endif