Repository navigation
Add support for Brotli compression of responses - #1173
Open
arturobernalg wants to merge 1 commit into
Open
arturobernalg wants to merge 1 commit into
arturobernalg wants to merge 1 commit into
Conversation
Add the compressionBrotliQuality attribute to the HTTP/1.1 connectors. When it is set to 0 to 11, a response is compressed with Brotli instead of gzip if the client accepts it and prefers it, taking the q values of the Accept-Encoding request header field into account. Otherwise gzip is used, or the response is not compressed, as before. HTTP/2 uses the same connector configuration and output filter. Tomcat does not include an encoder. The Brotli library of the operating system, libbrotlienc, is used with the FFM API. This is built and loaded as the FFM support for OpenSSL is, and requires a JRE that supports it. If the library can not be used, a warning is logged and gzip is used. The memory used by the encoder is not managed by the JVM. It is released when the response ends, when compression fails, when the filter is recycled or, for HTTP/2, when the stream is recycled. The quality is read once, when Brotli is chosen for the response.
| // output filter at this point. | ||
| stream.addOutputFilter(new GzipOutputFilter()); | ||
| if (CompressionConfig.isBrotli(coyoteResponse)) { | ||
| stream.addOutputFilter(new BrotliOutputFilter()); |
| if (CompressionConfig.isBrotli(coyoteResponse)) { | ||
| stream.addOutputFilter(new BrotliOutputFilter()); | ||
| } else { | ||
| stream.addOutputFilter(new GzipOutputFilter()); |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds a
compressionBrotliQualityattribute to the HTTP/1.1 connectors. When it is set to 0 to 11, a response is compressed with Brotli instead of gzip if the client accepts it and prefers it, taking theqvalues ofAccept-Encodinginto account. Otherwise gzip is used, or the response is not compressed, exactly as before. The default is-1, which keeps the current behaviour. HTTP/1.1 and HTTP/2 share the negotiation inCompressionConfigand the newBrotliOutputFilter.Tomcat has no Brotli encoder, so this uses
libbrotliencof the operating system through the FFM API. It is optional and is built and loaded as the FFM support for OpenSSL is (tomcat-coyote-ffm.jar, compiled for the release Java version, loaded by reflection).build.xmland the bnd file list the new package. If the library or FFM can not be used, a warning is logged and gzip is used.The encoder memory is not managed by the JVM, so it is released as soon as it is no longer needed: when the response ends, when compression fails, when the filter is recycled and, for HTTP/2, when the stream is recycled, which HTTP/2 did not do for filters. If compression fails the response is aborted, as with gzip, rather than ended as if it were complete. The quality is read once, when Brotli is chosen, and is kept with the response so a change to the configuration, e.g. with JMX, can not give an invalid quality. A
Cleaneris only a fallback.Tests:
TestCompressionConfigBrotli,TestBrotliOutputFilter,TestBrotliCompression,TestHttp2OutputBufferandTestStreamProcessorBrotli. The Brotli tests are skipped if the library or FFM is not available. Also run against a running Tomcat with HTTP/1.1 and HTTP/2, comparing the decompressed bytes with the original, including repeatedRST_STREAM, async timeouts and client disconnects, and with the library missing or native access denied.