summaryrefslogtreecommitdiffhomepage
path: root/server.cpp
blob: 71f39ac1f1c04c82e55ebecf9460b8214f0160fc (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#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/signal_set.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"
#include "statistics.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, Statistics& statistics)
 : m_config(config)
 , m_ioc(ioc)
 , m_socket(socket)
 , m_plugins(plugins)
 , m_statistics(statistics)
{
}

Server::~Server()
{
}

int run_server(Config& config, plugins_container_type& plugins)
{
 Statistics stats;

 auto const threads = std::max<int>(1, config.Threads());

 boost::asio::io_context ioc{threads};

 boost::asio::signal_set signals(ioc, SIGINT, SIGTERM);
 signals.async_wait([&](const boost::system::error_code& error, int signal_number){
                    std::cout << "Terminating via signal " << signal_number << std::endl;
                    ioc.stop();
                    });

 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, stats));
  } else {
   servers.push_back(std::make_shared<HTTPS::Server>(config, ioc, socket, plugins, stats));
  }
  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();

 for (auto& t: v) {
  t.join();
 }

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

Statistics& Server::GetStatistics()
{
 return m_statistics;
}