/* * COPYRIGHT (C) 2017-2021, zhllxt * * author : zhllxt * email : 37792738@qq.com * * Distributed under the GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 * (See accompanying file LICENSE or see ) */ #if defined(ASIO2_USE_SSL) #ifndef __ASIO2_HTTPS_SESSION_HPP__ #define __ASIO2_HTTPS_SESSION_HPP__ #if defined(_MSC_VER) && (_MSC_VER >= 1200) #pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include #include #include namespace asio2::detail { struct template_args_https_session : public template_args_http_session { using stream_t = websocket::stream&>; }; ASIO2_CLASS_FORWARD_DECLARE_BASE; ASIO2_CLASS_FORWARD_DECLARE_TCP_BASE; ASIO2_CLASS_FORWARD_DECLARE_TCP_SERVER; ASIO2_CLASS_FORWARD_DECLARE_TCP_SESSION; template class https_session_impl_t : public http_session_impl_t , public ssl_stream_cp { ASIO2_CLASS_FRIEND_DECLARE_BASE; ASIO2_CLASS_FRIEND_DECLARE_TCP_BASE; ASIO2_CLASS_FRIEND_DECLARE_TCP_SERVER; ASIO2_CLASS_FRIEND_DECLARE_TCP_SESSION; public: using super = http_session_impl_t ; using self = https_session_impl_t; using key_type = std::size_t; using body_type = typename args_t::body_t; using buffer_type = typename args_t::buffer_t; using ws_stream_comp = ws_stream_cp ; using ssl_stream_comp = ssl_stream_cp; using super::send; using super::async_send; public: /** * @constructor */ explicit https_session_impl_t( http_router_t & router, std::filesystem::path & root_directory, bool is_arg0_session, bool support_websocket, asio::ssl::context & ctx, session_mgr_t & sessions, listener_t & listener, io_t & rwio, std::size_t init_buf_size, std::size_t max_buf_size ) : super(router, root_directory, is_arg0_session, support_websocket, sessions, listener, rwio, init_buf_size, max_buf_size) , ssl_stream_comp(this->io_, ctx, asio::ssl::stream_base::server) , ctx_(ctx) { } /** * @destructor */ ~https_session_impl_t() { } /** * @function : get the stream object refrence */ inline typename ssl_stream_comp::stream_type & stream() { ASIO2_ASSERT(bool(this->ssl_stream_)); return (*(this->ssl_stream_)); } public: /** * @function : get this object hash key,used for session map */ inline key_type hash_key() const { return reinterpret_cast(this); } protected: template inline void _do_init(std::shared_ptr this_ptr, condition_wrap condition) { super::_do_init(std::move(this_ptr), condition); this->derived()._ssl_init(condition, this->socket_, this->ctx_); } template inline void _handle_connect(const error_code& ec, std::shared_ptr this_ptr, condition_wrap condition) { detail::ignore_unused(ec); asio::dispatch(this->derived().io().strand(), make_allocator(this->derived().wallocator(), [this, self_ptr = std::move(this_ptr), condition = std::move(condition)] () mutable { this->derived()._ssl_start(self_ptr, condition, this->socket_, this->ctx_); this->derived()._post_handshake(std::move(self_ptr), std::move(condition)); })); } inline void _handle_disconnect(const error_code& ec, std::shared_ptr this_ptr) { this->derived()._rdc_stop(); if (this->is_http()) { this->derived()._ssl_stop(this_ptr, [this, ec, this_ptr]() mutable { super::_handle_disconnect(ec, std::move(this_ptr)); }); } else { this->derived()._ws_stop(this_ptr, [this, ec, this_ptr]() mutable { this->derived()._ssl_stop(this_ptr, [this, ec, this_ptr]() mutable { super::super::_handle_disconnect(ec, std::move(this_ptr)); }); }); } } inline void _fire_handshake(std::shared_ptr& this_ptr, error_code ec) { // the _fire_handshake must be executed in the thread 0. ASIO2_ASSERT(this->sessions().io().strand().running_in_this_thread()); this->listener_.notify(event_type::handshake, this_ptr, ec); } protected: asio::ssl::context & ctx_; }; } namespace asio2 { class https_session : public detail::https_session_impl_t { public: using https_session_impl_t::https_session_impl_t; }; } #include #endif // !__ASIO2_HTTPS_SESSION_HPP__ #endif