summaryrefslogtreecommitdiffhomepage
path: root/config.cpp
blob: 8d6704cd10b3772cc60eee85dcae7d53a075489b (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include "config.h"

#include <boost/algorithm/string/predicate.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

#include <algorithm>
#include <exception>
#include <iostream>

namespace pt = boost::property_tree;
using namespace std::string_literals;

namespace {

 void RemovePortFromHostname(std::string& hostname)
 {
  // besides hostnames and IPv4 addresses, consider IPv6 addresses: [xx::yy]:8080
  // so only remove ":N" if after "]"
  size_t pos = hostname.find_last_of(":]");
  if (pos != hostname.npos && hostname[pos] == ':') {
   hostname = hostname.substr(0, pos);
  }
 }

} // anonymous namespace

void Config::readConfigfile(std::string filename)
{
 if (filename == "") {
  filename = default_filename;
 }

 pt::ptree tree;

 pt::read_xml(filename, tree, pt::xml_parser::no_comments | pt::xml_parser::trim_whitespace);

 // mandatory
 m_user = tree.get<std::string>("webserver.user");
 
 m_group = tree.get<std::string>("webserver.group");

 m_threads = tree.get<int>("webserver.threads");

 // optional entries
 auto elements = tree.get_child_optional("webserver");
 if (elements) {
  for (const auto& element: *elements) {
   if (element.first == "plugin-directory"s) {
    m_plugin_directories.push_back(element.second.data());
   } else if (element.first == "sites"s) {
    for (const auto& site: element.second) {
     if (site.first != "site"s)
      throw std::runtime_error("<site> expected in <sites>");
     Site site_struct;
     for (const auto& x: site.second) {
      if (x.first == "name"s) {
       site_struct.name = x.second.data();
      } else if (x.first == "host"s) {
       site_struct.hosts.insert(x.second.data());
      } else if (x.first == "path"s) {
       Path path;
       auto attrs = x.second.get_child("<xmlattr>");
       path.requested = attrs.get<std::string>("requested");
       for (const auto& param: x.second) { // get all sub-elements of <path>
        if (param.first.size() > 0 && param.first[0] != '<') // exclude meta-elements like <xmlattr>
         path.params[param.first.data()] = param.second.data();
       }
       site_struct.paths.push_back(path);
      } else if (x.first == "certpath"s) {
       site_struct.cert_path = x.second.data();
      } else if (x.first == "keypath"s) {
       site_struct.key_path = x.second.data();
      } else
       throw std::runtime_error("Unknown element: "s + x.first);
     }
     m_sites.push_back(site_struct);
    }
   } else if (element.first == "sockets"s) {
    for (const auto& socket: element.second) {
     if (socket.first != "socket"s)
      throw std::runtime_error("<socket> expected in <sockets>");
     Socket socket_struct;
     for (const auto& x: socket.second) {
      if (x.first == "address"s) {
       socket_struct.address = x.second.data();
      } else if (x.first == "port"s) {
       socket_struct.port = x.second.data();
      } else if (x.first == "protocol"s) {
       if (x.second.data() == "http"s)
        socket_struct.protocol = SocketProtocol::HTTP;
       else if (x.second.data() == "https"s)
        socket_struct.protocol = SocketProtocol::HTTPS;
       else
        throw std::runtime_error("Unknown protocol: "s + x.second.data());
      } else
       throw std::runtime_error("Unknown element: "s + x.first);
     }
     if (geteuid() != 0 && stoi(socket_struct.port) < 1024)
      std::cout << "Warning: Skipping privileged port " << socket_struct.port << std::endl;
     else
      m_sockets.push_back(socket_struct);
    }
   }
  }
 }

 // expand socket sites
 for (auto& socket: m_sockets) {
  if (socket.serve_sites.empty()) {
   for (const auto& site: m_sites) {
    socket.serve_sites.push_back(site.name);
   }
  }
 }
}

Config::Config(const std::string& filename)
{
 readConfigfile(filename);
 dump();
}

std::string Config::User() const
{
 return m_user;
}

std::string Config::Group() const
{
 return m_group;
}

int Config::Threads() const
{
 return m_threads;
}

const std::vector<std::string>& Config::PluginDirectories() const
{
 return m_plugin_directories;
}

const std::vector<Site>& Config::Sites() const
{
 return m_sites;
}

const std::vector<Socket>& Config::Sockets() const
{
 return m_sockets;
}

void Config::dump() const
{
 std::cout << "=== Configuration ===========================" << std::endl;
 std::cout << "User: " << m_user << std::endl;
 std::cout << "Group: " << m_user << std::endl;
 
 std::cout << "Threads: " << m_threads << std::endl;

 std::cout << "Plugin Directories:";
 for (const auto& dir: m_plugin_directories)
  std::cout << " " << dir;
 std::cout << std::endl;

 for (const auto& site: m_sites) {
  std::cout << "Site: " << site.name << ":";
  for (const auto& host: site.hosts)
   std::cout << " " << host;
  std::cout << std::endl;
  if (site.paths.size() == 0)
   std::cout << "  Warning: No paths configured." << std::endl;
  for (const auto& path: site.paths) {
   std::cout << "  Path: " << path.requested << std::endl;
   for (const auto& param: path.params) {
    std::cout << "    " << param.first << ": " << param.second << std::endl;
   }
  }
  if (site.key_path != ""s) {
   std::cout << "  Key: " << site.key_path.generic_string() << std::endl;
   std::cout << "  Cert: " << site.cert_path.generic_string() << std::endl;
  }
 }

 for (const auto& socket: m_sockets) {
  std::cout << "Socket: " << socket.address << ":" << socket.port << " (" << (socket.protocol == SocketProtocol::HTTP ? "HTTP" : "HTTPS") << ")" << std::endl;
  std::cout << "  Serving:";
  for (const auto& site: socket.serve_sites) {
   std::cout << " " << site;
  }
  std::cout << std::endl;
 }
 std::cout << "=============================================" << std::endl;
}

std::string Config::DocRoot(const Socket& socket, const std::string& requested_host, const std::string& requested_path) const
{
 // TODO: speed this up
 std::string host{requested_host};
 std::string result;
 size_t path_len{0}; // find longest matching prefix

 RemovePortFromHostname(host);

 for (const auto& site: m_sites) {
  if (std::find(socket.serve_sites.begin(), socket.serve_sites.end(), site.name) != socket.serve_sites.end()) {
   for (const auto& m_host: site.hosts) {
    if (m_host == host) {
     for (const auto& path: site.paths) {
      if (boost::starts_with(requested_path, path.requested) && path.requested.size() > path_len) {
       path_len = path.requested.size();
       try {
        result = path.params.at("target");
       } catch (const std::out_of_range& ex) {
        std::cout << "Out of range at Config::DocRoot(): target" << std::endl;
        std::rethrow_exception(std::current_exception());
       }
      }
     }
    }
   }
  }
 }

 return result;
}

std::string Config::GetPlugin(const Socket& socket, const std::string& requested_host, const std::string& requested_path) const
{
 // TODO: speed this up
 std::string host{requested_host};
 std::string result;
 size_t path_len{0};

 RemovePortFromHostname(host);

 for (const auto& site: m_sites) {
  if (std::find(socket.serve_sites.begin(), socket.serve_sites.end(), site.name) != socket.serve_sites.end()) {
   for (const auto& m_host: site.hosts) {
    if (m_host == host) {
     for (const auto& path: site.paths) {
      if (boost::starts_with(requested_path, path.requested) && path.requested.size() > path_len) {
       path_len = path.requested.size();
       try {
        result = path.params.at("plugin");
       } catch (const std::out_of_range& ex) {
        std::cout << "Out of range at Config::DocRoot(): target" << std::endl;
        std::rethrow_exception(std::current_exception());
       }
      }
     }
    }
   }
  }
 }

 return result;
}

std::string Config::GetPluginPath(const Socket& socket, const std::string& requested_host, const std::string& requested_path) const
{
 // TODO: speed this up
 std::string host{requested_host};
 std::string result;
 size_t path_len{0};

 RemovePortFromHostname(host);

 for (const auto& site: m_sites) {
  if (std::find(socket.serve_sites.begin(), socket.serve_sites.end(), site.name) != socket.serve_sites.end()) {
   for (const auto& m_host: site.hosts) {
    if (m_host == host) {
     for (const auto& path: site.paths) {
      if (boost::starts_with(requested_path, path.requested) && path.requested.size() > path_len) {
       path_len = path.requested.size();
       result = path.requested;
      }
     }
    }
   }
  }
 }

 return result;
}

std::string Config::GetRelativePath(const Socket& socket, const std::string& requested_host, const std::string& requested_path) const
{
 // TODO: speed this up
 std::string host{requested_host};
 std::string result;
 size_t path_len{0};

 RemovePortFromHostname(host);

 for (const auto& site: m_sites) {
  if (std::find(socket.serve_sites.begin(), socket.serve_sites.end(), site.name) != socket.serve_sites.end()) {
   for (const auto& m_host: site.hosts) {
    if (m_host == host) {
     for (const auto& path: site.paths) {
      if (boost::starts_with(requested_path, path.requested) && path.requested.size() > path_len) {
       path_len = path.requested.size();
       result = requested_path.substr(path_len);
      }
     }
    }
   }
  }
 }

 if (result.size() > 0 && result[0] == '/')
  return result.substr(1);
 return result;
}

bool Config::PluginIsConfigured(const std::string& name) const
{
 for (const auto& site: m_sites) {
  for (const auto& path: site.paths) {
   if (path.params.find("plugin") != path.params.end())
    return true;
  }
 }
 return false;
}