forked from zhllxt/asio2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttps_session.hpp
More file actions
183 lines (153 loc) · 4.84 KB
/
https_session.hpp
File metadata and controls
183 lines (153 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* 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 <http://www.gnu.org/licenses/>)
*/
#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 <asio2/base/detail/push_options.hpp>
#include <asio2/http/http_session.hpp>
#include <asio2/tcp/component/ssl_stream_cp.hpp>
namespace asio2::detail
{
struct template_args_https_session : public template_args_http_session
{
using stream_t = websocket::stream<asio::ssl::stream<asio::ip::tcp::socket&>&>;
};
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 derived_t, class args_t>
class https_session_impl_t
: public http_session_impl_t<derived_t, args_t>
, public ssl_stream_cp <derived_t, args_t>
{
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 <derived_t, args_t>;
using self = https_session_impl_t<derived_t, args_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 <derived_t, args_t>;
using ssl_stream_comp = ssl_stream_cp<derived_t, args_t>;
using super::send;
using super::async_send;
public:
/**
* @constructor
*/
explicit https_session_impl_t(
http_router_t<derived_t> & router,
std::filesystem::path & root_directory,
bool is_arg0_session,
bool support_websocket,
asio::ssl::context & ctx,
session_mgr_t<derived_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<key_type>(this);
}
protected:
template<typename MatchCondition>
inline void _do_init(std::shared_ptr<derived_t> this_ptr, condition_wrap<MatchCondition> condition)
{
super::_do_init(std::move(this_ptr), condition);
this->derived()._ssl_init(condition, this->socket_, this->ctx_);
}
template<typename MatchCondition>
inline void _handle_connect(const error_code& ec, std::shared_ptr<derived_t> this_ptr,
condition_wrap<MatchCondition> 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<derived_t> 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<derived_t>& 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<https_session, detail::template_args_https_session>
{
public:
using https_session_impl_t<https_session, detail::template_args_https_session>::https_session_impl_t;
};
}
#include <asio2/base/detail/pop_options.hpp>
#endif // !__ASIO2_HTTPS_SESSION_HPP__
#endif