Skip to content

Commit 70eb379

Browse files
committed
Honor content-length if present in the request
1 parent dfd5f4c commit 70eb379

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"category": "AWS SDK for Java v2",
3+
"type": "bugfix",
4+
"description": "Fix the issue where the `content-length` set on the request is not honored for streaming operations."
5+
}

core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/transform/AbstractStreamingRequestMarshaller.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ protected final void addHeaders(SdkHttpFullRequest.Builder marshalled,
5757
boolean requiresLength,
5858
boolean transferEncoding,
5959
boolean useHttp2) {
60+
61+
if (marshalled.firstMatchingHeader(CONTENT_LENGTH).isPresent()) {
62+
return;
63+
}
64+
6065
if (contentLength.isPresent()) {
6166
marshalled.putHeader(CONTENT_LENGTH, Long.toString(contentLength.get()));
6267
return;

core/sdk-core/src/test/java/software/amazon/awssdk/core/runtime/transform/AsyncStreamingRequestMarshallerTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,29 @@ public void setup() {
5353
}
5454

5555
@Test
56-
public void contentLengthHeaderIsSet_IfPresent() {
56+
public void contentLengthIsPresent_shouldNotOverride() {
57+
long contentLengthOnRequest = 1L;
58+
long contengLengthOnRequestBody = 5L;
59+
when(requestBody.contentLength()).thenReturn(Optional.of(contengLengthOnRequestBody));
60+
61+
AsyncStreamingRequestMarshaller marshaller = createMarshaller(true, true, true);
62+
63+
SdkHttpFullRequest requestWithContentLengthHeader = generateBasicRequest().toBuilder()
64+
.appendHeader(Header.CONTENT_LENGTH,
65+
String.valueOf(contentLengthOnRequest))
66+
.build();
67+
68+
69+
when(delegate.marshall(any())).thenReturn(requestWithContentLengthHeader);
70+
SdkHttpFullRequest httpFullRequest = marshaller.marshall(object);
71+
72+
assertThat(httpFullRequest.firstMatchingHeader(Header.CONTENT_LENGTH)).isPresent();
73+
assertContentLengthValue(httpFullRequest, contentLengthOnRequest);
74+
}
75+
76+
77+
@Test
78+
public void contentLengthOnRequestBody_shouldAddContentLengthHeader() {
5779
long value = 5L;
5880
when(requestBody.contentLength()).thenReturn(Optional.of(value));
5981

0 commit comments

Comments
 (0)