@@ -24,11 +24,6 @@ import NIOTLS
24
24
#endif
25
25
26
26
extension HTTPConnectionPool {
27
- enum NegotiatedProtocol {
28
- case http1_1( Channel )
29
- case http2_0( Channel )
30
- }
31
-
32
27
struct ConnectionFactory {
33
28
let key : ConnectionPool . Key
34
29
let clientConfiguration : HTTPClient . Configuration
@@ -48,6 +43,11 @@ extension HTTPConnectionPool {
48
43
}
49
44
50
45
extension HTTPConnectionPool . ConnectionFactory {
46
+ enum NegotiatedProtocol {
47
+ case http1_1( Channel )
48
+ case http2( Channel )
49
+ }
50
+
51
51
func makeHTTP1Channel(
52
52
connectionID: HTTPConnectionPool . Connection . ID ,
53
53
deadline: NIODeadline ,
@@ -59,8 +59,11 @@ extension HTTPConnectionPool.ConnectionFactory {
59
59
deadline: deadline,
60
60
eventLoop: eventLoop,
61
61
logger: logger
62
- ) . flatMapThrowing {
63
- ( channel, _) -> Channel in
62
+ ) . flatMapThrowing { ( negotiated) -> Channel in
63
+
64
+ guard case . http1_1( let channel) = negotiated else {
65
+ preconditionFailure ( " Expected to create http/1.1 connections only for now " )
66
+ }
64
67
65
68
// add the http1.1 channel handlers
66
69
let syncOperations = channel. pipeline. syncOperations
@@ -83,8 +86,8 @@ extension HTTPConnectionPool.ConnectionFactory {
83
86
deadline: NIODeadline ,
84
87
eventLoop: EventLoop ,
85
88
logger: Logger
86
- ) -> EventLoopFuture < ( Channel , HTTPVersion ) > {
87
- let channelFuture : EventLoopFuture < ( Channel , HTTPVersion ) >
89
+ ) -> EventLoopFuture < NegotiatedProtocol > {
90
+ let channelFuture : EventLoopFuture < NegotiatedProtocol >
88
91
89
92
if self . key. scheme. isProxyable, let proxy = self . clientConfiguration. proxy {
90
93
switch proxy. type {
@@ -110,7 +113,7 @@ extension HTTPConnectionPool.ConnectionFactory {
110
113
}
111
114
112
115
// let's map `ChannelError.connectTimeout` into a `HTTPClientError.connectTimeout`
113
- return channelFuture. flatMapErrorThrowing { error throws -> ( Channel , HTTPVersion ) in
116
+ return channelFuture. flatMapErrorThrowing { error throws -> NegotiatedProtocol in
114
117
switch error {
115
118
case ChannelError . connectTimeout:
116
119
throw HTTPClientError . connectTimeout
@@ -124,15 +127,15 @@ extension HTTPConnectionPool.ConnectionFactory {
124
127
deadline: NIODeadline ,
125
128
eventLoop: EventLoop ,
126
129
logger: Logger
127
- ) -> EventLoopFuture < ( Channel , HTTPVersion ) > {
130
+ ) -> EventLoopFuture < NegotiatedProtocol > {
128
131
switch self . key. scheme {
129
132
case . http, . http_unix, . unix:
130
- return self . makePlainChannel ( deadline: deadline, eventLoop: eventLoop) . map { ( $0, . http1_1 ) }
133
+ return self . makePlainChannel ( deadline: deadline, eventLoop: eventLoop) . map { . http1_1 ( $0) }
131
134
case . https, . https_unix:
132
135
return self . makeTLSChannel ( deadline: deadline, eventLoop: eventLoop, logger: logger) . flatMapThrowing {
133
136
channel, negotiated in
134
137
135
- ( channel , try self . matchALPNToHTTPVersion ( negotiated) )
138
+ try self . matchALPNToHTTPVersion ( negotiated, channel : channel )
136
139
}
137
140
}
138
141
}
@@ -156,7 +159,7 @@ extension HTTPConnectionPool.ConnectionFactory {
156
159
deadline: NIODeadline ,
157
160
eventLoop: EventLoop ,
158
161
logger: Logger
159
- ) -> EventLoopFuture < ( Channel , HTTPVersion ) > {
162
+ ) -> EventLoopFuture < NegotiatedProtocol > {
160
163
// A proxy connection starts with a plain text connection to the proxy server. After
161
164
// the connection has been established with the proxy server, the connection might be
162
165
// upgraded to TLS before we send our first request.
@@ -199,7 +202,7 @@ extension HTTPConnectionPool.ConnectionFactory {
199
202
deadline: NIODeadline ,
200
203
eventLoop: EventLoop ,
201
204
logger: Logger
202
- ) -> EventLoopFuture < ( Channel , HTTPVersion ) > {
205
+ ) -> EventLoopFuture < NegotiatedProtocol > {
203
206
// A proxy connection starts with a plain text connection to the proxy server. After
204
207
// the connection has been established with the proxy server, the connection might be
205
208
// upgraded to TLS before we send our first request.
@@ -231,12 +234,12 @@ extension HTTPConnectionPool.ConnectionFactory {
231
234
_ channel: Channel ,
232
235
deadline: NIODeadline ,
233
236
logger: Logger
234
- ) -> EventLoopFuture < ( Channel , HTTPVersion ) > {
237
+ ) -> EventLoopFuture < NegotiatedProtocol > {
235
238
switch self . key. scheme {
236
239
case . unix, . http_unix, . https_unix:
237
240
preconditionFailure ( " Unexpected scheme. Not supported for proxy! " )
238
241
case . http:
239
- return channel. eventLoop. makeSucceededFuture ( ( channel , . http1_1) )
242
+ return channel. eventLoop. makeSucceededFuture ( . http1_1( channel ) )
240
243
case . https:
241
244
var tlsConfig = self . tlsConfiguration
242
245
// since we can support h2, we need to advertise this in alpn
@@ -264,9 +267,9 @@ extension HTTPConnectionPool.ConnectionFactory {
264
267
} catch {
265
268
return channel. eventLoop. makeFailedFuture ( error)
266
269
}
267
- } . flatMap { negotiated -> EventLoopFuture < ( Channel , HTTPVersion ) > in
270
+ } . flatMap { negotiated -> EventLoopFuture < NegotiatedProtocol > in
268
271
channel. pipeline. removeHandler ( tlsEventHandler) . flatMapThrowing {
269
- ( channel , try self . matchALPNToHTTPVersion ( negotiated) )
272
+ try self . matchALPNToHTTPVersion ( negotiated, channel : channel )
270
273
}
271
274
}
272
275
}
@@ -399,12 +402,12 @@ extension HTTPConnectionPool.ConnectionFactory {
399
402
return eventLoop. makeSucceededFuture ( bootstrap)
400
403
}
401
404
402
- private func matchALPNToHTTPVersion( _ negotiated: String ? ) throws -> HTTPVersion {
405
+ private func matchALPNToHTTPVersion( _ negotiated: String ? , channel : Channel ) throws -> NegotiatedProtocol {
403
406
switch negotiated {
404
407
case . none, . some( " http/1.1 " ) :
405
- return . http1_1
408
+ return . http1_1( channel )
406
409
case . some( " h2 " ) :
407
- return . http2
410
+ return . http2( channel )
408
411
case . some( let unsupported) :
409
412
throw HTTPClientError . serverOfferedUnsupportedApplicationProtocol ( unsupported)
410
413
}
0 commit comments