summaryrefslogtreecommitdiffhomepage
path: root/config.h
blob: 42a979eb6e97f4f97b449ef09e1bc15df6f23de4 (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
#pragma once

#include <filesystem>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

// URL path, not FS
struct Path
{
 std::string requested; // the requested path
 // mandatory entries: "plugin", "target", others are optional
 std::unordered_map<std::string, std::string> params; // what to serve, e.g. which filesystem path (target), and which plugin
 std::unordered_map<std::string, std::string> auth; // optional
};

struct Site
{
 // std::string name; is the index in the m_sites map
 std::unordered_set<std::string> hosts;
 std::vector<Path> paths;
 std::filesystem::path cert_path;
 std::filesystem::path key_path;
};

enum class SocketProtocol
{
 HTTP,
 HTTPS
};

struct Socket
{
 std::string address;
 std::string port;
 SocketProtocol protocol;
 std::unordered_set<std::string> serve_sites; // if empty, automatically expand to all configured sites
 
 std::unordered_map <std::string, Site*> host_lut; // look up table for fast server decision in GetPath()
};

class Config
{
 const std::filesystem::path default_filename{"/etc/webserver.conf"};

 void readConfigfile(const std::filesystem::path& filename);
 void expand_socket_sites();
 void validate();
 void create_look_up_table();

 std::string m_user;
 std::string m_group;
 int m_threads;
 std::filesystem::path m_statistics_path;
 std::vector<std::string> m_plugin_directories;
 std::unordered_map<std::string, Site> m_sites;
 std::vector<Socket> m_sockets;

 public:
  Config(const std::filesystem::path& filename);
 
  // Data getters
  std::string User() const;
  std::string Group() const;

  int Threads() const;

  std::filesystem::path statistics_path() const;

  const std::vector<std::string>& PluginDirectories() const;
  const std::unordered_map<std::string, Site>& Sites() const;
  const std::vector<Socket>& Sockets() const;

  //
  // secondary calculation functions
  //

  const Path& GetPath(const Socket& socket, const std::string& requested_host, const std::string& requested_path) const;
 
  // return true iff plugin "name" is mentioned in config
  bool PluginIsConfigured(const std::string& name) const;

  void dump() const;
};