Skip to content

Add support for Brotli compression of responses - #1173

Open
arturobernalg wants to merge 1 commit into
apache:mainfrom
arturobernalg:brotli-compression
Open

arturobernalg wants to merge 1 commit into
apache:mainfrom
arturobernalg:brotli-compression

Conversation

@arturobernalg

Copy link
Copy Markdown
Member

Adds a 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 Accept-Encoding into 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 in CompressionConfig and the new BrotliOutputFilter.

Tomcat has no Brotli encoder, so this uses libbrotlienc of 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.xml and 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 Cleaner is only a fallback.

Tests: TestCompressionConfigBrotli, TestBrotliOutputFilter, TestBrotliCompression, TestHttp2OutputBuffer and TestStreamProcessorBrotli. 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 repeated RST_STREAM, async timeouts and client disconnects, and with the library missing or native access denied.

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());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants