#include <boost/beast/version.hpp> // Support both boost in Debian unstable (BOOST_LATEST) and in stable (boost 1.67) #if BOOST_VERSION >= 107100 #define BOOST_LATEST #endif #include <boost/beast/core.hpp> #include <boost/beast/http.hpp> #include <boost/beast/version.hpp> #ifdef BOOST_LATEST #include <boost/beast/ssl.hpp> #else #include <boost/asio/ip/tcp.hpp> #include <boost/asio/ssl/stream.hpp> #endif #include <boost/asio/dispatch.hpp> #include <boost/asio/strand.hpp> #include <boost/config.hpp> #include <exception> #include <iostream> #include <thread> #include <vector> #include "server.h" #include "http.h" #include "https.h" #include "privileges.h" namespace beast = boost::beast; // from <boost/beast.hpp> namespace http = beast::http; // from <boost/beast/http.hpp> namespace net = boost::asio; // from <boost/asio.hpp> namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp> using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp> Server::Server(Config& config, boost::asio::io_context& ioc, const Socket& socket, plugins_container_type& plugins) : m_config(config) , m_ioc(ioc) , m_socket(socket) , m_plugins(plugins) { } Server::~Server() { } int run_server(Config& config, plugins_container_type& plugins) { auto const threads = std::max<int>(1, config.Threads()); boost::asio::io_context ioc{threads}; std::vector<std::shared_ptr<Server>> servers; const auto& sockets {config.Sockets()}; for (const auto& socket: sockets) { if (socket.protocol == SocketProtocol::HTTP) { servers.push_back(std::make_shared<HTTP::Server>(config, ioc, socket, plugins)); } else { servers.push_back(std::make_shared<HTTPS::Server>(config, ioc, socket, plugins)); } servers.back()->start(); } // set UID, GID drop_privileges(config); // Run the I/O service on the requested number of threads std::vector<std::thread> v; v.reserve(threads - 1); for(auto i = threads - 1; i > 0; --i) v.emplace_back( [&ioc] { ioc.run(); }); ioc.run(); return EXIT_SUCCESS; } Config& Server::GetConfig() { return m_config; } const Socket& Server::GetSocket() { return m_socket; } plugin_type Server::GetPlugin(const std::string& name) { try { return m_plugins.at(name); } catch (const std::out_of_range& ex) { std::cout << "Out of range at Server::GetPlugin(): " << name << std::endl; std::rethrow_exception(std::current_exception()); } catch (...) { std::cout << "Unknown exception at Server::GetPlugin(): " << name << std::endl; std::rethrow_exception(std::current_exception()); } }