From 6081819802972c716745a44e3521ccb3b3cf7b1a Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 11 Apr 2020 13:17:32 +0200 Subject: Support IPv6 --- config.cpp | 50 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 18 deletions(-) (limited to 'config.cpp') diff --git a/config.cpp b/config.cpp index 78f33e9..8d6704c 100644 --- a/config.cpp +++ b/config.cpp @@ -4,11 +4,27 @@ #include #include +#include +#include #include namespace pt = boost::property_tree; using namespace std::string_literals; +namespace { + + void RemovePortFromHostname(std::string& hostname) + { + // besides hostnames and IPv4 addresses, consider IPv6 addresses: [xx::yy]:8080 + // so only remove ":N" if after "]" + size_t pos = hostname.find_last_of(":]"); + if (pos != hostname.npos && hostname[pos] == ':') { + hostname = hostname.substr(0, pos); + } + } + +} // anonymous namespace + void Config::readConfigfile(std::string filename) { if (filename == "") { @@ -185,10 +201,7 @@ std::string Config::DocRoot(const Socket& socket, const std::string& requested_h std::string result; size_t path_len{0}; // find longest matching prefix - auto pos {host.find(':')}; - if (pos != host.npos) { - host = host.substr(0, pos); - } + 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()) { @@ -197,7 +210,12 @@ 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(); - result = path.params.at("target"); + 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()); + } } } } @@ -215,10 +233,7 @@ std::string Config::GetPlugin(const Socket& socket, const std::string& requested std::string result; size_t path_len{0}; - auto pos {host.find(':')}; - if (pos != host.npos) { - host = host.substr(0, pos); - } + 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()) { @@ -227,7 +242,12 @@ std::string Config::GetPlugin(const Socket& socket, const std::string& requested 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.params.at("plugin"); + 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()); + } } } } @@ -245,10 +265,7 @@ std::string Config::GetPluginPath(const Socket& socket, const std::string& reque std::string result; size_t path_len{0}; - auto pos {host.find(':')}; - if (pos != host.npos) { - host = host.substr(0, pos); - } + 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()) { @@ -275,10 +292,7 @@ std::string Config::GetRelativePath(const Socket& socket, const std::string& req std::string result; size_t path_len{0}; - auto pos {host.find(':')}; - if (pos != host.npos) { - host = host.substr(0, pos); - } + 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()) { -- cgit v1.2.3