From 3f778eecc705990598f1033e6245522f42e2fcb5 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 12 Apr 2020 14:01:40 +0200 Subject: Refactor path concept --- config.cpp | 106 ++++++------------------------------------------------------- 1 file changed, 9 insertions(+), 97 deletions(-) (limited to 'config.cpp') diff --git a/config.cpp b/config.cpp index 8d6704c..0d9c117 100644 --- a/config.cpp +++ b/config.cpp @@ -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; } } -- cgit v1.2.3