-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathServerHttps.hpp
56 lines (49 loc) · 2.19 KB
/
ServerHttps.hpp
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
#pragma once
#include "ServerBase.hpp"
namespace WebServer {
typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> HTTPS;
template<typename socket_type = HTTPS>
class ServerHttps : public ServerBase<socket_type> {
public:
ServerHttps(unsigned short port,
size_t num_threads,
const std::string cert_file,
const std::string &private_key_file);
~ServerHttps();
private:
// HTTPS 会在 socket 这一步中对 IO 流进行加密
// 因此实现的 accept() 方法需要对 socket 用 ssl context 初始化
virtual void accept();
boost::asio::ssl::context context;
};
template<typename socket_type>
ServerHttps<socket_type>::ServerHttps(unsigned short port,
size_t num_threads,
const std::string cert_file,
const std::string &private_key_file) :
ServerBase<socket_type>::ServerBase(port, num_threads),
context(boost::asio::ssl::context::sslv23)
{
context.use_certificate_chain_file(cert_file);
context.use_private_key_file(private_key_file, boost::asio::ssl::context::pem);
}
template<typename socket_type>
ServerHttps<socket_type>::~ServerHttps() {
}
template<typename socket_type>
void ServerHttps<socket_type>::accept() {
std::shared_ptr<HTTPS> socket = std::make_shared<HTTPS>(ServerBase<socket_type>::m_io_service, context);
ServerBase<socket_type>::acceptor.async_accept((*socket).lowest_layer(),
[this, socket](const boost::system::error_code & ec) {
// 立即启动并接受一个新连接
accept();
if(!ec) {
(*socket).async_handshake(boost::asio::ssl::stream_base::server,
[this, socket](const boost::system::error_code & ec) {
Logger::LogNotification("HTTPS Connection established with remote address " + socketToIP(socket));
if(!ec) ServerBase<socket_type>::process_request_and_response(socket);
});
}
});
}
}