Files
swift-nio-http2/Sources/NIOHTTP2/HTTP2ChannelHandler.swift
T
Cory Benfield 3c90345c36 Pure-Swift HTTP/2 implementation (#40)
* Updated to use SwiftNIO 1.10+ to gain access to HTTPHeaders internals.

* Add pure-Swift HTTP/2 protocol implementation.

Motivation:

Currently we use nghttp2 for our basic protocol implementation. This is
plenty workable, but provides us a number of limitations. It adds
complexity to our build system, provides us with external dependencies we
have little control over, limits our users' ability to express low-level
protocol semantics if they so wish, causes us to heap-allocate like crazy over
the lifetime of a connection, and brings a world of pain around reentrancy.

This set of limitations has motivated a desire to move away from using
nghttp2. This is a part of that dream.

Modifications:

- Add pure Swift protocol implementations.
- Test them.

Results:

Another step towards protocol independence.

* Initial basic parser code.

* Adding unit tests.

* Add NIOHTTP2FlowControlHandler

Motivation:

One of the things that nghttp2 used to do for us was to handle buffering
DATA frames until flow control allowed them to go out. For users that
want that behaviour, we need to replace the nghttp2 code with a new
ChannelHandler that can do this work.

Modifications:

- Added NIOHTTP2FlowControlHandler.

Result:

Users will have the same feature set as they did with nghttp2.

* Generate Linux test information.

* Documentation comment.

* Updated new tests from master to use new payload types.

* Updated following review.

Notable changes:

- Implemented unit tests for the ALTSVC (0xa) and ORIGIN (0xc) frame types.
- Additional DATA frames are synthesized to wrap individual payload chunks as they arrive. If the payload of a DATA frame with 4MB data arrives in 4 separate 1MB chunks, the decoder will emit 4 DATA frames, with only the last containing the END_STREAM flag (if that was set on the incoming frame).
- HEADERS / PUSH_PROMISE frames and any following CONTINUATION frames are now handled properly: header fragments from all frames are gathered up into a single buffer. Once a frame with END_HEADERS is encountered, the resultant accumulated buffer is sent for decoding along with the flags and stream ID of the original HEADERS / PUSH_PROMISE frame.
- HPACK encoders/decoders now live in the frame encoder/decoder types, rather than being attached to a stream.

* Updated Linux tests.

* Headers placed in relevant files.

* Add pure-Swift HTTP/2 protocol implementation.

Motivation:

Currently we use nghttp2 for our basic protocol implementation. This is
plenty workable, but provides us a number of limitations. It adds
complexity to our build system, provides us with external dependencies we
have little control over, limits our users' ability to express low-level
protocol semantics if they so wish, causes us to heap-allocate like crazy over
the lifetime of a connection, and brings a world of pain around reentrancy.

This set of limitations has motivated a desire to move away from using
nghttp2. This is a part of that dream.

Modifications:

- Add pure Swift protocol implementations.
- Test them.

Results:

Another step towards protocol independence.

* Revamped frame decoder state machine and API. Now uses separate input and output methods to manage multiple frames in a single buffer.

* Updated Linux tests.

* Bumped swift-nio version to 1.12.0 and updated `HTTPHeaders` -> `HPACKHeaders` conversion initializer to directly copy the buffer and map the header name/value indices.

* Set up HTTP2Parser in handlerAdded if the channel had been previously activated. (#36)

By setting up the handler in the handlerAdded hook, HTTP2Parser supports
being added dynamically in a channelRead event, after the channel has sent
the channelActive event.

(cherry picked from commit 88bd6d369a)

* Minor tweaks to get the tests passing

* Initial integration here

* Minor clearing up of the handler

* Get the tests compiling

* Fix header issues.

We now pass the frame parser to the various setting operations to let it
update the state. We also fixed some issues with specific settings.

* Remove ngthttp2-based impl, clean up stream ID

* Tests with new HTTP2StreamID

* Simple test cleanup

* Get remaining non-core tests passing

* Allow GOAWAY even when fully quiesced

* We don't buffer outbound frames any more

* Emit StreamClosed events on GoAway frame

* Notify stream creation and closure

* This test started failing

* Goodbye, C code! 🎉

* Send PING ACK automatically

* Updated based on PR feedback.

* Correctly handle 1XX responses

* Add SETTINGS_MAX_FRAME_SIZE support to flow controller

* Correctly account for flow control

* Define ConcurrentStreamsHandler

* Prepare for signalling connection state changes

* Better state change message propagation

* Refactor to use result with effect everywhere

* Don't lose flow control info on stream error

* Add basic flow control management to stream multiplexer

* Better pipeline in sample server

* Clean up some reentrancy concerns

* Move to Swift 5 and NIO 2

* Move to new Swift 5 for CI

* Allow wider range of choices of years

* Catch up to NIO 2 again
2019-02-21 11:52:49 +00:00

424 lines
20 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIO
// Things that don't work yet:
//
// - We don't account for padding in flow control.
// - We don't process settings changes that affect HPACK or max frame size.
/// NIO's default settings used for initial settings values on HTTP/2 streams, when the user hasn't
/// overridden that. This limits the max concurrent streams to 100, and limits the max header list
/// size to 16kB, to avoid trivial resource exhaustion on NIO HTTP/2 users.
public let nioDefaultSettings = [
HTTP2Setting(parameter: .maxConcurrentStreams, value: 100),
HTTP2Setting(parameter: .maxHeaderListSize, value: 1<<16)
]
public final class NIOHTTP2Handler: ChannelDuplexHandler {
public typealias InboundIn = ByteBuffer
public typealias InboundOut = HTTP2Frame
public typealias OutboundIn = HTTP2Frame
public typealias OutboundOut = IOData
/// The magic string sent by clients at the start of a HTTP/2 connection.
private static let clientMagic: StaticString = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
/// The connection state machine. We always have one of these.
private var stateMachine: HTTP2ConnectionStateMachine
/// The frame decoder. Right now this is optional because it needs an allocator, which
/// we don't have until the channel is up. The rules of handler lifecycles mean that
/// this can never fail to unwrap in a proper program.
private var frameDecoder: HTTP2FrameDecoder!
/// The frame encoder. Right now this is optional because it needs an allocator, which
/// we don't have until the channel is up. The rules of handler lifecycles mean that
/// this can never fail to unwrap in a proper program.
private var frameEncoder: HTTP2FrameEncoder!
/// The buffer we write data into. This is optional because we need an allocator, which
/// we don't have until the channel is up. The rules of handler lifecycles mean that
/// this can never fail to unwrap in a proper program.
private var writeBuffer: ByteBuffer!
/// A buffer where we write inbound events before we deliver them. This avoids us reordering
/// user events and frames when re-entrant operations occur.
private var inboundEventBuffer: InboundEventBuffer = InboundEventBuffer()
/// This flag is set to false each time we get a channelReadComplete or flush, and set to true
/// each time we write a frame automatically from this handler. If set to true in channelReadComplete,
/// we will choose to flush automatically ourselves.
private var wroteAutomaticFrame: Bool = false
/// The mode this handler is operating in.
private let mode: ParserMode
/// The initial local settings of this connection. Sent as part of the preamble.
private let initialSettings: HTTP2Settings
// TODO(cory): We should revisit this: ideally we won't drop frames but would still deliver them where
// possible, but I'm not doing that right now.
/// Whether the channel has closed. If it has, we abort the decode loop, as we don't delay channelInactive.
private var channelClosed: Bool = false
/// The mode for this parser to operate in: client or server.
public enum ParserMode {
/// Client mode
case client
/// Server mode
case server
}
public init(mode: ParserMode, initialSettings: HTTP2Settings = nioDefaultSettings) {
self.stateMachine = HTTP2ConnectionStateMachine(role: .init(mode))
self.mode = mode
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)
if ctx.channel.isActive {
self.writeAndFlushPreamble(ctx: ctx)
}
}
public func channelActive(ctx: ChannelHandlerContext) {
self.writeAndFlushPreamble(ctx: ctx)
ctx.fireChannelActive()
}
public func channelInactive(ctx: ChannelHandlerContext) {
self.channelClosed = true
ctx.fireChannelInactive()
}
public func channelRead(ctx: 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)
// We parse eagerly to attempt to give back buffers to the reading channel wherever possible.
self.frameDecodeLoop(ctx: ctx)
}
public func channelReadComplete(ctx: ChannelHandlerContext) {
if self.wroteAutomaticFrame {
self.wroteAutomaticFrame = false
ctx.flush()
}
ctx.fireChannelReadComplete()
}
public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
let frame = self.unwrapOutboundIn(data)
self.processOutboundFrame(ctx: ctx, frame: frame, promise: promise)
}
public func flush(ctx: ChannelHandlerContext) {
self.wroteAutomaticFrame = false
ctx.flush()
}
}
/// 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 {
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)? {
do {
return try self.frameDecoder.nextFrame()
} catch InternalError.codecError(let code) {
self.inboundConnectionErrorTriggered(ctx: ctx, underlyingError: NIOHTTP2Errors.UnableToParseFrame(), reason: code)
return nil
} catch is NIOHTTP2Errors.BadClientMagic {
self.inboundConnectionErrorTriggered(ctx: ctx, underlyingError: NIOHTTP2Errors.BadClientMagic(), reason: .protocolError)
return nil
} catch {
self.inboundConnectionErrorTriggered(ctx: ctx, underlyingError: error, reason: .internalError)
return nil
}
}
enum FrameProcessResult {
case `continue`
case stop
}
private func processFrame(_ frame: HTTP2Frame, flowControlledLength: Int, ctx: 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.
let result: StateMachineResultWithEffect
switch frame.payload {
case .alternativeService, .origin:
// TODO(cory): Implement
fatalError("Currently some frames are unhandled.")
case .data:
result = self.stateMachine.receiveData(streamID: frame.streamID, flowControlledBytes: flowControlledLength, isEndStreamSet: frame.flags.contains(.endStream))
case .goAway(let lastStreamID, _, _):
result = self.stateMachine.receiveGoaway(lastStreamID: lastStreamID)
case .headers(let headers, _):
result = self.stateMachine.receiveHeaders(streamID: frame.streamID, headers: headers, isEndStreamSet: frame.flags.contains(.endStream))
case .ping:
let (stateMachineResult, postPingOperation) = self.stateMachine.receivePing(flags: frame.flags)
result = stateMachineResult
switch postPingOperation {
case .nothing:
break
case .sendAck:
self.writeBuffer.clear()
var responseFrame = frame
responseFrame.flags.insert(.ack)
self.encodeAndWriteFrame(ctx: ctx, frame: responseFrame, promise: nil)
self.wroteAutomaticFrame = true
}
case .priority:
result = self.stateMachine.receivePriority()
case .pushPromise(let pushedStreamID, let headers):
result = self.stateMachine.receivePushPromise(originalStreamID: frame.streamID, childStreamID: pushedStreamID, headers: headers)
case .rstStream(let reason):
result = self.stateMachine.receiveRstStream(streamID: frame.streamID, reason: reason)
case .settings(let newSettings):
let (stateMachineResult, postSettingsOperation) = self.stateMachine.receiveSettings(newSettings,
flags: frame.flags,
frameEncoder: &self.frameEncoder,
frameDecoder: &self.frameDecoder)
result = stateMachineResult
switch postSettingsOperation {
case .nothing:
break
case .sendAck:
self.writeBuffer.clear()
self.encodeAndWriteFrame(ctx: ctx, frame: HTTP2Frame(streamID: .rootStream, flags: .ack, payload: .settings([])), promise: nil)
self.wroteAutomaticFrame = true
}
case .windowUpdate(let increment):
result = self.stateMachine.receiveWindowUpdate(streamID: frame.streamID, windowIncrement: UInt32(increment))
}
self.processStateChange(result.effect)
let returnValue: FrameProcessResult
switch result.result {
case .succeed:
// Frame is good, we can pass it on.
ctx.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)
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)
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)
return returnValue
}
/// A connection error was hit while receiving a frame.
private func inboundConnectionErrorTriggered(ctx: 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)
}
/// A stream error was hit while receiving a frame.
private func inboundStreamErrorTriggered(ctx: 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)
}
/// Emit any pending user events.
private func processPendingUserEvents(ctx: ChannelHandlerContext) {
for event in self.inboundEventBuffer {
ctx.fireUserInboundEventTriggered(event)
}
}
}
/// Outbound frame handling.
extension NIOHTTP2Handler {
/// Issues the preamble when necessary.
private func writeAndFlushPreamble(ctx: ChannelHandlerContext) {
if case .client = self.mode {
self.writeBuffer.clear()
self.writeBuffer.writeStaticString(NIOHTTP2Handler.clientMagic)
ctx.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()
}
private func processOutboundFrame(ctx: ChannelHandlerContext, frame: HTTP2Frame, promise: EventLoopPromise<Void>?) {
let result: StateMachineResultWithEffect
switch frame.payload {
case .alternativeService, .origin:
// TODO(cory): Implement
fatalError("Currently some frames are unhandled.")
case .data(let data):
// TODO(cory): Correctly account for padding data.
result = self.stateMachine.sendData(streamID: frame.streamID, flowControlledBytes: data.readableBytes, isEndStreamSet: frame.flags.contains(.endStream))
case .goAway(let lastStreamID, _, _):
result = self.stateMachine.sendGoaway(lastStreamID: lastStreamID)
case .headers(let headers, _):
result = self.stateMachine.sendHeaders(streamID: frame.streamID, headers: headers, isEndStreamSet: frame.flags.contains(.endStream))
case .ping:
result = self.stateMachine.sendPing()
case .priority:
result = self.stateMachine.sendPriority()
case .pushPromise(let pushedStreamID, let headers):
result = self.stateMachine.sendPushPromise(originalStreamID: frame.streamID, childStreamID: pushedStreamID, headers: headers)
case .rstStream(let reason):
result = self.stateMachine.sendRstStream(streamID: frame.streamID, reason: reason)
case .settings(let newSettings):
result = self.stateMachine.sendSettings(newSettings)
case .windowUpdate(let increment):
result = self.stateMachine.sendWindowUpdate(streamID: frame.streamID, windowIncrement: UInt32(increment))
}
self.processStateChange(result.effect)
switch result.result {
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)
return
case .succeed:
self.writeBuffer.clear()
self.encodeAndWriteFrame(ctx: ctx, frame: frame, promise: promise)
}
// This may have caused user events that need to be fired, so do so.
self.processPendingUserEvents(ctx: ctx)
}
/// Encodes a frame and writes it to the network.
private func encodeAndWriteFrame(ctx: ChannelHandlerContext, frame: HTTP2Frame, promise: EventLoopPromise<Void>?) {
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())
return
} catch {
self.outboundErrorTriggered(ctx: ctx, 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)
} else {
ctx.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<Void>?, underlyingError: Error) {
promise?.fail(underlyingError)
ctx.fireErrorCaught(underlyingError)
}
}
// MARK:- Helpers
extension NIOHTTP2Handler {
private func processStateChange(_ stateChange: NIOHTTP2ConnectionStateChange?) {
guard let stateChange = stateChange else {
return
}
switch stateChange {
case .streamClosed(let streamClosedData):
self.inboundEventBuffer.pendingUserEvent(StreamClosedEvent(streamID: streamClosedData.streamID, reason: streamClosedData.reason))
case .streamCreated(let streamCreatedData):
self.inboundEventBuffer.pendingUserEvent(NIOHTTP2StreamCreatedEvent(streamID: streamCreatedData.streamID,
localInitialWindowSize: streamCreatedData.localStreamWindowSize.map(UInt32.init),
remoteInitialWindowSize: streamCreatedData.remoteStreamWindowSize.map(UInt32.init)))
case .bulkStreamClosure(let streamClosureData):
for droppedStream in streamClosureData.closedStreams {
self.inboundEventBuffer.pendingUserEvent(StreamClosedEvent(streamID: droppedStream, reason: .cancel))
}
case .flowControlChange(let change):
self.inboundEventBuffer.pendingUserEvent(NIOHTTP2WindowUpdatedEvent(streamID: .rootStream, inboundWindowSize: change.remoteConnectionWindowSize, outboundWindowSize: change.localConnectionWindowSize))
if let streamSize = change.localStreamWindowSize {
self.inboundEventBuffer.pendingUserEvent(NIOHTTP2WindowUpdatedEvent(streamID: streamSize.streamID, inboundWindowSize: streamSize.remoteStreamWindowSize, outboundWindowSize: streamSize.localStreamWindowSize))
}
case .settingsChanged, .streamCreatedAndClosed:
break
}
}
}
private extension HTTP2ConnectionStateMachine.ConnectionRole {
init(_ role: NIOHTTP2Handler.ParserMode) {
switch role {
case .client:
self = .client
case .server:
self = .server
}
}
}