mirror of
https://github.com/apple/swift-nio-http2
synced 2026-11-27 05:04:23 +00:00
Apply swift-format changes and CI (#472)
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
This commit is contained in:
1 parent
93586110f2
commit
1118370ec4
125 files changed
+21869
-11457
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -13,7 +13,6 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
import NIOCore
|
||||
|
||||
|
||||
/// A representation of the state of the HTTP/2 streams in a single HTTP/2 connection.
|
||||
struct ConnectionStreamState {
|
||||
/// The "safe" default value of SETTINGS_MAX_CONCURRENT_STREAMS.
|
||||
@@ -61,7 +60,7 @@ struct ConnectionStreamState {
|
||||
|
||||
/// The total number of streams currently active.
|
||||
var openStreams: Int {
|
||||
return Int(self.clientStreamCount) + Int(self.serverStreamCount)
|
||||
Int(self.clientStreamCount) + Int(self.serverStreamCount)
|
||||
}
|
||||
|
||||
init() {
|
||||
@@ -79,9 +78,17 @@ struct ConnectionStreamState {
|
||||
/// - remoteInitialWindowSize: The initial window size of the remote peer.
|
||||
/// - requestVerb: the HTTP method used on the request
|
||||
/// - throws: If the stream ID is invalid.
|
||||
mutating func createRemotelyPushedStream(streamID: HTTP2StreamID, remoteInitialWindowSize: UInt32, requestVerb: String?) throws {
|
||||
mutating func createRemotelyPushedStream(
|
||||
streamID: HTTP2StreamID,
|
||||
remoteInitialWindowSize: UInt32,
|
||||
requestVerb: String?
|
||||
) throws {
|
||||
try self.reserveServerStreamID(streamID)
|
||||
let streamState = HTTP2StreamStateMachine(receivedPushPromiseCreatingStreamID: streamID, remoteInitialWindowSize: remoteInitialWindowSize, requestVerb: requestVerb)
|
||||
let streamState = HTTP2StreamStateMachine(
|
||||
receivedPushPromiseCreatingStreamID: streamID,
|
||||
remoteInitialWindowSize: remoteInitialWindowSize,
|
||||
requestVerb: requestVerb
|
||||
)
|
||||
self.activeStreams.insert(streamState)
|
||||
}
|
||||
|
||||
@@ -94,9 +101,17 @@ struct ConnectionStreamState {
|
||||
/// - streamID: The ID of the pushed stream.
|
||||
/// - localInitialWindowSize: Our initial window size..
|
||||
/// - throws: If the stream ID is invalid.
|
||||
mutating func createLocallyPushedStream(streamID: HTTP2StreamID, localInitialWindowSize: UInt32, requestVerb: String?) throws {
|
||||
mutating func createLocallyPushedStream(
|
||||
streamID: HTTP2StreamID,
|
||||
localInitialWindowSize: UInt32,
|
||||
requestVerb: String?
|
||||
) throws {
|
||||
try self.reserveServerStreamID(streamID)
|
||||
let streamState = HTTP2StreamStateMachine(sentPushPromiseCreatingStreamID: streamID, localInitialWindowSize: localInitialWindowSize, requestVerb: requestVerb)
|
||||
let streamState = HTTP2StreamStateMachine(
|
||||
sentPushPromiseCreatingStreamID: streamID,
|
||||
localInitialWindowSize: localInitialWindowSize,
|
||||
requestVerb: requestVerb
|
||||
)
|
||||
self.activeStreams.insert(streamState)
|
||||
}
|
||||
|
||||
@@ -113,17 +128,21 @@ struct ConnectionStreamState {
|
||||
/// - modifier: A block that will be invoked to modify the stream state, if present.
|
||||
/// - throws: Any errors thrown from the creator.
|
||||
/// - Returns: The result of the state modification, as well as any state change that occurred to the stream.
|
||||
mutating func modifyStreamStateCreateIfNeeded(streamID: HTTP2StreamID,
|
||||
localRole: HTTP2StreamStateMachine.StreamRole,
|
||||
localInitialWindowSize: UInt32,
|
||||
remoteInitialWindowSize: UInt32,
|
||||
modifier: (inout HTTP2StreamStateMachine) -> StateMachineResultWithStreamEffect) throws -> StateMachineResultWithStreamEffect {
|
||||
mutating func modifyStreamStateCreateIfNeeded(
|
||||
streamID: HTTP2StreamID,
|
||||
localRole: HTTP2StreamStateMachine.StreamRole,
|
||||
localInitialWindowSize: UInt32,
|
||||
remoteInitialWindowSize: UInt32,
|
||||
modifier: (inout HTTP2StreamStateMachine) -> StateMachineResultWithStreamEffect
|
||||
) throws -> StateMachineResultWithStreamEffect {
|
||||
func creator() throws -> HTTP2StreamStateMachine {
|
||||
try self.reserveClientStreamID(streamID)
|
||||
let initialValue = HTTP2StreamStateMachine(streamID: streamID,
|
||||
localRole: localRole,
|
||||
localInitialWindowSize: localInitialWindowSize,
|
||||
remoteInitialWindowSize: remoteInitialWindowSize)
|
||||
let initialValue = HTTP2StreamStateMachine(
|
||||
streamID: streamID,
|
||||
localRole: localRole,
|
||||
localInitialWindowSize: localInitialWindowSize,
|
||||
remoteInitialWindowSize: remoteInitialWindowSize
|
||||
)
|
||||
return initialValue
|
||||
}
|
||||
|
||||
@@ -158,12 +177,21 @@ struct ConnectionStreamState {
|
||||
/// - ignoreClosed: Whether a closed stream should be ignored. Should be set to `true` when receiving window update or reset stream frames.
|
||||
/// - modifier: A block that will be invoked to modify the stream state, if present.
|
||||
/// - Returns: The result of the state modification, as well as any state change that occurred to the stream.
|
||||
mutating func modifyStreamState(streamID: HTTP2StreamID,
|
||||
ignoreRecentlyReset: Bool,
|
||||
ignoreClosed: Bool = false,
|
||||
_ modifier: (inout HTTP2StreamStateMachine) -> StateMachineResultWithStreamEffect) -> StateMachineResultWithStreamEffect {
|
||||
mutating func modifyStreamState(
|
||||
streamID: HTTP2StreamID,
|
||||
ignoreRecentlyReset: Bool,
|
||||
ignoreClosed: Bool = false,
|
||||
_ modifier: (inout HTTP2StreamStateMachine) -> StateMachineResultWithStreamEffect
|
||||
) -> StateMachineResultWithStreamEffect {
|
||||
guard let result = self.activeStreams.autoClosingTransform(streamID: streamID, modifier) else {
|
||||
return StateMachineResultWithStreamEffect(result: self.streamMissing(streamID: streamID, ignoreRecentlyReset: ignoreRecentlyReset, ignoreClosed: ignoreClosed), effect: nil)
|
||||
return StateMachineResultWithStreamEffect(
|
||||
result: self.streamMissing(
|
||||
streamID: streamID,
|
||||
ignoreRecentlyReset: ignoreRecentlyReset,
|
||||
ignoreClosed: ignoreClosed
|
||||
),
|
||||
effect: nil
|
||||
)
|
||||
}
|
||||
|
||||
if let effect = result.effect, effect.closedStream {
|
||||
@@ -185,14 +213,18 @@ struct ConnectionStreamState {
|
||||
/// - modifier: A block that will be invoked to modify the stream state, if present.
|
||||
/// - Returns: The result of the state modification, as well as any state change that occurred to the stream.
|
||||
@inline(__always)
|
||||
mutating func locallyResetStreamState(streamID: HTTP2StreamID,
|
||||
_ modifier: (inout HTTP2StreamStateMachine) -> StateMachineResultWithStreamEffect) -> StateMachineResultWithStreamEffect {
|
||||
mutating func locallyResetStreamState(
|
||||
streamID: HTTP2StreamID,
|
||||
_ modifier: (inout HTTP2StreamStateMachine) -> StateMachineResultWithStreamEffect
|
||||
) -> StateMachineResultWithStreamEffect {
|
||||
guard let result = self.activeStreams.autoClosingTransform(streamID: streamID, modifier) else {
|
||||
// We never ignore recently reset streams here, as this should only ever be used when *sending* frames.
|
||||
return StateMachineResultWithStreamEffect(result: self.streamMissing(streamID: streamID, ignoreRecentlyReset: false, ignoreClosed: false), effect: nil)
|
||||
return StateMachineResultWithStreamEffect(
|
||||
result: self.streamMissing(streamID: streamID, ignoreRecentlyReset: false, ignoreClosed: false),
|
||||
effect: nil
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
guard let effect = result.effect, effect.closedStream else {
|
||||
preconditionFailure("Locally resetting stream state did not close it!")
|
||||
}
|
||||
@@ -254,9 +286,11 @@ struct ConnectionStreamState {
|
||||
/// - droppedLocally: Whether this drop was caused by sending a GOAWAY frame or receiving it.
|
||||
/// - initiator: The peer that sent the GOAWAY frame.
|
||||
/// - Returns: the stream IDs closed by this operation.
|
||||
mutating func dropAllStreamsWithIDHigherThan(_ streamID: HTTP2StreamID,
|
||||
droppedLocally: Bool,
|
||||
initiatedBy initiator: HTTP2ConnectionStateMachine.ConnectionRole) -> [HTTP2StreamID]? {
|
||||
mutating func dropAllStreamsWithIDHigherThan(
|
||||
_ streamID: HTTP2StreamID,
|
||||
droppedLocally: Bool,
|
||||
initiatedBy initiator: HTTP2ConnectionStateMachine.ConnectionRole
|
||||
) -> [HTTP2StreamID]? {
|
||||
var droppedIDs: [HTTP2StreamID] = []
|
||||
self.activeStreams.dropDataWithStreamIDGreaterThan(streamID, initiatedBy: initiator) { data in
|
||||
droppedIDs = data.map { $0.streamID }
|
||||
@@ -287,22 +321,32 @@ struct ConnectionStreamState {
|
||||
/// - ignoreRecentlyReset: Whether a recently reset stream should be ignored.
|
||||
/// - ignoreClosed: Whether a closed stream should be ignored.
|
||||
/// - Returns: A `StateMachineResult` for this frame error.
|
||||
private func streamMissing(streamID: HTTP2StreamID, ignoreRecentlyReset: Bool, ignoreClosed: Bool) -> StateMachineResult {
|
||||
private func streamMissing(
|
||||
streamID: HTTP2StreamID,
|
||||
ignoreRecentlyReset: Bool,
|
||||
ignoreClosed: Bool
|
||||
) -> StateMachineResult {
|
||||
if ignoreRecentlyReset && self.recentlyResetStreams.contains(streamID) {
|
||||
return .ignoreFrame
|
||||
}
|
||||
|
||||
switch streamID.mayBeInitiatedBy(.client) {
|
||||
case true where streamID > self.lastClientStreamID,
|
||||
false where streamID > self.lastServerStreamID:
|
||||
false where streamID > self.lastServerStreamID:
|
||||
// The stream in question is idle.
|
||||
return .connectionError(underlyingError: NIOHTTP2Errors.noSuchStream(streamID: streamID), type: .protocolError)
|
||||
return .connectionError(
|
||||
underlyingError: NIOHTTP2Errors.noSuchStream(streamID: streamID),
|
||||
type: .protocolError
|
||||
)
|
||||
default:
|
||||
// This stream must have already been closed.
|
||||
if ignoreClosed {
|
||||
return .ignoreFrame
|
||||
} else {
|
||||
return .connectionError(underlyingError: NIOHTTP2Errors.noSuchStream(streamID: streamID), type: .streamClosed)
|
||||
return .connectionError(
|
||||
underlyingError: NIOHTTP2Errors.noSuchStream(streamID: streamID),
|
||||
type: .streamClosed
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -317,11 +361,10 @@ struct ConnectionStreamState {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension CircularBuffer {
|
||||
// CircularBuffer may never be "full": that is, capacity may never equal count.
|
||||
var effectiveCapacity: Int {
|
||||
return self.capacity - 1
|
||||
self.capacity - 1
|
||||
}
|
||||
|
||||
/// Prepends `element` without expanding the capacity, by dropping the
|
||||
@@ -357,7 +400,6 @@ extension CircularBuffer {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension StreamStateChange {
|
||||
fileprivate var closedStream: Bool {
|
||||
switch self {
|
||||
|
||||
@@ -22,4 +22,5 @@ protocol MayReceiveFrames:
|
||||
ReceivingPushPromiseState,
|
||||
ReceivingWindowUpdateState,
|
||||
ReceivingRstStreamState,
|
||||
ReceivingGoawayState { }
|
||||
ReceivingGoawayState
|
||||
{}
|
||||
+32
-10
@@ -24,17 +24,29 @@ protocol ReceivingDataState: HasFlowControlWindows {
|
||||
|
||||
extension ReceivingDataState {
|
||||
/// Called to receive a DATA frame.
|
||||
mutating func receiveData(streamID: HTTP2StreamID, contentLength: Int, flowControlledBytes: Int, isEndStreamSet endStream: Bool) -> StateMachineResultWithEffect {
|
||||
mutating func receiveData(
|
||||
streamID: HTTP2StreamID,
|
||||
contentLength: Int,
|
||||
flowControlledBytes: Int,
|
||||
isEndStreamSet endStream: Bool
|
||||
) -> StateMachineResultWithEffect {
|
||||
do {
|
||||
try self.inboundFlowControlWindow.consume(flowControlledBytes: flowControlledBytes)
|
||||
} catch let error where error is NIOHTTP2Errors.FlowControlViolation {
|
||||
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .flowControlError), effect: nil)
|
||||
return StateMachineResultWithEffect(
|
||||
result: .connectionError(underlyingError: error, type: .flowControlError),
|
||||
effect: nil
|
||||
)
|
||||
} catch {
|
||||
preconditionFailure("Unexpected error: \(error)")
|
||||
}
|
||||
|
||||
let result = self.streamState.modifyStreamState(streamID: streamID, ignoreRecentlyReset: true) {
|
||||
$0.receiveData(contentLength: contentLength, flowControlledBytes: flowControlledBytes, isEndStreamSet: endStream)
|
||||
$0.receiveData(
|
||||
contentLength: contentLength,
|
||||
flowControlledBytes: flowControlledBytes,
|
||||
isEndStreamSet: endStream
|
||||
)
|
||||
}
|
||||
|
||||
// We need to be a bit careful here. The backing code may have triggered either an ignoreFrame or streamError. While both of these
|
||||
@@ -43,16 +55,26 @@ extension ReceivingDataState {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private extension StateMachineResultWithEffect {
|
||||
static func withGuaranteedFlowControlEffect<ConnectionState: ReceivingDataState>(_ result: StateMachineResultWithStreamEffect, connectionState: ConnectionState) -> StateMachineResultWithEffect {
|
||||
extension StateMachineResultWithEffect {
|
||||
fileprivate static func withGuaranteedFlowControlEffect<ConnectionState: ReceivingDataState>(
|
||||
_ result: StateMachineResultWithStreamEffect,
|
||||
connectionState: ConnectionState
|
||||
) -> StateMachineResultWithEffect {
|
||||
// In most cases, this will update the underlying effect with the new flow control window info.
|
||||
var newResult = StateMachineResultWithEffect(result,
|
||||
inboundFlowControlWindow: connectionState.inboundFlowControlWindow,
|
||||
outboundFlowControlWindow: connectionState.outboundFlowControlWindow)
|
||||
var newResult = StateMachineResultWithEffect(
|
||||
result,
|
||||
inboundFlowControlWindow: connectionState.inboundFlowControlWindow,
|
||||
outboundFlowControlWindow: connectionState.outboundFlowControlWindow
|
||||
)
|
||||
if newResult.effect == nil {
|
||||
// But if we aren't noting it anywhere else, we note it here.
|
||||
newResult.effect = .flowControlChange(.init(localConnectionWindowSize: Int(connectionState.outboundFlowControlWindow), remoteConnectionWindowSize: Int(connectionState.inboundFlowControlWindow), localStreamWindowSize: nil))
|
||||
newResult.effect = .flowControlChange(
|
||||
.init(
|
||||
localConnectionWindowSize: Int(connectionState.outboundFlowControlWindow),
|
||||
remoteConnectionWindowSize: Int(connectionState.inboundFlowControlWindow),
|
||||
localStreamWindowSize: nil
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
return newResult
|
||||
|
||||
+31
-6
@@ -26,10 +26,20 @@ 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)
|
||||
return .init(
|
||||
result: .connectionError(
|
||||
underlyingError: NIOHTTP2Errors.invalidStreamIDForPeer(),
|
||||
type: .protocolError
|
||||
),
|
||||
effect: nil
|
||||
)
|
||||
}
|
||||
|
||||
let droppedStreams = self.streamState.dropAllStreamsWithIDHigherThan(lastStreamID, droppedLocally: false, initiatedBy: self.role)
|
||||
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)
|
||||
}
|
||||
@@ -39,18 +49,33 @@ 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)
|
||||
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)
|
||||
return .init(
|
||||
result: .connectionError(
|
||||
underlyingError: NIOHTTP2Errors.raisedGoawayLastStreamID(),
|
||||
type: .protocolError
|
||||
),
|
||||
effect: nil
|
||||
)
|
||||
}
|
||||
|
||||
let droppedStreams = self.streamState.dropAllStreamsWithIDHigherThan(lastStreamID, droppedLocally: false, initiatedBy: self.role)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
+57
-15
@@ -17,7 +17,9 @@ import NIOHPACK
|
||||
/// can validly accept headers.
|
||||
///
|
||||
/// This protocol should only be conformed to by states for the HTTP/2 connection state machine.
|
||||
protocol ReceivingHeadersState: HasFlowControlWindows, HasLocalExtendedConnectSettings, HasRemoteExtendedConnectSettings, ConnectionStateWithRole {
|
||||
protocol ReceivingHeadersState: HasFlowControlWindows, HasLocalExtendedConnectSettings,
|
||||
HasRemoteExtendedConnectSettings, ConnectionStateWithRole
|
||||
{
|
||||
var role: HTTP2ConnectionStateMachine.ConnectionRole { get }
|
||||
|
||||
var headerBlockValidation: HTTP2ConnectionStateMachine.ValidationState { get }
|
||||
@@ -33,7 +35,11 @@ protocol ReceivingHeadersState: HasFlowControlWindows, HasLocalExtendedConnectSe
|
||||
|
||||
extension ReceivingHeadersState {
|
||||
/// Called when we receive a HEADERS frame in this state.
|
||||
mutating func receiveHeaders(streamID: HTTP2StreamID, headers: HPACKHeaders, isEndStreamSet endStream: Bool) -> StateMachineResultWithEffect {
|
||||
mutating func receiveHeaders(
|
||||
streamID: HTTP2StreamID,
|
||||
headers: HPACKHeaders,
|
||||
isEndStreamSet endStream: Bool
|
||||
) -> StateMachineResultWithEffect {
|
||||
let result: StateMachineResultWithStreamEffect
|
||||
let validateHeaderBlock = self.headerBlockValidation == .enabled
|
||||
let validateContentLength = self.contentLengthValidation == .enabled
|
||||
@@ -42,33 +48,60 @@ extension ReceivingHeadersState {
|
||||
|
||||
if self.role == .server && streamID.mayBeInitiatedBy(.client) {
|
||||
do {
|
||||
result = try self.streamState.modifyStreamStateCreateIfNeeded(streamID: streamID, localRole: .server, localInitialWindowSize: self.localInitialWindowSize, remoteInitialWindowSize: self.remoteInitialWindowSize) {
|
||||
$0.receiveHeaders(headers: headers, validateHeaderBlock: validateHeaderBlock, validateContentLength: validateContentLength, localSupportsExtendedConnect: localSupportsExtendedConnect, remoteSupportsExtendedConnect: remoteSupportsExtendedConnect, isEndStreamSet: endStream)
|
||||
result = try self.streamState.modifyStreamStateCreateIfNeeded(
|
||||
streamID: streamID,
|
||||
localRole: .server,
|
||||
localInitialWindowSize: self.localInitialWindowSize,
|
||||
remoteInitialWindowSize: self.remoteInitialWindowSize
|
||||
) {
|
||||
$0.receiveHeaders(
|
||||
headers: headers,
|
||||
validateHeaderBlock: validateHeaderBlock,
|
||||
validateContentLength: validateContentLength,
|
||||
localSupportsExtendedConnect: localSupportsExtendedConnect,
|
||||
remoteSupportsExtendedConnect: remoteSupportsExtendedConnect,
|
||||
isEndStreamSet: endStream
|
||||
)
|
||||
}
|
||||
} catch {
|
||||
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .protocolError), effect: nil)
|
||||
return StateMachineResultWithEffect(
|
||||
result: .connectionError(underlyingError: error, type: .protocolError),
|
||||
effect: nil
|
||||
)
|
||||
}
|
||||
} else {
|
||||
// HEADERS cannot create streams for servers, so this must be for a stream we already know about.
|
||||
result = self.streamState.modifyStreamState(streamID: streamID, ignoreRecentlyReset: true) {
|
||||
$0.receiveHeaders(headers: headers, validateHeaderBlock: validateHeaderBlock, validateContentLength: validateContentLength, localSupportsExtendedConnect: localSupportsExtendedConnect, remoteSupportsExtendedConnect: remoteSupportsExtendedConnect, isEndStreamSet: endStream)
|
||||
$0.receiveHeaders(
|
||||
headers: headers,
|
||||
validateHeaderBlock: validateHeaderBlock,
|
||||
validateContentLength: validateContentLength,
|
||||
localSupportsExtendedConnect: localSupportsExtendedConnect,
|
||||
remoteSupportsExtendedConnect: remoteSupportsExtendedConnect,
|
||||
isEndStreamSet: endStream
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return StateMachineResultWithEffect(result,
|
||||
inboundFlowControlWindow: self.inboundFlowControlWindow,
|
||||
outboundFlowControlWindow: self.outboundFlowControlWindow)
|
||||
return StateMachineResultWithEffect(
|
||||
result,
|
||||
inboundFlowControlWindow: self.inboundFlowControlWindow,
|
||||
outboundFlowControlWindow: self.outboundFlowControlWindow
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension ReceivingHeadersState where Self: LocallyQuiescingState {
|
||||
/// Called when we receive a HEADERS frame in this state.
|
||||
///
|
||||
/// If we've quiesced this connection, the remote peer is no longer allowed to create new streams.
|
||||
/// We ignore any frame that appears to be creating a new stream, and then prevent this from creating
|
||||
/// new streams.
|
||||
mutating func receiveHeaders(streamID: HTTP2StreamID, headers: HPACKHeaders, isEndStreamSet endStream: Bool) -> StateMachineResultWithEffect {
|
||||
mutating func receiveHeaders(
|
||||
streamID: HTTP2StreamID,
|
||||
headers: HPACKHeaders,
|
||||
isEndStreamSet endStream: Bool
|
||||
) -> StateMachineResultWithEffect {
|
||||
let validateHeaderBlock = self.headerBlockValidation == .enabled
|
||||
let validateContentLength = self.contentLengthValidation == .enabled
|
||||
let localSupportsExtendedConnect = self.localSupportsExtendedConnect
|
||||
@@ -83,10 +116,19 @@ extension ReceivingHeadersState where Self: LocallyQuiescingState {
|
||||
|
||||
// At this stage we've quiesced, so the remote peer is not allowed to create new streams.
|
||||
let result = self.streamState.modifyStreamState(streamID: streamID, ignoreRecentlyReset: true) {
|
||||
$0.receiveHeaders(headers: headers, validateHeaderBlock: validateHeaderBlock, validateContentLength: validateContentLength, localSupportsExtendedConnect: localSupportsExtendedConnect, remoteSupportsExtendedConnect: remoteSupportsExtendedConnect, isEndStreamSet: endStream)
|
||||
$0.receiveHeaders(
|
||||
headers: headers,
|
||||
validateHeaderBlock: validateHeaderBlock,
|
||||
validateContentLength: validateContentLength,
|
||||
localSupportsExtendedConnect: localSupportsExtendedConnect,
|
||||
remoteSupportsExtendedConnect: remoteSupportsExtendedConnect,
|
||||
isEndStreamSet: endStream
|
||||
)
|
||||
}
|
||||
return StateMachineResultWithEffect(result,
|
||||
inboundFlowControlWindow: self.inboundFlowControlWindow,
|
||||
outboundFlowControlWindow: self.outboundFlowControlWindow)
|
||||
return StateMachineResultWithEffect(
|
||||
result,
|
||||
inboundFlowControlWindow: self.inboundFlowControlWindow,
|
||||
outboundFlowControlWindow: self.outboundFlowControlWindow
|
||||
)
|
||||
}
|
||||
}
|
||||
+51
-15
@@ -30,11 +30,19 @@ protocol ReceivingPushPromiseState: HasFlowControlWindows {
|
||||
}
|
||||
|
||||
extension ReceivingPushPromiseState {
|
||||
mutating func receivePushPromise(originalStreamID: HTTP2StreamID, childStreamID: HTTP2StreamID, headers: HPACKHeaders) -> StateMachineResultWithEffect {
|
||||
return self._receivePushPromise(originalStreamID: originalStreamID, childStreamID: childStreamID, headers: headers)
|
||||
mutating func receivePushPromise(
|
||||
originalStreamID: HTTP2StreamID,
|
||||
childStreamID: HTTP2StreamID,
|
||||
headers: HPACKHeaders
|
||||
) -> StateMachineResultWithEffect {
|
||||
self._receivePushPromise(originalStreamID: originalStreamID, childStreamID: childStreamID, headers: headers)
|
||||
}
|
||||
|
||||
fileprivate mutating func _receivePushPromise(originalStreamID: HTTP2StreamID, childStreamID: HTTP2StreamID, headers: HPACKHeaders) -> StateMachineResultWithEffect {
|
||||
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.
|
||||
//
|
||||
@@ -46,38 +54,62 @@ extension ReceivingPushPromiseState {
|
||||
//
|
||||
// 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)
|
||||
return StateMachineResultWithEffect(
|
||||
result: .connectionError(
|
||||
underlyingError: NIOHTTP2Errors.pushInViolationOfSetting(),
|
||||
type: .protocolError
|
||||
),
|
||||
effect: nil
|
||||
)
|
||||
}
|
||||
|
||||
let validateHeaderBlock = self.headerBlockValidation == .enabled
|
||||
let requestVerb = headers.first(name: ":method")
|
||||
do {
|
||||
try self.streamState.createRemotelyPushedStream(streamID: childStreamID,
|
||||
remoteInitialWindowSize: self.remoteInitialWindowSize, requestVerb: requestVerb)
|
||||
try self.streamState.createRemotelyPushedStream(
|
||||
streamID: childStreamID,
|
||||
remoteInitialWindowSize: self.remoteInitialWindowSize,
|
||||
requestVerb: requestVerb
|
||||
)
|
||||
|
||||
let result = self.streamState.modifyStreamState(streamID: originalStreamID, ignoreRecentlyReset: true) {
|
||||
$0.receivePushPromise(headers: headers, validateHeaderBlock: validateHeaderBlock)
|
||||
}
|
||||
return StateMachineResultWithEffect(result,
|
||||
inboundFlowControlWindow: self.inboundFlowControlWindow,
|
||||
outboundFlowControlWindow: self.outboundFlowControlWindow)
|
||||
return StateMachineResultWithEffect(
|
||||
result,
|
||||
inboundFlowControlWindow: self.inboundFlowControlWindow,
|
||||
outboundFlowControlWindow: self.outboundFlowControlWindow
|
||||
)
|
||||
} catch {
|
||||
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .protocolError), effect: nil)
|
||||
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 servers may push and clients may not.
|
||||
return self.role == .client
|
||||
self.role == .client
|
||||
}
|
||||
}
|
||||
|
||||
extension ReceivingPushPromiseState where Self: LocallyQuiescingState {
|
||||
mutating func receivePushPromise(originalStreamID: HTTP2StreamID, childStreamID: HTTP2StreamID, headers: HPACKHeaders) -> StateMachineResultWithEffect {
|
||||
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)
|
||||
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.
|
||||
@@ -87,12 +119,16 @@ extension ReceivingPushPromiseState where Self: LocallyQuiescingState {
|
||||
|
||||
// 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)
|
||||
return self._receivePushPromise(
|
||||
originalStreamID: originalStreamID,
|
||||
childStreamID: childStreamID,
|
||||
headers: headers
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
extension ReceivingPushPromiseState where Self: HasLocalSettings {
|
||||
var peerMayPush: Bool {
|
||||
return self.localSettings.enablePush == 1 && self.role == .client
|
||||
self.localSettings.enablePush == 1 && self.role == .client
|
||||
}
|
||||
}
|
||||
+10
-4
@@ -25,11 +25,17 @@ extension ReceivingRstStreamState {
|
||||
mutating func receiveRstStream(streamID: HTTP2StreamID, reason: HTTP2ErrorCode) -> StateMachineResultWithEffect {
|
||||
// RFC 7540 § 6.4 <https://httpwg.org/specs/rfc7540.html#RST_STREAM> does not explicitly forbid a peer sending
|
||||
// multiple RST_STREAMs for the same stream which means we should ignore subsequent RST_STREAMs.
|
||||
let result = self.streamState.modifyStreamState(streamID: streamID, ignoreRecentlyReset: true, ignoreClosed: true) {
|
||||
let result = self.streamState.modifyStreamState(
|
||||
streamID: streamID,
|
||||
ignoreRecentlyReset: true,
|
||||
ignoreClosed: true
|
||||
) {
|
||||
$0.receiveRstStream(reason: reason)
|
||||
}
|
||||
return StateMachineResultWithEffect(result,
|
||||
inboundFlowControlWindow: self.inboundFlowControlWindow,
|
||||
outboundFlowControlWindow: self.outboundFlowControlWindow)
|
||||
return StateMachineResultWithEffect(
|
||||
result,
|
||||
inboundFlowControlWindow: self.inboundFlowControlWindow,
|
||||
outboundFlowControlWindow: self.outboundFlowControlWindow
|
||||
)
|
||||
}
|
||||
}
|
||||
+25
-9
@@ -28,25 +28,41 @@ extension ReceivingWindowUpdateState {
|
||||
// This is an update for the connection. We police the errors here.
|
||||
do {
|
||||
try self.outboundFlowControlWindow.windowUpdate(by: increment)
|
||||
let flowControlSize: NIOHTTP2ConnectionStateChange = .flowControlChange(.init(localConnectionWindowSize: Int(self.outboundFlowControlWindow),
|
||||
remoteConnectionWindowSize: Int(self.inboundFlowControlWindow),
|
||||
localStreamWindowSize: nil))
|
||||
let flowControlSize: NIOHTTP2ConnectionStateChange = .flowControlChange(
|
||||
.init(
|
||||
localConnectionWindowSize: Int(self.outboundFlowControlWindow),
|
||||
remoteConnectionWindowSize: Int(self.inboundFlowControlWindow),
|
||||
localStreamWindowSize: nil
|
||||
)
|
||||
)
|
||||
return StateMachineResultWithEffect(result: .succeed, effect: flowControlSize)
|
||||
} catch let error where error is NIOHTTP2Errors.InvalidFlowControlWindowSize {
|
||||
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .flowControlError), effect: nil)
|
||||
return StateMachineResultWithEffect(
|
||||
result: .connectionError(underlyingError: error, type: .flowControlError),
|
||||
effect: nil
|
||||
)
|
||||
} catch let error where error is NIOHTTP2Errors.InvalidWindowIncrementSize {
|
||||
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .protocolError), effect: nil)
|
||||
return StateMachineResultWithEffect(
|
||||
result: .connectionError(underlyingError: error, type: .protocolError),
|
||||
effect: nil
|
||||
)
|
||||
} catch {
|
||||
preconditionFailure("Unexpected error: \(error)")
|
||||
}
|
||||
} else {
|
||||
// This is an update for a specific stream: it's responsible for policing any errors.
|
||||
let result = self.streamState.modifyStreamState(streamID: streamID, ignoreRecentlyReset: true, ignoreClosed: true) {
|
||||
let result = self.streamState.modifyStreamState(
|
||||
streamID: streamID,
|
||||
ignoreRecentlyReset: true,
|
||||
ignoreClosed: true
|
||||
) {
|
||||
$0.receiveWindowUpdate(windowIncrement: increment)
|
||||
}
|
||||
return StateMachineResultWithEffect(result,
|
||||
inboundFlowControlWindow: self.inboundFlowControlWindow,
|
||||
outboundFlowControlWindow: self.outboundFlowControlWindow)
|
||||
return StateMachineResultWithEffect(
|
||||
result,
|
||||
inboundFlowControlWindow: self.inboundFlowControlWindow,
|
||||
outboundFlowControlWindow: self.outboundFlowControlWindow
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,4 +22,5 @@ protocol MaySendFrames:
|
||||
SendingPushPromiseState,
|
||||
SendingWindowUpdateState,
|
||||
SendingRstStreamState,
|
||||
SendingGoawayState { }
|
||||
SendingGoawayState
|
||||
{}
|
||||
@@ -24,20 +24,34 @@ protocol SendingDataState: HasFlowControlWindows {
|
||||
|
||||
extension SendingDataState {
|
||||
/// Called to send a DATA frame.
|
||||
mutating func sendData(streamID: HTTP2StreamID, contentLength: Int, flowControlledBytes: Int, isEndStreamSet endStream: Bool) -> StateMachineResultWithEffect {
|
||||
mutating func sendData(
|
||||
streamID: HTTP2StreamID,
|
||||
contentLength: Int,
|
||||
flowControlledBytes: Int,
|
||||
isEndStreamSet endStream: Bool
|
||||
) -> StateMachineResultWithEffect {
|
||||
do {
|
||||
try self.outboundFlowControlWindow.consume(flowControlledBytes: flowControlledBytes)
|
||||
} catch let error where error is NIOHTTP2Errors.FlowControlViolation {
|
||||
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .flowControlError), effect: nil)
|
||||
return StateMachineResultWithEffect(
|
||||
result: .connectionError(underlyingError: error, type: .flowControlError),
|
||||
effect: nil
|
||||
)
|
||||
} catch {
|
||||
preconditionFailure("Unexpected error: \(error)")
|
||||
}
|
||||
|
||||
let result = self.streamState.modifyStreamState(streamID: streamID, ignoreRecentlyReset: false) {
|
||||
$0.sendData(contentLength: contentLength, flowControlledBytes: flowControlledBytes, isEndStreamSet: endStream)
|
||||
$0.sendData(
|
||||
contentLength: contentLength,
|
||||
flowControlledBytes: flowControlledBytes,
|
||||
isEndStreamSet: endStream
|
||||
)
|
||||
}
|
||||
return StateMachineResultWithEffect(result,
|
||||
inboundFlowControlWindow: self.inboundFlowControlWindow,
|
||||
outboundFlowControlWindow: self.outboundFlowControlWindow)
|
||||
return StateMachineResultWithEffect(
|
||||
result,
|
||||
inboundFlowControlWindow: self.inboundFlowControlWindow,
|
||||
outboundFlowControlWindow: self.outboundFlowControlWindow
|
||||
)
|
||||
}
|
||||
}
|
||||
+35
-7
@@ -24,12 +24,23 @@ protocol SendingGoawayState {
|
||||
|
||||
extension SendingGoawayState {
|
||||
mutating func sendGoAwayFrame(lastStreamID: HTTP2StreamID) -> StateMachineResultWithEffect {
|
||||
guard lastStreamID.mayBeInitiatedBy(self.peerRole) || lastStreamID == .rootStream || lastStreamID == .maxID else {
|
||||
guard lastStreamID.mayBeInitiatedBy(self.peerRole) || lastStreamID == .rootStream || lastStreamID == .maxID
|
||||
else {
|
||||
// The user has sent a GOAWAY with an invalid stream ID.
|
||||
return .init(result: .connectionError(underlyingError: NIOHTTP2Errors.invalidStreamIDForPeer(), type: .protocolError), effect: nil)
|
||||
return .init(
|
||||
result: .connectionError(
|
||||
underlyingError: NIOHTTP2Errors.invalidStreamIDForPeer(),
|
||||
type: .protocolError
|
||||
),
|
||||
effect: nil
|
||||
)
|
||||
}
|
||||
|
||||
let droppedStreams = self.streamState.dropAllStreamsWithIDHigherThan(lastStreamID, droppedLocally: true, initiatedBy: self.peerRole)
|
||||
let droppedStreams = self.streamState.dropAllStreamsWithIDHigherThan(
|
||||
lastStreamID,
|
||||
droppedLocally: true,
|
||||
initiatedBy: self.peerRole
|
||||
)
|
||||
let effect: NIOHTTP2ConnectionStateChange? = droppedStreams.map { .bulkStreamClosure(.init(closedStreams: $0)) }
|
||||
return .init(result: .succeed, effect: effect)
|
||||
}
|
||||
@@ -37,18 +48,35 @@ extension SendingGoawayState {
|
||||
|
||||
extension SendingGoawayState where Self: LocallyQuiescingState {
|
||||
mutating func sendGoAwayFrame(lastStreamID: HTTP2StreamID) -> StateMachineResultWithEffect {
|
||||
guard lastStreamID.mayBeInitiatedBy(self.peerRole) || lastStreamID == .rootStream || lastStreamID == .maxID else {
|
||||
guard lastStreamID.mayBeInitiatedBy(self.peerRole) || lastStreamID == .rootStream || lastStreamID == .maxID
|
||||
else {
|
||||
// The user has sent a GOAWAY with an invalid stream ID.
|
||||
return .init(result: .connectionError(underlyingError: NIOHTTP2Errors.invalidStreamIDForPeer(), type: .protocolError), effect: nil)
|
||||
return .init(
|
||||
result: .connectionError(
|
||||
underlyingError: NIOHTTP2Errors.invalidStreamIDForPeer(),
|
||||
type: .protocolError
|
||||
),
|
||||
effect: nil
|
||||
)
|
||||
}
|
||||
|
||||
if lastStreamID > self.lastRemoteStreamID {
|
||||
// The user has attempted to raise the lastStreamID.
|
||||
return .init(result: .connectionError(underlyingError: NIOHTTP2Errors.raisedGoawayLastStreamID(), type: .protocolError), effect: nil)
|
||||
return .init(
|
||||
result: .connectionError(
|
||||
underlyingError: NIOHTTP2Errors.raisedGoawayLastStreamID(),
|
||||
type: .protocolError
|
||||
),
|
||||
effect: nil
|
||||
)
|
||||
}
|
||||
|
||||
// Ok, this is a valid request, so we can now process it like .active.
|
||||
let droppedStreams = self.streamState.dropAllStreamsWithIDHigherThan(lastStreamID, droppedLocally: true, initiatedBy: self.peerRole)
|
||||
let droppedStreams = self.streamState.dropAllStreamsWithIDHigherThan(
|
||||
lastStreamID,
|
||||
droppedLocally: true,
|
||||
initiatedBy: self.peerRole
|
||||
)
|
||||
let effect: NIOHTTP2ConnectionStateChange? = droppedStreams.map { .bulkStreamClosure(.init(closedStreams: $0)) }
|
||||
self.lastRemoteStreamID = lastStreamID
|
||||
return .init(result: .succeed, effect: effect)
|
||||
|
||||
+60
-22
@@ -13,7 +13,6 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
import NIOHPACK
|
||||
|
||||
|
||||
/// A protocol that provides implementation for sending HEADERS frames, for those states that
|
||||
/// can validly send headers.
|
||||
///
|
||||
@@ -34,7 +33,11 @@ protocol SendingHeadersState: HasFlowControlWindows, HasLocalExtendedConnectSett
|
||||
|
||||
extension SendingHeadersState {
|
||||
/// Called when we send a HEADERS frame in this state.
|
||||
mutating func sendHeaders(streamID: HTTP2StreamID, headers: HPACKHeaders, isEndStreamSet endStream: Bool) -> StateMachineResultWithEffect {
|
||||
mutating func sendHeaders(
|
||||
streamID: HTTP2StreamID,
|
||||
headers: HPACKHeaders,
|
||||
isEndStreamSet endStream: Bool
|
||||
) -> StateMachineResultWithEffect {
|
||||
let result: StateMachineResultWithStreamEffect
|
||||
let validateHeaderBlock = self.headerBlockValidation == .enabled
|
||||
let validateContentLength = self.contentLengthValidation == .enabled
|
||||
@@ -43,48 +46,83 @@ extension SendingHeadersState {
|
||||
|
||||
if self.role == .client && streamID.mayBeInitiatedBy(.client) {
|
||||
do {
|
||||
result = try self.streamState.modifyStreamStateCreateIfNeeded(streamID: streamID,
|
||||
localRole: .client,
|
||||
localInitialWindowSize: self.localInitialWindowSize,
|
||||
remoteInitialWindowSize: self.remoteInitialWindowSize) {
|
||||
$0.sendHeaders(headers: headers, validateHeaderBlock: validateHeaderBlock, validateContentLength: validateContentLength, localSupportsExtendedConnect: localSupportsExtendedConnect, remoteSupportsExtendedConnect: remoteSupportsExtendedConnect, isEndStreamSet: endStream)
|
||||
result = try self.streamState.modifyStreamStateCreateIfNeeded(
|
||||
streamID: streamID,
|
||||
localRole: .client,
|
||||
localInitialWindowSize: self.localInitialWindowSize,
|
||||
remoteInitialWindowSize: self.remoteInitialWindowSize
|
||||
) {
|
||||
$0.sendHeaders(
|
||||
headers: headers,
|
||||
validateHeaderBlock: validateHeaderBlock,
|
||||
validateContentLength: validateContentLength,
|
||||
localSupportsExtendedConnect: localSupportsExtendedConnect,
|
||||
remoteSupportsExtendedConnect: remoteSupportsExtendedConnect,
|
||||
isEndStreamSet: endStream
|
||||
)
|
||||
}
|
||||
} catch {
|
||||
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .protocolError), effect: nil)
|
||||
return StateMachineResultWithEffect(
|
||||
result: .connectionError(underlyingError: error, type: .protocolError),
|
||||
effect: nil
|
||||
)
|
||||
}
|
||||
} else {
|
||||
// HEADERS cannot create streams for servers, so this must be for a stream we already know about.
|
||||
result = self.streamState.modifyStreamState(streamID: streamID, ignoreRecentlyReset: false) {
|
||||
$0.sendHeaders(headers: headers, validateHeaderBlock: validateHeaderBlock, validateContentLength: validateContentLength, localSupportsExtendedConnect: localSupportsExtendedConnect, remoteSupportsExtendedConnect: remoteSupportsExtendedConnect, isEndStreamSet: endStream)
|
||||
$0.sendHeaders(
|
||||
headers: headers,
|
||||
validateHeaderBlock: validateHeaderBlock,
|
||||
validateContentLength: validateContentLength,
|
||||
localSupportsExtendedConnect: localSupportsExtendedConnect,
|
||||
remoteSupportsExtendedConnect: remoteSupportsExtendedConnect,
|
||||
isEndStreamSet: endStream
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return StateMachineResultWithEffect(result,
|
||||
inboundFlowControlWindow: self.inboundFlowControlWindow,
|
||||
outboundFlowControlWindow: self.outboundFlowControlWindow)
|
||||
return StateMachineResultWithEffect(
|
||||
result,
|
||||
inboundFlowControlWindow: self.inboundFlowControlWindow,
|
||||
outboundFlowControlWindow: self.outboundFlowControlWindow
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension SendingHeadersState where Self: RemotelyQuiescingState {
|
||||
/// If we've been remotely quiesced, we're forbidden from creating new streams. So we can only possibly
|
||||
/// be modifying an existing one.
|
||||
mutating func sendHeaders(streamID: HTTP2StreamID, headers: HPACKHeaders, isEndStreamSet endStream: Bool) -> StateMachineResultWithEffect {
|
||||
mutating func sendHeaders(
|
||||
streamID: HTTP2StreamID,
|
||||
headers: HPACKHeaders,
|
||||
isEndStreamSet endStream: Bool
|
||||
) -> StateMachineResultWithEffect {
|
||||
let validateHeaderBlock = self.headerBlockValidation == .enabled
|
||||
let validateContentLength = self.contentLengthValidation == .enabled
|
||||
let localSupportsExtendedConnect = self.localSupportsExtendedConnect
|
||||
let remoteSupportsExtendedConnect = self.remoteSupportsExtendedConnect
|
||||
if streamID.mayBeInitiatedBy(.client) &&
|
||||
self.role == .client &&
|
||||
streamID > self.streamState.lastClientStreamID {
|
||||
if streamID.mayBeInitiatedBy(.client) && self.role == .client && streamID > self.streamState.lastClientStreamID
|
||||
{
|
||||
let error = NIOHTTP2Errors.createdStreamAfterGoaway()
|
||||
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .protocolError), effect: nil)
|
||||
return StateMachineResultWithEffect(
|
||||
result: .connectionError(underlyingError: error, type: .protocolError),
|
||||
effect: nil
|
||||
)
|
||||
}
|
||||
let result = self.streamState.modifyStreamState(streamID: streamID, ignoreRecentlyReset: false) {
|
||||
$0.sendHeaders(headers: headers, validateHeaderBlock: validateHeaderBlock, validateContentLength: validateContentLength, localSupportsExtendedConnect: localSupportsExtendedConnect, remoteSupportsExtendedConnect: remoteSupportsExtendedConnect, isEndStreamSet: endStream)
|
||||
$0.sendHeaders(
|
||||
headers: headers,
|
||||
validateHeaderBlock: validateHeaderBlock,
|
||||
validateContentLength: validateContentLength,
|
||||
localSupportsExtendedConnect: localSupportsExtendedConnect,
|
||||
remoteSupportsExtendedConnect: remoteSupportsExtendedConnect,
|
||||
isEndStreamSet: endStream
|
||||
)
|
||||
}
|
||||
return StateMachineResultWithEffect(result,
|
||||
inboundFlowControlWindow: self.inboundFlowControlWindow,
|
||||
outboundFlowControlWindow: self.outboundFlowControlWindow)
|
||||
return StateMachineResultWithEffect(
|
||||
result,
|
||||
inboundFlowControlWindow: self.inboundFlowControlWindow,
|
||||
outboundFlowControlWindow: self.outboundFlowControlWindow
|
||||
)
|
||||
}
|
||||
}
|
||||
+40
-11
@@ -30,11 +30,19 @@ protocol SendingPushPromiseState: HasFlowControlWindows {
|
||||
}
|
||||
|
||||
extension SendingPushPromiseState {
|
||||
mutating func sendPushPromise(originalStreamID: HTTP2StreamID, childStreamID: HTTP2StreamID, headers: HPACKHeaders) -> StateMachineResultWithEffect {
|
||||
return self._sendPushPromise(originalStreamID: originalStreamID, childStreamID: childStreamID, headers: headers)
|
||||
mutating func sendPushPromise(
|
||||
originalStreamID: HTTP2StreamID,
|
||||
childStreamID: HTTP2StreamID,
|
||||
headers: HPACKHeaders
|
||||
) -> StateMachineResultWithEffect {
|
||||
self._sendPushPromise(originalStreamID: originalStreamID, childStreamID: childStreamID, headers: headers)
|
||||
}
|
||||
|
||||
fileprivate mutating func _sendPushPromise(originalStreamID: HTTP2StreamID, childStreamID: HTTP2StreamID, headers: HPACKHeaders) -> StateMachineResultWithEffect {
|
||||
fileprivate mutating func _sendPushPromise(
|
||||
originalStreamID: HTTP2StreamID,
|
||||
childStreamID: HTTP2StreamID,
|
||||
headers: HPACKHeaders
|
||||
) -> StateMachineResultWithEffect {
|
||||
let validateHeaderBlock = self.headerBlockValidation == .enabled
|
||||
|
||||
// While receivePushPromise has a two step process involving creating the child stream first, here we do it the other
|
||||
@@ -42,17 +50,27 @@ extension SendingPushPromiseState {
|
||||
// we don't have to emit a frame to report the error (we just return it to the user), we don't have to have a stream
|
||||
// whose state we can modify.
|
||||
func parentStateModifier(stateMachine: inout HTTP2StreamStateMachine) -> StateMachineResultWithStreamEffect {
|
||||
return stateMachine.sendPushPromise(headers: headers, validateHeaderBlock: validateHeaderBlock)
|
||||
stateMachine.sendPushPromise(headers: headers, validateHeaderBlock: validateHeaderBlock)
|
||||
}
|
||||
|
||||
// First, however, we need to check we can push at all!
|
||||
guard self.mayPush else {
|
||||
return StateMachineResultWithEffect(result: .connectionError(underlyingError: NIOHTTP2Errors.pushInViolationOfSetting(), type: .protocolError), effect: nil)
|
||||
return StateMachineResultWithEffect(
|
||||
result: .connectionError(
|
||||
underlyingError: NIOHTTP2Errors.pushInViolationOfSetting(),
|
||||
type: .protocolError
|
||||
),
|
||||
effect: nil
|
||||
)
|
||||
}
|
||||
|
||||
do {
|
||||
let result = StateMachineResultWithEffect(
|
||||
self.streamState.modifyStreamState(streamID: originalStreamID, ignoreRecentlyReset: false, parentStateModifier),
|
||||
self.streamState.modifyStreamState(
|
||||
streamID: originalStreamID,
|
||||
ignoreRecentlyReset: false,
|
||||
parentStateModifier
|
||||
),
|
||||
inboundFlowControlWindow: self.inboundFlowControlWindow,
|
||||
outboundFlowControlWindow: self.outboundFlowControlWindow
|
||||
)
|
||||
@@ -60,23 +78,34 @@ extension SendingPushPromiseState {
|
||||
return result
|
||||
}
|
||||
let requestVerb = headers.first(name: ":method")
|
||||
try self.streamState.createLocallyPushedStream(streamID: childStreamID, localInitialWindowSize: self.localInitialWindowSize, requestVerb: requestVerb)
|
||||
try self.streamState.createLocallyPushedStream(
|
||||
streamID: childStreamID,
|
||||
localInitialWindowSize: self.localInitialWindowSize,
|
||||
requestVerb: requestVerb
|
||||
)
|
||||
return result
|
||||
} catch {
|
||||
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .protocolError), effect: nil)
|
||||
return StateMachineResultWithEffect(
|
||||
result: .connectionError(underlyingError: error, type: .protocolError),
|
||||
effect: nil
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether we may push.
|
||||
var mayPush: Bool {
|
||||
// In the case where we don't have remote settings, we have to assume the default value, in which case if we're a server we may push.
|
||||
return self.role == .server
|
||||
self.role == .server
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension SendingPushPromiseState where Self: RemotelyQuiescingState {
|
||||
mutating func sendPushPromise(originalStreamID: HTTP2StreamID, childStreamID: HTTP2StreamID, headers: HPACKHeaders) -> StateMachineResultWithEffect {
|
||||
mutating func sendPushPromise(
|
||||
originalStreamID: HTTP2StreamID,
|
||||
childStreamID: HTTP2StreamID,
|
||||
headers: HPACKHeaders
|
||||
) -> StateMachineResultWithEffect {
|
||||
// This call should never be used, but we do want to ensure that conforming types cannot enter the above method.
|
||||
// The state machine should return early in all cases where we might end up calling this function.
|
||||
preconditionFailure("Must not be called")
|
||||
@@ -85,6 +114,6 @@ extension SendingPushPromiseState where Self: RemotelyQuiescingState {
|
||||
|
||||
extension SendingPushPromiseState where Self: HasRemoteSettings {
|
||||
var mayPush: Bool {
|
||||
return self.remoteSettings.enablePush == 1 && self.role == .server
|
||||
self.remoteSettings.enablePush == 1 && self.role == .server
|
||||
}
|
||||
}
|
||||
+5
-3
@@ -26,8 +26,10 @@ extension SendingRstStreamState {
|
||||
let result = self.streamState.locallyResetStreamState(streamID: streamID) {
|
||||
$0.sendRstStream(reason: reason)
|
||||
}
|
||||
return StateMachineResultWithEffect(result,
|
||||
inboundFlowControlWindow: self.inboundFlowControlWindow,
|
||||
outboundFlowControlWindow: self.outboundFlowControlWindow)
|
||||
return StateMachineResultWithEffect(
|
||||
result,
|
||||
inboundFlowControlWindow: self.inboundFlowControlWindow,
|
||||
outboundFlowControlWindow: self.outboundFlowControlWindow
|
||||
)
|
||||
}
|
||||
}
|
||||
+13
-5
@@ -30,9 +30,15 @@ extension SendingWindowUpdateState {
|
||||
try self.inboundFlowControlWindow.windowUpdate(by: increment)
|
||||
return StateMachineResultWithEffect(result: .succeed, effect: nil)
|
||||
} catch let error where error is NIOHTTP2Errors.InvalidFlowControlWindowSize {
|
||||
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .flowControlError), effect: nil)
|
||||
return StateMachineResultWithEffect(
|
||||
result: .connectionError(underlyingError: error, type: .flowControlError),
|
||||
effect: nil
|
||||
)
|
||||
} catch let error where error is NIOHTTP2Errors.InvalidWindowIncrementSize {
|
||||
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .protocolError), effect: nil)
|
||||
return StateMachineResultWithEffect(
|
||||
result: .connectionError(underlyingError: error, type: .protocolError),
|
||||
effect: nil
|
||||
)
|
||||
} catch {
|
||||
preconditionFailure("Unexpected error: \(error)")
|
||||
}
|
||||
@@ -41,9 +47,11 @@ extension SendingWindowUpdateState {
|
||||
let result = self.streamState.modifyStreamState(streamID: streamID, ignoreRecentlyReset: false) {
|
||||
$0.sendWindowUpdate(windowIncrement: increment)
|
||||
}
|
||||
return StateMachineResultWithEffect(result,
|
||||
inboundFlowControlWindow: self.inboundFlowControlWindow,
|
||||
outboundFlowControlWindow: self.outboundFlowControlWindow)
|
||||
return StateMachineResultWithEffect(
|
||||
result,
|
||||
inboundFlowControlWindow: self.inboundFlowControlWindow,
|
||||
outboundFlowControlWindow: self.outboundFlowControlWindow
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,11 +27,15 @@ struct HTTP2SettingsState {
|
||||
private var unacknowlegedSettingsFrames: [HTTP2Settings]
|
||||
|
||||
/// A callback that is invoked whenever a setting value has changed.
|
||||
typealias OnValueChangeCallback = (_ setting: HTTP2SettingsParameter, _ oldValue: UInt32?, _ newValue: UInt32) throws -> Void
|
||||
typealias OnValueChangeCallback = (_ setting: HTTP2SettingsParameter, _ oldValue: UInt32?, _ newValue: UInt32)
|
||||
throws -> Void
|
||||
|
||||
init(localState: Bool) {
|
||||
// Create the settings dictionary, and ensure it has space for the known SETTINGS values.
|
||||
self.currentSettingsValues = [.headerTableSize: 4096, .enablePush: 1, .initialWindowSize: HTTP2SettingsState.defaultInitialWindowSize, .maxFrameSize: 1<<14]
|
||||
self.currentSettingsValues = [
|
||||
.headerTableSize: 4096, .enablePush: 1, .initialWindowSize: HTTP2SettingsState.defaultInitialWindowSize,
|
||||
.maxFrameSize: 1 << 14,
|
||||
]
|
||||
self.currentSettingsValues.reserveCapacity(8)
|
||||
|
||||
// Create space for the unacknowledged SETTINGS frame data to be stored. In general this will be empty,
|
||||
@@ -54,25 +58,25 @@ struct HTTP2SettingsState {
|
||||
/// Obtain the current value of a settings parameter.
|
||||
subscript(_ parameter: HTTP2SettingsParameter) -> UInt32? {
|
||||
get {
|
||||
return self.currentSettingsValues[parameter]
|
||||
self.currentSettingsValues[parameter]
|
||||
}
|
||||
}
|
||||
|
||||
/// The current value of SETTINGS_INITIAL_WINDOW_SIZE.
|
||||
var initialWindowSize: UInt32 {
|
||||
// We can force-unwrap here as this setting always has a value.
|
||||
return self[.initialWindowSize]!
|
||||
self[.initialWindowSize]!
|
||||
}
|
||||
|
||||
/// The current value of SETTINGS_ENABLE_PUSH.
|
||||
var enablePush: UInt32 {
|
||||
// We can force-unwrap here as this setting always has a value.
|
||||
return self[.enablePush]!
|
||||
self[.enablePush]!
|
||||
}
|
||||
|
||||
/// The current value of SETTINGS_ENABLE_CONNECT_PROTOCOL
|
||||
var enableConnectProtocol: UInt32? {
|
||||
return self[.enableConnectProtocol]
|
||||
self[.enableConnectProtocol]
|
||||
}
|
||||
|
||||
/// The default value of SETTINGS_INITIAL_WINDOW_SIZE.
|
||||
@@ -112,7 +116,7 @@ struct HTTP2SettingsState {
|
||||
/// - settings: The received settings.
|
||||
/// - onValueChange: A callback that will be invoked once for each setting change.
|
||||
mutating func receiveSettings(_ settings: HTTP2Settings, onValueChange: OnValueChangeCallback) rethrows {
|
||||
return try self.applySettings(settings, onValueChange: onValueChange)
|
||||
try self.applySettings(settings, onValueChange: onValueChange)
|
||||
}
|
||||
|
||||
/// Applies the given HTTP/2 settings to this state.
|
||||
@@ -131,6 +135,6 @@ struct HTTP2SettingsState {
|
||||
|
||||
/// Obtain an empty dummy value, suitable for using as a temporary to avoid CoW operations.
|
||||
static func dummyValue() -> HTTP2SettingsState {
|
||||
return HTTP2SettingsState()
|
||||
HTTP2SettingsState()
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,10 @@ extension HasRemoteExtendedConnectSettings where Self: HasRemoteSettings {
|
||||
}
|
||||
|
||||
extension HasRemoteSettings {
|
||||
mutating func receiveSettingsChange(_ settings: HTTP2Settings, frameEncoder: inout HTTP2FrameEncoder) -> (StateMachineResultWithEffect, PostFrameOperation) {
|
||||
mutating func receiveSettingsChange(
|
||||
_ settings: HTTP2Settings,
|
||||
frameEncoder: inout HTTP2FrameEncoder
|
||||
) -> (StateMachineResultWithEffect, PostFrameOperation) {
|
||||
// We do a little switcheroo here to avoid problems with overlapping accesses to
|
||||
// self. It's a little more complex than normal because HTTP2SettingsState has
|
||||
// two CoWable objects, and we don't want to CoW either of them, so we shove a dummy
|
||||
@@ -74,7 +77,9 @@ extension HasRemoteSettings {
|
||||
case .enableConnectProtocol:
|
||||
// Must not transition from 1 -> 0
|
||||
if originalValue == 1 && newValue == 0 {
|
||||
throw NIOHTTP2Errors.invalidSetting(setting: HTTP2Setting(parameter: setting, value: Int(newValue)))
|
||||
throw NIOHTTP2Errors.invalidSetting(
|
||||
setting: HTTP2Setting(parameter: setting, value: Int(newValue))
|
||||
)
|
||||
}
|
||||
effect.enableConnectProtocol = newValue == 1
|
||||
default:
|
||||
@@ -84,7 +89,9 @@ extension HasRemoteSettings {
|
||||
}
|
||||
return (.init(result: .succeed, effect: .remoteSettingsChanged(effect)), .sendAck)
|
||||
} catch let err where err is NIOHTTP2Errors.InvalidFlowControlWindowSize {
|
||||
return (.init(result: .connectionError(underlyingError: err, type: .flowControlError), effect: nil), .nothing)
|
||||
return (
|
||||
.init(result: .connectionError(underlyingError: err, type: .flowControlError), effect: nil), .nothing
|
||||
)
|
||||
} catch let err where err is NIOHTTP2Errors.InvalidSetting {
|
||||
return (.init(result: .connectionError(underlyingError: err, type: .protocolError), effect: nil), .nothing)
|
||||
} catch {
|
||||
|
||||
@@ -17,6 +17,6 @@
|
||||
/// This protocol should only be conformed to by states for the HTTP/2 connection state machine.
|
||||
protocol QuiescingState {
|
||||
var streamState: ConnectionStreamState { get }
|
||||
|
||||
|
||||
var quiescedByServer: Bool { get }
|
||||
}
|
||||
@@ -44,7 +44,6 @@ enum StateMachineResult {
|
||||
case succeed
|
||||
}
|
||||
|
||||
|
||||
/// Operations that may need to be performed after receiving a frame.
|
||||
enum PostFrameOperation {
|
||||
/// An appropriate ACK must be sent.
|
||||
@@ -54,7 +53,6 @@ enum PostFrameOperation {
|
||||
case nothing
|
||||
}
|
||||
|
||||
|
||||
/// An encapsulation of a state machine result along with a possible triggered state change.
|
||||
struct StateMachineResultWithEffect {
|
||||
var result: StateMachineResult
|
||||
@@ -66,12 +64,18 @@ struct StateMachineResultWithEffect {
|
||||
self.effect = effect
|
||||
}
|
||||
|
||||
init(_ streamEffect: StateMachineResultWithStreamEffect,
|
||||
inboundFlowControlWindow: HTTP2FlowControlWindow,
|
||||
outboundFlowControlWindow: HTTP2FlowControlWindow) {
|
||||
init(
|
||||
_ streamEffect: StateMachineResultWithStreamEffect,
|
||||
inboundFlowControlWindow: HTTP2FlowControlWindow,
|
||||
outboundFlowControlWindow: HTTP2FlowControlWindow
|
||||
) {
|
||||
self.result = streamEffect.result
|
||||
self.effect = streamEffect.effect.map {
|
||||
NIOHTTP2ConnectionStateChange($0, inboundFlowControlWindow: inboundFlowControlWindow, outboundFlowControlWindow: outboundFlowControlWindow)
|
||||
NIOHTTP2ConnectionStateChange(
|
||||
$0,
|
||||
inboundFlowControlWindow: inboundFlowControlWindow,
|
||||
outboundFlowControlWindow: outboundFlowControlWindow
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user