HttpsServer.cpp 1.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
/*
 * HttpsServer.cpp
 *
 *  Created on: 25.05.2018
 *      Author: Micha Mueller
 */

#include "HttpsServer.h"
#include <iostream>
10
11
#include <memory>
#include <functional>
12

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void HttpsServer::requestHandler::operator()(server::request const &request, server::connection_ptr connection) {
	server::string_type ip = source(request);
	unsigned int port = request.source_port;
	std::ostringstream data;

	server::response_header headers[] = { {"Connection", "close"}, {"Content-Type", "text/plain"} };
	data << "Hello, " << ip << ':' << port << '!' << std::endl;

	connection->set_status(server::connection::ok);
	connection->set_headers(boost::make_iterator_range(headers, headers + 2));
	connection->write(data.str());
}

void HttpsServer::requestHandler::log(const server::string_type& message) {
	LOG(error) << message;
}
29
30
31
32
33

HttpsServer::HttpsServer(const std::string& host, const std::string& port,
		pluginVector_t& plugins) :
		_host(host), _port(port), _plugins(plugins) {

34
35
36
37
38
39
40
41
42
43
	std::shared_ptr<asio::ssl::context> ctx = std::make_shared<asio::ssl::context>(asio::ssl::context::sslv23);
	ctx->set_options(asio::ssl::context::default_workarounds | asio::ssl::context::no_sslv3 | asio::ssl::context::single_dh_use);

	// Set keys
	// Currently we are only using the demo certificates provided by the OpenSSL lib...
	//ctx->set_password_callback(HttpsServer::password_callback);
	ctx->use_certificate_chain_file("../deps/openssl-1.0.2l/certs/demo/ca-cert.pem");
	ctx->use_private_key_file("../deps/openssl-1.0.2l/certs/demo/ca-cert.pem", asio::ssl::context::pem);
	ctx->use_tmp_dh_file("../deps/openssl-1.0.2l/crypto/dh/dh2048.pem");

44
	server::options options(_handler);
45
	_server = new server(options.address(_host).port(_port).context(ctx));
46
47
48
49
50
51
}

HttpsServer::~HttpsServer() {
	delete _server;
}

52
53
54
55
56
/*
std::string HttpsServer::password_callback(std::size_t max_length, asio::ssl::context_base::password_purpose purpose) {
    return std::string("pwd");
}
*/