Files
swift-nio-http2/Sources/NIOHTTP2/HTTP2Frame.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

164 lines
4.6 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 NIOHTTP1
import CNIONghttp2
/// A representation of a single HTTP/2 frame.
public struct HTTP2Frame {
/// The payload of this HTTP/2 frame.
public var payload: FramePayload
/// The frame flags as an 8-bit integer. To set/unset well-defined flags, consider using the
/// other properties on this object (e.g. `endStream`).
public var flags: UInt8
/// The frame stream ID as a 32-bit integer.
public var streamID: Int32
// Whether the END_STREAM flag bit is set.
public var endStream: Bool {
get {
switch self.payload {
case .data, .headers:
return (self.flags & UInt8(NGHTTP2_FLAG_END_STREAM.rawValue) != 0)
default:
return false
}
}
set {
switch self.payload {
case .data, .headers:
self.flags |= UInt8(NGHTTP2_FLAG_END_STREAM.rawValue)
default:
break
}
}
}
// Whether the END_HEADERS flag bit is set.
public var endHeaders: Bool {
get {
switch self.payload {
case .headers, .pushPromise, .continuation:
return (self.flags & UInt8(NGHTTP2_FLAG_END_HEADERS.rawValue) != 0)
default:
return false
}
}
set {
switch self.payload {
case .headers, .pushPromise, .continuation:
self.flags |= UInt8(NGHTTP2_FLAG_END_HEADERS.rawValue)
default:
break
}
}
}
// Whether the PADDED flag bit is set.
public var padded: Bool {
get {
switch self.payload {
case .data, .headers, .pushPromise:
return (self.flags & UInt8(NGHTTP2_FLAG_PADDED.rawValue) != 0)
default:
return false
}
}
set {
switch self.payload {
case .data, .headers, .pushPromise:
self.flags |= UInt8(NGHTTP2_FLAG_PADDED.rawValue)
default:
break
}
}
}
// Whether the PRIORITY flag bit is set.
public var priority: Bool {
get {
if case .headers = self.payload {
return (self.flags & UInt8(NGHTTP2_FLAG_PRIORITY.rawValue) != 0)
} else {
return false
}
}
set {
if case .headers = self.payload {
self.flags |= UInt8(NGHTTP2_FLAG_PRIORITY.rawValue)
}
}
}
// Whether the ACK flag bit is set.
public var ack: Bool {
get {
switch self.payload {
case .settings, .ping:
return (self.flags & UInt8(NGHTTP2_FLAG_ACK.rawValue) != 0)
default:
return false
}
}
set {
switch self.payload {
case .settings, .ping:
self.flags |= UInt8(NGHTTP2_FLAG_ACK.rawValue)
default:
break
}
}
}
public enum FramePayload {
case data(IOData)
case headers(HTTP2HeadersCategory)
case priority
case rstStream
case settings([(Int32, UInt32)])
case pushPromise
case ping
case goAway
case windowUpdate(windowSizeIncrement: Int)
case continuation
case alternativeService
}
}
internal extension HTTP2Frame {
internal init(nghttp2FrameHeader header: nghttp2_frame_hd, payload: HTTP2Frame.FramePayload) {
self.streamID = header.stream_id
self.flags = header.flags
self.payload = payload
}
}
public extension HTTP2Frame {
/// Constructs a frame for a given stream ID. All flags are unset.
public init(streamID: Int, payload: HTTP2Frame.FramePayload) {
self.init(streamID: Int32(streamID), payload: payload)
}
/// Constructs a frame header for a given stream ID. All flags are unset.
public init(streamID: Int32, payload: HTTP2Frame.FramePayload) {
self.streamID = streamID
self.flags = 0
self.payload = payload
}
}