diff --git a/Sources/NIOHTTP2/HTTP2StreamChannel.swift b/Sources/NIOHTTP2/HTTP2StreamChannel.swift index 05e719b..64b135c 100644 --- a/Sources/NIOHTTP2/HTTP2StreamChannel.swift +++ b/Sources/NIOHTTP2/HTTP2StreamChannel.swift @@ -15,6 +15,28 @@ import NIO import CNIONghttp2 +/// `StreamIDOption` allows users to query the stream ID for a given `HTTP2StreamChannel`. +/// +/// On active `HTTP2StreamChannel`s, it is possible that a channel handler or user may need to know which +/// stream ID the channel owns. This channel option allows that query. Please note that this channel option +/// is *get-only*: that is, it cannot be used with `setOption`. The stream ID for a given `HTTP2StreamChannel` +/// is immutable. +public enum StreamIDOption: ChannelOption { + public typealias AssociatedValueType = Void + public typealias OptionType = Int + + case const(Void) +} + +/// The various channel options specific to `HTTP2StreamChannel`s. +/// +/// Please note that some of NIO's regular `ChannelOptions` are valid on `HTTP2StreamChannel`s. +public struct HTTP2StreamChannelOptions { + /// - seealso: `StreamIDOption`. + public static let streamID: StreamIDOption = .const(()) +} + + final class HTTP2StreamChannel: Channel, ChannelCore { public init(allocator: ByteBufferAllocator, parent: Channel, streamID: Int32, initializer: ((Channel, Int) -> EventLoopFuture)?) { self.allocator = allocator @@ -66,8 +88,27 @@ final class HTTP2StreamChannel: Channel, ChannelCore { fatalError() } - func getOption(option: T) -> EventLoopFuture where T : ChannelOption { - fatalError() + public func getOption(option: T) -> EventLoopFuture where T: ChannelOption { + if eventLoop.inEventLoop { + do { + return eventLoop.newSucceededFuture(result: try getOption0(option: option)) + } catch { + return eventLoop.newFailedFuture(error: error) + } + } else { + return eventLoop.submit { try self.getOption0(option: option) } + } + } + + func getOption0(option: T) throws -> T.OptionType { + assert(eventLoop.inEventLoop) + + switch option { + case _ as StreamIDOption: + return self.streamID as! T.OptionType + default: + fatalError("option \(option) not supported") + } } public let isWritable: Bool @@ -124,3 +165,5 @@ final class HTTP2StreamChannel: Channel, ChannelCore { fatalError("not implemented \(#function)") } } + + diff --git a/Sources/NIOHTTP2Server/main.swift b/Sources/NIOHTTP2Server/main.swift index 27a6627..0783851 100644 --- a/Sources/NIOHTTP2Server/main.swift +++ b/Sources/NIOHTTP2Server/main.swift @@ -21,15 +21,17 @@ final class HTTP1TestServer: ChannelInboundHandler { public typealias OutboundOut = HTTPServerResponsePart public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { - var headers = HTTPHeaders() - headers.add(name: "content-length", value: "5") - ctx.channel.write(self.wrapOutboundOut(HTTPServerResponsePart.head(HTTPResponseHead(version: .init(major: 2, minor: 0), status: .ok, headers: headers))), promise: nil) + ctx.channel.getOption(option: HTTP2StreamChannelOptions.streamID).then { (streamID) -> EventLoopFuture in + var headers = HTTPHeaders() + headers.add(name: "content-length", value: "5") + headers.add(name: "x-stream-id", value: String(streamID)) + ctx.channel.write(self.wrapOutboundOut(HTTPServerResponsePart.head(HTTPResponseHead(version: .init(major: 2, minor: 0), status: .ok, headers: headers))), promise: nil) - var buffer = ctx.channel.allocator.buffer(capacity: 12) - buffer.write(staticString: "hello") - ctx.channel.write(self.wrapOutboundOut(HTTPServerResponsePart.body(.byteBuffer(buffer))), promise: nil) - - ctx.channel.writeAndFlush(self.wrapOutboundOut(HTTPServerResponsePart.end(nil))).whenComplete { + var buffer = ctx.channel.allocator.buffer(capacity: 12) + buffer.write(staticString: "hello") + ctx.channel.write(self.wrapOutboundOut(HTTPServerResponsePart.body(.byteBuffer(buffer))), promise: nil) + return ctx.channel.writeAndFlush(self.wrapOutboundOut(HTTPServerResponsePart.end(nil))) + }.whenComplete { ctx.close(promise: nil) } }