From bab2bb7cfc1d00755241877347536bb3bba4bd16 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Wed, 18 Apr 2018 16:28:12 +0100 Subject: [PATCH] Removed debugging handlers --- Sources/NIOHTTP2/HTTP2StreamChannel.swift | 33 +++++------------- Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift | 10 ++++-- Sources/NIOHTTP2/HTTP2ToHTTP1Codec.swift | 24 ++----------- Sources/NIOHTTP2Server/main.swift | 34 ++++++++++++++++--- 4 files changed, 48 insertions(+), 53 deletions(-) diff --git a/Sources/NIOHTTP2/HTTP2StreamChannel.swift b/Sources/NIOHTTP2/HTTP2StreamChannel.swift index da901aa..05e719b 100644 --- a/Sources/NIOHTTP2/HTTP2StreamChannel.swift +++ b/Sources/NIOHTTP2/HTTP2StreamChannel.swift @@ -15,42 +15,23 @@ import NIO import CNIONghttp2 -public final class PrintEverythingHandler: ChannelInboundHandler, ChannelOutboundHandler { - public typealias InboundIn = Any - public typealias OutboundIn = Any - - private let prefix: String - - public init(prefix: String) { - self.prefix = prefix - } - - public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { - print("-> \(self.prefix): \(self.unwrapInboundIn(data))") - ctx.fireChannelRead(data) - } - - public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { - print("<- \(self.prefix): \(self.unwrapInboundIn(data))") - ctx.write(data, promise: promise) - } -} - final class HTTP2StreamChannel: Channel, ChannelCore { - public init(allocator: ByteBufferAllocator, parent: Channel, streamID: Int32) { + public init(allocator: ByteBufferAllocator, parent: Channel, streamID: Int32, initializer: ((Channel, Int) -> EventLoopFuture)?) { self.allocator = allocator self.closePromise = parent.eventLoop.newPromise() self.localAddress = parent.localAddress self.remoteAddress = parent.remoteAddress self.parent = parent self.eventLoop = parent.eventLoop + self.streamID = Int(streamID) // FIXME: that's just wrong self.isWritable = true self.isActive = true self._pipeline = ChannelPipeline(channel: self) - _ = self._pipeline.add(handler: HTTP2ToHTTP1Codec(streamID: streamID)) - _ = self._pipeline.add(handler: PrintEverythingHandler(prefix: "inner")) - _ = self._pipeline.add(handler: HTTP1TestServer()) + + // TODO(cory): This needs to do some state management, but we can't until we do the proper interface for communicating between + // the multiplexer and this channel. + _ = initializer?(self, self.streamID) } private var _pipeline: ChannelPipeline! @@ -99,6 +80,8 @@ final class HTTP2StreamChannel: Channel, ChannelCore { public let eventLoop: EventLoop + private let streamID: Int + public func register0(promise: EventLoopPromise?) { fatalError("not implemented \(#function)") } diff --git a/Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift b/Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift index 7f105f1..31a3e45 100644 --- a/Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift +++ b/Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift @@ -21,6 +21,7 @@ public final class HTTP2StreamMultiplexer: ChannelInboundHandler, ChannelOutboun public typealias OutboundOut = HTTP2Frame private var streams: [Int32: Channel] = [:] + private let streamStateInitializer: ((Channel, Int) -> EventLoopFuture)? public func channelActive(ctx: ChannelHandlerContext) { let frame = HTTP2Frame(header: .init(streamID: 0), payload: .settings([])) @@ -36,7 +37,10 @@ public final class HTTP2StreamMultiplexer: ChannelInboundHandler, ChannelOutboun switch (streamID, frame.payload) { case (0, _), (_, .headers(_)): /* this is legal, new stream starts with a header frame */ - self.streams[streamID] = HTTP2StreamChannel(allocator: ctx.channel.allocator, parent: ctx.channel, streamID: streamID) + self.streams[streamID] = HTTP2StreamChannel(allocator: ctx.channel.allocator, + parent: ctx.channel, + streamID: streamID, + initializer: self.streamStateInitializer) self.channelRead(ctx: ctx, data: data) default: fatalError("unknown stream \(frame.header.streamID) with \(frame.payload)") @@ -49,5 +53,7 @@ public final class HTTP2StreamMultiplexer: ChannelInboundHandler, ChannelOutboun ctx.write(data, promise: promise) } - public init() {} + public init(streamStateInitializer: ((Channel, Int) -> EventLoopFuture)? = nil) { + self.streamStateInitializer = streamStateInitializer + } } diff --git a/Sources/NIOHTTP2/HTTP2ToHTTP1Codec.swift b/Sources/NIOHTTP2/HTTP2ToHTTP1Codec.swift index d6858ff..fef094a 100644 --- a/Sources/NIOHTTP2/HTTP2ToHTTP1Codec.swift +++ b/Sources/NIOHTTP2/HTTP2ToHTTP1Codec.swift @@ -27,26 +27,6 @@ private extension HTTPMethod { } } -public final class HTTP1TestServer: ChannelInboundHandler { - public typealias InboundIn = HTTPServerRequestPart - public typealias OutboundOut = HTTPServerResponsePart - - public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { - var headers = HTTPHeaders() - headers.add(name: "content-length", value: "5") - headers.add(name: "x-cory-kicks-nghttp2s-ass", value: "true") - 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 { - ctx.close(promise: nil) - } - } -} - public final class HTTP2ToHTTP1Codec: ChannelInboundHandler, ChannelOutboundHandler { public typealias InboundIn = HTTP2Frame public typealias InboundOut = HTTPServerRequestPart @@ -56,8 +36,8 @@ public final class HTTP2ToHTTP1Codec: ChannelInboundHandler, ChannelOutboundHand private let streamID: Int32 - public init(streamID: Int32) { - self.streamID = streamID + public init(streamID: Int) { + self.streamID = Int32(streamID) } public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { diff --git a/Sources/NIOHTTP2Server/main.swift b/Sources/NIOHTTP2Server/main.swift index 423c154..a5f029f 100644 --- a/Sources/NIOHTTP2Server/main.swift +++ b/Sources/NIOHTTP2Server/main.swift @@ -13,8 +13,29 @@ //===----------------------------------------------------------------------===// import NIO +import NIOHTTP1 import NIOHTTP2 +final class HTTP1TestServer: ChannelInboundHandler { + public typealias InboundIn = HTTPServerRequestPart + 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) + + 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 { + ctx.close(promise: nil) + } + } +} + + // First argument is the program path let arguments = CommandLine.arguments let arg1 = arguments.dropFirst().first @@ -49,6 +70,13 @@ default: htdocs = defaultHtdocs bindTarget = BindTo.ip(host: defaultHost, port: defaultPort) } + +let multiplexer = HTTP2StreamMultiplexer { channel, streamID in + return channel.pipeline.add(handler: HTTP2ToHTTP1Codec(streamID: streamID)).then { () in + return channel.pipeline.add(handler: HTTP1TestServer()) + } +} + let group = MultiThreadedEventLoopGroup(numThreads: System.coreCount) let bootstrap = ServerBootstrap(group: group) // Specify backlog and enable SO_REUSEADDR for the server itself @@ -57,10 +85,8 @@ let bootstrap = ServerBootstrap(group: group) // Set the handlers that are applied to the accepted Channels .childChannelInitializer { channel in - return channel.pipeline.add(handler: HTTP2Parser()).then { () in - return channel.pipeline.add(handler: PrintEverythingHandler(prefix: "outer")).then { () in - return channel.pipeline.add(handler: HTTP2StreamMultiplexer()) - } + return channel.pipeline.add(handler: HTTP2Parser()).then { + return channel.pipeline.add(handler: multiplexer) } }