Commit 59bfaada authored by Marcin Siodelski's avatar Marcin Siodelski
Browse files

[5620] Added Url method for retrieving stripped hostname

parent a8b6b26e
// Copyright (C) 2017 Internet Systems Consortium, Inc. ("ISC")
// Copyright (C) 2017-2018 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
......@@ -32,7 +32,7 @@ public:
Url url(text_url);
ASSERT_TRUE(url.isValid()) << url.getErrorMessage();
EXPECT_EQ(expected_scheme, url.getScheme());
EXPECT_EQ(expected_hostname, url.getHostname());
EXPECT_EQ(expected_hostname, url.getStrippedHostname());
EXPECT_EQ(expected_port, url.getPort());
EXPECT_EQ(expected_path, url.getPath());
}
......@@ -58,7 +58,7 @@ TEST_F(UrlTest, schemeHostnameSlash) {
// URL contains scheme, IPv6 address and slash.
TEST_F(UrlTest, schemeIPv6AddressSlash) {
testValidUrl("http://[2001:db8:1::100]/", Url::HTTP, "[2001:db8:1::100]", 0, "/");
testValidUrl("http://[2001:db8:1::100]/", Url::HTTP, "2001:db8:1::100", 0, "/");
}
// URL contains scheme, IPv4 address and slash.
......@@ -84,7 +84,7 @@ TEST_F(UrlTest, schemeHostnamePortSlash) {
// URL contains scheme, IPv6 address and port.
TEST_F(UrlTest, schemeIPv6AddressPort) {
testValidUrl("http://[2001:db8:1::1]:8080/", Url::HTTP, "[2001:db8:1::1]", 8080, "/");
testValidUrl("http://[2001:db8:1::1]:8080/", Url::HTTP, "2001:db8:1::1", 8080, "/");
}
// URL contains scheme, hostname, port and path.
......
......@@ -37,6 +37,16 @@ Url::getHostname() const {
return (hostname_);
}
std::string
Url::getStrippedHostname() const {
std::string hostname = getHostname();
if ((hostname.length() >= 2) && (hostname.at(0) == '[')) {
return (hostname.substr(1, hostname.length() - 2));
}
return (hostname);
}
unsigned
Url::getPort() const {
checkValid();
......
......@@ -63,6 +63,12 @@ public:
/// @throw InvalidOperation if URL is invalid.
std::string getHostname() const;
/// @brief Returns hostname stripped from [ ] characters surrounding
/// IPv6 address.
///
/// @throw InvalidOperation of URL is invalid.
std::string getStrippedHostname() const;
/// @brief Returns port number.
///
/// @return Port number or 0 if URL doesn't contain port number.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment