Skip to content

Commit

Permalink
Add requirement that channel is accessible in ServerChildChannel.Value (
Browse files Browse the repository at this point in the history
#450)

* Add requirement that channel is accessible in ServerChildChannel.Value

* Update Sources/HummingbirdHTTP2/HTTP2Channel.swift

Co-authored-by: Joannis Orlandos <joannis@orlandos.nl>

* Update Sources/HummingbirdHTTP2/HTTP2Channel.swift

Co-authored-by: Joannis Orlandos <joannis@orlandos.nl>

* Update Sources/HummingbirdHTTP2/HTTP2Channel.swift

Co-authored-by: Joannis Orlandos <joannis@orlandos.nl>

---------

Co-authored-by: Joannis Orlandos <joannis@orlandos.nl>
  • Loading branch information
adam-fowler and Joannis committed May 15, 2024
1 parent b303533 commit 3f9d6b3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
7 changes: 7 additions & 0 deletions Sources/HummingbirdCore/Server/HTTP/HTTP1Channel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,10 @@ public struct HTTP1Channel: ServerChildChannel, HTTPChannelHandler {
public let responder: @Sendable (Request, Channel) async throws -> Response
let additionalChannelHandlers: @Sendable () -> [any RemovableChannelHandler]
}

/// Extend NIOAsyncChannel to ServerChildChannelValue so it can be used in a ServerChildChannel
#if hasFeature(RetroactiveAttribute)
extension NIOAsyncChannel: @retroactive ServerChildChannelValue {}
#else
extension NIOAsyncChannel: ServerChildChannelValue {}
#endif
10 changes: 8 additions & 2 deletions Sources/HummingbirdCore/Server/ServerChildChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ import Logging
import NIOCore
import ServiceLifecycle

/// HTTPServer child channel setup protocol
/// Protocol for typed server child channel
public protocol ServerChildChannelValue: Sendable {
/// Child channel that spawned child channel
var channel: Channel { get }
}

/// Generic server child channel setup protocol
public protocol ServerChildChannel: Sendable {
associatedtype Value: Sendable
associatedtype Value: ServerChildChannelValue

/// Setup child channel
/// - Parameters:
Expand Down
9 changes: 7 additions & 2 deletions Sources/HummingbirdHTTP2/HTTP2Channel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import NIOSSL

/// Child channel for processing HTTP1 with the option of upgrading to HTTP2
public struct HTTP2UpgradeChannel: HTTPChannelHandler {
public typealias Value = EventLoopFuture<NIONegotiatedHTTPVersion<HTTP1Channel.Value, (NIOAsyncChannel<HTTP2Frame, HTTP2Frame>, NIOHTTP2Handler.AsyncStreamMultiplexer<HTTP1Channel.Value>)>>
public struct Value: ServerChildChannelValue {
let negotiatedHTTPVersion: EventLoopFuture<NIONegotiatedHTTPVersion<HTTP1Channel.Value, (NIOAsyncChannel<HTTP2Frame, HTTP2Frame>, NIOHTTP2Handler.AsyncStreamMultiplexer<HTTP1Channel.Value>)>>
public let channel: Channel
}

private let sslContext: NIOSSLContext
private let http1: HTTP1Channel
Expand Down Expand Up @@ -91,6 +94,8 @@ public struct HTTP2UpgradeChannel: HTTPChannelHandler {
}.flatMapThrowing {
try HTTP1Channel.Value(wrappingChannelSynchronously: http2ChildChannel)
}
}.map {
.init(negotiatedHTTPVersion: $0, channel: channel)
}
}

Expand All @@ -100,7 +105,7 @@ public struct HTTP2UpgradeChannel: HTTPChannelHandler {
/// - logger: Logger to use while processing messages
public func handle(value: Value, logger: Logger) async {
do {
let channel = try await value.get()
let channel = try await value.negotiatedHTTPVersion.get()
switch channel {
case .http1_1(let http1):
await handleHTTP(asyncChannel: http1, logger: logger)
Expand Down

0 comments on commit 3f9d6b3

Please sign in to comment.