Skip to content

Commit 2d47644

Browse files
committed
Review comments
1 parent 03e5a62 commit 2d47644

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/chunkedencoding/ChunkedEncodedPublisher.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public Publisher<ByteBuffer> map(Publisher<ByteBuffer> upstream, Function<? supe
133133

134134
private ByteBuffer encodeChunk(ByteBuffer byteBuffer) {
135135
int contentLen = byteBuffer.remaining();
136-
byte[] chunkSizeHex = Integer.toHexString(contentLen).getBytes(StandardCharsets.UTF_8);
136+
byte[] chunkSizeHex = Integer.toHexString(byteBuffer.remaining()).getBytes(StandardCharsets.UTF_8);
137137

138138
List<Pair<byte[], byte[]>> chunkExtensions = this.extensions.stream()
139139
.map(e -> {
@@ -152,10 +152,18 @@ private ByteBuffer encodeChunk(ByteBuffer byteBuffer) {
152152
trailerData = Collections.emptyList();
153153
}
154154

155-
int trailerLen = trailerData.stream().mapToInt(t -> t.remaining() + 2).sum();
155+
//
156+
int trailerLen = trailerData.stream()
157+
// + 2 for each CRLF that ends the header-field
158+
.mapToInt(t -> t.remaining() + 2)
159+
.sum();
156160

157161
int encodedLen = chunkSizeHex.length + extensionsLength + CRLF.length + contentLen + trailerLen + CRLF.length;
158162

163+
if (isTrailerChunk) {
164+
encodedLen += CRLF.length;
165+
}
166+
159167
ByteBuffer encoded = ByteBuffer.allocate(encodedLen);
160168

161169
encoded.put(chunkSizeHex); // chunk-size
@@ -181,6 +189,7 @@ private ByteBuffer encodeChunk(ByteBuffer byteBuffer) {
181189
encoded.put(t);
182190
encoded.put(CRLF);
183191
});
192+
encoded.put(CRLF);
184193
}
185194

186195
encoded.flip();

core/http-auth-aws/src/test/java/software/amazon/awssdk/http/auth/aws/internal/signer/chunkedencoding/ChunkedEncodedPublisherTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,29 @@ public void setup() {
4848
CRC32.reset();
4949
}
5050

51+
@Test
52+
public void subscribe_publisherEmpty_onlyProducesTrailer() {
53+
Publisher<ByteBuffer> emptyPublisher = Flowable.empty();
54+
55+
ChunkedEncodedPublisher build = newChunkedBuilder(emptyPublisher)
56+
.addTrailer(() -> Pair.of("foo", Collections.singletonList("1")))
57+
.addTrailer(() -> Pair.of("bar", Collections.singletonList("2")))
58+
.addEmptyTrailingChunk(true)
59+
.build();
60+
61+
List<ByteBuffer> chunks = getAllElements(build);
62+
63+
assertThat(chunks.size()).isEqualTo(1);
64+
65+
String trailerAsString = StandardCharsets.UTF_8.decode(chunks.get(0)).toString();
66+
67+
assertThat(trailerAsString).isEqualTo(
68+
"0\r\n" +
69+
"foo:1\r\n" +
70+
"bar:2\r\n" +
71+
"\r\n");
72+
}
73+
5174
@Test
5275
void subscribe_trailerProviderPresent_trailerPartAdded() {
5376
TestPublisher upstream = randomPublisherOfLength(8);
@@ -121,7 +144,8 @@ void subscribe_trailerPresent_trailerFormattedCorrectly() {
121144
ByteBuffer last = chunks.get(chunks.size() - 1);
122145

123146
String expected = "0\r\n" +
124-
"foo:bar\r\n";
147+
"foo:bar\r\n" +
148+
"\r\n";
125149

126150
assertThat(chunkAsString(last)).isEqualTo(expected);
127151
}

0 commit comments

Comments
 (0)