]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[5620] Added Url method for retrieving stripped hostname
authorMarcin Siodelski <marcin@isc.org>
Mon, 14 May 2018 14:18:38 +0000 (16:18 +0200)
committerMarcin Siodelski <marcin@isc.org>
Mon, 14 May 2018 14:18:38 +0000 (16:18 +0200)
src/lib/http/tests/url_unittests.cc
src/lib/http/url.cc
src/lib/http/url.h

index 9ba615a21293e163689db197b8fe37401ad48e27..f024e61a92be5de94db57b707588e720d74719eb 100644 (file)
@@ -1,4 +1,4 @@
-// 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.
index 94ff5015f56668fb24c4a5c064f736dcbf2c6604..fe68f58e6a1cffe11751eca932046b3365e1149b 100644 (file)
@@ -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();
index 0d9b50994104e017464abb4c232a5ce2a33078b1..c96641b120786c33031f1c17d6afa95dcf3c85fc 100644 (file)
@@ -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.