Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bodyLengthMissmatch error handling #490

Merged
merged 8 commits into from
Nov 24, 2021

Conversation

dnadoba
Copy link
Collaborator

@dnadoba dnadoba commented Nov 24, 2021

Motivation

If we detect that a user tries to write more bytes as initially specified, we fail the request with a HTTPClientError. bodyLengthMissmatch. However, we forgot to update our internal state that the request is failed. One way this manifests is that subsequent errors are also propagated through the stack but should be silently ignored because the request is already failed.

In addition, two compound cases in the switch statement inside of errorHappened(_:) had a where clause which did only apply to the last case but should have applied to each compound cases. This affects the bodyLengthMissmatch error handling but also all error handling because errors were sometimes treated as NIOSSLError.uncleanShutdown even if they were something completely different.

Changes

  • set state to failed if we fail the request with a .bodyLengthMissmatch error
  • duplicate where clause too each compound cases to only enter the cases body if the error is NIOSSLError.uncleanShutdown

@dnadoba dnadoba requested a review from fabianfett November 24, 2021 06:29
@dnadoba dnadoba force-pushed the dn-body-length-missmatch branch from df9b524 to e097940 Compare November 24, 2021 06:30
@dnadoba dnadoba added the 🔨 semver/patch No public API change. label Nov 24, 2021
let metadata = RequestFramingMetadata(connectionClose: false, body: .fixedSize(0))
XCTAssertEqual(state.startRequest(head: requestHead, metadata: metadata), .sendRequestHead(requestHead, startBody: false))

XCTAssertEqual(state.errorHappened(ArbitraryError()), .failRequest(ArbitraryError(), .close))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error is not included in the Equatable conformance. To make sure you will need to unwrap the enum with a guard and make the error equatable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay... but we have 18 places where we need to fix this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doing it correctly here is a start ;)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed all of them :)

because `HTTPRequestStateMachine.Action` Equatable conformance ignores the error completly
guard case .failRequest(let error, .close) = state.requestCancelled() else {
return XCTFail("unexpected action")
}
XCTAssertEqual(error as? HTTPClientError, .cancelled)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously we checked for .remoteConnectionClosed but that was probably wrong and I think we actually wanted to check for .cancel.

Copy link
Member

@fabianfett fabianfett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Thanks! Tests should pass though!

@dnadoba dnadoba merged commit 4fd1150 into swift-server:main Nov 24, 2021
@dnadoba dnadoba deleted the dn-body-length-missmatch branch November 24, 2021 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants