Skip to content

Commit

Permalink
Fixes for Swift 6 mode with latest SwiftNIO (#605)
Browse files Browse the repository at this point in the history
Co-authored-by: Joannis Orlandos <[email protected]>
  • Loading branch information
adam-fowler and Joannis authored Nov 15, 2024
1 parent 6d796fd commit 8921230
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
39 changes: 19 additions & 20 deletions Sources/HummingbirdHTTP2/HTTP2Channel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,27 +215,26 @@ extension Channel {
http1ConnectionInitializer: @escaping NIOChannelInitializerWithOutput<HTTP1Output>,
http2ConnectionInitializer: @escaping NIOChannelInitializerWithOutput<HTTP2Output>
) -> EventLoopFuture<EventLoopFuture<NIONegotiatedHTTPVersion<HTTP1Output, HTTP2Output>>> {
let alpnHandler = NIOTypedApplicationProtocolNegotiationHandler<NIONegotiatedHTTPVersion<HTTP1Output, HTTP2Output>>() { result in
switch result {
case .negotiated("h2"):
// Successful upgrade to HTTP/2. Let the user configure the pipeline.
return http2ConnectionInitializer(self).map { http2Output in .http2(http2Output) }
case .negotiated("http/1.1"), .fallback:
// Explicit or implicit HTTP/1.1 choice.
return http1ConnectionInitializer(self).map { http1Output in .http1_1(http1Output) }
case .negotiated:
// We negotiated something that isn't HTTP/1.1. This is a bad scene, and is a good indication
// of a user configuration error. We're going to close the connection directly.
return self.close().flatMap { self.eventLoop.makeFailedFuture(NIOHTTP2Errors.invalidALPNToken()) }
}
}

return self.pipeline
.addHandler(alpnHandler)
.flatMap { _ in
self.pipeline.handler(type: NIOTypedApplicationProtocolNegotiationHandler<NIONegotiatedHTTPVersion<HTTP1Output, HTTP2Output>>.self).map { alpnHandler in
alpnHandler.protocolNegotiationResult
return self.eventLoop.makeCompletedFuture {
let alpnHandler = NIOTypedApplicationProtocolNegotiationHandler<NIONegotiatedHTTPVersion<HTTP1Output, HTTP2Output>>() { result in
switch result {
case .negotiated("h2"):
// Successful upgrade to HTTP/2. Let the user configure the pipeline.
return http2ConnectionInitializer(self).map { http2Output in .http2(http2Output) }
case .negotiated("http/1.1"), .fallback:
// Explicit or implicit HTTP/1.1 choice.
return http1ConnectionInitializer(self).map { http1Output in .http1_1(http1Output) }
case .negotiated:
// We negotiated something that isn't HTTP/1.1. This is a bad scene, and is a good indication
// of a user configuration error. We're going to close the connection directly.
return self.close().flatMap { self.eventLoop.makeFailedFuture(NIOHTTP2Errors.invalidALPNToken()) }
}
}
try self.pipeline.syncOperations.addHandler(alpnHandler)
}.flatMap { _ in
self.pipeline.handler(type: NIOTypedApplicationProtocolNegotiationHandler<NIONegotiatedHTTPVersion<HTTP1Output, HTTP2Output>>.self).map { alpnHandler in
alpnHandler.protocolNegotiationResult
}
}
}
}
4 changes: 3 additions & 1 deletion Sources/HummingbirdTLS/TLSChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public struct TLSChannel<BaseChannel: ServerChildChannel>: ServerChildChannel {
/// - Returns: Object to process input/output on child channel
@inlinable
public func setup(channel: Channel, logger: Logger) -> EventLoopFuture<Value> {
return channel.pipeline.addHandler(NIOSSLServerHandler(context: self.sslContext)).flatMap {
return channel.eventLoop.makeCompletedFuture {
try channel.pipeline.syncOperations.addHandler(NIOSSLServerHandler(context: self.sslContext))
}.flatMap {
self.baseChannel.setup(channel: channel, logger: logger)
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/HummingbirdTesting/TestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ public struct TestClient: Sendable {
.channelOption(ChannelOptions.socket(SocketOptionLevel(IPPROTO_TCP), TCP_NODELAY), value: 1)
.channelInitializer { channel in
return channel.pipeline.addHTTPClientHandlers()
.flatMap {
.flatMapThrowing {
let handlers: [ChannelHandler] = [
HTTP1ToHTTPClientCodec(),
HTTPClientRequestSerializer(),
HTTPClientResponseHandler(),
HTTPTaskHandler(),
]
return channel.pipeline.addHandlers(handlers)
return try channel.pipeline.syncOperations.addHandlers(handlers)
}
}
.connectTimeout(.seconds(5))
Expand Down

0 comments on commit 8921230

Please sign in to comment.