1 #ifndef SERVER_HTTPS_HPP
2 #define SERVER_HTTPS_HPP
4 #include "server_http.h"
6 #ifdef USE_STANDALONE_ASIO
7 #include <asio/ssl.hpp>
9 #include <boost/asio/ssl.hpp>
13 #include <openssl/ssl.h>
16 using HTTPS = asio::ssl::stream<asio::ip::tcp::socket>;
20 std::string session_id_context;
21 bool set_session_id_context =
false;
24 Server(
const std::string &cert_file,
const std::string &private_key_file,
const std::string &verify_file = std::string())
26 context.use_certificate_chain_file(cert_file);
27 context.use_private_key_file(private_key_file, asio::ssl::context::pem);
29 if(verify_file.size() > 0) {
30 context.load_verify_file(verify_file);
31 context.set_verify_mode(asio::ssl::verify_peer | asio::ssl::verify_fail_if_no_peer_cert | asio::ssl::verify_client_once);
32 set_session_id_context =
true;
36 void start()
override {
37 if(set_session_id_context) {
39 session_id_context = std::to_string(
config.
port) +
':';
41 SSL_CTX_set_session_id_context(context.native_handle(),
reinterpret_cast<const unsigned char *
>(session_id_context.data()),
42 std::min<std::size_t>(session_id_context.size(), SSL_MAX_SSL_SESSION_ID_LENGTH));
48 asio::ssl::context context;
50 void accept()
override {
51 auto connection = create_connection(*
io_service, context);
53 acceptor->async_accept(connection->socket->lowest_layer(), [
this, connection](
const error_code &ec) {
54 auto lock = connection->handler_runner->continue_lock();
58 if(ec != asio::error::operation_aborted)
64 asio::ip::tcp::no_delay option(
true);
66 session->connection->socket->lowest_layer().set_option(option, ec);
69 session->connection->socket->async_handshake(asio::ssl::stream_base::server, [
this, session](
const error_code &ec) {
70 session->connection->cancel_timeout();
71 auto lock = session->connection->handler_runner->continue_lock();
75 this->read_request_and_content(session);
76 else if(this->on_error)
77 this->on_error(session->request, ec);
80 else if(this->on_error)
81 this->on_error(session->request, ec);
std::size_t max_request_streambuf_size
Definition: server_http.h:296
std::string address
Definition: server_http.h:299
Definition: server_http.h:48
long timeout_request
Timeout on request handling. Defaults to 5 seconds.
Definition: server_http.h:291
Definition: server_http.h:51
std::shared_ptr< asio::io_service > io_service
If you have your own asio::io_service, store its pointer here before running start().
Definition: server_http.h:329
unsigned short port
Port number to use. Defaults to 80 for HTTP and 443 for HTTPS.
Definition: server_http.h:286
Config config
Set before calling start().
Definition: server_http.h:304