summaryrefslogtreecommitdiffhomepage
path: root/config.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'config.cpp')
-rw-r--r--config.cpp89
1 files changed, 72 insertions, 17 deletions
diff --git a/config.cpp b/config.cpp
index 9468e39..78f33e9 100644
--- a/config.cpp
+++ b/config.cpp
@@ -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) {