From ffe5102b7e7daea62588cb16bee551b6d0e2f9f7 Mon Sep 17 00:00:00 2001 From: Johannes Weiss Date: Tue, 26 Feb 2019 12:57:39 +0000 Subject: [PATCH] update to latest NIO 2 code (#45) Motivation: Code should compile and have the latest NIO 2 API. Modifications: - rename `ctx` to `context` Result: compiles again --- Sources/NIOHTTP2/HTTP2ChannelHandler.swift | 122 +++++++++--------- .../HTTP2ConcurrentStreamsHandler.swift | 36 +++--- Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift | 26 ++-- Sources/NIOHTTP2/HTTP2ToHTTP1Codec.swift | 44 +++---- .../NIOHTTP2/NIOHTTP2FlowControlHandler.swift | 32 ++--- Sources/NIOHTTP2Server/main.swift | 18 +-- .../ConfiguringPipelineTests.swift | 4 +- .../FlowControlHandlerTests.swift | 4 +- .../HTTP2ConcurrentStreamsHandlerTest.swift | 14 +- .../HTTP2StreamMultiplexerTests.swift | 24 ++-- .../HTTP2ToHTTP1CodecTests.swift | 4 +- Tests/NIOHTTP2Tests/ReentrancyTests.swift | 8 +- .../SimpleClientServerTests.swift | 48 +++---- 13 files changed, 192 insertions(+), 192 deletions(-) diff --git a/Sources/NIOHTTP2/HTTP2ChannelHandler.swift b/Sources/NIOHTTP2/HTTP2ChannelHandler.swift index eebcd9e..06a2f2e 100644 --- a/Sources/NIOHTTP2/HTTP2ChannelHandler.swift +++ b/Sources/NIOHTTP2/HTTP2ChannelHandler.swift @@ -90,55 +90,55 @@ public final class NIOHTTP2Handler: ChannelDuplexHandler { self.initialSettings = initialSettings } - public func handlerAdded(ctx: ChannelHandlerContext) { - self.frameDecoder = HTTP2FrameDecoder(allocator: ctx.channel.allocator, expectClientMagic: self.mode == .server) - self.frameEncoder = HTTP2FrameEncoder(allocator: ctx.channel.allocator) - self.writeBuffer = ctx.channel.allocator.buffer(capacity: 128) + public func handlerAdded(context: ChannelHandlerContext) { + self.frameDecoder = HTTP2FrameDecoder(allocator: context.channel.allocator, expectClientMagic: self.mode == .server) + self.frameEncoder = HTTP2FrameEncoder(allocator: context.channel.allocator) + self.writeBuffer = context.channel.allocator.buffer(capacity: 128) - if ctx.channel.isActive { - self.writeAndFlushPreamble(ctx: ctx) + if context.channel.isActive { + self.writeAndFlushPreamble(context: context) } } - public func channelActive(ctx: ChannelHandlerContext) { - self.writeAndFlushPreamble(ctx: ctx) - ctx.fireChannelActive() + public func channelActive(context: ChannelHandlerContext) { + self.writeAndFlushPreamble(context: context) + context.fireChannelActive() } - public func channelInactive(ctx: ChannelHandlerContext) { + public func channelInactive(context: ChannelHandlerContext) { self.channelClosed = true - ctx.fireChannelInactive() + context.fireChannelInactive() } - public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { + public func channelRead(context: ChannelHandlerContext, data: NIOAny) { var data = self.unwrapInboundIn(data) self.frameDecoder.append(bytes: &data) // Before we go in here we need to deliver any pending user events. This is because // we may have been called re-entrantly. - self.processPendingUserEvents(ctx: ctx) + self.processPendingUserEvents(context: context) // We parse eagerly to attempt to give back buffers to the reading channel wherever possible. - self.frameDecodeLoop(ctx: ctx) + self.frameDecodeLoop(context: context) } - public func channelReadComplete(ctx: ChannelHandlerContext) { + public func channelReadComplete(context: ChannelHandlerContext) { if self.wroteAutomaticFrame { self.wroteAutomaticFrame = false - ctx.flush() + context.flush() } - ctx.fireChannelReadComplete() + context.fireChannelReadComplete() } - public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { + public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { let frame = self.unwrapOutboundIn(data) - self.processOutboundFrame(ctx: ctx, frame: frame, promise: promise) + self.processOutboundFrame(context: context, frame: frame, promise: promise) } - public func flush(ctx: ChannelHandlerContext) { + public func flush(context: ChannelHandlerContext) { self.wroteAutomaticFrame = false - ctx.flush() + context.flush() } } @@ -146,26 +146,26 @@ public final class NIOHTTP2Handler: ChannelDuplexHandler { /// Inbound frame handling. extension NIOHTTP2Handler { /// Spins over the frame decoder parsing frames and sending them down the channel pipeline. - private func frameDecodeLoop(ctx: ChannelHandlerContext) { - while !self.channelClosed, let (nextFrame, length) = self.decodeFrame(ctx: ctx) { - guard case .continue = self.processFrame(nextFrame, flowControlledLength: length, ctx: ctx) else { + private func frameDecodeLoop(context: ChannelHandlerContext) { + while !self.channelClosed, let (nextFrame, length) = self.decodeFrame(context: context) { + guard case .continue = self.processFrame(nextFrame, flowControlledLength: length, context: context) else { break } } } /// Decodes a single frame. Returns `nil` if there is no frame to process, or if an error occurred. - private func decodeFrame(ctx: ChannelHandlerContext) -> (HTTP2Frame, flowControlledLength: Int)? { + private func decodeFrame(context: ChannelHandlerContext) -> (HTTP2Frame, flowControlledLength: Int)? { do { return try self.frameDecoder.nextFrame() } catch InternalError.codecError(let code) { - self.inboundConnectionErrorTriggered(ctx: ctx, underlyingError: NIOHTTP2Errors.UnableToParseFrame(), reason: code) + self.inboundConnectionErrorTriggered(context: context, underlyingError: NIOHTTP2Errors.UnableToParseFrame(), reason: code) return nil } catch is NIOHTTP2Errors.BadClientMagic { - self.inboundConnectionErrorTriggered(ctx: ctx, underlyingError: NIOHTTP2Errors.BadClientMagic(), reason: .protocolError) + self.inboundConnectionErrorTriggered(context: context, underlyingError: NIOHTTP2Errors.BadClientMagic(), reason: .protocolError) return nil } catch { - self.inboundConnectionErrorTriggered(ctx: ctx, underlyingError: error, reason: .internalError) + self.inboundConnectionErrorTriggered(context: context, underlyingError: error, reason: .internalError) return nil } } @@ -175,7 +175,7 @@ extension NIOHTTP2Handler { case stop } - private func processFrame(_ frame: HTTP2Frame, flowControlledLength: Int, ctx: ChannelHandlerContext) -> FrameProcessResult { + private func processFrame(_ frame: HTTP2Frame, flowControlledLength: Int, context: ChannelHandlerContext) -> FrameProcessResult { // All frames have one basic processing step: do we send them on, or drop them? // Some frames have further processing steps, regarding triggering user events or other operations. // Here we centralise this processing. @@ -201,7 +201,7 @@ extension NIOHTTP2Handler { self.writeBuffer.clear() var responseFrame = frame responseFrame.flags.insert(.ack) - self.encodeAndWriteFrame(ctx: ctx, frame: responseFrame, promise: nil) + self.encodeAndWriteFrame(context: context, frame: responseFrame, promise: nil) self.wroteAutomaticFrame = true } case .priority: @@ -221,7 +221,7 @@ extension NIOHTTP2Handler { break case .sendAck: self.writeBuffer.clear() - self.encodeAndWriteFrame(ctx: ctx, frame: HTTP2Frame(streamID: .rootStream, flags: .ack, payload: .settings([])), promise: nil) + self.encodeAndWriteFrame(context: context, frame: HTTP2Frame(streamID: .rootStream, flags: .ack, payload: .settings([])), promise: nil) self.wroteAutomaticFrame = true } @@ -235,55 +235,55 @@ extension NIOHTTP2Handler { switch result.result { case .succeed: // Frame is good, we can pass it on. - ctx.fireChannelRead(self.wrapInboundOut(frame)) + context.fireChannelRead(self.wrapInboundOut(frame)) returnValue = .continue case .ignoreFrame: // Frame is good but no action needs to be taken. returnValue = .continue case .connectionError(let underlyingError, let errorCode): // We should stop parsing on received connection errors, the connection is going away anyway. - self.inboundConnectionErrorTriggered(ctx: ctx, underlyingError: underlyingError, reason: errorCode) + self.inboundConnectionErrorTriggered(context: context, underlyingError: underlyingError, reason: errorCode) returnValue = .stop case .streamError(let streamID, let underlyingError, let errorCode): // We can continue parsing on stream errors in most cases, the frame is just ignored. - self.inboundStreamErrorTriggered(ctx: ctx, streamID: streamID, underlyingError: underlyingError, reason: errorCode) + self.inboundStreamErrorTriggered(context: context, streamID: streamID, underlyingError: underlyingError, reason: errorCode) returnValue = .continue } // Before we return the loop we process any user events that are currently pending. // These will likely only be ones that were generated now. - self.processPendingUserEvents(ctx: ctx) + self.processPendingUserEvents(context: context) return returnValue } /// A connection error was hit while receiving a frame. - private func inboundConnectionErrorTriggered(ctx: ChannelHandlerContext, underlyingError: Error, reason: HTTP2ErrorCode) { + private func inboundConnectionErrorTriggered(context: ChannelHandlerContext, underlyingError: Error, reason: HTTP2ErrorCode) { // A connection error brings the entire connection down. We attempt to write a GOAWAY frame, and then report this // error. It's possible that we'll be unable to write the GOAWAY frame, but that also just logs the error. // Because we don't know what data the user handled before we got this, we propose that they may have seen all of it. // The user may choose to fire a more specific error if they wish. let goAwayFrame = HTTP2Frame(streamID: .rootStream, payload: .goAway(lastStreamID: .maxID, errorCode: reason, opaqueData: nil)) - self.processOutboundFrame(ctx: ctx, frame: goAwayFrame, promise: nil) - ctx.flush() - ctx.fireErrorCaught(underlyingError) + self.processOutboundFrame(context: context, frame: goAwayFrame, promise: nil) + context.flush() + context.fireErrorCaught(underlyingError) } /// A stream error was hit while receiving a frame. - private func inboundStreamErrorTriggered(ctx: ChannelHandlerContext, streamID: HTTP2StreamID, underlyingError: Error, reason: HTTP2ErrorCode) { + private func inboundStreamErrorTriggered(context: ChannelHandlerContext, streamID: HTTP2StreamID, underlyingError: Error, reason: HTTP2ErrorCode) { // A stream error brings down a single stream, causing a RST_STREAM frame. We attempt to write this, and then report // the error. It's possible that we'll be unable to write this, which will likely escalate this error, but that's // the user's issue. let rstStreamFrame = HTTP2Frame(streamID: streamID, payload: .rstStream(reason)) - self.processOutboundFrame(ctx: ctx, frame: rstStreamFrame, promise: nil) - ctx.flush() - ctx.fireErrorCaught(underlyingError) + self.processOutboundFrame(context: context, frame: rstStreamFrame, promise: nil) + context.flush() + context.fireErrorCaught(underlyingError) } /// Emit any pending user events. - private func processPendingUserEvents(ctx: ChannelHandlerContext) { + private func processPendingUserEvents(context: ChannelHandlerContext) { for event in self.inboundEventBuffer { - ctx.fireUserInboundEventTriggered(event) + context.fireUserInboundEventTriggered(event) } } } @@ -292,19 +292,19 @@ extension NIOHTTP2Handler { /// Outbound frame handling. extension NIOHTTP2Handler { /// Issues the preamble when necessary. - private func writeAndFlushPreamble(ctx: ChannelHandlerContext) { + private func writeAndFlushPreamble(context: ChannelHandlerContext) { if case .client = self.mode { self.writeBuffer.clear() self.writeBuffer.writeStaticString(NIOHTTP2Handler.clientMagic) - ctx.write(self.wrapOutboundOut(.byteBuffer(self.writeBuffer)), promise: nil) + context.write(self.wrapOutboundOut(.byteBuffer(self.writeBuffer)), promise: nil) } let initialSettingsFrame = HTTP2Frame(streamID: .rootStream, payload: .settings(self.initialSettings)) - self.processOutboundFrame(ctx: ctx, frame: initialSettingsFrame, promise: nil) - ctx.flush() + self.processOutboundFrame(context: context, frame: initialSettingsFrame, promise: nil) + context.flush() } - private func processOutboundFrame(ctx: ChannelHandlerContext, frame: HTTP2Frame, promise: EventLoopPromise?) { + private func processOutboundFrame(context: ChannelHandlerContext, frame: HTTP2Frame, promise: EventLoopPromise?) { let result: StateMachineResultWithEffect switch frame.payload { @@ -338,45 +338,45 @@ extension NIOHTTP2Handler { case .ignoreFrame: preconditionFailure("Cannot be asked to ignore outbound frames.") case .connectionError(let underlyingError, _), .streamError(_, let underlyingError, _): - self.outboundErrorTriggered(ctx: ctx, promise: promise, underlyingError: underlyingError) + self.outboundErrorTriggered(context: context, promise: promise, underlyingError: underlyingError) return case .succeed: self.writeBuffer.clear() - self.encodeAndWriteFrame(ctx: ctx, frame: frame, promise: promise) + self.encodeAndWriteFrame(context: context, frame: frame, promise: promise) } // This may have caused user events that need to be fired, so do so. - self.processPendingUserEvents(ctx: ctx) + self.processPendingUserEvents(context: context) } /// Encodes a frame and writes it to the network. - private func encodeAndWriteFrame(ctx: ChannelHandlerContext, frame: HTTP2Frame, promise: EventLoopPromise?) { + private func encodeAndWriteFrame(context: ChannelHandlerContext, frame: HTTP2Frame, promise: EventLoopPromise?) { let extraFrameData: IOData? do { extraFrameData = try self.frameEncoder.encode(frame: frame, to: &self.writeBuffer) } catch InternalError.codecError { - self.outboundErrorTriggered(ctx: ctx, promise: promise, underlyingError: NIOHTTP2Errors.UnableToSerializeFrame()) + self.outboundErrorTriggered(context: context, promise: promise, underlyingError: NIOHTTP2Errors.UnableToSerializeFrame()) return } catch { - self.outboundErrorTriggered(ctx: ctx, promise: promise, underlyingError: error) + self.outboundErrorTriggered(context: context, promise: promise, underlyingError: error) return } // Ok, if we got here we're good to send data. We want to attach the promise to the latest write, not // always the frame header. if let extraFrameData = extraFrameData { - ctx.write(self.wrapOutboundOut(.byteBuffer(self.writeBuffer)), promise: nil) - ctx.write(self.wrapOutboundOut(extraFrameData), promise: promise) + context.write(self.wrapOutboundOut(.byteBuffer(self.writeBuffer)), promise: nil) + context.write(self.wrapOutboundOut(extraFrameData), promise: promise) } else { - ctx.write(self.wrapOutboundOut(.byteBuffer(self.writeBuffer)), promise: promise) + context.write(self.wrapOutboundOut(.byteBuffer(self.writeBuffer)), promise: promise) } } /// A stream or connection error was hit while attempting to send a frame. - private func outboundErrorTriggered(ctx: ChannelHandlerContext, promise: EventLoopPromise?, underlyingError: Error) { + private func outboundErrorTriggered(context: ChannelHandlerContext, promise: EventLoopPromise?, underlyingError: Error) { promise?.fail(underlyingError) - ctx.fireErrorCaught(underlyingError) + context.fireErrorCaught(underlyingError) } } diff --git a/Sources/NIOHTTP2/HTTP2ConcurrentStreamsHandler.swift b/Sources/NIOHTTP2/HTTP2ConcurrentStreamsHandler.swift index 017904e..6ea5c34 100644 --- a/Sources/NIOHTTP2/HTTP2ConcurrentStreamsHandler.swift +++ b/Sources/NIOHTTP2/HTTP2ConcurrentStreamsHandler.swift @@ -52,7 +52,7 @@ public class NIOHTTP2ConcurrentStreamsHandler: ChannelDuplexHandler { self.frameBuffer = StreamFrameBuffer(mode: mode, initialMaxOutboundStreams: initialMaxOutboundStreams) } - public func userInboundEventTriggered(ctx: ChannelHandlerContext, event: Any) { + public func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) { switch event { case let event as StreamClosedEvent: // This event may make some frames writable. If it does, then we may be in write() @@ -67,38 +67,38 @@ public class NIOHTTP2ConcurrentStreamsHandler: ChannelDuplexHandler { break } - ctx.fireUserInboundEventTriggered(event) + context.fireUserInboundEventTriggered(event) } - public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { + public func channelRead(context: ChannelHandlerContext, data: NIOAny) { let frame = self.unwrapInboundIn(data) guard case .settings(let newSettings) = frame.payload, !frame.flags.contains(.ack) else { // Either this is not a settings frame, or it's a settings ACK. Either way we don't care. // TODO(cory): We should handle GOAWAY! - ctx.fireChannelRead(data) + context.fireChannelRead(data) return } guard let newMaxConcurrentStreams = newSettings.lazy.reversed().first(where: { $0.parameter == .maxConcurrentStreams }).map( { $0.value } ) else { // This settings frame didn't change the value of SETTINGS_MAX_CONCURRENT_STREAMS - ctx.fireChannelRead(data) + context.fireChannelRead(data) return } // This is allowed to shrink maxConcurrentStreams. self.frameBuffer.maxOutboundStreams = newMaxConcurrentStreams - ctx.fireChannelRead(data) + context.fireChannelRead(data) } - public func channelReadComplete(ctx: ChannelHandlerContext) { - if self.writeIfPossible(ctx: ctx) { - ctx.flush() + public func channelReadComplete(context: ChannelHandlerContext) { + if self.writeIfPossible(context: context) { + context.flush() } - ctx.fireChannelReadComplete() + context.fireChannelReadComplete() } - public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { + public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { let frame = self.unwrapOutboundIn(data) let operation: StreamFrameBuffer.OutboundFrameAction @@ -113,10 +113,10 @@ public class NIOHTTP2ConcurrentStreamsHandler: ChannelDuplexHandler { case .nothing: break case .forward: - ctx.write(data, promise: promise) + context.write(data, promise: promise) case .forwardAndDrop(let writesToDrop, let error): // We forward *first*, drop *second*. - ctx.write(data, promise: promise) + context.write(data, promise: promise) for (_, promise) in writesToDrop { promise?.fail(error) } @@ -129,14 +129,14 @@ public class NIOHTTP2ConcurrentStreamsHandler: ChannelDuplexHandler { } } - public func flush(ctx: ChannelHandlerContext) { + public func flush(context: ChannelHandlerContext) { self.frameBuffer.flushReceived() - self.writeIfPossible(ctx: ctx) - ctx.flush() + self.writeIfPossible(context: context) + context.flush() } @discardableResult - private func writeIfPossible(ctx: ChannelHandlerContext) -> Bool { + private func writeIfPossible(context: ChannelHandlerContext) -> Bool { // We need to spin our writing loop. How does this work? // // We may be buffering a number of frames for streams that are now ready to be unbuffered. @@ -147,7 +147,7 @@ public class NIOHTTP2ConcurrentStreamsHandler: ChannelDuplexHandler { var didWrite = false while let (frame, promise) = self.frameBuffer.nextFlushedWritableFrame() { - ctx.write(self.wrapOutboundOut(frame), promise: promise) + context.write(self.wrapOutboundOut(frame), promise: promise) didWrite = true } diff --git a/Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift b/Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift index b6cd959..82c76bf 100644 --- a/Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift +++ b/Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift @@ -33,19 +33,19 @@ public final class HTTP2StreamMultiplexer: ChannelInboundHandler, ChannelOutboun private var nextOutboundStreamID: HTTP2StreamID private let connectionFlowControlManager: InboundWindowManager - public func handlerAdded(ctx: ChannelHandlerContext) { + public func handlerAdded(context: ChannelHandlerContext) { // We now need to check that we're on the same event loop as the one we were originally given. // If we weren't, this is a hard failure, as there is a thread-safety issue here. self.channel.eventLoop.preconditionInEventLoop() } - public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { + public func channelRead(context: ChannelHandlerContext, data: NIOAny) { let frame = self.unwrapInboundIn(data) let streamID = frame.streamID guard streamID != .rootStream else { // For stream 0 we forward all frames on to the main channel. - ctx.fireChannelRead(data) + context.fireChannelRead(data) return } @@ -67,27 +67,27 @@ public final class HTTP2StreamMultiplexer: ChannelInboundHandler, ChannelOutboun // This frame is for a stream we know nothing about. We can't do much about it, so we // are going to fire an error and drop the frame. let error = NIOHTTP2Errors.NoSuchStream(streamID: streamID) - ctx.fireErrorCaught(error) + context.fireErrorCaught(error) } } - public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { + public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { /* for now just forward */ - ctx.write(data, promise: promise) + context.write(data, promise: promise) } - public func channelActive(ctx: ChannelHandlerContext) { + public func channelActive(context: ChannelHandlerContext) { // We just got channelActive. Any previously existing channels may be marked active. for channel in self.streams.values { // We double-check the channel activity here, because it's possible action taken during // the activation of one of the child channels will cause the parent to close! - if ctx.channel.isActive { + if context.channel.isActive { channel.performActivation() } } } - public func userInboundEventTriggered(ctx: ChannelHandlerContext, event: Any) { + public func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) { switch event { case let evt as StreamClosedEvent: if let channel = self.streams[evt.streamID] { @@ -95,7 +95,7 @@ public final class HTTP2StreamMultiplexer: ChannelInboundHandler, ChannelOutboun } case let evt as NIOHTTP2WindowUpdatedEvent where evt.streamID == .rootStream: // This force-unwrap is safe: we always have a connection window. - self.newConnectionWindowSize(newSize: evt.inboundWindowSize!, ctx: ctx) + self.newConnectionWindowSize(newSize: evt.inboundWindowSize!, context: context) case let evt as NIOHTTP2WindowUpdatedEvent: if let channel = self.streams[evt.streamID], let windowSize = evt.inboundWindowSize { channel.receiveWindowUpdatedEvent(windowSize) @@ -104,21 +104,21 @@ public final class HTTP2StreamMultiplexer: ChannelInboundHandler, ChannelOutboun break } - ctx.fireUserInboundEventTriggered(event) + context.fireUserInboundEventTriggered(event) } private func childChannelClosed(streamID: HTTP2StreamID) { self.streams.removeValue(forKey: streamID) } - private func newConnectionWindowSize(newSize: Int, ctx: ChannelHandlerContext) { + private func newConnectionWindowSize(newSize: Int, context: ChannelHandlerContext) { guard let increment = self.connectionFlowControlManager.newWindowSize(newSize) else { return } // This is too much flushing, but for now it'll have to do. let frame = HTTP2Frame(streamID: .rootStream, payload: .windowUpdate(windowSizeIncrement: increment)) - ctx.writeAndFlush(self.wrapOutboundOut(frame), promise: nil) + context.writeAndFlush(self.wrapOutboundOut(frame), promise: nil) } /// Create a new `HTTP2StreamMultiplexer`. diff --git a/Sources/NIOHTTP2/HTTP2ToHTTP1Codec.swift b/Sources/NIOHTTP2/HTTP2ToHTTP1Codec.swift index e9ad313..0617518 100644 --- a/Sources/NIOHTTP2/HTTP2ToHTTP1Codec.swift +++ b/Sources/NIOHTTP2/HTTP2ToHTTP1Codec.swift @@ -52,24 +52,24 @@ public final class HTTP2ToHTTP1ClientCodec: ChannelInboundHandler, ChannelOutbou } } - public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { + public func channelRead(context: ChannelHandlerContext, data: NIOAny) { let frame = self.unwrapInboundIn(data) switch frame.payload { case .headers(let headers, _): if case .trailer = self.headerStateMachine.newHeaders(block: headers.asH1Headers()) { - ctx.fireChannelRead(self.wrapInboundOut(.end(headers.asH1Headers()))) + context.fireChannelRead(self.wrapInboundOut(.end(headers.asH1Headers()))) } else { let respHead = HTTPResponseHead(http2HeaderBlock: headers.asH1Headers()) - ctx.fireChannelRead(self.wrapInboundOut(.head(respHead))) + context.fireChannelRead(self.wrapInboundOut(.head(respHead))) if frame.flags.contains(.endStream) { - ctx.fireChannelRead(self.wrapInboundOut(.end(nil))) + context.fireChannelRead(self.wrapInboundOut(.end(nil))) } } case .data(.byteBuffer(let b)): - ctx.fireChannelRead(self.wrapInboundOut(.body(b))) + context.fireChannelRead(self.wrapInboundOut(.body(b))) if frame.flags.contains(.endStream) { - ctx.fireChannelRead(self.wrapInboundOut(.end(nil))) + context.fireChannelRead(self.wrapInboundOut(.end(nil))) } case .alternativeService, .rstStream, .priority, .windowUpdate: // All these frames may be sent on a stream, but are ignored by the multiplexer and will be @@ -80,17 +80,17 @@ public final class HTTP2ToHTTP1ClientCodec: ChannelInboundHandler, ChannelOutbou } } - public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { + public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { let responsePart = self.unwrapOutboundIn(data) switch responsePart { case .head(let head): let h1Headers = HTTPHeaders(requestHead: head, protocolString: self.protocolString) let frame = HTTP2Frame(streamID: self.streamID, flags: .endHeaders, payload: .headers(HPACKHeaders(httpHeaders: h1Headers), nil)) - ctx.write(self.wrapOutboundOut(frame), promise: promise) + context.write(self.wrapOutboundOut(frame), promise: promise) case .body(let body): let payload = HTTP2Frame.FramePayload.data(body) let frame = HTTP2Frame(streamID: self.streamID, payload: payload) - ctx.write(self.wrapOutboundOut(frame), promise: promise) + context.write(self.wrapOutboundOut(frame), promise: promise) case .end(let trailers): let payload: HTTP2Frame.FramePayload var flags: HTTP2Frame.FrameFlags = .endStream @@ -98,11 +98,11 @@ public final class HTTP2ToHTTP1ClientCodec: ChannelInboundHandler, ChannelOutbou payload = .headers(HPACKHeaders(httpHeaders: trailers), nil) flags.insert(.endHeaders) } else { - payload = HTTP2Frame.FramePayload.data(.byteBuffer(ctx.channel.allocator.buffer(capacity: 0))) + payload = HTTP2Frame.FramePayload.data(.byteBuffer(context.channel.allocator.buffer(capacity: 0))) } let frame = HTTP2Frame(streamID: self.streamID, flags: flags, payload: payload) - ctx.write(self.wrapOutboundOut(frame), promise: promise) + context.write(self.wrapOutboundOut(frame), promise: promise) } } } @@ -129,24 +129,24 @@ public final class HTTP2ToHTTP1ServerCodec: ChannelInboundHandler, ChannelOutbou self.streamID = streamID } - public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { + public func channelRead(context: ChannelHandlerContext, data: NIOAny) { let frame = self.unwrapInboundIn(data) switch frame.payload { case .headers(let headers, _): if case .trailer = self.headerStateMachine.newHeaders(block: headers.asH1Headers()) { - ctx.fireChannelRead(self.wrapInboundOut(.end(headers.asH1Headers()))) + context.fireChannelRead(self.wrapInboundOut(.end(headers.asH1Headers()))) } else { let reqHead = HTTPRequestHead(http2HeaderBlock: headers.asH1Headers()) - ctx.fireChannelRead(self.wrapInboundOut(.head(reqHead))) + context.fireChannelRead(self.wrapInboundOut(.head(reqHead))) if frame.flags.contains(.endStream) { - ctx.fireChannelRead(self.wrapInboundOut(.end(nil))) + context.fireChannelRead(self.wrapInboundOut(.end(nil))) } } case .data(.byteBuffer(let b)): - ctx.fireChannelRead(self.wrapInboundOut(.body(b))) + context.fireChannelRead(self.wrapInboundOut(.body(b))) if frame.flags.contains(.endStream) { - ctx.fireChannelRead(self.wrapInboundOut(.end(nil))) + context.fireChannelRead(self.wrapInboundOut(.end(nil))) } case .alternativeService, .rstStream, .priority, .windowUpdate: // All these frames may be sent on a stream, but are ignored by the multiplexer and will be @@ -157,17 +157,17 @@ public final class HTTP2ToHTTP1ServerCodec: ChannelInboundHandler, ChannelOutbou } } - public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { + public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { let responsePart = self.unwrapOutboundIn(data) switch responsePart { case .head(let head): let h1 = HTTPHeaders(responseHead: head) let frame = HTTP2Frame(streamID: self.streamID, flags: .endHeaders, payload: .headers(HPACKHeaders(httpHeaders: h1), nil)) - ctx.write(self.wrapOutboundOut(frame), promise: promise) + context.write(self.wrapOutboundOut(frame), promise: promise) case .body(let body): let payload = HTTP2Frame.FramePayload.data(body) let frame = HTTP2Frame(streamID: self.streamID, payload: payload) - ctx.write(self.wrapOutboundOut(frame), promise: promise) + context.write(self.wrapOutboundOut(frame), promise: promise) case .end(let trailers): let payload: HTTP2Frame.FramePayload var flags: HTTP2Frame.FrameFlags = .endStream @@ -175,11 +175,11 @@ public final class HTTP2ToHTTP1ServerCodec: ChannelInboundHandler, ChannelOutbou payload = .headers(HPACKHeaders(httpHeaders: trailers), nil) flags.insert(.endHeaders) } else { - payload = HTTP2Frame.FramePayload.data(.byteBuffer(ctx.channel.allocator.buffer(capacity: 0))) + payload = HTTP2Frame.FramePayload.data(.byteBuffer(context.channel.allocator.buffer(capacity: 0))) } let frame = HTTP2Frame(streamID: self.streamID, flags: flags, payload: payload) - ctx.write(self.wrapOutboundOut(frame), promise: promise) + context.write(self.wrapOutboundOut(frame), promise: promise) } } } diff --git a/Sources/NIOHTTP2/NIOHTTP2FlowControlHandler.swift b/Sources/NIOHTTP2/NIOHTTP2FlowControlHandler.swift index 256277f..aa6a0fe 100644 --- a/Sources/NIOHTTP2/NIOHTTP2FlowControlHandler.swift +++ b/Sources/NIOHTTP2/NIOHTTP2FlowControlHandler.swift @@ -45,7 +45,7 @@ public class NIOHTTP2FlowControlHandler: ChannelDuplexHandler { self.maxFrameSize = initialMaxFrameSize } - public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { + public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { let frame = self.unwrapOutboundIn(data) switch frame.payload { case .data(let body): @@ -55,7 +55,7 @@ public class NIOHTTP2FlowControlHandler: ChannelDuplexHandler { // it can happen due to channel handler misconfiguration or other weirdness. We'll just complain. let error = NIOHTTP2Errors.NoSuchStream(streamID: frame.streamID) promise?.fail(error) - ctx.fireErrorCaught(error) + context.fireErrorCaught(error) } case .headers(let headers, let priorityData): // Headers are special. If we have a data frame buffered, we buffer behind it to avoid frames @@ -76,15 +76,15 @@ public class NIOHTTP2FlowControlHandler: ChannelDuplexHandler { break case .some(false), .none: // We don't need to buffer this, pass it on. - ctx.write(data, promise: promise) + context.write(data, promise: promise) } default: // For all other frame types, we don't care about them, pass them on. - ctx.write(data, promise: promise) + context.write(data, promise: promise) } } - public func flush(ctx: ChannelHandlerContext) { + public func flush(context: ChannelHandlerContext) { // Two stages of flushing. The first thing is to mark the flush point on all of the streams we have. self.streamDataBuffers.mutatingForEachValue { let hadData = $0.hasPendingData @@ -99,11 +99,11 @@ public class NIOHTTP2FlowControlHandler: ChannelDuplexHandler { // This loop here is a while because we make outcalls here, and these outcalls // may cause re-entrant behaviour. We need to not be holding our structures mutably // over the duration of this outcall. - self.writeIfPossible(ctx: ctx) - ctx.flush() + self.writeIfPossible(context: context) + context.flush() } - public func userInboundEventTriggered(ctx: ChannelHandlerContext, event: Any) { + public func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) { switch event { case let event as NIOHTTP2WindowUpdatedEvent: if let windowSize = event.outboundWindowSize { @@ -119,10 +119,10 @@ public class NIOHTTP2FlowControlHandler: ChannelDuplexHandler { break } - ctx.fireUserInboundEventTriggered(event) + context.fireUserInboundEventTriggered(event) } - public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { + public func channelRead(context: ChannelHandlerContext, data: NIOAny) { let frame = self.unwrapInboundIn(data) if case .settings(let newSettings) = frame.payload, !frame.flags.contains(.ack) { // This is a SETTINGS frame for new settings from the remote peer. We @@ -133,13 +133,13 @@ public class NIOHTTP2FlowControlHandler: ChannelDuplexHandler { } } - ctx.fireChannelRead(data) + context.fireChannelRead(data) } - public func channelReadComplete(ctx: ChannelHandlerContext) { - let didWrite = self.writeIfPossible(ctx: ctx) + public func channelReadComplete(context: ChannelHandlerContext) { + let didWrite = self.writeIfPossible(context: context) if didWrite { - ctx.flush() + context.flush() } } @@ -188,7 +188,7 @@ public class NIOHTTP2FlowControlHandler: ChannelDuplexHandler { /// /// - returns: Whether this issued a write. @discardableResult - private func writeIfPossible(ctx: ChannelHandlerContext) -> Bool { + private func writeIfPossible(context: ChannelHandlerContext) -> Bool { // This loop here is a while because we make outcalls here, and these outcalls // may cause re-entrant behaviour. We need to not be holding our structures mutably // over the duration of this outcall. @@ -209,7 +209,7 @@ public class NIOHTTP2FlowControlHandler: ChannelDuplexHandler { } let frame = HTTP2Frame(streamID: nextStreamID, flags: flags, payload: payload) - ctx.write(self.wrapOutboundOut(frame), promise: promise) + context.write(self.wrapOutboundOut(frame), promise: promise) didWrite = true } diff --git a/Sources/NIOHTTP2Server/main.swift b/Sources/NIOHTTP2Server/main.swift index 1f93095..6767d06 100644 --- a/Sources/NIOHTTP2Server/main.swift +++ b/Sources/NIOHTTP2Server/main.swift @@ -24,23 +24,23 @@ final class HTTP1TestServer: ChannelInboundHandler { public typealias InboundIn = HTTPServerRequestPart public typealias OutboundOut = HTTPServerResponsePart - public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { + public func channelRead(context: ChannelHandlerContext, data: NIOAny) { guard case .end = self.unwrapInboundIn(data) else { return } - ctx.channel.getOption(HTTP2StreamChannelOptions.streamID).flatMap { (streamID) -> EventLoopFuture in + context.channel.getOption(HTTP2StreamChannelOptions.streamID).flatMap { (streamID) -> EventLoopFuture in var headers = HTTPHeaders() headers.add(name: "content-length", value: "5") headers.add(name: "x-stream-id", value: String(Int(streamID))) - ctx.channel.write(self.wrapOutboundOut(HTTPServerResponsePart.head(HTTPResponseHead(version: .init(major: 2, minor: 0), status: .ok, headers: headers))), promise: nil) + context.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) + var buffer = context.channel.allocator.buffer(capacity: 12) buffer.writeStaticString("hello") - ctx.channel.write(self.wrapOutboundOut(HTTPServerResponsePart.body(.byteBuffer(buffer))), promise: nil) - return ctx.channel.writeAndFlush(self.wrapOutboundOut(HTTPServerResponsePart.end(nil))) + context.channel.write(self.wrapOutboundOut(HTTPServerResponsePart.body(.byteBuffer(buffer))), promise: nil) + return context.channel.writeAndFlush(self.wrapOutboundOut(HTTPServerResponsePart.end(nil))) }.whenComplete { _ in - ctx.close(promise: nil) + context.close(promise: nil) } } } @@ -49,9 +49,9 @@ final class HTTP1TestServer: ChannelInboundHandler { final class ErrorHandler: ChannelInboundHandler { typealias InboundIn = Never - func errorCaught(ctx: ChannelHandlerContext, error: Error) { + func errorCaught(context: ChannelHandlerContext, error: Error) { print("Server received error: \(error)") - ctx.close(promise: nil) + context.close(promise: nil) } } diff --git a/Tests/NIOHTTP2Tests/ConfiguringPipelineTests.swift b/Tests/NIOHTTP2Tests/ConfiguringPipelineTests.swift index 62a7948..3f0196d 100644 --- a/Tests/NIOHTTP2Tests/ConfiguringPipelineTests.swift +++ b/Tests/NIOHTTP2Tests/ConfiguringPipelineTests.swift @@ -25,9 +25,9 @@ class FailOnWriteHandler: ChannelOutboundHandler { typealias OutboundIn = Never typealias OutboundOut = Never - func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { + func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { XCTFail("Write received") - ctx.write(data, promise: promise) + context.write(data, promise: promise) } } diff --git a/Tests/NIOHTTP2Tests/FlowControlHandlerTests.swift b/Tests/NIOHTTP2Tests/FlowControlHandlerTests.swift index 5969518..a87ed47 100644 --- a/Tests/NIOHTTP2Tests/FlowControlHandlerTests.swift +++ b/Tests/NIOHTTP2Tests/FlowControlHandlerTests.swift @@ -30,13 +30,13 @@ class DataFrameCatcher: ChannelOutboundHandler { var writtenFrames: [HTTP2Frame] = [] - func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { + func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { let frame = self.unwrapOutboundIn(data) self.writtenFrames.append(frame) switch frame.payload { case .data(let data): - ctx.write(self.wrapOutboundOut(data), promise: promise) + context.write(self.wrapOutboundOut(data), promise: promise) default: promise?.succeed(()) } diff --git a/Tests/NIOHTTP2Tests/HTTP2ConcurrentStreamsHandlerTest.swift b/Tests/NIOHTTP2Tests/HTTP2ConcurrentStreamsHandlerTest.swift index 274b1eb..2c62aaa 100644 --- a/Tests/NIOHTTP2Tests/HTTP2ConcurrentStreamsHandlerTest.swift +++ b/Tests/NIOHTTP2Tests/HTTP2ConcurrentStreamsHandlerTest.swift @@ -33,22 +33,22 @@ final class OutboundFrameRecorder: ChannelOutboundHandler { private var openStreams: Set = [] - func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { + func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { let frame = self.unwrapOutboundIn(data) self._pendingWrites.append((frame, promise)) if !self.openStreams.contains(frame.streamID) { self.openStreams.insert(frame.streamID) - ctx.fireUserInboundEventTriggered(NIOHTTP2StreamCreatedEvent(streamID: frame.streamID, localInitialWindowSize: 65535, remoteInitialWindowSize: 65535)) + context.fireUserInboundEventTriggered(NIOHTTP2StreamCreatedEvent(streamID: frame.streamID, localInitialWindowSize: 65535, remoteInitialWindowSize: 65535)) } if frame.flags.contains(.endStream) { self.openStreams.remove(frame.streamID) - ctx.fireUserInboundEventTriggered(StreamClosedEvent(streamID: frame.streamID, reason: nil)) + context.fireUserInboundEventTriggered(StreamClosedEvent(streamID: frame.streamID, reason: nil)) } } - func flush(ctx: ChannelHandlerContext) { + func flush(context: ChannelHandlerContext) { let flushedWrites = self._pendingWrites self._pendingWrites = [] @@ -71,12 +71,12 @@ final class CloseOnWriteForStreamHandler: ChannelOutboundHandler { self.reason = reason } - func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { + func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { let frame = self.unwrapOutboundIn(data) if frame.streamID == self.streamToFail { - ctx.fireUserInboundEventTriggered(StreamClosedEvent(streamID: self.streamToFail, reason: self.reason)) + context.fireUserInboundEventTriggered(StreamClosedEvent(streamID: self.streamToFail, reason: self.reason)) } - ctx.write(data, promise: promise) + context.write(data, promise: promise) } } diff --git a/Tests/NIOHTTP2Tests/HTTP2StreamMultiplexerTests.swift b/Tests/NIOHTTP2Tests/HTTP2StreamMultiplexerTests.swift index 5a34f88..692e5eb 100644 --- a/Tests/NIOHTTP2Tests/HTTP2StreamMultiplexerTests.swift +++ b/Tests/NIOHTTP2Tests/HTTP2StreamMultiplexerTests.swift @@ -43,13 +43,13 @@ final class FrameExpecter: ChannelInboundHandler { self.expectedFrames = expectedFrames } - func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { + func channelRead(context: ChannelHandlerContext, data: NIOAny) { XCTAssertFalse(self.inactive) let frame = self.unwrapInboundIn(data) self.actualFrames.append(frame) } - func channelInactive(ctx: ChannelHandlerContext) { + func channelInactive(context: ChannelHandlerContext) { XCTAssertFalse(self.inactive) self.inactive = true @@ -72,15 +72,15 @@ final class FrameWriteRecorder: ChannelOutboundHandler { var flushedWrites: [HTTP2Frame] = [] private var unflushedWrites: [HTTP2Frame] = [] - func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { + func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { self.unflushedWrites.append(self.unwrapOutboundIn(data)) - ctx.write(data, promise: promise) + context.write(data, promise: promise) } - func flush(ctx: ChannelHandlerContext) { + func flush(context: ChannelHandlerContext) { self.flushedWrites.append(contentsOf: self.unflushedWrites) self.unflushedWrites = [] - ctx.flush() + context.flush() } } @@ -91,7 +91,7 @@ final class InboundFrameRecorder: ChannelInboundHandler { var receivedFrames: [HTTP2Frame] = [] - func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { + func channelRead(context: ChannelHandlerContext, data: NIOAny) { self.receivedFrames.append(self.unwrapInboundIn(data)) } } @@ -104,7 +104,7 @@ final class ReadCounter: ChannelOutboundHandler { var readCount = 0 - func read(ctx: ChannelHandlerContext) { + func read(context: ChannelHandlerContext) { readCount += 1 } } @@ -121,7 +121,7 @@ final class HandlerRemovedHandler: ChannelInboundHandler { self.removedPromise = removedPromise } - func handlerRemoved(ctx: ChannelHandlerContext) { + func handlerRemoved(context: ChannelHandlerContext) { self.removedPromise.succeed(()) } } @@ -137,13 +137,13 @@ final class ActiveHandler: ChannelInboundHandler { self.activatedPromise = activatedPromise } - func handlerAdded(ctx: ChannelHandlerContext) { - if ctx.channel.isActive { + func handlerAdded(context: ChannelHandlerContext) { + if context.channel.isActive { self.activatedPromise.succeed(()) } } - func channelActive(ctx: ChannelHandlerContext) { + func channelActive(context: ChannelHandlerContext) { self.activatedPromise.succeed(()) } } diff --git a/Tests/NIOHTTP2Tests/HTTP2ToHTTP1CodecTests.swift b/Tests/NIOHTTP2Tests/HTTP2ToHTTP1CodecTests.swift index a97e797..a50de0d 100644 --- a/Tests/NIOHTTP2Tests/HTTP2ToHTTP1CodecTests.swift +++ b/Tests/NIOHTTP2Tests/HTTP2ToHTTP1CodecTests.swift @@ -70,9 +70,9 @@ final class PromiseRecorder: ChannelOutboundHandler { var recordedPromises: [EventLoopPromise?] = [] - func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { + func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { recordedPromises.append(promise) - ctx.write(data, promise: promise) + context.write(data, promise: promise) } } diff --git a/Tests/NIOHTTP2Tests/ReentrancyTests.swift b/Tests/NIOHTTP2Tests/ReentrancyTests.swift index 5abaa94..13e4db1 100644 --- a/Tests/NIOHTTP2Tests/ReentrancyTests.swift +++ b/Tests/NIOHTTP2Tests/ReentrancyTests.swift @@ -31,14 +31,14 @@ final class ReenterOnReadHandler: ChannelInboundHandler { self.reEnterCallback = reEnterCallback } - func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { + func channelRead(context: ChannelHandlerContext, data: NIOAny) { guard self.shouldReenter else { - ctx.fireChannelRead(data) + context.fireChannelRead(data) return } self.shouldReenter = false - ctx.fireChannelRead(data) - self.reEnterCallback(ctx.pipeline) + context.fireChannelRead(data) + self.reEnterCallback(context.pipeline) } } diff --git a/Tests/NIOHTTP2Tests/SimpleClientServerTests.swift b/Tests/NIOHTTP2Tests/SimpleClientServerTests.swift index 32e2ebc..d751c6d 100644 --- a/Tests/NIOHTTP2Tests/SimpleClientServerTests.swift +++ b/Tests/NIOHTTP2Tests/SimpleClientServerTests.swift @@ -35,12 +35,12 @@ final class EOFOnWriteHandler: ChannelOutboundHandler { self.type = type } - func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { + func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { switch self.type { case .halfClose: - ctx.fireUserInboundEventTriggered(ChannelEvent.inputClosed) + context.fireUserInboundEventTriggered(ChannelEvent.inputClosed) case .fullClose: - ctx.fireChannelInactive() + context.fireChannelInactive() case .doNothing: break } @@ -63,12 +63,12 @@ final class WriteOnWriteHandler: ChannelOutboundHandler { self.writePromise = promise } - func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { + func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { if !self.written { self.written = true - ctx.channel.write(self.frame, promise: self.writePromise) + context.channel.write(self.frame, promise: self.writePromise) } - ctx.write(data, promise: promise) + context.write(data, promise: promise) } } @@ -80,12 +80,12 @@ final class NoEmptyFlushesHandler: ChannelOutboundHandler { var writeCount = 0 - func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { + func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { self.writeCount += 1 - ctx.write(data, promise: promise) + context.write(data, promise: promise) } - func flush(ctx: ChannelHandlerContext) { + func flush(context: ChannelHandlerContext) { XCTAssertGreaterThan(self.writeCount, 0) self.writeCount = 0 } @@ -96,11 +96,11 @@ final class InstaResetHandler: ChannelInboundHandler { typealias InboundIn = HTTP2Frame typealias OutboundOut = HTTP2Frame - func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { + func channelRead(context: ChannelHandlerContext, data: NIOAny) { let frame = self.unwrapInboundIn(data) if case .headers = frame.payload { let kaboomFrame = HTTP2Frame(streamID: frame.streamID, payload: .rstStream(.refusedStream)) - ctx.writeAndFlush(self.wrapOutboundOut(kaboomFrame), promise: nil) + context.writeAndFlush(self.wrapOutboundOut(kaboomFrame), promise: nil) } } } @@ -110,11 +110,11 @@ final class InstaGoawayHandler: ChannelInboundHandler { typealias InboundIn = HTTP2Frame typealias OutboundOut = HTTP2Frame - func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { + func channelRead(context: ChannelHandlerContext, data: NIOAny) { let frame = self.unwrapInboundIn(data) if case .headers = frame.payload { let kaboomFrame = HTTP2Frame(streamID: .rootStream, payload: .goAway(lastStreamID: .rootStream, errorCode: .inadequateSecurity, opaqueData: nil)) - ctx.writeAndFlush(self.wrapOutboundOut(kaboomFrame), promise: nil) + context.writeAndFlush(self.wrapOutboundOut(kaboomFrame), promise: nil) } } } @@ -125,7 +125,7 @@ final class UserEventRecorder: ChannelInboundHandler { var events: [Any] = [] - func userInboundEventTriggered(ctx: ChannelHandlerContext, event: Any) { + func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) { events.append(event) } } @@ -143,7 +143,7 @@ final class ClosedEventVsFrameOrderingHandler: ChannelInboundHandler { self.targetStreamID = targetStreamID } - func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { + func channelRead(context: ChannelHandlerContext, data: NIOAny) { let frame = self.unwrapInboundIn(data) switch (frame.payload, frame.streamID) { case (.rstStream, self.targetStreamID), @@ -154,10 +154,10 @@ final class ClosedEventVsFrameOrderingHandler: ChannelInboundHandler { default: break } - ctx.fireChannelRead(data) + context.fireChannelRead(data) } - func userInboundEventTriggered(ctx: ChannelHandlerContext, event: Any) { + func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) { guard let evt = event as? StreamClosedEvent, evt.streamID == self.targetStreamID else { return } @@ -173,11 +173,11 @@ final class ClosedEventVsFrameOrderingHandler: ChannelInboundHandler { class HTTP2ParserProxyHandler: ChannelInboundHandler, RemovableChannelHandler { typealias InboundIn = ByteBuffer - func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { - XCTAssertNoThrow(try ctx.pipeline.addHandler( + func channelRead(context: ChannelHandlerContext, data: NIOAny) { + XCTAssertNoThrow(try context.pipeline.addHandler( NIOHTTP2Handler(mode: .server)).wait()) - ctx.fireChannelRead(data) - _ = ctx.pipeline.removeHandler(ctx: ctx) + context.fireChannelRead(data) + _ = context.pipeline.removeHandler(context: context) } } @@ -187,7 +187,7 @@ class FrameRecorderHandler: ChannelInboundHandler { var receivedFrames: [HTTP2Frame] = [] - func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { + func channelRead(context: ChannelHandlerContext, data: NIOAny) { self.receivedFrames.append(self.unwrapInboundIn(data)) } } @@ -1008,11 +1008,11 @@ class SimpleClientServerTests: XCTestCase { self.errorSeenPromise = errorSeenPromise } - func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { + func channelRead(context: ChannelHandlerContext, data: NIOAny) { XCTFail("shouldnt' have received \(data)") } - func errorCaught(ctx: ChannelHandlerContext, error: Error) { + func errorCaught(context: ChannelHandlerContext, error: Error) { if let errorSeenPromise = self.errorSeenPromise { errorSeenPromise.succeed(error) } else {