Skip to content

Commit 178e2c0

Browse files
authored
fix: Set ports and scheme correctly for non-TLS HTTP connections (#668)
1 parent e74b039 commit 178e2c0

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

Sources/ClientRuntime/Networking/Endpoint.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ public struct Endpoint: Hashable {
3131
throw ClientError.unknownError("invalid host \(String(describing: url.host))")
3232
}
3333

34+
let protocolType = ProtocolType(rawValue: url.scheme ?? "") ?? .https
3435
self.init(host: host,
3536
path: url.path,
36-
port: Int16(url.port ?? 443),
37+
port: Int16(url.port ?? protocolType.port),
3738
queryItems: url.toQueryItems(),
38-
protocolType: ProtocolType(rawValue: url.scheme ?? ProtocolType.https.rawValue),
39+
protocolType: protocolType,
3940
headers: headers,
4041
properties: properties)
4142
}

Sources/ClientRuntime/Networking/Http/ProtocolType.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,12 @@ public enum ALPNProtocol: String {
1515
case http1 = "http/1.1"
1616
case http2 = "h2"
1717
}
18+
19+
extension ProtocolType {
20+
var port: Int {
21+
switch self {
22+
case .http: return 80
23+
case .https: return 443
24+
}
25+
}
26+
}

Sources/ClientRuntime/Networking/Http/URLSession/URLSessionHTTPClient.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ public final class URLSessionHTTPClient: HTTPClient {
279279
var components = URLComponents()
280280
components.scheme = config.protocolType?.rawValue ?? request.endpoint.protocolType?.rawValue ?? "https"
281281
components.host = request.endpoint.host
282+
components.port = port(for: request)
282283
components.percentEncodedPath = request.path
283284
if let queryItems = request.queryItems, !queryItems.isEmpty {
284285
components.percentEncodedQueryItems = queryItems.map {
@@ -296,6 +297,16 @@ public final class URLSessionHTTPClient: HTTPClient {
296297
}
297298
return urlRequest
298299
}
300+
301+
private func port(for request: SdkHttpRequest) -> Int? {
302+
switch (request.endpoint.protocolType, request.endpoint.port) {
303+
case (.https, 443), (.http, 80):
304+
// Don't set port explicitly if it's the default port for the scheme
305+
return nil
306+
default:
307+
return Int(request.endpoint.port)
308+
}
309+
}
299310
}
300311

301312
/// Errors that are particular to the URLSession-based Smithy HTTP client.

0 commit comments

Comments
 (0)