forked from zhllxt/asio2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttps_server.hpp
More file actions
150 lines (129 loc) · 4.24 KB
/
https_server.hpp
File metadata and controls
150 lines (129 loc) · 4.24 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
/*
* 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_SERVER_HPP__
#define __ASIO2_HTTPS_SERVER_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/tcps_server.hpp>
#include <asio2/http/https_session.hpp>
namespace asio2::detail
{
ASIO2_CLASS_FORWARD_DECLARE_BASE;
ASIO2_CLASS_FORWARD_DECLARE_TCP_BASE;
ASIO2_CLASS_FORWARD_DECLARE_TCP_SERVER;
template<class derived_t, class session_t>
class https_server_impl_t
: public tcps_server_impl_t<derived_t, session_t>
, public http_router_t <session_t >
{
ASIO2_CLASS_FRIEND_DECLARE_BASE;
ASIO2_CLASS_FRIEND_DECLARE_TCP_BASE;
ASIO2_CLASS_FRIEND_DECLARE_TCP_SERVER;
public:
using super = tcps_server_impl_t <derived_t, session_t>;
using self = https_server_impl_t<derived_t, session_t>;
using session_type = session_t;
public:
/**
* @constructor
*/
template<class... Args>
explicit https_server_impl_t(
asio::ssl::context::method method = asio::ssl::context::sslv23,
Args&&... args
)
: super(method, std::forward<Args>(args)...)
{
}
/**
* @destructor
*/
~https_server_impl_t()
{
this->stop();
}
/**
* @function : start the server
* @param host A string identifying a location. May be a descriptive name or
* a numeric address string.
* @param service A string identifying the requested service. This may be a
* descriptive name or a numeric string corresponding to a port number.
*/
template<typename String, typename StrOrInt, typename... Args>
inline bool start(String&& host, StrOrInt&& service, Args&&... args)
{
return this->derived()._do_start(std::forward<String>(host), std::forward<StrOrInt>(service),
condition_helper::make_condition('0', std::forward<Args>(args)...));
}
public:
/**
* @function : bind recv listener
* @param : fun - a user defined callback function
* Function signature : void(std::shared_ptr<asio2::https_session>& session_ptr,
* http::request& req, http::response& rep)
* or : void(http::request& req, http::response& rep)
*/
template<class F, class ...C>
inline derived_t & bind_recv(F&& fun, C&&... obj)
{
if constexpr (is_template_callable_v<F,
std::shared_ptr<session_t>&, http::request&, http::response&>)
{
this->is_arg0_session_ = true;
this->listener_.bind(event_type::recv, observer_t<std::shared_ptr<session_t>&,
http::request&, http::response&>(std::forward<F>(fun), std::forward<C>(obj)...));
}
else
{
this->is_arg0_session_ = false;
this->listener_.bind(event_type::recv, observer_t<
http::request&, http::response&>(std::forward<F>(fun), std::forward<C>(obj)...));
}
return (this->derived());
}
/**
* @function : bind websocket upgrade listener
* @param : fun - a user defined callback function
* Function signature : void(std::shared_ptr<asio2::http_session>& session_ptr, asio::error_code ec)
*/
template<class F, class ...C>
inline derived_t & bind_upgrade(F&& fun, C&&... obj)
{
this->listener_.bind(event_type::upgrade, observer_t<std::shared_ptr<session_t>&, error_code>
(std::forward<F>(fun), std::forward<C>(obj)...));
return (this->derived());
}
protected:
template<typename... Args>
inline std::shared_ptr<session_t> _make_session(Args&&... args)
{
return super::_make_session(std::forward<Args>(args)..., *this,
this->root_directory_, this->is_arg0_session_, this->support_websocket_);
}
protected:
bool is_arg0_session_ = false;
};
}
namespace asio2
{
template<class session_t>
class https_server_t : public detail::https_server_impl_t<https_server_t<session_t>, session_t>
{
public:
using detail::https_server_impl_t<https_server_t<session_t>, session_t>::https_server_impl_t;
};
using https_server = https_server_t<https_session>;
}
#include <asio2/base/detail/pop_options.hpp>
#endif // !__ASIO2_HTTPS_SERVER_HPP__
#endif