From ec6614ba32a79c7a3ea63257e9ff16aa1d659991 Mon Sep 17 00:00:00 2001 From: Koncord Date: Sat, 20 May 2017 00:02:12 +0800 Subject: [PATCH] [Master] Add RestAPI --- apps/master/CMakeLists.txt | 2 +- apps/master/MasterServer.cpp | 5 + apps/master/MasterServer.hpp | 31 +- apps/master/RestServer.cpp | 191 +++++++++ apps/master/RestServer.hpp | 30 ++ apps/master/SimpleWeb/base_server.hpp | 511 +++++++++++++++++++++++++ apps/master/SimpleWeb/http_server.hpp | 55 +++ apps/master/SimpleWeb/https_server.hpp | 91 +++++ 8 files changed, 899 insertions(+), 17 deletions(-) create mode 100644 apps/master/RestServer.cpp create mode 100644 apps/master/RestServer.hpp create mode 100644 apps/master/SimpleWeb/base_server.hpp create mode 100644 apps/master/SimpleWeb/http_server.hpp create mode 100644 apps/master/SimpleWeb/https_server.hpp diff --git a/apps/master/CMakeLists.txt b/apps/master/CMakeLists.txt index 85b87048e..d29218d67 100644 --- a/apps/master/CMakeLists.txt +++ b/apps/master/CMakeLists.txt @@ -3,7 +3,7 @@ project(masterserver) set(CMAKE_CXX_STANDARD 14) include_directories("./") -set(SOURCE_FILES main.cpp MasterServer.cpp MasterServer.hpp) +set(SOURCE_FILES main.cpp MasterServer.cpp MasterServer.hpp RestServer.cpp RestServer.hpp) add_executable(masterserver ${SOURCE_FILES}) target_link_libraries(masterserver ${RakNet_LIBRARY} components) diff --git a/apps/master/MasterServer.cpp b/apps/master/MasterServer.cpp index f09c58ee3..b0f925331 100644 --- a/apps/master/MasterServer.cpp +++ b/apps/master/MasterServer.cpp @@ -208,3 +208,8 @@ void MasterServer::Wait() tMasterThread.join(); } } + +MasterServer::ServerMap *MasterServer::GetServers() +{ + return &servers; +} diff --git a/apps/master/MasterServer.hpp b/apps/master/MasterServer.hpp index 1b2a161be..c06a63263 100644 --- a/apps/master/MasterServer.hpp +++ b/apps/master/MasterServer.hpp @@ -13,33 +13,32 @@ class MasterServer { public: - MasterServer(unsigned short maxConnections, unsigned short port); - ~MasterServer(); - - void Start(); - void Stop(bool wait = false); - bool isRunning(); - void Wait(); - - struct SServer : QueryData - { - std::chrono::steady_clock::time_point lastUpdate; - }; - struct Ban { RakNet::SystemAddress sa; bool permanent; struct Date { - } date; }; - + struct SServer : QueryData + { + std::chrono::steady_clock::time_point lastUpdate; + }; typedef std::map ServerMap; - typedef ServerMap::const_iterator ServerCIter; + //typedef ServerMap::const_iterator ServerCIter; typedef ServerMap::iterator ServerIter; + MasterServer(unsigned short maxConnections, unsigned short port); + ~MasterServer(); + + void Start(); + void Stop(bool wait = false); + bool isRunning(); + void Wait(); + + ServerMap* GetServers(); + private: void Thread(); diff --git a/apps/master/RestServer.cpp b/apps/master/RestServer.cpp new file mode 100644 index 000000000..6730d46b0 --- /dev/null +++ b/apps/master/RestServer.cpp @@ -0,0 +1,191 @@ +// +// Created by koncord on 13.05.17. +// + +#include "RestServer.hpp" + +#include +#include + +using namespace std; +using namespace chrono; +using namespace boost::property_tree; + +static string response201 = "HTTP/1.1 201 Created\r\nContent-Length: 11\r\n\r\n201 Created"; +static string response202 = "HTTP/1.1 202 Accepted\r\nContent-Length: 12\r\n\r\n202 Accepted"; +static string response400 = "HTTP/1.1 400 Bad Request\r\nContent-Length: 15\r\n\r\n400 Bad Request"; + +inline void ResponseStr(HttpServer::Response &response, string content, string type = "", string code = "200 OK") +{ + response << "HTTP/1.1 " << code << "\r\n"; + if (!type.empty()) + response << "Content-Type: " << type <<"\r\n"; + response << "Content-Length: " << content.length() << "\r\n\r\n" << content; +} + +inline void ptreeToServer(boost::property_tree::ptree &pt, MasterServer::SServer &server) +{ + server.SetName(pt.get("hostname").c_str()); + server.SetGameMode(pt.get("modname").c_str()); + server.SetVersion(pt.get("version").c_str()); + server.SetPassword(pt.get("passw")); + //server.query_port = pt.get("query_port"); + server.SetPlayers(pt.get("players")); + server.SetMaxPlayers(pt.get("max_players")); +} + +inline void queryToStringStream(stringstream &ss, string addr, MasterServer::SServer &query) +{ + ss <<"\"" << addr << "\":{"; + ss << "\"modname\": \"" << query.GetGameMode() << "\"" << ", "; + ss << "\"passw\": " << (query.GetPassword() ? "true" : "false") << ", "; + ss << "\"hostname\": \"" << query.GetName() << "\"" << ", "; + ss << "\"query_port\": " << 0 << ", "; + ss << "\"last_update\": " << duration_cast(steady_clock::now() - query.lastUpdate).count() << ", "; + ss << "\"players\": " << query.GetPlayers() << ", "; + ss << "\"version\": \"" << query.GetVersion() << "\"" << ", "; + ss << "\"max_players\": " << query.GetMaxPlayers(); + ss << "}"; +} + +RestServer::RestServer(unsigned short port, MasterServer::ServerMap *pMap) : serverMap(pMap) +{ + httpServer.config.port = port; +} + +void RestServer::start() +{ + static const string ValidIpAddressRegex = "(?:[0-9]{1,3}\\.){3}[0-9]{1,3}"; + static const string ValidPortRegex = "(?:[0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$"; + static const string ServersRegex = "^/api/servers(?:/(" + ValidIpAddressRegex + "\\:" + ValidPortRegex + "))?"; + + httpServer.resource[ServersRegex]["GET"] = [this](auto response, auto request) { + if(request->path_match[1].length() > 0) + { + try + { + stringstream ss; + ss << "{"; + auto addr = request->path_match[1].str(); + auto port = (unsigned short)stoi(&(addr[addr.find(':')])); + queryToStringStream(ss, "server", serverMap->at(RakNet::SystemAddress(addr.c_str(), port))); + ss << "}"; + ResponseStr(*response, ss.str(), "application/json"); + } + catch(out_of_range e) + { + *response << response400; + } + } + else + { + static string str; + + //if(updatedCache) + { + stringstream ss; + ss << "{"; + ss << "\"list servers\":{"; + for (auto query = serverMap->begin(); query != serverMap->end(); query++) + { + queryToStringStream(ss, query->first.ToString(true, ':'), query->second); + if (next(query) != serverMap->end()) + ss << ", "; + } + ss << "}}"; + ResponseStr(*response, ss.str(), "application/json"); + updatedCache = false; + } + *response << str; + } + }; + + //Add query for < 0.6 servers + httpServer.resource[ServersRegex]["POST"] = [this](auto response, auto request) { + try + { + ptree pt; + read_json(request->content, pt); + + MasterServer::SServer server; + ptreeToServer(pt, server); + + unsigned short port = pt.get("query_port"); + serverMap->insert({RakNet::SystemAddress(request->remote_endpoint_address.c_str(), port), server}); + updatedCache = true; + + *response << response201; + } + catch (exception& e) + { + cout << e.what() << endl; + *response << response400; + } + }; + + //Update query for < 0.6 servers + httpServer.resource[ServersRegex]["PUT"] = [this](auto response, auto request) { + auto addr = request->path_match[1].str(); + auto port = (unsigned short)stoi(&(addr[addr.find(':')])); + + auto query = serverMap->find(RakNet::SystemAddress(addr.c_str(), port)); + + if(query == serverMap->end()) + { + cout << request->remote_endpoint_address + ": Trying to update a non-existent server or without permissions." << endl; + *response << response400; + return; + } + + if(request->content.size() != 0) + { + try + { + ptree pt; + read_json(request->content, pt); + + ptreeToServer(pt, query->second); + + updatedCache = true; + } + catch(exception &e) + { + cout << e.what() << endl; + *response << response400; + } + } + + query->second.lastUpdate = steady_clock::now(); + + *response << response202; + }; + + httpServer.resource["/api/servers/info"]["GET"] = [this](auto response, auto /*request*/) { + stringstream ss; + ss << '{'; + ss << "servers: " << serverMap->size(); + unsigned int players = 0; + for(auto s : *serverMap) + players += s.second.GetPlayers(); + ss << ", players: " << players; + ss << "}"; + + ResponseStr(*response, ss.str(), "application/json"); + }; + + httpServer.default_resource["GET"]=[](auto response, auto /*request*/) { + *response << response400; + }; + + httpServer.start(); +} + +void RestServer::cacheUpdated() +{ + updatedCache = true; +} + +void RestServer::stop() +{ + httpServer.stop(); +} diff --git a/apps/master/RestServer.hpp b/apps/master/RestServer.hpp new file mode 100644 index 000000000..1107660cf --- /dev/null +++ b/apps/master/RestServer.hpp @@ -0,0 +1,30 @@ +// +// Created by koncord on 13.05.17. +// + +#ifndef NEWRESTAPI_RESTSERVER_HPP +#define NEWRESTAPI_RESTSERVER_HPP + +#include +#include +#include "MasterServer.hpp" +#include "SimpleWeb/http_server.hpp" + +typedef SimpleWeb::Server HttpServer; + +class RestServer +{ +public: + RestServer(unsigned short port, MasterServer::ServerMap *pMap); + void start(); + void stop(); + void cacheUpdated(); + +private: + HttpServer httpServer; + MasterServer::ServerMap *serverMap; + bool updatedCache = true; +}; + + +#endif //NEWRESTAPI_RESTSERVER_HPP diff --git a/apps/master/SimpleWeb/base_server.hpp b/apps/master/SimpleWeb/base_server.hpp new file mode 100644 index 000000000..d540a5de9 --- /dev/null +++ b/apps/master/SimpleWeb/base_server.hpp @@ -0,0 +1,511 @@ +#ifndef BASE_SERVER_HPP +#define BASE_SERVER_HPP + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#ifndef CASE_INSENSITIVE_EQUALS_AND_HASH +#define CASE_INSENSITIVE_EQUALS_AND_HASH + +//Based on http://www.boost.org/doc/libs/1_60_0/doc/html/unordered/hash_equality.html +struct case_insensitive_equals +{ + bool operator()(const std::string &key1, const std::string &key2) const + { + return boost::algorithm::iequals(key1, key2); + } +}; + +struct case_insensitive_hash +{ + size_t operator()(const std::string &key) const + { + std::size_t seed = 0; + for (auto &c: key) + boost::hash_combine(seed, std::tolower(c)); + return seed; + } +}; + +#endif + +namespace SimpleWeb +{ + template + class Server; + + template + class ServerBase + { + public: + virtual ~ServerBase() + {} + + class Response : public std::ostream + { + friend class ServerBase; + + boost::asio::streambuf streambuf; + + std::shared_ptr socket; + + Response(const std::shared_ptr &socket) : std::ostream(&streambuf), socket(socket) + {} + + public: + size_t size() + { + return streambuf.size(); + } + + /// If true, force server to close the connection after the response have been sent. + /// + /// This is useful when implementing a HTTP/1.0-server sending content + /// without specifying the content length. + bool close_connection_after_response = false; + }; + + class Content : public std::istream + { + friend class ServerBase; + + public: + size_t size() + { + return streambuf.size(); + } + + std::string string() + { + std::stringstream ss; + ss << rdbuf(); + return ss.str(); + } + + private: + boost::asio::streambuf &streambuf; + + Content(boost::asio::streambuf &streambuf) : std::istream(&streambuf), streambuf(streambuf) + {} + }; + + class Request + { + friend class ServerBase; + + friend class Server; + + public: + std::string method, path, http_version; + + Content content; + + std::unordered_multimap header; + + std::smatch path_match; + + std::string remote_endpoint_address; + unsigned short remote_endpoint_port; + + private: + Request(const socket_type &socket) : content(streambuf) + { + try + { + remote_endpoint_address = socket.lowest_layer().remote_endpoint().address().to_string(); + remote_endpoint_port = socket.lowest_layer().remote_endpoint().port(); + } + catch (...) + {} + } + + boost::asio::streambuf streambuf; + }; + + class Config + { + friend class ServerBase; + + Config(unsigned short port) : port(port) + {} + + public: + /// Port number to use. Defaults to 80 for HTTP and 443 for HTTPS. + unsigned short port; + /// Number of threads that the server will use when start() is called. Defaults to 1 thread. + size_t thread_pool_size = 1; + /// Timeout on request handling. Defaults to 5 seconds. + size_t timeout_request = 5; + /// Timeout on content handling. Defaults to 300 seconds. + size_t timeout_content = 300; + /// IPv4 address in dotted decimal form or IPv6 address in hexadecimal notation. + /// If empty, the address will be any address. + std::string address; + /// Set to false to avoid binding the socket to an address that is already in use. Defaults to true. + bool reuse_address = true; + }; + + ///Set before calling start(). + Config config; + + private: + class regex_orderable : public std::regex + { + std::string str; + public: + regex_orderable(const char *regex_cstr) : std::regex(regex_cstr), str(regex_cstr) + {} + + regex_orderable(const std::string ®ex_str) : std::regex(regex_str), str(regex_str) + {} + + bool operator<(const regex_orderable &rhs) const + { + return str < rhs.str; + } + }; + + public: + /// Warning: do not add or remove resources after start() is called + std::map::Response>, + std::shared_ptr::Request>)>>> + resource; + + std::map::Response>, + std::shared_ptr::Request>)>> default_resource; + + std::function< + void(std::shared_ptr::Request>, + const boost::system::error_code &)> + on_error; + + std::function socket, + std::shared_ptr::Request>)> on_upgrade; + + virtual void start() + { + if (!io_service) + io_service = std::make_shared(); + + if (io_service->stopped()) + io_service->reset(); + + boost::asio::ip::tcp::endpoint endpoint; + if (config.address.size() > 0) + endpoint = boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(config.address), + config.port); + else + endpoint = boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), config.port); + + if (!acceptor) + acceptor = std::unique_ptr( + new boost::asio::ip::tcp::acceptor(*io_service)); + acceptor->open(endpoint.protocol()); + acceptor->set_option(boost::asio::socket_base::reuse_address(config.reuse_address)); + acceptor->bind(endpoint); + acceptor->listen(); + + accept(); + + //If thread_pool_size>1, start m_io_service.run() in (thread_pool_size-1) threads for thread-pooling + threads.clear(); + for (size_t c = 1; c < config.thread_pool_size; c++) + { + threads.emplace_back([this]() + { + io_service->run(); + }); + } + + //Main thread + if (config.thread_pool_size > 0) + io_service->run(); + + //Wait for the rest of the threads, if any, to finish as well + for (auto &t: threads) + { + t.join(); + } + } + + void stop() + { + acceptor->close(); + if (config.thread_pool_size > 0) + io_service->stop(); + } + + ///Use this function if you need to recursively send parts of a longer message + void send(const std::shared_ptr &response, + const std::function &callback = nullptr) const + { + boost::asio::async_write(*response->socket, response->streambuf, [this, response, callback] + (const boost::system::error_code &ec, size_t /*bytes_transferred*/) + { + if (callback) + callback(ec); + }); + } + + /// If you have your own boost::asio::io_service, store its pointer here before running start(). + /// You might also want to set config.thread_pool_size to 0. + std::shared_ptr io_service; + protected: + std::unique_ptr acceptor; + std::vector threads; + + ServerBase(unsigned short port) : config(port) + {} + + virtual void accept()=0; + + std::shared_ptr + get_timeout_timer(const std::shared_ptr &socket, long seconds) + { + if (seconds == 0) + return nullptr; + + auto timer = std::make_shared(*io_service); + timer->expires_from_now(boost::posix_time::seconds(seconds)); + timer->async_wait([socket](const boost::system::error_code &ec) + { + if (!ec) + { + boost::system::error_code ec; + socket->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec); + socket->lowest_layer().close(); + } + }); + return timer; + } + + void read_request_and_content(const std::shared_ptr &socket) + { + //Create new streambuf (Request::streambuf) for async_read_until() + //shared_ptr is used to pass temporary objects to the asynchronous functions + std::shared_ptr request(new Request(*socket)); + + //Set timeout on the following boost::asio::async-read or write function + auto timer = this->get_timeout_timer(socket, config.timeout_request); + + boost::asio::async_read_until(*socket, request->streambuf, "\r\n\r\n", [this, socket, request, timer] + (const boost::system::error_code &ec, + size_t bytes_transferred) + { + if (timer) + timer->cancel(); + if (!ec) + { + //request->streambuf.size() is not necessarily the same as bytes_transferred, from Boost-docs: + //"After a successful async_read_until operation, the streambuf may contain additional data beyond the delimiter" + //The chosen solution is to extract lines from the stream directly when parsing the header. What is left of the + //streambuf (maybe some bytes of the content) is appended to in the async_read-function below (for retrieving content). + size_t num_additional_bytes = + request->streambuf.size() - bytes_transferred; + + if (!this->parse_request(request)) + return; + + //If content, read that as well + auto it = request->header.find("Content-Length"); + if (it != request->header.end()) + { + unsigned long long content_length; + try + { + content_length = stoull(it->second); + } + catch (const std::exception &e) + { + if (on_error) + on_error(request, boost::system::error_code( + boost::system::errc::protocol_error, + boost::system::generic_category())); + return; + } + if (content_length > num_additional_bytes) + { + //Set timeout on the following boost::asio::async-read or write function + auto timer = this->get_timeout_timer(socket, + config.timeout_content); + boost::asio::async_read(*socket, request->streambuf, + boost::asio::transfer_exactly( + content_length - + num_additional_bytes), + [this, socket, request, timer] + (const boost::system::error_code &ec, + size_t /*bytes_transferred*/) + { + if (timer) + timer->cancel(); + if (!ec) + this->find_resource(socket, + request); + else if (on_error) + on_error(request, ec); + }); + } + else + this->find_resource(socket, request); + } + else + this->find_resource(socket, request); + } + else if (on_error) + on_error(request, ec); + }); + } + + bool parse_request(const std::shared_ptr &request) const + { + std::string line; + getline(request->content, line); + size_t method_end; + if ((method_end = line.find(' ')) != std::string::npos) + { + size_t path_end; + if ((path_end = line.find(' ', method_end + 1)) != std::string::npos) + { + request->method = line.substr(0, method_end); + request->path = line.substr(method_end + 1, path_end - method_end - 1); + + size_t protocol_end; + if ((protocol_end = line.find('/', path_end + 1)) != std::string::npos) + { + if (line.compare(path_end + 1, protocol_end - path_end - 1, "HTTP") != 0) + return false; + request->http_version = line.substr(protocol_end + 1, line.size() - protocol_end - 2); + } + else + return false; + + getline(request->content, line); + size_t param_end; + while ((param_end = line.find(':')) != std::string::npos) + { + size_t value_start = param_end + 1; + if ((value_start) < line.size()) + { + if (line[value_start] == ' ') + value_start++; + if (value_start < line.size()) + request->header.emplace(line.substr(0, param_end), + line.substr(value_start, line.size() - value_start - 1)); + } + + getline(request->content, line); + } + } + else + return false; + } + else + return false; + return true; + } + + void find_resource(const std::shared_ptr &socket, const std::shared_ptr &request) + { + //Upgrade connection + if (on_upgrade) + { + auto it = request->header.find("Upgrade"); + if (it != request->header.end()) + { + on_upgrade(socket, request); + return; + } + } + //Find path- and method-match, and call write_response + for (auto ®ex_method: resource) + { + auto it = regex_method.second.find(request->method); + if (it != regex_method.second.end()) + { + std::smatch sm_res; + if (std::regex_match(request->path, sm_res, regex_method.first)) + { + request->path_match = std::move(sm_res); + write_response(socket, request, it->second); + return; + } + } + } + auto it = default_resource.find(request->method); + if (it != default_resource.end()) + { + write_response(socket, request, it->second); + } + } + + void write_response(const std::shared_ptr &socket, const std::shared_ptr &request, + std::function::Response>, + std::shared_ptr< + typename ServerBase::Request>)> &resource_function) + { + //Set timeout on the following boost::asio::async-read or write function + auto timer = this->get_timeout_timer(socket, config.timeout_content); + + auto response = std::shared_ptr(new Response(socket), [this, request, timer] + (Response *response_ptr) + { + auto response = std::shared_ptr(response_ptr); + this->send(response, [this, response, request, timer]( + const boost::system::error_code &ec) + { + if (timer) + timer->cancel(); + if (!ec) + { + if (response->close_connection_after_response) + return; + + auto range = request->header.equal_range( + "Connection"); + for (auto it = range.first; it != range.second; it++) + { + if (boost::iequals(it->second, "close")) + { + return; + } + else if (boost::iequals(it->second, "keep-alive")) + { + this->read_request_and_content( + response->socket); + return; + } + } + if (request->http_version >= "1.1") + this->read_request_and_content(response->socket); + } + else if (on_error) + on_error(request, ec); + }); + }); + + try + { + resource_function(response, request); + } + catch (const std::exception &e) + { + if (on_error) + on_error(request, boost::system::error_code(boost::system::errc::operation_canceled, + boost::system::generic_category())); + return; + } + } + }; +} +#endif //BASE_SERVER_HPP diff --git a/apps/master/SimpleWeb/http_server.hpp b/apps/master/SimpleWeb/http_server.hpp new file mode 100644 index 000000000..99a5bbd4b --- /dev/null +++ b/apps/master/SimpleWeb/http_server.hpp @@ -0,0 +1,55 @@ +/* + * https://github.com/eidheim/Simple-Web-Server/ + * + * The MIT License (MIT) + * Copyright (c) 2014-2016 Ole Christian Eidheim + */ + +#ifndef SERVER_HTTP_HPP +#define SERVER_HTTP_HPP + +#include "base_server.hpp" + +namespace SimpleWeb +{ + + template + class Server : public ServerBase {}; + + typedef boost::asio::ip::tcp::socket HTTP; + + template<> + class Server : public ServerBase + { + public: + Server() : ServerBase::ServerBase(80) + {} + + protected: + virtual void accept() + { + //Create new socket for this connection + //Shared_ptr is used to pass temporary objects to the asynchronous functions + auto socket = std::make_shared(*io_service); + + acceptor->async_accept(*socket, [this, socket](const boost::system::error_code &ec) + { + //Immediately start accepting a new connection (if io_service hasn't been stopped) + if (ec != boost::asio::error::operation_aborted) + accept(); + + if (!ec) + { + boost::asio::ip::tcp::no_delay option(true); + socket->set_option(option); + + this->read_request_and_content(socket); + } + else if (on_error) + on_error(std::shared_ptr(new Request(*socket)), ec); + }); + } + }; +} + +#endif //SERVER_HTTP_HPP diff --git a/apps/master/SimpleWeb/https_server.hpp b/apps/master/SimpleWeb/https_server.hpp new file mode 100644 index 000000000..fb8268889 --- /dev/null +++ b/apps/master/SimpleWeb/https_server.hpp @@ -0,0 +1,91 @@ +#ifndef HTTPS_SERVER_HPP +#define HTTPS_SERVER_HPP + +#include "base_server.hpp" +#include +#include +#include + +namespace SimpleWeb +{ + typedef boost::asio::ssl::stream HTTPS; + + template<> + class Server : public ServerBase + { + std::string session_id_context; + bool set_session_id_context = false; + public: + Server(const std::string &cert_file, const std::string &private_key_file, + const std::string &verify_file = std::string()) : ServerBase::ServerBase(443), + context(boost::asio::ssl::context::tlsv12) + { + context.use_certificate_chain_file(cert_file); + context.use_private_key_file(private_key_file, boost::asio::ssl::context::pem); + + if (verify_file.size() > 0) + { + context.load_verify_file(verify_file); + context.set_verify_mode(boost::asio::ssl::verify_peer | boost::asio::ssl::verify_fail_if_no_peer_cert | + boost::asio::ssl::verify_client_once); + set_session_id_context = true; + } + } + + void start() + { + if (set_session_id_context) + { + // Creating session_id_context from address:port but reversed due to small SSL_MAX_SSL_SESSION_ID_LENGTH + session_id_context = std::to_string(config.port) + ':'; + session_id_context.append(config.address.rbegin(), config.address.rend()); + SSL_CTX_set_session_id_context(context.native_handle(), + reinterpret_cast(session_id_context.data()), + std::min(session_id_context.size(), + SSL_MAX_SSL_SESSION_ID_LENGTH)); + } + ServerBase::start(); + } + + protected: + boost::asio::ssl::context context; + + virtual void accept() + { + //Create new socket for this connection + //Shared_ptr is used to pass temporary objects to the asynchronous functions + auto socket = std::make_shared(*io_service, context); + + acceptor->async_accept((*socket).lowest_layer(), [this, socket](const boost::system::error_code &ec) + { + //Immediately start accepting a new connection (if io_service hasn't been stopped) + if (ec != boost::asio::error::operation_aborted) + accept(); + + + if (!ec) + { + boost::asio::ip::tcp::no_delay option(true); + socket->lowest_layer().set_option(option); + + //Set timeout on the following boost::asio::ssl::stream::async_handshake + auto timer = get_timeout_timer(socket, config.timeout_request); + socket->async_handshake(boost::asio::ssl::stream_base::server, [this, socket, timer] + (const boost::system::error_code &ec) + { + if (timer) + timer->cancel(); + if (!ec) + read_request_and_content(socket); + else if (on_error) + on_error(std::shared_ptr(new Request(*socket)), ec); + }); + } + else if (on_error) + on_error(std::shared_ptr(new Request(*socket)), ec); + }); + } + }; +} + +#endif //HTTPS_SERVER_HPP