Files
swift-nio-http2/Sources/NIOHTTP2/HTTP2Settings.swift
T
Jim Dovey f2c2534d4c More Swift-y types for HTTP/2 frame, flags, and settings. (#16)
Motivation:

Prior to bringing in my HTTP/2 encoder/decoder, I think my implementations of HTTP/2 frame flags as `OptionSet`s and settings as an enum (i.e. existentials) make for a nicer API, and provide somewhat more information for the compiler to work with when optimizing.

Modifications:

- `HTTP2Frame.FrameFlags` is now an `OptionSet`.
- `HTTP2Frame.FramePayload` now supports all current HTTP/2 frame types, can return their on-wire identifier, and can return the set of `HTTP2Frame.FrameFlags` that the frame allows.
- `HTTP2Setting` is now an enum with associated values, and includes the SETTINGS_ENABLE_CONNECT_PROTOCOL value.
- Added `NIOHTTP2Errors.InvalidSettings` error, used when bad settings are encountered by decoder.
- Updated tests to use the new `HTTP2Setting` type.

Result:

Not a lot changes, beyond the in-memory layout of some things. All these types still have nghttp2 conversion methods, and all tests still run successfully.
2018-08-23 14:56:20 +01:00

116 lines
3.9 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 CNIONghttp2
/// A HTTP/2 settings parameter that allows representing both known and unknown HTTP/2
/// settings parameters.
public struct HTTP2SettingsParameter {
internal let networkRepresentation: UInt16
/// Create a `HTTP2SettingsParameter` that is not known to NIO.
///
/// If this is a known parameter, use one of the static values.
public init(extensionSetting: Int) {
self.networkRepresentation = UInt16(extensionSetting)
}
/// Initialize a `HTTP2SettingsParameter` from nghttp2's representation.
internal init(fromNetwork value: Int32) {
self.networkRepresentation = UInt16(value)
}
/// A helper to initialize the static parameters.
private init(_ value: UInt16) {
self.networkRepresentation = value
}
/// Corresponds to SETTINGS_HEADER_TABLE_SIZE
public static let headerTableSize = HTTP2SettingsParameter(1)
/// Corresponds to SETTINGS_ENABLE_PUSH.
public static let enablePush = HTTP2SettingsParameter(2)
/// Corresponds to SETTINGS_MAX_CONCURRENT_STREAMS
public static let maxConcurrentStreams = HTTP2SettingsParameter(3)
/// Corresponds to SETTINGS_INITIAL_WINDOW_SIZE
public static let initialWindowSize = HTTP2SettingsParameter(4)
/// Corresponds to SETTINGS_MAX_FRAME_SIZE
public static let maxFrameSize = HTTP2SettingsParameter(5)
/// Corresponds to SETTINGS_MAX_HEADER_LIST_SIZE
public static let maxHeaderListSize = HTTP2SettingsParameter(6)
/// Corresponds to SETTINGS_ENABLE_CONNECT_PROTOCOL from RFC 8441.
public static let enableConnectProtocol = HTTP2SettingsParameter(8)
}
extension HTTP2SettingsParameter: Equatable {
public static func ==(lhs: HTTP2SettingsParameter, rhs: HTTP2SettingsParameter) -> Bool {
return lhs.networkRepresentation == rhs.networkRepresentation
}
}
extension HTTP2SettingsParameter: Hashable {
public var hashValue: Int {
return self.networkRepresentation.hashValue
}
}
/// A single setting for HTTP/2, a combination of a `HTTP2SettingsParameter` and its value.
public struct HTTP2Setting {
/// The settings parameter for this setting.
public var parameter: HTTP2SettingsParameter
/// The value of the settings parameter. This must be a 32-bit number.
public var value: Int {
get {
return Int(self._value)
}
set {
self._value = UInt32(newValue)
}
}
/// The value of the setting. Swift doesn't like using explicitly-sized integers in general,
/// so we use this as an internal implementation detail and expose it via a computed Int
/// property.
internal var _value: UInt32
/// Create a new `HTTP2Setting`.
public init(parameter: HTTP2SettingsParameter, value: Int) {
self.parameter = parameter
self._value = UInt32(value)
}
/// Create a new `HTTP2Setting` from nghttp2's raw representation.
internal init(fromNghttp2 setting: nghttp2_settings_entry) {
self.parameter = HTTP2SettingsParameter(fromNetwork: setting.settings_id)
self._value = setting.value
}
}
extension HTTP2Setting: Equatable {
public static func ==(lhs: HTTP2Setting, rhs: HTTP2Setting) -> Bool {
return lhs.parameter == rhs.parameter && lhs._value == rhs._value
}
}
internal extension nghttp2_settings_entry {
internal init(nioSetting: HTTP2Setting) {
self.init(settings_id: Int32(nioSetting.parameter.networkRepresentation), value: nioSetting._value)
}
}