Skip to content

Support zero-length chunked-encoded responses from XML services. #2964

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 1 commit into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/next-release/bugfix-AWSSDKforJavav2-2c7120a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"category": "AWS SDK for Java v2",
"contributor": "",
"type": "bugfix",
"description": "Do not fail with a parsing error when receiving 0-length chunk-encoded responses for XML services."
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

package software.amazon.awssdk.protocols.xml.internal.unmarshall;

import static software.amazon.awssdk.http.Header.CONTENT_LENGTH;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.util.Optional;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.core.SdkField;
Expand Down Expand Up @@ -45,27 +47,42 @@ private XmlResponseParserUtils() {
* @return A parsed XML document or an empty XML document if no payload/contents were found in the response.
*/
public static XmlElement parse(SdkPojo sdkPojo, SdkHttpFullResponse response) {

try {
Optional<AbortableInputStream> responseContent = response.content();

// In some cases the responseContent is present but empty, so when we are not expecting a body we should
// not attempt to parse it even if the body appears to be present.
if ((!response.isSuccessful() || hasPayloadMembers(sdkPojo)) && responseContent.isPresent() &&
!contentLengthZero(response) && !getBlobTypePayloadMemberToUnmarshal(sdkPojo).isPresent()) {
return XmlDomParser.parse(responseContent.get());
} else {
if (!responseContent.isPresent() ||
(response.isSuccessful() && !hasPayloadMembers(sdkPojo)) ||
getBlobTypePayloadMemberToUnmarshal(sdkPojo).isPresent()) {
return XmlElement.empty();
}

// Make sure there is content in the stream before passing it to the parser.
InputStream content = ensureMarkSupported(responseContent.get());
content.mark(2);
if (content.read() == -1) {
return XmlElement.empty();
}
content.reset();

return XmlDomParser.parse(content);
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (RuntimeException e) {
if (response.isSuccessful()) {
throw e;
}

return XmlElement.empty();
}
}

private static InputStream ensureMarkSupported(AbortableInputStream content) {
if (content.markSupported()) {
return content;
}

return new BufferedInputStream(content);
}

/**
* Gets the Member which is a Payload and which is of Blob Type.
* @param sdkPojo
Expand All @@ -85,10 +102,4 @@ private static boolean hasPayloadMembers(SdkPojo sdkPojo) {
return sdkPojo.sdkFields().stream()
.anyMatch(f -> f.location() == MarshallLocation.PAYLOAD);
}

private static boolean contentLengthZero(SdkHttpFullResponse response) {
return response.firstMatchingHeader(CONTENT_LENGTH).map(l -> Long.parseLong(l) == 0).orElse(false);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.services.s3.functionaltests;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import java.net.URI;
import org.junit.Rule;
import org.junit.Test;
import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;

public class EmptyResponseTest {
@Rule
public WireMockRule mockServer = new WireMockRule(0);

@Test
public void emptyChunkedEncodingResponseWorks() {
stubFor(get(anyUrl())
.willReturn(aResponse().withStatus(200)
.withHeader("Transfer-Encoding", "chunked")));

S3Client client = S3Client.builder()
.endpointOverride(URI.create("http://localhost:" + mockServer.port()))
.region(Region.US_WEST_2)
.credentialsProvider(AnonymousCredentialsProvider.create())
.build();

client.listBuckets(); // Should not fail
}
}