Files
swift-nio-http2/Sources/NIOHTTP2/HTTP2ToHTTP1Codec.swift
T
Cory Benfield 2044c89450 Improve HTTP2StreamChannel state handling.
Motivation:

The HTTP2StreamChannel needs to be aware of the HTTP/2 state machine,
at least at some level. This is to ensure it can map that state
machine onto the (unfortunately implicit) NIO stream state machine
and appropriately clean up resources like stream promises.

Modifications:

Lots. Added stream state tracking, refactored the frame structure,
stopped creating stream 0 as a sub-stream, and more.

Result:

We no longer crash when the curl TCP connection goes away.
2018-04-23 17:15:17 +01:00

83 lines
2.7 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
import CNIONghttp2
import NIOHTTP1
private extension HTTPMethod {
init?(http2Method: String) {
switch http2Method {
case "GET":
self = .GET
default:
return nil
}
}
}
public final class HTTP2ToHTTP1Codec: ChannelInboundHandler, ChannelOutboundHandler {
public typealias InboundIn = HTTP2Frame
public typealias InboundOut = HTTPServerRequestPart
public typealias OutboundIn = HTTPServerResponsePart
public typealias OutboundOut = HTTP2Frame
private let streamID: Int32
public init(streamID: Int) {
self.streamID = Int32(streamID)
}
public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
let frame = self.unwrapInboundIn(data)
switch frame.payload {
case .headers(.request(let reqHead)):
let req = InboundOut.head(reqHead)
ctx.fireChannelRead(self.wrapInboundOut(req))
case .settings(_), .windowUpdate(windowSizeIncrement: _):
// FIXME: ignored
()
case .goAway:
ctx.close(promise: nil)
case .rstStream:
print(self.streamID)
// FIXME: ignored
()
default:
fatalError("unimplemented frame \(frame)")
}
}
public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
let responsePart = self.unwrapOutboundIn(data)
switch responsePart {
case .head(let head):
var frame = HTTP2Frame(streamID: self.streamID, payload: .headers(.response(head)))
frame.endHeaders = true
ctx.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)
case .end(_):
let payload = HTTP2Frame.FramePayload.data(.byteBuffer(ctx.channel.allocator.buffer(capacity: 0)))
var frame = HTTP2Frame(streamID: self.streamID, payload: payload)
frame.endStream = true
ctx.write(self.wrapOutboundOut(frame), promise: promise)
}
}
}