forked from changkun/modern-cpp-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.https.hpp
66 lines (56 loc) · 2.37 KB
/
server.https.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
57
58
59
60
61
62
63
64
65
66
//
// server_https.hpp
// web_server
// created by changkun at changkun.de
// https://github.com/changkun/modern-cpp-tutorial/
//
#ifndef SERVER_HTTPS_HPP
#define SERVER_HTTPS_HPP
#include "server.http.hpp"
#include <boost/asio/ssl.hpp>
namespace Web {
// define HTTPS type
typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> HTTPS;
// define HTTPS service, template type is HTTPS
template<>
class Server<HTTPS> : public ServerBase<HTTPS> {
public:
// a HTTPS server requires two more parameters: certificate file and private key file
Server(unsigned short port, size_t num_threads,
const std::string& cert_file, const std::string& private_key_file) :
ServerBase<HTTPS>::ServerBase(port, num_threads),
context(boost::asio::ssl::context::sslv23) {
// use certificate file
context.use_certificate_chain_file(cert_file);
// use private key file, we need pass a new parameter to specify the format
context.use_private_key_file(private_key_file, boost::asio::ssl::context::pem);
}
private:
// compare to HTTP server, we must define ssl context object
boost::asio::ssl::context context;
// the difference between HTTPS and HTTP server
// is the construct difference of socket object
// HTTPS will encrypt the IO stream socket
// thus, accept() method must initialize ssl context
void accept() {
// create a new socket for current connection
// shared_ptr is used for passing temporal object to anonymous function
// socket will be deduce as std::shared_ptr<HTTPS>
auto socket = std::make_shared<HTTPS>(m_io_service, context);
acceptor.async_accept(
(*socket).lowest_layer(),
[this, socket](const boost::system::error_code& ec) {
// accept a new connection
accept();
// if no error
if(!ec) {
(*socket).async_handshake(boost::asio::ssl::stream_base::server,
[this, socket](const boost::system::error_code& ec) {
if(!ec) process_request_and_respond(socket);
});
}
});
}
};
}
#endif /* SERVER_HTTPS_HPP */