-
Notifications
You must be signed in to change notification settings - Fork 974
Support trailers in chunked publisher #6264
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
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,6 +51,8 @@ | |
| * chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-val ] ) | ||
| * chunk-ext-name = token | ||
| * chunk-ext-val = token / quoted-string | ||
| * | ||
| * trailer-part = *( header-field CRLF ) | ||
| * </pre> | ||
| * | ||
| * @see ChunkedEncodedInputStream | ||
|
|
@@ -60,9 +62,12 @@ public class ChunkedEncodedPublisher implements Publisher<ByteBuffer> { | |
| private static final byte[] CRLF = {'\r', '\n'}; | ||
| private static final byte SEMICOLON = ';'; | ||
| private static final byte EQUALS = '='; | ||
| private static final byte COLON = ':'; | ||
| private static final byte COMMA = ','; | ||
|
|
||
| private final Publisher<ByteBuffer> wrapped; | ||
| private final List<ChunkExtensionProvider> extensions = new ArrayList<>(); | ||
| private final List<TrailerProvider> trailers = new ArrayList<>(); | ||
| private final int chunkSize; | ||
| private ByteBuffer chunkBuffer; | ||
| private final boolean addEmptyTrailingChunk; | ||
|
|
@@ -71,6 +76,7 @@ public ChunkedEncodedPublisher(Builder b) { | |
| this.wrapped = b.publisher; | ||
| this.chunkSize = b.chunkSize; | ||
| this.extensions.addAll(b.extensions); | ||
| this.trailers.addAll(b.trailers); | ||
| this.addEmptyTrailingChunk = b.addEmptyTrailingChunk; | ||
| } | ||
|
|
||
|
|
@@ -125,7 +131,6 @@ public Publisher<ByteBuffer> map(Publisher<ByteBuffer> upstream, Function<? supe | |
| return subscriber -> upstream.subscribe(MappingSubscriber.create(subscriber, mapper)); | ||
| } | ||
|
|
||
| // TODO: Trailing checksum | ||
| private ByteBuffer encodeChunk(ByteBuffer byteBuffer) { | ||
| int contentLen = byteBuffer.remaining(); | ||
| byte[] chunkSizeHex = Integer.toHexString(contentLen).getBytes(StandardCharsets.UTF_8); | ||
|
|
@@ -138,24 +143,46 @@ private ByteBuffer encodeChunk(ByteBuffer byteBuffer) { | |
|
|
||
| int extensionsLength = calculateExtensionsLength(chunkExtensions); | ||
|
|
||
| int encodedLen = chunkSizeHex.length + extensionsLength + CRLF.length + contentLen + CRLF.length; | ||
| boolean isTrailerChunk = contentLen == 0; | ||
|
|
||
| List<ByteBuffer> trailerData; | ||
| if (isTrailerChunk) { | ||
| trailerData = getTrailerData(); | ||
| } else { | ||
| trailerData = Collections.emptyList(); | ||
| } | ||
|
|
||
| int trailerLen = trailerData.stream().mapToInt(t -> t.remaining() + 2).sum(); | ||
|
||
|
|
||
| int encodedLen = chunkSizeHex.length + extensionsLength + CRLF.length + contentLen + trailerLen + CRLF.length; | ||
|
|
||
| ByteBuffer encoded = ByteBuffer.allocate(encodedLen); | ||
| encoded.put(chunkSizeHex); | ||
|
|
||
| chunkExtensions.forEach(p -> { | ||
| encoded.put(chunkSizeHex); // chunk-size | ||
| chunkExtensions.forEach(p -> { // chunk-ext | ||
| encoded.put(SEMICOLON); | ||
| encoded.put(p.left()); | ||
| if (p.right() != null && p.right().length > 0) { | ||
| encoded.put(EQUALS); | ||
| encoded.put(p.right()); | ||
| } | ||
| }); | ||
|
|
||
| encoded.put(CRLF); | ||
| encoded.put(byteBuffer); | ||
| encoded.put(CRLF); | ||
|
|
||
| // chunk-data | ||
| if (byteBuffer.hasRemaining()) { | ||
| encoded.put(byteBuffer); | ||
| encoded.put(CRLF); | ||
| } | ||
|
|
||
| if (isTrailerChunk) { | ||
| // trailer-part | ||
| trailerData.forEach(t -> { | ||
| encoded.put(t); | ||
| encoded.put(CRLF); | ||
| }); | ||
| } | ||
|
|
||
| encoded.flip(); | ||
|
|
||
| return encoded; | ||
|
|
@@ -174,6 +201,41 @@ private int calculateExtensionsLength(List<Pair<byte[], byte[]>> chunkExtensions | |
| }).sum(); | ||
| } | ||
|
|
||
| private List<ByteBuffer> getTrailerData() { | ||
| List<ByteBuffer> data = new ArrayList<>(); | ||
|
|
||
| for (TrailerProvider provider : trailers) { | ||
dagnir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Pair<String, List<String>> trailer = provider.get(); | ||
|
|
||
| byte[] key = trailer.left().getBytes(StandardCharsets.UTF_8); | ||
| List<byte[]> values = trailer.right() | ||
| .stream().map(v -> v.getBytes(StandardCharsets.UTF_8)) | ||
| .collect(Collectors.toList()); | ||
| int valuesLen = values.stream().mapToInt(v -> v.length).sum(); | ||
| // name:value1,value2,.. | ||
| int size = key.length | ||
| + 1 // colon | ||
| + valuesLen | ||
| + values.size() - 1; // commas | ||
dagnir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ByteBuffer trailerData = ByteBuffer.allocate(size); | ||
|
|
||
| trailerData.put(key); | ||
| trailerData.put(COLON); | ||
|
|
||
| for (int i = 0; i < values.size(); ++i) { | ||
| trailerData.put(values.get(i)); | ||
| if (i + 1 != values.size()) { | ||
| trailerData.put(COMMA); | ||
| } | ||
| } | ||
|
|
||
| trailerData.flip(); | ||
| data.add(trailerData); | ||
| } | ||
| return data; | ||
| } | ||
|
|
||
| private class ChunkingSubscriber extends DelegatingSubscriber<ByteBuffer, Iterable<ByteBuffer>> { | ||
| protected ChunkingSubscriber(Subscriber<? super Iterable<ByteBuffer>> subscriber) { | ||
| super(subscriber); | ||
|
|
@@ -222,6 +284,7 @@ public static class Builder { | |
| private int chunkSize; | ||
| private boolean addEmptyTrailingChunk; | ||
| private final List<ChunkExtensionProvider> extensions = new ArrayList<>(); | ||
| private final List<TrailerProvider> trailers = new ArrayList<>(); | ||
|
|
||
| public Builder publisher(Publisher<ByteBuffer> publisher) { | ||
| this.publisher = publisher; | ||
|
|
@@ -243,6 +306,11 @@ public Builder addExtension(ChunkExtensionProvider extension) { | |
| return this; | ||
| } | ||
|
|
||
| public Builder addTrailer(TrailerProvider trailerProvider) { | ||
| this.trailers.add(trailerProvider); | ||
| return this; | ||
| } | ||
|
|
||
| public ChunkedEncodedPublisher build() { | ||
| return new ChunkedEncodedPublisher(this); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.