mirror of
https://github.com/apple/swift-nio-http2
synced 2026-11-30 05:28:45 +00:00
Validate no uppercase characters in header field names. (#84)
Motivation: RFC 7540 forbids uppercase characters in header field names, but we currently allow them. We shouldn't. This is the first in a series of patches that will validate header blocks when reasonable. As a result, it seems larger than necessary, as it adds a bunch of scaffolding for extending with further validation code. Modifications: - Added header field validation scaffolding. - Validates that header field names do not contain uppercase chars. - Allows sending RST_STREAM frame before stream setup. Result: Better header field validation.
This commit is contained in:
1 parent
70ac404a42
commit
74b8f4f110
13 files changed
+470
-63
No files matched your search
@@ -63,13 +63,15 @@ import NIOHPACK
|
||||
/// frames that just incidentally have a stream ID on them, rather than stream-scoped frames like all the others.
|
||||
struct HTTP2ConnectionStateMachine {
|
||||
/// The state required for a connection that is currently idle.
|
||||
private struct IdleConnectionState: ConnectionStateWithRole {
|
||||
private struct IdleConnectionState: ConnectionStateWithRole, ConnectionStateWithConfiguration {
|
||||
let role: ConnectionRole
|
||||
var headerBlockValidation: ValidationState
|
||||
}
|
||||
|
||||
/// The state required for a connection that has sent a connection preface.
|
||||
private struct PrefaceSentState: ConnectionStateWithRole, MaySendFrames, HasLocalSettings, HasFlowControlWindows {
|
||||
private struct PrefaceSentState: ConnectionStateWithRole, ConnectionStateWithConfiguration, MaySendFrames, HasLocalSettings, HasFlowControlWindows {
|
||||
let role: ConnectionRole
|
||||
var headerBlockValidation: ValidationState
|
||||
var localSettings: HTTP2SettingsState
|
||||
var streamState: ConnectionStreamState
|
||||
var inboundFlowControlWindow: HTTP2FlowControlWindow
|
||||
@@ -85,6 +87,7 @@ struct HTTP2ConnectionStateMachine {
|
||||
|
||||
init(fromIdle idleState: IdleConnectionState, localSettings settings: HTTP2SettingsState) {
|
||||
self.role = idleState.role
|
||||
self.headerBlockValidation = idleState.headerBlockValidation
|
||||
self.localSettings = settings
|
||||
self.streamState = ConnectionStreamState()
|
||||
|
||||
@@ -94,8 +97,9 @@ struct HTTP2ConnectionStateMachine {
|
||||
}
|
||||
|
||||
/// The state required for a connection that has received a connection preface.
|
||||
private struct PrefaceReceivedState: ConnectionStateWithRole, MayReceiveFrames, HasRemoteSettings, HasFlowControlWindows {
|
||||
private struct PrefaceReceivedState: ConnectionStateWithRole, ConnectionStateWithConfiguration, MayReceiveFrames, HasRemoteSettings, HasFlowControlWindows {
|
||||
let role: ConnectionRole
|
||||
var headerBlockValidation: ValidationState
|
||||
var remoteSettings: HTTP2SettingsState
|
||||
var streamState: ConnectionStreamState
|
||||
var inboundFlowControlWindow: HTTP2FlowControlWindow
|
||||
@@ -111,6 +115,7 @@ struct HTTP2ConnectionStateMachine {
|
||||
|
||||
init(fromIdle idleState: IdleConnectionState, remoteSettings settings: HTTP2SettingsState) {
|
||||
self.role = idleState.role
|
||||
self.headerBlockValidation = idleState.headerBlockValidation
|
||||
self.remoteSettings = settings
|
||||
self.streamState = ConnectionStreamState()
|
||||
|
||||
@@ -120,8 +125,9 @@ struct HTTP2ConnectionStateMachine {
|
||||
}
|
||||
|
||||
/// The state required for a connection that is active.
|
||||
private struct ActiveConnectionState: ConnectionStateWithRole, MaySendFrames, MayReceiveFrames, HasLocalSettings, HasRemoteSettings, HasFlowControlWindows {
|
||||
private struct ActiveConnectionState: ConnectionStateWithRole, ConnectionStateWithConfiguration, MaySendFrames, MayReceiveFrames, HasLocalSettings, HasRemoteSettings, HasFlowControlWindows {
|
||||
let role: ConnectionRole
|
||||
var headerBlockValidation: ValidationState
|
||||
var localSettings: HTTP2SettingsState
|
||||
var remoteSettings: HTTP2SettingsState
|
||||
var streamState: ConnectionStreamState
|
||||
@@ -138,6 +144,7 @@ struct HTTP2ConnectionStateMachine {
|
||||
|
||||
init(fromPrefaceReceived state: PrefaceReceivedState, localSettings settings: HTTP2SettingsState) {
|
||||
self.role = state.role
|
||||
self.headerBlockValidation = state.headerBlockValidation
|
||||
self.remoteSettings = state.remoteSettings
|
||||
self.streamState = state.streamState
|
||||
self.localSettings = settings
|
||||
@@ -148,6 +155,7 @@ struct HTTP2ConnectionStateMachine {
|
||||
|
||||
init(fromPrefaceSent state: PrefaceSentState, remoteSettings settings: HTTP2SettingsState) {
|
||||
self.role = state.role
|
||||
self.headerBlockValidation = state.headerBlockValidation
|
||||
self.localSettings = state.localSettings
|
||||
self.streamState = state.streamState
|
||||
self.remoteSettings = settings
|
||||
@@ -159,8 +167,9 @@ struct HTTP2ConnectionStateMachine {
|
||||
|
||||
/// The state required for a connection that is quiescing, but where the local peer has not yet sent its
|
||||
/// preface.
|
||||
private struct QuiescingPrefaceReceivedState: ConnectionStateWithRole, RemotelyQuiescingState, MayReceiveFrames, HasRemoteSettings, QuiescingState, HasFlowControlWindows {
|
||||
private struct QuiescingPrefaceReceivedState: ConnectionStateWithRole, ConnectionStateWithConfiguration, RemotelyQuiescingState, MayReceiveFrames, HasRemoteSettings, QuiescingState, HasFlowControlWindows {
|
||||
let role: ConnectionRole
|
||||
var headerBlockValidation: ValidationState
|
||||
var remoteSettings: HTTP2SettingsState
|
||||
var streamState: ConnectionStreamState
|
||||
var inboundFlowControlWindow: HTTP2FlowControlWindow
|
||||
@@ -182,6 +191,7 @@ struct HTTP2ConnectionStateMachine {
|
||||
|
||||
init(fromPrefaceReceived state: PrefaceReceivedState, lastStreamID: HTTP2StreamID) {
|
||||
self.role = state.role
|
||||
self.headerBlockValidation = state.headerBlockValidation
|
||||
self.remoteSettings = state.remoteSettings
|
||||
self.streamState = state.streamState
|
||||
self.inboundFlowControlWindow = state.inboundFlowControlWindow
|
||||
@@ -193,8 +203,9 @@ struct HTTP2ConnectionStateMachine {
|
||||
|
||||
/// The state required for a connection that is quiescing, but where the remote peer has not yet sent its
|
||||
/// preface.
|
||||
private struct QuiescingPrefaceSentState: ConnectionStateWithRole, LocallyQuiescingState, MaySendFrames, HasLocalSettings, QuiescingState, HasFlowControlWindows {
|
||||
private struct QuiescingPrefaceSentState: ConnectionStateWithRole, ConnectionStateWithConfiguration, LocallyQuiescingState, MaySendFrames, HasLocalSettings, QuiescingState, HasFlowControlWindows {
|
||||
let role: ConnectionRole
|
||||
var headerBlockValidation: ValidationState
|
||||
var localSettings: HTTP2SettingsState
|
||||
var streamState: ConnectionStreamState
|
||||
var inboundFlowControlWindow: HTTP2FlowControlWindow
|
||||
@@ -216,6 +227,7 @@ struct HTTP2ConnectionStateMachine {
|
||||
|
||||
init(fromPrefaceSent state: PrefaceSentState, lastStreamID: HTTP2StreamID) {
|
||||
self.role = state.role
|
||||
self.headerBlockValidation = state.headerBlockValidation
|
||||
self.localSettings = state.localSettings
|
||||
self.streamState = state.streamState
|
||||
self.inboundFlowControlWindow = state.inboundFlowControlWindow
|
||||
@@ -226,8 +238,9 @@ struct HTTP2ConnectionStateMachine {
|
||||
}
|
||||
|
||||
/// The state required for a connection that is quiescing due to the remote peer quiescing the connection.
|
||||
private struct RemotelyQuiescedState: ConnectionStateWithRole, RemotelyQuiescingState, MayReceiveFrames, MaySendFrames, HasLocalSettings, HasRemoteSettings, QuiescingState, HasFlowControlWindows {
|
||||
private struct RemotelyQuiescedState: ConnectionStateWithRole, ConnectionStateWithConfiguration, RemotelyQuiescingState, MayReceiveFrames, MaySendFrames, HasLocalSettings, HasRemoteSettings, QuiescingState, HasFlowControlWindows {
|
||||
let role: ConnectionRole
|
||||
var headerBlockValidation: ValidationState
|
||||
var localSettings: HTTP2SettingsState
|
||||
var remoteSettings: HTTP2SettingsState
|
||||
var streamState: ConnectionStreamState
|
||||
@@ -250,6 +263,7 @@ struct HTTP2ConnectionStateMachine {
|
||||
|
||||
init(fromActive state: ActiveConnectionState, lastLocalStreamID streamID: HTTP2StreamID) {
|
||||
self.role = state.role
|
||||
self.headerBlockValidation = state.headerBlockValidation
|
||||
self.localSettings = state.localSettings
|
||||
self.remoteSettings = state.remoteSettings
|
||||
self.streamState = state.streamState
|
||||
@@ -260,6 +274,7 @@ struct HTTP2ConnectionStateMachine {
|
||||
|
||||
init(fromQuiescingPrefaceReceived state: QuiescingPrefaceReceivedState, localSettings settings: HTTP2SettingsState) {
|
||||
self.role = state.role
|
||||
self.headerBlockValidation = state.headerBlockValidation
|
||||
self.remoteSettings = state.remoteSettings
|
||||
self.localSettings = settings
|
||||
self.streamState = state.streamState
|
||||
@@ -270,8 +285,9 @@ struct HTTP2ConnectionStateMachine {
|
||||
}
|
||||
|
||||
/// The state required for a connection that is quiescing due to the local user quiescing the connection.
|
||||
private struct LocallyQuiescedState: ConnectionStateWithRole, LocallyQuiescingState, MaySendFrames, MayReceiveFrames, HasLocalSettings, HasRemoteSettings, QuiescingState, HasFlowControlWindows {
|
||||
private struct LocallyQuiescedState: ConnectionStateWithRole, ConnectionStateWithConfiguration, LocallyQuiescingState, MaySendFrames, MayReceiveFrames, HasLocalSettings, HasRemoteSettings, QuiescingState, HasFlowControlWindows {
|
||||
let role: ConnectionRole
|
||||
var headerBlockValidation: ValidationState
|
||||
var localSettings: HTTP2SettingsState
|
||||
var remoteSettings: HTTP2SettingsState
|
||||
var streamState: ConnectionStreamState
|
||||
@@ -294,6 +310,7 @@ struct HTTP2ConnectionStateMachine {
|
||||
|
||||
init(fromActive state: ActiveConnectionState, lastRemoteStreamID streamID: HTTP2StreamID) {
|
||||
self.role = state.role
|
||||
self.headerBlockValidation = state.headerBlockValidation
|
||||
self.localSettings = state.localSettings
|
||||
self.remoteSettings = state.remoteSettings
|
||||
self.streamState = state.streamState
|
||||
@@ -304,6 +321,7 @@ struct HTTP2ConnectionStateMachine {
|
||||
|
||||
init(fromQuiescingPrefaceSent state: QuiescingPrefaceSentState, remoteSettings settings: HTTP2SettingsState) {
|
||||
self.role = state.role
|
||||
self.headerBlockValidation = state.headerBlockValidation
|
||||
self.localSettings = state.localSettings
|
||||
self.remoteSettings = settings
|
||||
self.streamState = state.streamState
|
||||
@@ -314,8 +332,9 @@ struct HTTP2ConnectionStateMachine {
|
||||
}
|
||||
|
||||
/// The state required for a connection that is quiescing due to both peers sending GOAWAY.
|
||||
private struct BothQuiescingState: ConnectionStateWithRole, LocallyQuiescingState, RemotelyQuiescingState, MaySendFrames, MayReceiveFrames, HasLocalSettings, HasRemoteSettings, QuiescingState, HasFlowControlWindows {
|
||||
private struct BothQuiescingState: ConnectionStateWithRole, ConnectionStateWithConfiguration, LocallyQuiescingState, RemotelyQuiescingState, MaySendFrames, MayReceiveFrames, HasLocalSettings, HasRemoteSettings, QuiescingState, HasFlowControlWindows {
|
||||
let role: ConnectionRole
|
||||
var headerBlockValidation: ValidationState
|
||||
var localSettings: HTTP2SettingsState
|
||||
var remoteSettings: HTTP2SettingsState
|
||||
var streamState: ConnectionStreamState
|
||||
@@ -338,6 +357,7 @@ struct HTTP2ConnectionStateMachine {
|
||||
|
||||
init(fromRemotelyQuiesced state: RemotelyQuiescedState, lastRemoteStreamID streamID: HTTP2StreamID) {
|
||||
self.role = state.role
|
||||
self.headerBlockValidation = state.headerBlockValidation
|
||||
self.localSettings = state.localSettings
|
||||
self.remoteSettings = state.remoteSettings
|
||||
self.streamState = state.streamState
|
||||
@@ -350,6 +370,7 @@ struct HTTP2ConnectionStateMachine {
|
||||
|
||||
init(fromLocallyQuiesced state: LocallyQuiescedState, lastLocalStreamID streamID: HTTP2StreamID) {
|
||||
self.role = state.role
|
||||
self.headerBlockValidation = state.headerBlockValidation
|
||||
self.localSettings = state.localSettings
|
||||
self.remoteSettings = state.remoteSettings
|
||||
self.streamState = state.streamState
|
||||
@@ -362,28 +383,32 @@ struct HTTP2ConnectionStateMachine {
|
||||
}
|
||||
|
||||
/// The state required for a connection that has completely quiesced.
|
||||
private struct FullyQuiescedState: ConnectionStateWithRole, LocallyQuiescingState, RemotelyQuiescingState, SendAndReceiveGoawayState {
|
||||
private struct FullyQuiescedState: ConnectionStateWithRole, ConnectionStateWithConfiguration, LocallyQuiescingState, RemotelyQuiescingState, SendAndReceiveGoawayState {
|
||||
let role: ConnectionRole
|
||||
var headerBlockValidation: ValidationState
|
||||
var streamState: ConnectionStreamState
|
||||
var lastLocalStreamID: HTTP2StreamID
|
||||
var lastRemoteStreamID: HTTP2StreamID
|
||||
|
||||
init<PreviousState: LocallyQuiescingState & RemotelyQuiescingState & SendAndReceiveGoawayState & ConnectionStateWithRole>(previousState: PreviousState) {
|
||||
init<PreviousState: LocallyQuiescingState & RemotelyQuiescingState & SendAndReceiveGoawayState & ConnectionStateWithRole & ConnectionStateWithConfiguration>(previousState: PreviousState) {
|
||||
self.role = previousState.role
|
||||
self.headerBlockValidation = previousState.headerBlockValidation
|
||||
self.streamState = previousState.streamState
|
||||
self.lastLocalStreamID = previousState.lastLocalStreamID
|
||||
self.lastRemoteStreamID = previousState.lastRemoteStreamID
|
||||
}
|
||||
|
||||
init<PreviousState: LocallyQuiescingState & SendAndReceiveGoawayState & ConnectionStateWithRole>(previousState: PreviousState) {
|
||||
init<PreviousState: LocallyQuiescingState & SendAndReceiveGoawayState & ConnectionStateWithRole & ConnectionStateWithConfiguration>(previousState: PreviousState) {
|
||||
self.role = previousState.role
|
||||
self.headerBlockValidation = previousState.headerBlockValidation
|
||||
self.streamState = previousState.streamState
|
||||
self.lastLocalStreamID = .maxID
|
||||
self.lastRemoteStreamID = previousState.lastRemoteStreamID
|
||||
}
|
||||
|
||||
init<PreviousState: RemotelyQuiescingState & SendAndReceiveGoawayState & ConnectionStateWithRole>(previousState: PreviousState) {
|
||||
init<PreviousState: RemotelyQuiescingState & SendAndReceiveGoawayState & ConnectionStateWithRole & ConnectionStateWithConfiguration>(previousState: PreviousState) {
|
||||
self.role = previousState.role
|
||||
self.headerBlockValidation = previousState.headerBlockValidation
|
||||
self.streamState = previousState.streamState
|
||||
self.lastLocalStreamID = previousState.lastLocalStreamID
|
||||
self.lastRemoteStreamID = .maxID
|
||||
@@ -445,10 +470,16 @@ struct HTTP2ConnectionStateMachine {
|
||||
case client
|
||||
}
|
||||
|
||||
/// The state of a specific validation option.
|
||||
enum ValidationState {
|
||||
case enabled
|
||||
case disabled
|
||||
}
|
||||
|
||||
private var state: State
|
||||
|
||||
init(role: ConnectionRole) {
|
||||
self.state = .idle(.init(role: role))
|
||||
init(role: ConnectionRole, headerBlockValidation: ValidationState = .enabled) {
|
||||
self.state = .idle(.init(role: role, headerBlockValidation: headerBlockValidation))
|
||||
}
|
||||
|
||||
/// Whether this connection is closed.
|
||||
@@ -1293,7 +1324,7 @@ extension HTTP2ConnectionStateMachine {
|
||||
//
|
||||
// We should only call this when a server has quiesced the connection. As long as only the client has quiesced the
|
||||
// connection more work can always be done.
|
||||
private mutating func closeIfNeeded<State: QuiescingState & LocallyQuiescingState & RemotelyQuiescingState & SendAndReceiveGoawayState & ConnectionStateWithRole>(_ state: State) {
|
||||
private mutating func closeIfNeeded<State: QuiescingState & LocallyQuiescingState & RemotelyQuiescingState & SendAndReceiveGoawayState & ConnectionStateWithRole & ConnectionStateWithConfiguration>(_ state: State) {
|
||||
if state.quiescedByServer && state.streamState.openStreams == 0 {
|
||||
self.state = .fullyQuiesced(.init(previousState: state))
|
||||
}
|
||||
@@ -1303,7 +1334,7 @@ extension HTTP2ConnectionStateMachine {
|
||||
//
|
||||
// We should only call this when a server has quiesced the connection. As long as only the client has quiesced the
|
||||
// connection more work can always be done.
|
||||
private mutating func closeIfNeeded<State: QuiescingState & LocallyQuiescingState & SendAndReceiveGoawayState & ConnectionStateWithRole>(_ state: State) {
|
||||
private mutating func closeIfNeeded<State: QuiescingState & LocallyQuiescingState & SendAndReceiveGoawayState & ConnectionStateWithRole & ConnectionStateWithConfiguration>(_ state: State) {
|
||||
if state.quiescedByServer && state.streamState.openStreams == 0 {
|
||||
self.state = .fullyQuiesced(.init(previousState: state))
|
||||
}
|
||||
@@ -1313,7 +1344,7 @@ extension HTTP2ConnectionStateMachine {
|
||||
//
|
||||
// We should only call this when a server has quiesced the connection. As long as only the client has quiesced the
|
||||
// connection more work can always be done.
|
||||
private mutating func closeIfNeeded<State: QuiescingState & RemotelyQuiescingState & SendAndReceiveGoawayState & ConnectionStateWithRole>(_ state: State) {
|
||||
private mutating func closeIfNeeded<State: QuiescingState & RemotelyQuiescingState & SendAndReceiveGoawayState & ConnectionStateWithRole & ConnectionStateWithConfiguration>(_ state: State) {
|
||||
if state.quiescedByServer && state.streamState.openStreams == 0 {
|
||||
self.state = .fullyQuiesced(.init(previousState: state))
|
||||
}
|
||||
@@ -1353,3 +1384,8 @@ extension ConnectionStateWithRole {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A simple protocol that provides helpers that apply to all connection states that have configuration.
|
||||
private protocol ConnectionStateWithConfiguration {
|
||||
var headerBlockValidation: HTTP2ConnectionStateMachine.ValidationState { get }
|
||||
}
|
||||
+8
-3
@@ -20,6 +20,8 @@ import NIOHPACK
|
||||
protocol ReceivingHeadersState: HasFlowControlWindows {
|
||||
var role: HTTP2ConnectionStateMachine.ConnectionRole { get }
|
||||
|
||||
var headerBlockValidation: HTTP2ConnectionStateMachine.ValidationState { get }
|
||||
|
||||
var streamState: ConnectionStreamState { get set }
|
||||
|
||||
var localInitialWindowSize: UInt32 { get }
|
||||
@@ -31,11 +33,12 @@ extension ReceivingHeadersState {
|
||||
/// Called when we receive a HEADERS frame in this state.
|
||||
mutating func receiveHeaders(streamID: HTTP2StreamID, headers: HPACKHeaders, isEndStreamSet endStream: Bool) -> StateMachineResultWithEffect {
|
||||
let result: StateMachineResultWithStreamEffect
|
||||
let validateHeaderBlock = self.headerBlockValidation == .enabled
|
||||
|
||||
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, isEndStreamSet: endStream)
|
||||
$0.receiveHeaders(headers: headers, validateHeaderBlock: validateHeaderBlock, isEndStreamSet: endStream)
|
||||
}
|
||||
} catch {
|
||||
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .protocolError), effect: nil)
|
||||
@@ -43,7 +46,7 @@ extension ReceivingHeadersState {
|
||||
} 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, isEndStreamSet: endStream)
|
||||
$0.receiveHeaders(headers: headers, validateHeaderBlock: validateHeaderBlock, isEndStreamSet: endStream)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,13 +62,15 @@ extension ReceivingHeadersState where Self: LocallyQuiescingState {
|
||||
/// 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 {
|
||||
let validateHeaderBlock = self.headerBlockValidation == .enabled
|
||||
|
||||
if streamID.mayBeInitiatedBy(.client) && streamID > self.lastRemoteStreamID {
|
||||
return StateMachineResultWithEffect(result: .ignoreFrame, effect: nil)
|
||||
}
|
||||
|
||||
// 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, isEndStreamSet: endStream)
|
||||
$0.receiveHeaders(headers: headers, validateHeaderBlock: validateHeaderBlock, isEndStreamSet: endStream)
|
||||
}
|
||||
return StateMachineResultWithEffect(result, connectionState: self)
|
||||
}
|
||||
|
||||
+5
-1
@@ -20,6 +20,8 @@ import NIOHPACK
|
||||
protocol ReceivingPushPromiseState: HasFlowControlWindows {
|
||||
var role: HTTP2ConnectionStateMachine.ConnectionRole { get }
|
||||
|
||||
var headerBlockValidation: HTTP2ConnectionStateMachine.ValidationState { get }
|
||||
|
||||
var streamState: ConnectionStreamState { get set }
|
||||
|
||||
var remoteInitialWindowSize: UInt32 { get }
|
||||
@@ -47,12 +49,14 @@ extension ReceivingPushPromiseState {
|
||||
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)
|
||||
$0.receivePushPromise(headers: headers, validateHeaderBlock: validateHeaderBlock)
|
||||
}
|
||||
return StateMachineResultWithEffect(result, connectionState: self)
|
||||
} catch {
|
||||
|
||||
+8
-3
@@ -21,6 +21,8 @@ import NIOHPACK
|
||||
protocol SendingHeadersState: HasFlowControlWindows {
|
||||
var role: HTTP2ConnectionStateMachine.ConnectionRole { get }
|
||||
|
||||
var headerBlockValidation: HTTP2ConnectionStateMachine.ValidationState { get }
|
||||
|
||||
var streamState: ConnectionStreamState { get set }
|
||||
|
||||
var localInitialWindowSize: UInt32 { get }
|
||||
@@ -32,6 +34,7 @@ extension SendingHeadersState {
|
||||
/// Called when we send a HEADERS frame in this state.
|
||||
mutating func sendHeaders(streamID: HTTP2StreamID, headers: HPACKHeaders, isEndStreamSet endStream: Bool) -> StateMachineResultWithEffect {
|
||||
let result: StateMachineResultWithStreamEffect
|
||||
let validateHeaderBlock = self.headerBlockValidation == .enabled
|
||||
|
||||
if self.role == .client && streamID.mayBeInitiatedBy(.client) {
|
||||
do {
|
||||
@@ -39,7 +42,7 @@ extension SendingHeadersState {
|
||||
localRole: .client,
|
||||
localInitialWindowSize: self.localInitialWindowSize,
|
||||
remoteInitialWindowSize: self.remoteInitialWindowSize) {
|
||||
$0.sendHeaders(headers: headers, isEndStreamSet: endStream)
|
||||
$0.sendHeaders(headers: headers, validateHeaderBlock: validateHeaderBlock, isEndStreamSet: endStream)
|
||||
}
|
||||
} catch {
|
||||
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .protocolError), effect: nil)
|
||||
@@ -47,7 +50,7 @@ extension SendingHeadersState {
|
||||
} 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, isEndStreamSet: endStream)
|
||||
$0.sendHeaders(headers: headers, validateHeaderBlock: validateHeaderBlock, isEndStreamSet: endStream)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,8 +63,10 @@ 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 {
|
||||
let validateHeaderBlock = self.headerBlockValidation == .enabled
|
||||
|
||||
let result = self.streamState.modifyStreamState(streamID: streamID, ignoreRecentlyReset: false) {
|
||||
$0.sendHeaders(headers: headers, isEndStreamSet: endStream)
|
||||
$0.sendHeaders(headers: headers, validateHeaderBlock: validateHeaderBlock, isEndStreamSet: endStream)
|
||||
}
|
||||
return StateMachineResultWithEffect(result, connectionState: self)
|
||||
}
|
||||
|
||||
+5
-1
@@ -18,6 +18,8 @@ import NIOHPACK
|
||||
///
|
||||
/// This protocol should only be conformed to by states for the HTTP/2 connection state machine.
|
||||
protocol SendingPushPromiseState: HasFlowControlWindows {
|
||||
var headerBlockValidation: HTTP2ConnectionStateMachine.ValidationState { get }
|
||||
|
||||
var streamState: ConnectionStreamState { get set }
|
||||
|
||||
var localInitialWindowSize: UInt32 { get }
|
||||
@@ -31,12 +33,14 @@ extension SendingPushPromiseState {
|
||||
}
|
||||
|
||||
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
|
||||
// way around. This is because we don't want to bother creating a child stream if the headers aren't valid, and because
|
||||
// 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)
|
||||
return stateMachine.sendPushPromise(headers: headers, validateHeaderBlock: validateHeaderBlock)
|
||||
}
|
||||
|
||||
// First, however, we need to check we can push at all!
|
||||
|
||||
Reference in new issue
Block a user