#include // Support both boost in Debian unstable (BOOST_LATEST) and in stable (boost 1.67) #if BOOST_VERSION >= 107100 #define BOOST_LATEST #endif #include #include #include #ifdef BOOST_LATEST #include #else #include #include #endif #include #include #include #include #include #include #include #include "server.h" #include "http.h" #include "https.h" #include "privileges.h" namespace beast = boost::beast; // from namespace http = beast::http; // from namespace net = boost::asio; // from namespace ssl = boost::asio::ssl; // from using tcp = boost::asio::ip::tcp; // from 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(1, config.Threads()); boost::asio::io_context ioc{threads}; std::vector> servers; const auto& sockets {config.Sockets()}; for (const auto& socket: sockets) { if (socket.protocol == SocketProtocol::HTTP) { servers.push_back(std::make_shared(config, ioc, socket, plugins)); } else { servers.push_back(std::make_shared(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 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()); } }