Skip to content

Commit

Permalink
Add isPercentEncoded check to username, password and host in URI
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewFossAWS committed May 22, 2024
1 parent 622b09f commit d513bd8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
28 changes: 22 additions & 6 deletions Sources/ClientRuntime/Message/URI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ public final class URIBuilder {

@discardableResult
public func withHost(_ value: String) -> URIBuilder {
self.urlComponents.host = value
if value.isPercentEncoded {
self.urlComponents.percentEncodedHost = value
} else {
self.urlComponents.host = value
}
return self
}

Expand Down Expand Up @@ -129,13 +133,25 @@ public final class URIBuilder {

@discardableResult
public func withUsername(_ value: String?) -> URIBuilder {
self.urlComponents.user = value
if let username = value {
if username.isPercentEncoded {
self.urlComponents.percentEncodedUser = username
} else {
self.urlComponents.user = username
}
}
return self
}

@discardableResult
public func withPassword(_ value: String?) -> URIBuilder {
self.urlComponents.password = value
if let password = value {
if password.isPercentEncoded {
self.urlComponents.percentEncodedPassword = password
} else {
self.urlComponents.password = password
}
}
return self
}

Expand All @@ -154,13 +170,13 @@ public final class URIBuilder {
public func build() -> URI {
return URI(scheme: Scheme(rawValue: self.urlComponents.scheme!)!,
path: self.urlComponents.percentEncodedPath,
host: self.urlComponents.host!,
host: self.urlComponents.percentEncodedHost!,
port: self.urlComponents.port.map { Int16($0) },
queryItems: self.urlComponents.percentEncodedQueryItems?.map {
SDKURLQueryItem(name: $0.name, value: $0.value)
} ?? [],
username: self.urlComponents.user,
password: self.urlComponents.password,
username: self.urlComponents.percentEncodedUser,
password: self.urlComponents.percentEncodedPassword,
fragment: self.urlComponents.percentEncodedFragment)
}

Expand Down
8 changes: 4 additions & 4 deletions Tests/ClientRuntimeTests/MessageTests/URITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ class URITests: XCTestCase {

uri = uri.toBuilder()
.withPath("/x%2Dy%2Dz")
.withHost("xctest2.amazonaws.com")
.withHost("%63xctest2.com")
.appendQueryItem(SDKURLQueryItem(name: "test", value: "1%2B2"))
.withFragment("fragment%21")
.withUsername("dan")
.withPassword("008")
.withUsername("dan%21")
.withPassword("%24008")
.build()

XCTAssertEqual(uri.url?.absoluteString,
"https://dan:[email protected].com/x%2Dy%2Dz?abc=def&ghi=jkl&mno=pqr&test=1%2B2#fragment%21")
"https://dan%21:%24008@cxctest2.com/x%2Dy%2Dz?abc=def&ghi=jkl&mno=pqr&test=1%2B2#fragment%21")
}
}

0 comments on commit d513bd8

Please sign in to comment.