Files
swift-nio-http2/Sources/NIOHTTP2/HTTP2Error.swift
T
Cory Benfield d9f1131749 Be emphatic about remote peers making mistakes. (#65)
Motivation:

Receiving a CONTINUATION frame at the wrong stage of a connection is
a PROTOCOL_ERROR, but out of an abundance of caution we were categorising
it as an INTERNAL_ERROR. We should be more confident about our error
categories.

Modifications:

- Changed INTERNAL_ERROR to PROTOCOL_ERROR.
- Added a test for this error case.

Result:

More confidence, more style, more pizazz.
2019-03-15 14:37:17 +00:00

207 lines
6.2 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
//
//===----------------------------------------------------------------------===//
public protocol NIOHTTP2Error: Equatable, Error { }
/// Errors that NIO raises when handling HTTP/2 connections.
public enum NIOHTTP2Errors {
/// NIO's upgrade handler encountered a successful upgrade to a protocol that it
/// does not recognise.
public struct InvalidALPNToken: NIOHTTP2Error {
public init() { }
}
/// An attempt was made to issue a write on a stream that does not exist.
public struct NoSuchStream: NIOHTTP2Error {
/// The stream ID that was used that does not exist.
public var streamID: HTTP2StreamID
public init(streamID: HTTP2StreamID) {
self.streamID = streamID
}
}
/// A stream was closed.
public struct StreamClosed: NIOHTTP2Error {
/// The stream ID that was closed.
public var streamID: HTTP2StreamID
/// The error code associated with the closure.
public var errorCode: HTTP2ErrorCode
public init(streamID: HTTP2StreamID, errorCode: HTTP2ErrorCode) {
self.streamID = streamID
self.errorCode = errorCode
}
}
public struct BadClientMagic: NIOHTTP2Error {
public init() {}
}
/// A stream state transition was attempted that was not valid.
public struct BadStreamStateTransition: NIOHTTP2Error {
public init() { }
}
/// An attempt was made to change the flow control window size, either via
/// SETTINGS or WINDOW_UPDATE, but this change would move the flow control
/// window size out of bounds.
public struct InvalidFlowControlWindowSize: NIOHTTP2Error {
/// The delta being applied to the flow control window.
public var delta: Int
/// The size of the flow control window before the delta was applied.
public var currentWindowSize: Int
public init(delta: Int, currentWindowSize: Int) {
self.delta = delta
self.currentWindowSize = currentWindowSize
}
}
/// A frame was sent or received that violates HTTP/2 flow control rules.
public struct FlowControlViolation: NIOHTTP2Error {
public init() { }
}
/// A SETTINGS frame was sent or received with an invalid setting.
public struct InvalidSetting: NIOHTTP2Error {
/// The invalid setting.
public var setting: HTTP2Setting
public init(setting: HTTP2Setting) {
self.setting = setting
}
}
/// An attempt to perform I/O was made on a connection that is already closed.
public struct IOOnClosedConnection: NIOHTTP2Error {
public init() { }
}
/// A SETTINGS frame was received that is invalid.
public struct ReceivedBadSettings: NIOHTTP2Error {
public init() { }
}
/// A violation of SETTINGS_MAX_CONCURRENT_STREAMS occurred.
public struct MaxStreamsViolation: NIOHTTP2Error {
public init() { }
}
/// An attempt was made to use a stream ID that is too small.
public struct StreamIDTooSmall: NIOHTTP2Error {
public init() { }
}
/// An attempt was made to send a frame without having previously sent a connection preface!
public struct MissingPreface: NIOHTTP2Error {
public init() { }
}
/// An attempt was made to create a stream after a GOAWAY frame has forbidden further
/// stream creation.
public struct CreatedStreamAfterGoaway: NIOHTTP2Error {
public init() { }
}
/// A peer has attempted to create a stream with a stream ID it is not permitted to use.
public struct InvalidStreamIDForPeer: NIOHTTP2Error {
public init() { }
}
/// An attempt was made to send a new GOAWAY frame whose lastStreamID is higher than the previous value.
public struct RaisedGoawayLastStreamID: NIOHTTP2Error {
public init() { }
}
/// The size of the window increment is invalid.
public struct InvalidWindowIncrementSize: NIOHTTP2Error {
public init() { }
}
/// An attempt was made to push a stream, even though the settings forbid it.
public struct PushInViolationOfSetting: NIOHTTP2Error {
public init() { }
}
/// An attempt was made to use a currently unsupported feature.
public struct Unsupported: NIOHTTP2Error {
public var info: String
public init(info: String) {
self.info = info
}
}
public struct UnableToSerializeFrame: NIOHTTP2Error {
public init() { }
}
public struct UnableToParseFrame: NIOHTTP2Error {
public init() { }
}
/// A pseudo-header field is missing.
public struct MissingPseudoHeader: NIOHTTP2Error {
public var name: String
public init(_ name: String) {
self.name = name
}
}
/// A pseudo-header field has been duplicated.
public struct DuplicatePseudoHeader: NIOHTTP2Error {
public var name: String
public init(_ name: String) {
self.name = name
}
}
/// An outbound request was about to be sent, but does not contain a Host header.
public struct MissingHostHeader: NIOHTTP2Error {
public init() { }
}
/// An outbound request was about to be sent, but it contains a duplicated Host header.
public struct DuplicateHostHeader: NIOHTTP2Error {
public init() { }
}
/// A :status header was received with an invalid value.
public struct InvalidStatusValue: NIOHTTP2Error {
var value: String
public init(_ value: String) {
self.value = value
}
}
}
/// This enum covers errors that are thrown internally for messaging reasons. These should
/// not leak.
internal enum InternalError: Error {
case attemptedToCreateStream
case codecError(code: HTTP2ErrorCode)
}
extension InternalError: Hashable { }