Skip to content

Improved Expect: 100-continue response handling in the URL connection client. #3050

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

Merged
merged 3 commits into from
Feb 23, 2022

Conversation

millems
Copy link
Contributor

@millems millems commented Feb 22, 2022

HTTPURLConnection currently throws a ProtocolException if the service returns anything other than a 100 or 417 response code to an expect-100-continue request. S3 sometimes triggers this behavior. This change catches this exception and treats it according to its response code, assuming that it can verify that no information was lost (e.g. because of a response payload being included in the response). If it still fails, it instructs the customer that the apache HTTP client could be a true solution to the problem.

@millems millems requested a review from a team as a code owner February 22, 2022 18:41
@millems millems force-pushed the millem/improve-expect-100-continue branch from fd70636 to 2b4fba5 Compare February 22, 2022 20:38
private boolean exceptionCausedBy100HandlingBug(RuntimeException e) {
return requestWasExpect100Continue() &&
e.getMessage() != null &&
e.getMessage().contains("Server rejected operation");
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Checking for ProtocolException first may prevent some unnecessary string comparisons here.

EmbeddedServer server = new EmbeddedServer(handler)) {
HttpExecuteResponse response = sendRequest(client, server);
assertThat(response.httpResponse().statusCode()).isEqualTo(500);
assertThat(response.httpResponse().firstMatchingHeader("x-amz-test-header")).hasValue("foo");
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Could also assert a warning was logged with LogCaptor.

return getAndHandle100Bug(() -> invokeSafely(connection::getErrorStream), Expect100BugHandlingBehavior.LOG);
}

private <T> Optional<T> getAndHandle100Bug(Supplier<T> supplier, Expect100BugHandlingBehavior bugBehavior) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice helper method! A small description of the bug may be helpful for posterity's sake.

@millems millems force-pushed the millem/improve-expect-100-continue branch from 8348b07 to 514ae13 Compare February 22, 2022 22:11
… client.

HTTPURLConnection currently throws a ProtocolException if the service returns anything other than a 100 or 417 response code to an expect-100-continue request. S3 sometimes triggers this behavior. This change catches this exception and treats it according to its response code, assuming that it can verify that no information was lost (e.g. because of a response payload being included in the response). If it still fails, it instructs the customer that the apache HTTP client could be a true solution to the problem.
@millems millems force-pushed the millem/improve-expect-100-continue branch from 514ae13 to adac996 Compare February 22, 2022 22:12
throw e;
}

expect100BugEncountered = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we put responseHasNoContent() before this, so that we only emit warnings when we're actually missing some potential behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. responseHasNoContent() is relatively slow to check, so I'll have to rearrange some things to ensure we don't check it twice in the success case.

private Optional<InputStream> tryGetErrorStream() {
InputStream result = invokeSafely(connection::getErrorStream);
if (result == null && expect100BugEncountered) {
log.debug(() -> "The response payload has been dropped because of a limitation of the JDK's URL Connection "
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there anyway we can trace/correlate this to a given request? Should we include the status line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since it's a sync client, they can use the thread name?

}

@Test
public void expect100ContinueWorksWithZeroContentLength500() throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Should we assert that no warnings were logged?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is that to prevent someone upgrading the log to warn?

Copy link
Contributor

Choose a reason for hiding this comment

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

I meant to assert that nothing was logged at all (if there was no missing behavior).

@millems millems enabled auto-merge (squash) February 23, 2022 00:29
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

89.6% 89.6% Coverage
0.0% 0.0% Duplication

@millems millems merged commit 03b58f9 into master Feb 23, 2022
@millems millems deleted the millem/improve-expect-100-continue branch October 19, 2022 19:35
aws-sdk-java-automation added a commit that referenced this pull request May 28, 2024
…f6f8acfeb

Pull request: release <- staging/a801662b-81c4-4e38-a626-c01f6f8acfeb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants