NIOTypedHTTPServerUpgradeHandler: propagate error and remove self on invalid HTTP ordering - #3714
Open
shoemoney wants to merge 1 commit into
Open
Conversation
…invalid HTTP ordering Motivation: When channelReadRequestPart() sees an unexpected ordering of HTTP parts, the state machine returns .failUpgradePromise(error) and the handler only fails the internal upgradeResultPromise. It never calls context.fireErrorCaught(error), so nothing downstream in the pipeline learns that anything went wrong, and it never removes itself from the pipeline, so it lingers there after the failure. The untyped sibling, HTTPServerUpgradeHandler, handles the equivalent invalidHTTPOrdering case in firstRequestHeadReceived() by firing the error down the pipeline and then removing itself (via notUpgrading()). Modifications: In NIOTypedHTTPServerUpgradeHandler.channelRead(context:requestPart:), the .failUpgradePromise case now also calls context.fireErrorCaught( error) and context.pipeline.syncOperations.removeHandler(self, promise: nil), matching the order already used by the .fireErrorCaughtAndRemoveHandler case elsewhere in the same handler. Added a test, testTypedUpgradeHandlerBarfsOnUnexpectedOrdering, that drives the typed handler directly on an EmbeddedChannel with an out-of-order HTTPServerRequestPart and asserts that both the upgrade result future fails and the handler removes itself from the pipeline. Result: The typed upgrade handler now surfaces invalid-ordering errors to the rest of the pipeline and cleans itself up, matching the behavior of the untyped HTTPServerUpgradeHandler.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
NIOTypedHTTPServerUpgradeHandler.channelRead(context:requestPart:)handles the.failUpgradePromise(error)action from the state machine like this:That's the only effect. It fails the internal
upgradeResultFuture, but nothing else happens: the error is never fired down the pipeline, and the handler never removes itself, so it just sits there in the pipeline having silently absorbed a protocol error.The untyped sibling,
HTTPServerUpgradeHandler, handles the equivalent case (an unexpected HTTP part ordering,firstRequestHeadReceivedinHTTPServerUpgradeHandler.swift) differently:notUpgrading(context:data:)ends with:So the untyped handler both fires the error down the pipeline and removes itself; the typed handler does neither.
Modifications
NIOTypedHTTPServerUpgradeHandler's.failUpgradePromisecase now also callscontext.fireErrorCaught(error)andcontext.pipeline.syncOperations.removeHandler(self, promise: nil), in the same order already used by the existing.fireErrorCaughtAndRemoveHandlercase a few lines below it in the same file.Added
testTypedUpgradeHandlerBarfsOnUnexpectedOrdering, which drives the typed handler directly on anEmbeddedChannelwith an out-of-orderHTTPServerRequestPartand asserts both thatupgradeResultFuturefails with.invalidHTTPOrderingand that the handler removes itself from the pipeline.Result
The typed upgrade handler now surfaces invalid-ordering errors to the rest of the pipeline and cleans itself up, matching
HTTPServerUpgradeHandler's existing behavior.