forked from zhllxt/asio2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_session.hpp
More file actions
446 lines (373 loc) · 13 KB
/
Copy pathhttp_session.hpp
File metadata and controls
446 lines (373 loc) · 13 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
* 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/>)
*/
#ifndef __ASIO2_HTTP_SESSION_HPP__
#define __ASIO2_HTTP_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/tcp/tcp_session.hpp>
#include <asio2/http/component/ws_stream_cp.hpp>
#include <asio2/http/impl/http_send_op.hpp>
#include <asio2/http/impl/http_recv_op.hpp>
#include <asio2/http/impl/ws_send_op.hpp>
#include <asio2/http/detail/http_router.hpp>
namespace asio2::detail
{
struct template_args_http_session : public template_args_tcp_session
{
using stream_t = websocket::stream<asio::ip::tcp::socket&>;
using body_t = http::string_body;
using buffer_t = beast::flat_buffer;
using send_data_t = std::string_view;
using recv_data_t = std::string_view;
};
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 = template_args_http_session>
class http_session_impl_t
: public tcp_session_impl_t<derived_t, args_t>
, public http_send_op <derived_t, args_t>
, public http_recv_op <derived_t, args_t>
, public ws_stream_cp <derived_t, args_t>
, public ws_send_op <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 = tcp_session_impl_t <derived_t, args_t>;
using self = http_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 send_data_t = typename args_t::send_data_t;
using recv_data_t = typename args_t::recv_data_t;
using ws_stream_comp = ws_stream_cp<derived_t, args_t>;
using super::send;
using super::async_send;
public:
/**
* @constructor
*/
explicit http_session_impl_t(
http_router_t<derived_t> & router,
std::filesystem::path & root_directory,
bool is_arg0_session,
bool support_websocket,
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(sessions, listener, rwio, init_buf_size, max_buf_size)
, http_send_op<derived_t, args_t>()
, ws_stream_cp<derived_t, args_t>()
, ws_send_op <derived_t, args_t>()
, req_()
, rep_()
, router_(router)
, root_directory_ (root_directory)
, is_arg0_session_ (is_arg0_session)
, support_websocket_(support_websocket)
{
this->silence_timeout_ = std::chrono::milliseconds(http_silence_timeout);
}
/**
* @destructor
*/
~http_session_impl_t()
{
}
protected:
/**
* @function : start the session for prepare to recv/send msg
*/
template<typename MatchCondition>
inline void start(condition_wrap<MatchCondition> condition)
{
this->rep_.root_directory(this->root_directory_);
this->rep_.defer_callback_ = [this, condition,
wptr = std::weak_ptr<derived_t>(this->selfptr())]() mutable
{
std::shared_ptr<derived_t> this_ptr = wptr.lock();
if (this_ptr)
{
this->derived()._send_response(std::move(this_ptr), std::move(condition));
}
};
super::start(std::move(condition));
}
public:
/**
* @function : get this object hash key,used for session map
*/
inline key_type hash_key() const
{
return reinterpret_cast<key_type>(this);
}
inline bool is_websocket() { return (bool(this->websocket_router_)); }
inline bool is_http() { return (!(this->is_websocket())); }
/**
* @function : get the request object
*/
inline const http::request & request() { return this->req_; }
/**
* @function : get the response object
*/
inline const http::response& response() { return this->rep_; }
protected:
inline http_router_t<derived_t>& _router()
{
return (this->router_);
}
inline void _handle_disconnect(const error_code& ec, std::shared_ptr<derived_t> this_ptr)
{
this->derived()._rdc_stop();
if (this->is_http())
{
super::_handle_disconnect(ec, std::move(this_ptr));
}
else
{
this->derived()._ws_stop(this_ptr, [this, ec, this_ptr]() mutable
{
super::_handle_disconnect(ec, std::move(this_ptr));
});
}
}
inline void _do_stop(const error_code& ec, std::shared_ptr<derived_t> this_ptr)
{
// call the base class _do_stop function
super::_do_stop(ec, std::move(this_ptr));
// reset the callback shared_ptr, to avoid the callback owned this self shared_ptr.
this->websocket_router_.reset();
}
template<typename MatchCondition>
inline void _send_response(std::shared_ptr<derived_t> this_ptr,
condition_wrap<MatchCondition> condition)
{
ASIO2_ASSERT(this->is_http());
if (!this->derived().io().strand().running_in_this_thread())
{
asio::post(this->derived().io().strand(), make_allocator(this->derived().wallocator(),
[this, this_ptr = std::move(this_ptr), condition = std::move(condition)]() mutable
{
this->derived()._send_response(std::move(this_ptr), std::move(condition));
}));
return;
}
if (this->is_websocket())
return;
this->derived().async_send(std::move(this->rep_.base()),
[this, this_ptr = std::move(this_ptr), condition = std::move(condition)]() mutable
{
this->derived()._post_recv(std::move(this_ptr), std::move(condition));
});
}
protected:
template<class Data, class Callback>
inline bool _do_send(Data& data, Callback&& callback)
{
if (this->is_websocket())
return this->derived()._ws_send(data, std::forward<Callback>(callback));
return this->derived()._http_send(data, std::forward<Callback>(callback));
}
template<class Data>
inline send_data_t _rdc_convert_to_send_data(Data& data)
{
ASIO2_ASSERT(this->websocket_router_ && "Only available in websocket mode");
//if (!this->is_websocket())
//{
// asio::detail::throw_error(asio::error::operation_not_supported);
//}
auto buffer = asio::buffer(data);
return send_data_t{ reinterpret_cast<
std::string_view::const_pointer>(buffer.data()),buffer.size() };
}
template<class Invoker>
inline void _rdc_invoke_with_none(const error_code& ec, Invoker& invoker)
{
ASIO2_ASSERT(this->websocket_router_ && "Only available in websocket mode");
if (this->is_websocket() && invoker)
invoker(ec, send_data_t{}, recv_data_t{});
}
template<class Invoker>
inline void _rdc_invoke_with_recv(const error_code& ec, Invoker& invoker, recv_data_t data)
{
ASIO2_ASSERT(this->websocket_router_ && "Only available in websocket mode");
if (this->is_websocket() && invoker)
invoker(ec, send_data_t{}, data);
}
template<class Invoker, class FnData>
inline void _rdc_invoke_with_send(const error_code& ec, Invoker& invoker, FnData& fn_data)
{
ASIO2_ASSERT(this->websocket_router_ && "Only available in websocket mode");
if (this->is_websocket() && invoker)
invoker(ec, fn_data(), recv_data_t{});
}
protected:
template<typename MatchCondition>
inline void _post_recv(std::shared_ptr<derived_t> this_ptr, condition_wrap<MatchCondition> condition)
{
this->derived()._http_post_recv(std::move(this_ptr), std::move(condition));
}
template<typename MatchCondition>
inline void _handle_recv(const error_code& ec, std::size_t bytes_recvd,
std::shared_ptr<derived_t> this_ptr, condition_wrap<MatchCondition> condition)
{
this->derived()._http_handle_recv(ec, bytes_recvd, std::move(this_ptr), std::move(condition));
}
template<typename MatchCondition>
inline void _handle_upgrade(const error_code & ec, std::shared_ptr<derived_t> self_ptr,
condition_wrap<MatchCondition> condition)
{
this->derived().sessions().post(
[this, ec, this_ptr = std::move(self_ptr), condition = std::move(condition)]
() mutable
{
try
{
set_last_error(ec);
this->derived()._fire_upgrade(this_ptr, ec);
asio::detail::throw_error(ec);
asio::post(this->derived().io().strand(), make_allocator(this->derived().wallocator(),
[this, this_ptr = std::move(this_ptr), condition = std::move(condition)]
() mutable
{
this->derived()._post_recv(std::move(this_ptr), std::move(condition));
}));
}
catch (system_error & e)
{
set_last_error(e);
this->derived()._do_disconnect(e.code());
}
});
}
template<typename MatchCondition>
inline bool _check_upgrade(std::shared_ptr<derived_t>& this_ptr,
condition_wrap<MatchCondition>& condition)
{
if (this->support_websocket_ && this->derived().is_http() && this->req_.is_upgrade())
{
this->websocket_router_ = this->router_.template _find<false>(this->req_, this->rep_);
if (this->websocket_router_)
{
this->req_.ws_frame_type_ = websocket::frame::open;
this->req_.ws_frame_data_ = {};
this->derived().silence_timeout_ = std::chrono::milliseconds(tcp_silence_timeout);
this->derived()._ws_init(condition, this->derived().stream());
this->derived()._ws_start(this_ptr, condition, this->derived().stream());
if (this->router_._route(this_ptr, this->req_, this->rep_))
{
this->derived()._post_control_callback(this_ptr, condition);
this->derived().push_event(
[this, this_ptr = std::move(this_ptr), condition = std::move(condition)]
(event_queue_guard<derived_t>&& g) mutable
{
detail::ignore_unused(g);
this->derived()._post_upgrade(
std::move(this_ptr), std::move(condition), this->req_.base());
});
return true;
}
else
{
ASIO2_ASSERT(false);
}
}
}
return false;
}
template<typename MatchCondition>
inline void _handle_control_ping(beast::string_view payload,
std::shared_ptr<derived_t> this_ptr, condition_wrap<MatchCondition> condition)
{
detail::ignore_unused(payload, this_ptr, condition);
this->req_.ws_frame_type_ = websocket::frame::ping;
this->req_.ws_frame_data_ = payload;
this->router_._route(this_ptr, this->req_, this->rep_);
}
template<typename MatchCondition>
inline void _handle_control_pong(beast::string_view payload,
std::shared_ptr<derived_t> this_ptr, condition_wrap<MatchCondition> condition)
{
detail::ignore_unused(payload, this_ptr, condition);
this->req_.ws_frame_type_ = websocket::frame::pong;
this->req_.ws_frame_data_ = payload;
this->router_._route(this_ptr, this->req_, this->rep_);
}
template<typename MatchCondition>
inline void _handle_control_close(beast::string_view payload,
std::shared_ptr<derived_t> this_ptr, condition_wrap<MatchCondition> condition)
{
detail::ignore_unused(payload, this_ptr, condition);
this->req_.ws_frame_type_ = websocket::frame::close;
this->req_.ws_frame_data_ = payload;
this->router_._route(this_ptr, this->req_, this->rep_);
this->derived()._do_disconnect(websocket::error::closed);
}
inline void _fire_upgrade(std::shared_ptr<derived_t>& this_ptr, error_code ec)
{
// the _fire_upgrade must be executed in the thread 0.
ASIO2_ASSERT(this->sessions().io().strand().running_in_this_thread());
this->listener_.notify(event_type::upgrade, this_ptr, ec);
}
template<typename MatchCondition>
inline void _fire_recv(std::shared_ptr<derived_t>& this_ptr, condition_wrap<MatchCondition>& condition)
{
if (this->is_arg0_session_)
this->listener_.notify(event_type::recv, this_ptr, this->req_, this->rep_);
else
this->listener_.notify(event_type::recv, this->req_, this->rep_);
this->router_._route(this_ptr, this->req_, this->rep_);
if (this->is_websocket())
this->derived()._rdc_handle_recv(this_ptr, this->req_.ws_frame_data_, condition);
if (this->rep_.defer_guard_)
{
this->rep_.defer_guard_.reset();
}
else
{
if (this->is_http())
this->derived()._send_response(this_ptr, condition);
}
}
protected:
http::request req_;
http::response rep_;
http_router_t<derived_t>& router_;
std::filesystem::path & root_directory_;
bool is_arg0_session_ = false;
bool support_websocket_ = false;
std::shared_ptr<typename http_router_t<derived_t>::optype> websocket_router_;
};
}
namespace asio2
{
template<class derived_t>
class http_session_t : public detail::http_session_impl_t<derived_t, detail::template_args_http_session>
{
public:
using detail::http_session_impl_t<derived_t, detail::template_args_http_session>::http_session_impl_t;
};
class http_session : public http_session_t<http_session>
{
public:
using http_session_t<http_session>::http_session_t;
};
}
#include <asio2/base/detail/pop_options.hpp>
#endif // !__ASIO2_HTTP_SESSION_HPP__