summaryrefslogtreecommitdiffhomepage
path: root/server.cpp
blob: 6e15466722768584eb20b6ecd67e6c68c1f38d2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#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>

const std::string Server::VersionString{ "Reichwein.IT Webserver "s + std::string{VERSION} };

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());
 }
}