Skip to content

Commit 3e44e51

Browse files
authored
[HTTP2] Make HTTPVersion public and set to .automatic by default (#473)
1 parent 38bbe25 commit 3e44e51

File tree

3 files changed

+29
-55
lines changed

3 files changed

+29
-55
lines changed

Diff for: Sources/AsyncHTTPClient/HTTPClient.swift

+6-32
Original file line numberDiff line numberDiff line change
@@ -607,10 +607,8 @@ public class HTTPClient {
607607
set {}
608608
}
609609

610-
// TODO: make public
611-
// TODO: set to automatic by default
612-
/// HTTP/2 is by default disabled
613-
internal var httpVersion: HTTPVersion
610+
/// is set to `.automatic` by default which will use HTTP/2 if run over https and the server supports it, otherwise HTTP/1
611+
public var httpVersion: HTTPVersion
614612

615613
public init(
616614
tlsConfiguration: TLSConfiguration? = nil,
@@ -620,37 +618,14 @@ public class HTTPClient {
620618
proxy: Proxy? = nil,
621619
ignoreUncleanSSLShutdown: Bool = false,
622620
decompression: Decompression = .disabled
623-
) {
624-
self.init(
625-
tlsConfiguration: tlsConfiguration,
626-
redirectConfiguration: redirectConfiguration,
627-
timeout: timeout, connectionPool: connectionPool,
628-
proxy: proxy,
629-
ignoreUncleanSSLShutdown: ignoreUncleanSSLShutdown,
630-
decompression: decompression,
631-
// TODO: set to automatic by default
632-
httpVersion: .http1Only
633-
)
634-
}
635-
636-
// TODO: make public
637-
internal init(
638-
tlsConfiguration: TLSConfiguration? = nil,
639-
redirectConfiguration: RedirectConfiguration? = nil,
640-
timeout: Timeout = Timeout(),
641-
connectionPool: ConnectionPool = ConnectionPool(),
642-
proxy: Proxy? = nil,
643-
ignoreUncleanSSLShutdown: Bool = false,
644-
decompression: Decompression = .disabled,
645-
httpVersion: HTTPVersion
646621
) {
647622
self.tlsConfiguration = tlsConfiguration
648623
self.redirectConfiguration = redirectConfiguration ?? RedirectConfiguration()
649624
self.timeout = timeout
650625
self.connectionPool = connectionPool
651626
self.proxy = proxy
652627
self.decompression = decompression
653-
self.httpVersion = httpVersion
628+
self.httpVersion = .automatic
654629
}
655630

656631
public init(tlsConfiguration: TLSConfiguration? = nil,
@@ -865,18 +840,17 @@ extension HTTPClient.Configuration {
865840
}
866841
}
867842

868-
// TODO: make this struct and its static properties public
869-
internal struct HTTPVersion {
843+
public struct HTTPVersion {
870844
internal enum Configuration {
871845
case http1Only
872846
case automatic
873847
}
874848

875849
/// we only use HTTP/1, even if the server would supports HTTP/2
876-
internal static let http1Only: Self = .init(configuration: .http1Only)
850+
public static let http1Only: Self = .init(configuration: .http1Only)
877851

878852
/// HTTP/2 is used if we connect to a server with HTTPS and the server supports HTTP/2, otherwise we use HTTP/1
879-
internal static let automatic: Self = .init(configuration: .automatic)
853+
public static let automatic: Self = .init(configuration: .automatic)
880854

881855
internal var configuration: Configuration
882856
}

Diff for: Tests/AsyncHTTPClientTests/HTTP2ClientTests.swift

+17-21
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
// TODO: remove @testable once we officially support HTTP/2
16-
@testable import AsyncHTTPClient // Tests that really need @testable go into HTTP2ClientInternalTests.swift
15+
/* NOT @testable */ import AsyncHTTPClient // Tests that really need @testable go into HTTP2ClientInternalTests.swift
1716
#if canImport(Network)
1817
import Network
1918
#endif
@@ -28,14 +27,13 @@ class HTTP2ClientTests: XCTestCase {
2827
func makeDefaultHTTPClient(
2928
eventLoopGroupProvider: HTTPClient.EventLoopGroupProvider = .createNew
3029
) -> HTTPClient {
31-
var tlsConfig = TLSConfiguration.makeClientConfiguration()
32-
tlsConfig.certificateVerification = .none
30+
var config = HTTPClient.Configuration()
31+
config.tlsConfiguration = .clientDefault
32+
config.tlsConfiguration?.certificateVerification = .none
33+
config.httpVersion = .automatic
3334
return HTTPClient(
3435
eventLoopGroupProvider: eventLoopGroupProvider,
35-
configuration: HTTPClient.Configuration(
36-
tlsConfiguration: tlsConfig,
37-
httpVersion: .automatic
38-
),
36+
configuration: config,
3937
backgroundActivityLogger: Logger(label: "HTTPClient", factory: StreamLogHandler.standardOutput(label:))
4038
)
4139
}
@@ -138,14 +136,13 @@ class HTTP2ClientTests: XCTestCase {
138136

139137
let localHTTPBin = HTTPBin(.http2(compress: false))
140138
let elg = MultiThreadedEventLoopGroup(numberOfThreads: numberOfWorkers)
141-
var tlsConfig = TLSConfiguration.makeClientConfiguration()
142-
tlsConfig.certificateVerification = .none
139+
var config = HTTPClient.Configuration()
140+
config.tlsConfiguration = .clientDefault
141+
config.tlsConfiguration?.certificateVerification = .none
142+
config.httpVersion = .automatic
143143
let localClient = HTTPClient(
144144
eventLoopGroupProvider: .shared(elg),
145-
configuration: HTTPClient.Configuration(
146-
tlsConfiguration: tlsConfig,
147-
httpVersion: .automatic
148-
),
145+
configuration: config,
149146
backgroundActivityLogger: Logger(label: "HTTPClient", factory: StreamLogHandler.standardOutput(label:))
150147
)
151148
defer {
@@ -303,15 +300,14 @@ class HTTP2ClientTests: XCTestCase {
303300
let el1 = clientGroup.next()
304301
let el2 = clientGroup.next()
305302
defer { XCTAssertNoThrow(try clientGroup.syncShutdownGracefully()) }
306-
var tlsConfig = TLSConfiguration.makeClientConfiguration()
307-
tlsConfig.certificateVerification = .none
303+
var config = HTTPClient.Configuration()
304+
config.tlsConfiguration = .clientDefault
305+
config.tlsConfiguration?.certificateVerification = .none
306+
config.httpVersion = .automatic
307+
config.timeout.connect = .milliseconds(1000)
308308
let client = HTTPClient(
309309
eventLoopGroupProvider: .shared(clientGroup),
310-
configuration: HTTPClient.Configuration(
311-
tlsConfiguration: tlsConfig,
312-
timeout: .init(connect: .milliseconds(1000)),
313-
httpVersion: .automatic
314-
),
310+
configuration: config,
315311
backgroundActivityLogger: Logger(label: "HTTPClient", factory: StreamLogHandler.standardOutput(label:))
316312
)
317313
defer { XCTAssertNoThrow(try client.syncShutdown()) }

Diff for: Tests/AsyncHTTPClientTests/HTTP2ConnectionTests.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,12 @@ class TestConnectionCreator {
250250

251251
var tlsConfiguration = TLSConfiguration.makeClientConfiguration()
252252
tlsConfiguration.certificateVerification = .none
253+
var config = HTTPClient.Configuration()
254+
config.httpVersion = .automatic
253255
let factory = HTTPConnectionPool.ConnectionFactory(
254256
key: .init(request),
255257
tlsConfiguration: tlsConfiguration,
256-
clientConfiguration: .init(httpVersion: .automatic),
258+
clientConfiguration: config,
257259
sslContextCache: .init()
258260
)
259261

@@ -291,10 +293,12 @@ class TestConnectionCreator {
291293

292294
var tlsConfiguration = TLSConfiguration.makeClientConfiguration()
293295
tlsConfiguration.certificateVerification = .none
296+
var config = HTTPClient.Configuration()
297+
config.httpVersion = .automatic
294298
let factory = HTTPConnectionPool.ConnectionFactory(
295299
key: .init(request),
296300
tlsConfiguration: tlsConfiguration,
297-
clientConfiguration: .init(httpVersion: .automatic),
301+
clientConfiguration: config,
298302
sslContextCache: .init()
299303
)
300304

0 commit comments

Comments
 (0)