mirror of
https://github.com/apple/swift-nio-http2
synced 2026-11-25 04:47:50 +00:00
Motivation: Standardised formatting makes the project cleaner to look at and work on. Modifications: Setup swift-format with the same settings as Swift-NIO Result: Common formatting throughout
82 lines
3.2 KiB
Swift
82 lines
3.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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// A protocol that provides implementation for receiving GOAWAY frames, for those states that
|
|
/// can validly be quiesced.
|
|
///
|
|
/// This protocol should only be conformed to by states for the HTTP/2 connection state machine.
|
|
protocol ReceivingGoawayState {
|
|
var role: HTTP2ConnectionStateMachine.ConnectionRole { get }
|
|
|
|
var streamState: ConnectionStreamState { get set }
|
|
}
|
|
|
|
extension ReceivingGoawayState {
|
|
mutating func receiveGoAwayFrame(lastStreamID: HTTP2StreamID) -> StateMachineResultWithEffect {
|
|
guard lastStreamID.mayBeInitiatedBy(self.role) || lastStreamID == .rootStream || lastStreamID == .maxID else {
|
|
// The remote peer has sent a GOAWAY with an invalid stream ID.
|
|
return .init(
|
|
result: .connectionError(
|
|
underlyingError: NIOHTTP2Errors.invalidStreamIDForPeer(),
|
|
type: .protocolError
|
|
),
|
|
effect: nil
|
|
)
|
|
}
|
|
|
|
let droppedStreams = self.streamState.dropAllStreamsWithIDHigherThan(
|
|
lastStreamID,
|
|
droppedLocally: false,
|
|
initiatedBy: self.role
|
|
)
|
|
let effect: NIOHTTP2ConnectionStateChange? = droppedStreams.map { .bulkStreamClosure(.init(closedStreams: $0)) }
|
|
return .init(result: .succeed, effect: effect)
|
|
}
|
|
}
|
|
|
|
extension ReceivingGoawayState where Self: RemotelyQuiescingState {
|
|
mutating func receiveGoAwayFrame(lastStreamID: HTTP2StreamID) -> StateMachineResultWithEffect {
|
|
guard lastStreamID.mayBeInitiatedBy(self.role) || lastStreamID == .rootStream || lastStreamID == .maxID else {
|
|
// The remote peer has sent a GOAWAY with an invalid stream ID.
|
|
return .init(
|
|
result: .connectionError(
|
|
underlyingError: NIOHTTP2Errors.invalidStreamIDForPeer(),
|
|
type: .protocolError
|
|
),
|
|
effect: nil
|
|
)
|
|
}
|
|
|
|
if lastStreamID > self.lastLocalStreamID {
|
|
// The remote peer has attempted to raise the lastStreamID.
|
|
return .init(
|
|
result: .connectionError(
|
|
underlyingError: NIOHTTP2Errors.raisedGoawayLastStreamID(),
|
|
type: .protocolError
|
|
),
|
|
effect: nil
|
|
)
|
|
}
|
|
|
|
let droppedStreams = self.streamState.dropAllStreamsWithIDHigherThan(
|
|
lastStreamID,
|
|
droppedLocally: false,
|
|
initiatedBy: self.role
|
|
)
|
|
let effect: NIOHTTP2ConnectionStateChange? = droppedStreams.map { .bulkStreamClosure(.init(closedStreams: $0)) }
|
|
self.lastLocalStreamID = lastStreamID
|
|
return .init(result: .succeed, effect: effect)
|
|
}
|
|
}
|