diff options
author | Roland Reichwein <mail@reichwein.it> | 2020-04-10 15:36:59 +0200 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2020-04-10 15:36:59 +0200 |
commit | c0ccf16c69d43a89674640c61d13ec2c02b128d6 (patch) | |
tree | ae840bc16f0ddb430bdd68aacef4d7cb2af970d9 /config.cpp | |
parent | 0d157fb407a35f8afe6d6f0f4c2cc5cd5d5a1933 (diff) |
First working plugin: static-files
Diffstat (limited to 'config.cpp')
-rw-r--r-- | config.cpp | 89 |
1 files changed, 72 insertions, 17 deletions
@@ -46,14 +46,7 @@ void Config::readConfigfile(std::string filename) Path path; auto attrs = x.second.get_child("<xmlattr>"); path.requested = attrs.get<std::string>("requested"); - std::string type = attrs.get<std::string>("type"); - if (type == "files") { - path.type = Files; - } else if (type == "plugin") { - path.type = Plugin; - } else - throw std::runtime_error("Unknown type: "s + type); - for (const auto& param: x.second) { + 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(); } @@ -163,7 +156,7 @@ void Config::dump() const if (site.paths.size() == 0) std::cout << " Warning: No paths configured." << std::endl; for (const auto& path: site.paths) { - std::cout << " Path: " << path.requested << " -> " << ((path.type == Files) ? "files" : "plugin") << std::endl; + std::cout << " Path: " << path.requested << std::endl; for (const auto& param: path.params) { std::cout << " " << param.first << ": " << param.second << std::endl; } @@ -190,6 +183,7 @@ std::string Config::DocRoot(const Socket& socket, const std::string& requested_h // TODO: speed this up std::string host{requested_host}; std::string result; + size_t path_len{0}; // find longest matching prefix auto pos {host.find(':')}; if (pos != host.npos) { @@ -201,10 +195,9 @@ std::string Config::DocRoot(const Socket& socket, const std::string& requested_h 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)) { - const auto& root { path.params.at("target")}; - if (root.size() > result.size()) - result = root; + if (boost::starts_with(requested_path, path.requested) && path.requested.size() > path_len) { + path_len = path.requested.size(); + result = path.params.at("target"); } } } @@ -220,6 +213,7 @@ std::string Config::GetPlugin(const Socket& socket, const std::string& requested // TODO: speed this up std::string host{requested_host}; std::string result; + size_t path_len{0}; auto pos {host.find(':')}; if (pos != host.npos) { @@ -231,10 +225,9 @@ std::string Config::GetPlugin(const Socket& socket, const std::string& requested 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)) { - const auto& root { path.params.at("plugin")}; - if (root.size() > result.size()) - result = root; + if (boost::starts_with(requested_path, path.requested) && path.requested.size() > path_len) { + path_len = path.requested.size(); + result = path.params.at("plugin"); } } } @@ -245,6 +238,68 @@ std::string Config::GetPlugin(const Socket& socket, const std::string& requested 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}; + + auto pos {host.find(':')}; + if (pos != host.npos) { + host = host.substr(0, pos); + } + + 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}; + + auto pos {host.find(':')}; + if (pos != host.npos) { + host = host.substr(0, pos); + } + + 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) { |