diff options
author | Roland Reichwein <mail@reichwein.it> | 2020-04-12 14:01:40 +0200 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2020-04-12 14:01:40 +0200 |
commit | 3f778eecc705990598f1033e6245522f42e2fcb5 (patch) | |
tree | dfa2af27ef4e6b6a299ecb014a684c272db77992 /config.cpp | |
parent | 77a68fbe16246245937c5d692bb8c89dc14d7800 (diff) |
Refactor path concept
Diffstat (limited to 'config.cpp')
-rw-r--r-- | config.cpp | 106 |
1 files changed, 9 insertions, 97 deletions
@@ -194,11 +194,12 @@ void Config::dump() const std::cout << "=============================================" << std::endl; } -std::string Config::DocRoot(const Socket& socket, const std::string& requested_host, const std::string& requested_path) const +// throws std::out_of_range if not found +const Path& Config::GetPath(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; + const Path* result{nullptr}; size_t path_len{0}; // find longest matching prefix RemovePortFromHostname(host); @@ -210,44 +211,7 @@ std::string Config::DocRoot(const Socket& socket, const std::string& requested_h 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()); - } + result = &path; } } } @@ -255,70 +219,18 @@ 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}; - - 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 == nullptr) + throw std::out_of_range("Path not found for "s + requested_host + " " + requested_path); - if (result.size() > 0 && result[0] == '/') - return result.substr(1); - return result; + 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()) + auto it{path.params.find("plugin")}; + if (it != path.params.end() && it->second == name) return true; } } |