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
Fix BrotliEncoder bug that does not mark ByteBuf it encodes as read #13497
Fix BrotliEncoder bug that does not mark ByteBuf it encodes as read #13497
Conversation
The contract between HttpContentCompressor and compression encoders assumes that encoders will mark the encoded ByteBuf as read after they have been encoded, otherwise the HttpContentCompressor will send encoded chunk twice (one time decoded and one time encoded). The BrotliEncoder peeks the ByteBuf nio buffer and pass it to the BrotliChannelEncoder without marking the ByteBuf and does not respect the implicit contract with HttpContentCompressor. The BrotliEncoder now will set skip bytes of encoded ByteBuf, in addition the test generic AbstractEncoderTest has been modified to ensure that the readable bytes of a ByteBuf is equals to 0 after it has been encoded. This fixes the bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, good find @vietj !!
@hyperxpro wanna take a look bud? |
codec/src/main/java/io/netty/handler/codec/compression/BrotliEncoder.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fix. :)
I was under the impression that |
Actually it can be passed to the HTTP encoder when it is used in a response that has a content. I think that having the HttpContentCompressor recreate a new response with the new encoded buffer and discard the other one would be much cleaner. Currently it relies on the fact that the HTTP encoder will skip encoding a zero length chunk as side effect. |
|
@hyperxpro my bad ... fixed |
@vietj maybe you want to do another PR as well ? |
I will give a try @normanmaurer |
@vietj thanks a lot! |
Expected behavior
The BrotliEncoder respects the implicit HttpContentCompressor contract that ByteBuf should be marked as read (readableBytes() == 0).
Actual behavior
The BrotliEncoder does not mark encoded ByteBuf as read, HttpContent encoded by HttpContentCompressor can have their content be sent twice once decoded and once encoded, corrupting the response. This happens because the Brotli4J library uses the ByteBuf NIO buffer and does not read it through the ByteBuf like other compressor usually do.
Steps to reproduce
Sending a chunked HTTP response encoded with the Brotli compressor reproduces it.
Netty version
Any Brotli4j version, but I haven't checked.