Repository navigation
Expand file tree
/
Copy pathssl_server.cpp
More file actions
114 lines (95 loc) · 3.17 KB
/
Copy pathssl_server.cpp
File metadata and controls
114 lines (95 loc) · 3.17 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
/*
* Sample application based loosely on existing async server sample to
*demonstrate ssl
*
* Requires openssl lib to run (https://www.openssl.org/)
*
* (C) Copyright Jelle Van den Driessche 2014.
*
* Distributed under the Boost Software License, Version 1.0. (See copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
#include <memory>
#include <boost/network/protocol/http/server.hpp>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <iostream>
#include <signal.h>
struct handler;
typedef boost::network::http::server<handler> server;
std::string password_callback(
std::size_t max_length,
boost::asio::ssl::context_base::password_purpose purpose) {
return std::string("test");
}
/**
* request + connection encapsulation (work item)
*/
struct request_data {
const server::request req;
server::connection_ptr conn;
typedef std::shared_ptr<request_data> pointer;
request_data(server::request const& req, const server::connection_ptr& conn)
: req(req), conn(conn) {}
};
struct handler {
void operator()(server::request const& req,
const server::connection_ptr& conn) {
conn->set_status(server::connection::ok);
conn->write(std::string("Hello World!!"));
}
};
/**
* Clean shutdown signal handler
*
* @param error
* @param signal
* @param p_server_instance
*/
void shut_me_down(const boost::system::error_code& error, int signal,
std::shared_ptr<server> p_server_instance) {
if (!error) p_server_instance->stop();
}
int main(void) try {
// setup boost::asio::io_service
std::shared_ptr<boost::asio::io_service> p_io_service(
std::make_shared<boost::asio::io_service>());
// Initialize SSL context
std::shared_ptr<boost::asio::ssl::context> ctx =
std::make_shared<boost::asio::ssl::context>(
boost::asio::ssl::context::sslv23);
ctx->set_options(boost::asio::ssl::context::default_workarounds |
boost::asio::ssl::context::no_sslv2 |
boost::asio::ssl::context::single_dh_use);
// Set keys
ctx->set_password_callback(password_callback);
ctx->use_certificate_chain_file("server.pem");
ctx->use_private_key_file("server.pem", boost::asio::ssl::context::pem);
ctx->use_tmp_dh_file("dh2048.pem");
// setup the async server
handler request_handler;
std::shared_ptr<server> p_server_instance(std::make_shared<server>(
server::options(request_handler)
.address("0.0.0.0")
.port("8442")
.io_service(p_io_service)
.reuse_address(true)
.thread_pool(
std::make_shared<boost::network::utils::thread_pool>(2))
.context(ctx)));
// setup clean shutdown
boost::asio::signal_set signals(*p_io_service, SIGINT, SIGTERM);
signals.async_wait([=] (boost::system::error_code const &ec, int signal) {
shut_me_down(ec, signal, p_server_instance);
});
// run the async server
p_server_instance->run();
// we are stopped - shutting down
p_io_service->stop();
std::cout << "Terminated normally" << std::endl;
exit(EXIT_SUCCESS);
}
catch (const std::exception& e) {
std::cout << "Abnormal termination - exception:" << e.what() << std::endl;
exit(EXIT_FAILURE);
}