-
Notifications
You must be signed in to change notification settings - Fork 125
Introduce a ConnectionTarget
enum
#501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,11 +15,16 @@ | |
enum ConnectionPool { | ||
/// Used by the `ConnectionPool` to index its `HTTP1ConnectionProvider`s | ||
/// | ||
/// A key is initialized from a `URL`, it uses the components to derive a hashed value | ||
/// A key is initialized from a `Request`, it uses the components to derive a hashed value | ||
/// used by the `providers` dictionary to allow retrieving and creating | ||
/// connection providers associated to a certain request in constant time. | ||
struct Key: Hashable, CustomStringConvertible { | ||
var scheme: Scheme | ||
var connectionTarget: ConnectionTarget | ||
private var tlsConfiguration: BestEffortHashableTLSConfiguration? | ||
|
||
init(_ request: HTTPClient.Request) { | ||
self.connectionTarget = request.connectionTarget | ||
switch request.scheme { | ||
case "http": | ||
self.scheme = .http | ||
|
@@ -34,20 +39,11 @@ enum ConnectionPool { | |
default: | ||
fatalError("HTTPClient.Request scheme should already be a valid one") | ||
} | ||
self.port = request.port | ||
self.host = request.host | ||
self.unixPath = request.socketPath | ||
if let tls = request.tlsConfiguration { | ||
self.tlsConfiguration = BestEffortHashableTLSConfiguration(wrapping: tls) | ||
} | ||
} | ||
|
||
var scheme: Scheme | ||
var host: String | ||
var port: Int | ||
var unixPath: String | ||
private var tlsConfiguration: BestEffortHashableTLSConfiguration? | ||
|
||
enum Scheme: Hashable { | ||
case http | ||
case https | ||
|
@@ -78,13 +74,16 @@ enum ConnectionPool { | |
var hasher = Hasher() | ||
self.tlsConfiguration?.hash(into: &hasher) | ||
let hash = hasher.finalize() | ||
var path = "" | ||
if self.unixPath != "" { | ||
path = self.unixPath | ||
} else { | ||
path = "\(self.host):\(self.port)" | ||
var hostDescription = "" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Swift should see that we set |
||
switch self.connectionTarget { | ||
case .ipAddress(let serialization, let addr): | ||
hostDescription = "\(serialization):\(addr.port!)" | ||
case .domain(let domain, port: let port): | ||
hostDescription = "\(domain):\(port)" | ||
case .unixSocket(let socketPath): | ||
hostDescription = socketPath | ||
} | ||
return "\(self.scheme)://\(path) TLS-hash: \(hash)" | ||
return "\(self.scheme)://\(hostDescription) TLS-hash: \(hash)" | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,30 @@ final class HTTP1ProxyConnectHandler: ChannelDuplexHandler, RemovableChannelHand | |
return self.proxyEstablishedPromise?.futureResult | ||
} | ||
|
||
convenience | ||
init(target: ConnectionTarget, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor nit: I'd like to put |
||
proxyAuthorization: HTTPClient.Authorization?, | ||
deadline: NIODeadline) { | ||
let targetHost: String | ||
let targetPort: Int | ||
switch target { | ||
case .ipAddress(serialization: let serialization, address: let address): | ||
targetHost = serialization | ||
targetPort = address.port! | ||
case .domain(name: let domain, port: let port): | ||
targetHost = domain | ||
targetPort = port | ||
case .unixSocket: | ||
fatalError("Unix Domain Sockets do not support proxies") | ||
} | ||
self.init( | ||
targetHost: targetHost, | ||
targetPort: targetPort, | ||
proxyAuthorization: proxyAuthorization, | ||
deadline: deadline | ||
) | ||
} | ||
|
||
init(targetHost: String, | ||
targetPort: Int, | ||
proxyAuthorization: HTTPClient.Authorization?, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the AsyncHTTPClient open source project | ||
// | ||
// Copyright (c) 2019-2021 Apple Inc. and the AsyncHTTPClient project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import enum NIOCore.SocketAddress | ||
|
||
enum ConnectionTarget: Equatable, Hashable { | ||
// We keep the IP address serialization precisely as it is in the URL. | ||
// Some platforms have quirks in their implementations of 'ntop', for example | ||
// writing IPv6 addresses as having embedded IPv4 sections (e.g. [::192.168.0.1] vs [::c0a8:1]). | ||
// This serialization includes square brackets, so it is safe to write next to a port number. | ||
// Note: `address` must have an explicit port. | ||
case ipAddress(serialization: String, address: SocketAddress) | ||
case domain(name: String, port: Int) | ||
case unixSocket(path: String) | ||
|
||
init(remoteHost: String, port: Int) { | ||
if let addr = try? SocketAddress(ipAddress: remoteHost, port: port) { | ||
switch addr { | ||
case .v6: | ||
self = .ipAddress(serialization: "[\(remoteHost)]", address: addr) | ||
case .v4: | ||
self = .ipAddress(serialization: remoteHost, address: addr) | ||
case .unixDomainSocket: | ||
fatalError("Expected a remote host") | ||
} | ||
} else { | ||
precondition(!remoteHost.isEmpty, "HTTPClient.Request should already reject empty remote hostnames") | ||
self = .domain(name: remoteHost, port: port) | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.