Files
swift-nio-http2/Sources/NIOHTTP2/ConnectionStateMachine/FrameReceivingStates/ReceivingPushPromiseState.swift
T
George Barnett 440d64d99a Add a location to each of 'NIOHTTP2Errors' (#247)
Motivation:

Without knowing where an error was thrown it can be unnecessarily
difficult to work out the reasons an application ended up in an error
state.

Modifications:

- Add a 'file' string to each of the 'NIOHTTP2Errors' denoting the file
  and line where an error was created
- Add static methods to create errors to 'NIOHTTP2Errors' defaulting
  file and line, this is unfortunately necessary as defaulting file and
  line in the `init` makes the defaulted and non-defaulted errors
  indistinguishable, and the compiler will pick the one with fewest
  defaulted values.
- Add equatable conformance manually so as to ignore 'file'
- Add backing storage to errors which no longer be stored in an existential
  container
- Add 'CustomStringConvertible' conformance to the errors using backing
  storage so that the 'file' is actually visible when converting the
  error
- Deprecate the old `init`s
- Fix all deprecation warnings

Result:

- NIOHTTP2Errors carry a 'file' with them indicating where they were
  created
2020-09-24 15:21:58 +01:00

97 lines
5.0 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 NIOHPACK
/// A protocol that provides implementation for receiving PUSH_PROMISE frames, for those states that
/// can validly accept pushed streams.
///
/// This protocol should only be conformed to by states for the HTTP/2 connection state machine.
protocol ReceivingPushPromiseState: HasFlowControlWindows {
var role: HTTP2ConnectionStateMachine.ConnectionRole { get }
var headerBlockValidation: HTTP2ConnectionStateMachine.ValidationState { get }
var streamState: ConnectionStreamState { get set }
var remoteInitialWindowSize: UInt32 { get }
var peerMayPush: Bool { get }
}
extension ReceivingPushPromiseState {
mutating func receivePushPromise(originalStreamID: HTTP2StreamID, childStreamID: HTTP2StreamID, headers: HPACKHeaders) -> StateMachineResultWithEffect {
return self._receivePushPromise(originalStreamID: originalStreamID, childStreamID: childStreamID, headers: headers)
}
fileprivate mutating func _receivePushPromise(originalStreamID: HTTP2StreamID, childStreamID: HTTP2StreamID, headers: HPACKHeaders) -> StateMachineResultWithEffect {
// In states that support a push promise we have two steps. Firstly, we want to create the child stream; then we want to
// pass the PUSH_PROMISE frame through the stream state machine for the parent stream.
//
// The reason we do things in this order is that if for any reason the PUSH_PROMISE frame is invalid on the parent stream,
// we want to take out both the child stream and the parent stream. We can only do that if we have a child stream state to
// modify. For this reason, we unconditionally allow the remote peer to consume the stream. The only case where this is *not*
// true is when the child stream itself cannot be validly created, because the stream ID used by the remote peer is invalid.
// In this case this is a connection error, anyway, so we don't worry too much about it.
//
// Before any of this, though, we need to check whether the remote peer is even allowed to push!
guard self.peerMayPush else {
return StateMachineResultWithEffect(result: .connectionError(underlyingError: NIOHTTP2Errors.pushInViolationOfSetting(), type: .protocolError), effect: nil)
}
let validateHeaderBlock = self.headerBlockValidation == .enabled
do {
try self.streamState.createRemotelyPushedStream(streamID: childStreamID,
remoteInitialWindowSize: self.remoteInitialWindowSize)
let result = self.streamState.modifyStreamState(streamID: originalStreamID, ignoreRecentlyReset: true) {
$0.receivePushPromise(headers: headers, validateHeaderBlock: validateHeaderBlock)
}
return StateMachineResultWithEffect(result, connectionState: self)
} catch {
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .protocolError), effect: nil)
}
}
/// Whether the remote peer may push.
var peerMayPush: Bool {
// In the case where we don't have local settings, we have to assume the default value, in which the peer may push.
return true
}
}
extension ReceivingPushPromiseState where Self: LocallyQuiescingState {
mutating func receivePushPromise(originalStreamID: HTTP2StreamID, childStreamID: HTTP2StreamID, headers: HPACKHeaders) -> StateMachineResultWithEffect {
// This check is duplicated here, because the protocol error of violating this setting is more important than ignoring the frame.
guard self.peerMayPush else {
return StateMachineResultWithEffect(result: .connectionError(underlyingError: NIOHTTP2Errors.pushInViolationOfSetting(), type: .protocolError), effect: nil)
}
// If we're a client, the server is forbidden from initiating new streams, as we quiesced. However, RFC 7540 wants us to ignore this.
if self.role == .client {
return StateMachineResultWithEffect(result: .ignoreFrame, effect: nil)
}
// We're a server, so the remote peer can't initiate a stream with a PUSH_PROMISE, but that's ok, the stream state machine
// will forbid this as it normally does.
return self._receivePushPromise(originalStreamID: originalStreamID, childStreamID: childStreamID, headers: headers)
}
}
extension ReceivingPushPromiseState where Self: HasLocalSettings {
var peerMayPush: Bool {
return self.localSettings.enablePush == 1
}
}