@@ -24,11 +24,6 @@ import NIOTLS
2424#endif
2525
2626extension HTTPConnectionPool {
27- enum NegotiatedProtocol {
28- case http1_1( Channel )
29- case http2_0( Channel )
30- }
31-
3227 struct ConnectionFactory {
3328 let key : ConnectionPool . Key
3429 let clientConfiguration : HTTPClient . Configuration
@@ -48,6 +43,11 @@ extension HTTPConnectionPool {
4843}
4944
5045extension HTTPConnectionPool . ConnectionFactory {
46+ enum NegotiatedProtocol {
47+ case http1_1( Channel )
48+ case http2( Channel )
49+ }
50+
5151 func makeHTTP1Channel(
5252 connectionID: HTTPConnectionPool . Connection . ID ,
5353 deadline: NIODeadline ,
@@ -59,8 +59,11 @@ extension HTTPConnectionPool.ConnectionFactory {
5959 deadline: deadline,
6060 eventLoop: eventLoop,
6161 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+ }
6467
6568 // add the http1.1 channel handlers
6669 let syncOperations = channel. pipeline. syncOperations
@@ -83,8 +86,8 @@ extension HTTPConnectionPool.ConnectionFactory {
8386 deadline: NIODeadline ,
8487 eventLoop: EventLoop ,
8588 logger: Logger
86- ) -> EventLoopFuture < ( Channel , HTTPVersion ) > {
87- let channelFuture : EventLoopFuture < ( Channel , HTTPVersion ) >
89+ ) -> EventLoopFuture < NegotiatedProtocol > {
90+ let channelFuture : EventLoopFuture < NegotiatedProtocol >
8891
8992 if self . key. scheme. isProxyable, let proxy = self . clientConfiguration. proxy {
9093 switch proxy. type {
@@ -110,7 +113,7 @@ extension HTTPConnectionPool.ConnectionFactory {
110113 }
111114
112115 // 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
114117 switch error {
115118 case ChannelError . connectTimeout:
116119 throw HTTPClientError . connectTimeout
@@ -124,15 +127,15 @@ extension HTTPConnectionPool.ConnectionFactory {
124127 deadline: NIODeadline ,
125128 eventLoop: EventLoop ,
126129 logger: Logger
127- ) -> EventLoopFuture < ( Channel , HTTPVersion ) > {
130+ ) -> EventLoopFuture < NegotiatedProtocol > {
128131 switch self . key. scheme {
129132 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) }
131134 case . https, . https_unix:
132135 return self . makeTLSChannel ( deadline: deadline, eventLoop: eventLoop, logger: logger) . flatMapThrowing {
133136 channel, negotiated in
134137
135- ( channel , try self . matchALPNToHTTPVersion ( negotiated) )
138+ try self . matchALPNToHTTPVersion ( negotiated, channel : channel )
136139 }
137140 }
138141 }
@@ -156,7 +159,7 @@ extension HTTPConnectionPool.ConnectionFactory {
156159 deadline: NIODeadline ,
157160 eventLoop: EventLoop ,
158161 logger: Logger
159- ) -> EventLoopFuture < ( Channel , HTTPVersion ) > {
162+ ) -> EventLoopFuture < NegotiatedProtocol > {
160163 // A proxy connection starts with a plain text connection to the proxy server. After
161164 // the connection has been established with the proxy server, the connection might be
162165 // upgraded to TLS before we send our first request.
@@ -199,7 +202,7 @@ extension HTTPConnectionPool.ConnectionFactory {
199202 deadline: NIODeadline ,
200203 eventLoop: EventLoop ,
201204 logger: Logger
202- ) -> EventLoopFuture < ( Channel , HTTPVersion ) > {
205+ ) -> EventLoopFuture < NegotiatedProtocol > {
203206 // A proxy connection starts with a plain text connection to the proxy server. After
204207 // the connection has been established with the proxy server, the connection might be
205208 // upgraded to TLS before we send our first request.
@@ -231,12 +234,12 @@ extension HTTPConnectionPool.ConnectionFactory {
231234 _ channel: Channel ,
232235 deadline: NIODeadline ,
233236 logger: Logger
234- ) -> EventLoopFuture < ( Channel , HTTPVersion ) > {
237+ ) -> EventLoopFuture < NegotiatedProtocol > {
235238 switch self . key. scheme {
236239 case . unix, . http_unix, . https_unix:
237240 preconditionFailure ( " Unexpected scheme. Not supported for proxy! " )
238241 case . http:
239- return channel. eventLoop. makeSucceededFuture ( ( channel , . http1_1) )
242+ return channel. eventLoop. makeSucceededFuture ( . http1_1( channel ) )
240243 case . https:
241244 var tlsConfig = self . tlsConfiguration
242245 // since we can support h2, we need to advertise this in alpn
@@ -264,9 +267,9 @@ extension HTTPConnectionPool.ConnectionFactory {
264267 } catch {
265268 return channel. eventLoop. makeFailedFuture ( error)
266269 }
267- } . flatMap { negotiated -> EventLoopFuture < ( Channel , HTTPVersion ) > in
270+ } . flatMap { negotiated -> EventLoopFuture < NegotiatedProtocol > in
268271 channel. pipeline. removeHandler ( tlsEventHandler) . flatMapThrowing {
269- ( channel , try self . matchALPNToHTTPVersion ( negotiated) )
272+ try self . matchALPNToHTTPVersion ( negotiated, channel : channel )
270273 }
271274 }
272275 }
@@ -399,12 +402,12 @@ extension HTTPConnectionPool.ConnectionFactory {
399402 return eventLoop. makeSucceededFuture ( bootstrap)
400403 }
401404
402- private func matchALPNToHTTPVersion( _ negotiated: String ? ) throws -> HTTPVersion {
405+ private func matchALPNToHTTPVersion( _ negotiated: String ? , channel : Channel ) throws -> NegotiatedProtocol {
403406 switch negotiated {
404407 case . none, . some( " http/1.1 " ) :
405- return . http1_1
408+ return . http1_1( channel )
406409 case . some( " h2 " ) :
407- return . http2
410+ return . http2( channel )
408411 case . some( let unsupported) :
409412 throw HTTPClientError . serverOfferedUnsupportedApplicationProtocol ( unsupported)
410413 }
0 commit comments