Skip to content

Commit 66712fa

Browse files
committed
handle brackets in ipv6 authority string
1 parent 027e1f7 commit 66712fa

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

src/aws-cpp-sdk-core/include/aws/core/http/URI.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,14 @@ namespace Aws
7171
void SetScheme(Scheme value);
7272

7373
/**
74-
* Gets the domain portion of the uri
74+
* Gets the authority portion of the URI. Differs from the authority definition in RFC 3986. user information
75+
* and the port information are not included. It is functionally the host as defined by RFC 3986 with encoding included.
7576
*/
7677
inline const Aws::String& GetAuthority() const { return m_authority; }
7778

7879
/**
79-
* Sets the domain portion of the uri
80+
* Sets the authority portion of the URI. Differs from the authority definition in RFC 3986. user information
81+
* and the port information are not included. It is functionally the host as defined by RFC 3986 with encoding included.
8082
*/
8183
inline void SetAuthority(const Aws::String& value) { m_authority = value; }
8284

@@ -213,6 +215,17 @@ namespace Aws
213215
*/
214216
static Aws::String URLEncodePathRFC3986(const Aws::String& path, bool rfcCompliantEncoding = false);
215217

218+
/**
219+
* The host portion of the URI as described in rfc3986.
220+
*
221+
* The host subcomponent of authority is identified by an IPv6 literal
222+
* encapsulated within square brackets, an IPv4 address in dotted-
223+
* decimal form, or a registered name.
224+
*
225+
* @return The host portion of the URI
226+
*/
227+
Aws::String GetHost() const;
228+
216229
private:
217230
void ParseURIParts(const Aws::String& uri);
218231
void ExtractAndSetScheme(const Aws::String& uri);

src/aws-cpp-sdk-core/source/client/AWSClient.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ HttpResponseOutcome AWSClient::AttemptExhaustively(const Aws::Http::URI& uri,
259259
const char* signerRegionOverride,
260260
const char* signerServiceNameOverride) const
261261
{
262-
if (!Aws::Utils::IsValidHost(uri.GetAuthority()))
262+
if (!Aws::Utils::IsValidHost(uri.GetHost()))
263263
{
264264
return HttpResponseOutcome(AWSError<CoreErrors>(CoreErrors::VALIDATION, "", "Invalid DNS Label found in URI host", false/*retryable*/));
265265
}
@@ -415,7 +415,7 @@ HttpResponseOutcome AWSClient::AttemptExhaustively(const Aws::Http::URI& uri,
415415
const char* signerRegionOverride,
416416
const char* signerServiceNameOverride) const
417417
{
418-
if (!Aws::Utils::IsValidHost(uri.GetAuthority()))
418+
if (!Aws::Utils::IsValidHost(uri.GetHost()))
419419
{
420420
return HttpResponseOutcome(AWSError<CoreErrors>(CoreErrors::VALIDATION, "", "Invalid DNS Label found in URI host", false/*retryable*/));
421421
}

src/aws-cpp-sdk-core/source/http/URI.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,3 +600,15 @@ bool URI::CompareURIParts(const URI& other) const
600600
{
601601
return m_scheme == other.m_scheme && m_authority == other.m_authority && GetPath() == other.GetPath() && m_queryString == other.m_queryString;
602602
}
603+
604+
Aws::String URI::GetHost() const {
605+
Aws::String host{m_authority};
606+
const auto begin = host.find('[');
607+
const auto end = host.rfind(']');
608+
if (begin != Aws::String::npos && end != Aws::String::npos && begin + 1 < end) {
609+
host = host.substr(begin + 1, end - begin - 1);
610+
} else if (begin != Aws::String::npos && end != Aws::String::npos && begin + 1 == end) {
611+
host = "";
612+
}
613+
return host;
614+
}

tests/aws-cpp-sdk-core-tests/http/URITest.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,3 +383,35 @@ TEST_F(URITest, TestGetRFC3986URLEncodedPathCompliant)
383383

384384
Aws::Http::SetCompliantRfc3986Encoding(false);
385385
}
386+
387+
TEST_F(URITest, TestHostParsesCorrectly) {
388+
URI uri = "https://test.com";
389+
EXPECT_STREQ("test.com", uri.GetHost().c_str());
390+
391+
uri = "https://test.com:9000";
392+
EXPECT_STREQ("test.com", uri.GetHost().c_str());
393+
394+
uri = "https://[::]";
395+
EXPECT_STREQ("::", uri.GetHost().c_str());
396+
397+
uri = "https://[::]:9000";
398+
EXPECT_STREQ("::", uri.GetHost().c_str());
399+
400+
uri = "https://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]";
401+
EXPECT_STREQ("2001:0db8:85a3:0000:0000:8a2e:0370:7334", uri.GetHost().c_str());
402+
403+
uri = "https://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:9000";
404+
EXPECT_STREQ("2001:0db8:85a3:0000:0000:8a2e:0370:7334", uri.GetHost().c_str());
405+
406+
uri = "https://[]";
407+
EXPECT_STREQ("", uri.GetHost().c_str());
408+
409+
uri = "https://[]:9000";
410+
EXPECT_STREQ("", uri.GetHost().c_str());
411+
412+
uri = "https://127.0.0.1";
413+
EXPECT_STREQ("127.0.0.1", uri.GetHost().c_str());
414+
415+
uri = "https://127.0.0.1:9000";
416+
EXPECT_STREQ("127.0.0.1", uri.GetHost().c_str());
417+
}

0 commit comments

Comments
 (0)