Skip to content

Introduce BrotliEncoder - #11256

Merged
normanmaurer merged 52 commits into
netty:4.1from
hyperxpro:brotli
Jul 8, 2021
Merged

normanmaurer merged 52 commits into
netty:4.1from
hyperxpro:brotli

Conversation

@hyperxpro

Copy link
Copy Markdown
Contributor

Motivation:
Currently, Netty only has BrotliDecoder which can decode Brotli encoded data. However, BrotliEncoder is missing which will encode normal data to Brotli encoded data.

Modification:
Added BrotliEncoder

Result:
Fixes #6899.

@hyperxpro
hyperxpro force-pushed the brotli branch 2 times, most recently from 0adae1b to faa0621 Compare May 14, 2021 14:59
@hyperxpro

Copy link
Copy Markdown
Contributor Author

\cc @slandelle @idelpivnitskiy

@normanmaurer normanmaurer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also please add tests

Fix failing test

Revert IDE formatting

Revert IDE formatting

fixes

fix test

Revert more
@slandelle

Copy link
Copy Markdown
Contributor

@hyperxpro Is brotli compression really supposed to be performed on the fly? From what I've experienced so far, compression seems to be really slow and best performed once and for all for static content, then cached.

@hyperxpro

Copy link
Copy Markdown
Contributor Author

@slandelle I agree with your point but Brotli compression speed cost is compensated by the compression ratio. So yeah, we can have it.

@idelpivnitskiy idelpivnitskiy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR @hyperxpro!

+1 for adding tests. For BrotliEncoder it's enough to add a subclass of AbstractEncoderTest. See LzfEncoderTest as an example. But we also need tests for new features of HttpContentCompressor and CompressorHttp2ConnectionEncoder.

Please, also add reference to brotli4j in NOTICE.txt file, and add LICENSE.brotli4j.txt file in license folder. We forgot to do that when we added decoder.

@hyperxpro

Copy link
Copy Markdown
Contributor Author

I will add tests once you guys are okay with new commits.

@idelpivnitskiy idelpivnitskiy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BrotliEncoder and its test class LGTM! I will leave HTTP-related classes for others to review as I don't have much context on it.

Please, also add tests for HTTP with "br" compression to cover BrotliContentCompressor and CompressorHttp2ConnectionEncoder.

@hyperxpro

Copy link
Copy Markdown
Contributor Author
Error:  Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:testCompile (default-testCompile) on project netty-codec: Compilation failure: Compilation failure: 
Error:  /home/runner/work/netty/netty/codec/src/test/java/io/netty/handler/codec/compression/BrotliEncoderTest.java:[23,7] error: BrotliEncoderTest is not abstract and does not override abstract method createChannel() in AbstractEncoderTest
Error:  /home/runner/work/netty/netty/codec/src/test/java/io/netty/handler/codec/compression/BrotliEncoderTest.java:[26,16] error: initChannel() in BrotliEncoderTest cannot override initChannel() in AbstractEncoderTest
Error:    overridden method is final

@normanmaurer Can you help? The test is same as LzfEncoderTest but it's not compiling.

@kashike

kashike commented Jun 3, 2021

Copy link
Copy Markdown
Contributor
Error:  Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:testCompile (default-testCompile) on project netty-codec: Compilation failure: Compilation failure: 
Error:  /home/runner/work/netty/netty/codec/src/test/java/io/netty/handler/codec/compression/BrotliEncoderTest.java:[23,7] error: BrotliEncoderTest is not abstract and does not override abstract method createChannel() in AbstractEncoderTest
Error:  /home/runner/work/netty/netty/codec/src/test/java/io/netty/handler/codec/compression/BrotliEncoderTest.java:[26,16] error: initChannel() in BrotliEncoderTest cannot override initChannel() in AbstractEncoderTest
Error:    overridden method is final

@normanmaurer Can you help? The test is same as LzfEncoderTest but it's not compiling.

@hyperxpro
https://github.com/netty/netty/pull/11256/files#diff-129844c472b5cf087ae4119467312848077644c9e11f52563e0659d92d5c0df6R25-R28 should be:

            @Override
            protected EmbeddedChannel createChannel() {
                return new EmbeddedChannel(new BrotliEncoder());
            }

ref: https://github.com/netty/netty/blob/master/codec/src/test/java/io/netty/handler/codec/compression/LzfEncoderTest.java#L25-L28

@hyperxpro

Copy link
Copy Markdown
Contributor Author

It's in the last call.

The compression bug is fixed (by using TEXT mode).

@normanmaurer

Copy link
Copy Markdown
Member

@hyperxpro PTAL ... I think moving the options make more sense

@hyperxpro

Copy link
Copy Markdown
Contributor Author

@hyperxpro PTAL ... I think moving the options make more sense

The API looks amazing. Thanks for the change. :)

@normanmaurer
normanmaurer merged commit fef761d into netty:4.1 Jul 8, 2021
@normanmaurer

Copy link
Copy Markdown
Member

@hyperxpro thanks a lot!

@hyperxpro

Copy link
Copy Markdown
Contributor Author

Thanks a lot to everyone who participated in this PR and gave their feedback. :)

@hyperxpro
hyperxpro deleted the brotli branch July 8, 2021 09:52
normanmaurer added a commit that referenced this pull request Jul 8, 2021
Motivation:
Currently, Netty only has BrotliDecoder which can decode Brotli encoded data. However, BrotliEncoder is missing which will encode normal data to Brotli encoded data.

Modification:
Added BrotliEncoder and CompressionOption

Result:
Fixes #6899.

Co-authored-by: Norman Maurer <[email protected]>
}

/**
* @see BrotliOptions#DEFAULT

@trustin trustin Jul 8, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This, as well as other member references in this class's Javadoc, won't be linked properly in generated Javadoc because they are not public. We need to write a proper first sentence, such as:

 * Returns the {@link BrotliOptions} with the default compression settings.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trustin want to do a PR ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do it as I've upcoming PR to enable Brotli tests on Windows.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @hyperxpro 🙇

@trustin

trustin commented Jul 8, 2021

Copy link
Copy Markdown
Member

Thanks a lot for your hard work, @hyperxpro! Looking great indeed. Too bad we're not at Java 8 yet, though 😅

normanmaurer pushed a commit that referenced this pull request Jul 13, 2021
…11482)

Motivation:
In #11256, We introduced `Iterable` as a parameter but later in review, it was removed. But we forgot to change `compressionOptionsIterable` to just `compressionOptions`.

Modification:
Changed `compressionOptionsIterable` to `compressionOptions`.

Result:
Correct ObjectUtil message
normanmaurer pushed a commit that referenced this pull request Jul 13, 2021
…11482)

Motivation:
In #11256, We introduced `Iterable` as a parameter but later in review, it was removed. But we forgot to change `compressionOptionsIterable` to just `compressionOptions`.

Modification:
Changed `compressionOptionsIterable` to `compressionOptions`.

Result:
Correct ObjectUtil message
raidyue pushed a commit to raidyue/netty that referenced this pull request Jul 8, 2022
Motivation:
Currently, Netty only has BrotliDecoder which can decode Brotli encoded data. However, BrotliEncoder is missing which will encode normal data to Brotli encoded data.

Modification:
Added BrotliEncoder and CompressionOption

Result:
Fixes netty#6899.

Co-authored-by: Norman Maurer <[email protected]>
raidyue pushed a commit to raidyue/netty that referenced this pull request Jul 8, 2022
…etty#11482)

Motivation:
In netty#11256, We introduced `Iterable` as a parameter but later in review, it was removed. But we forgot to change `compressionOptionsIterable` to just `compressionOptions`.

Modification:
Changed `compressionOptionsIterable` to `compressionOptions`.

Result:
Correct ObjectUtil message
dongjoon-hyun added a commit to apache/spark that referenced this pull request Dec 5, 2025
…` license

### What changes were proposed in this pull request?

This PR aims to update `NOTICE-binary` with `Netty` 4.2.7 license.
- https://github.com/netty/netty/blob/netty-4.2.7.Final/NOTICE.txt

### Why are the changes needed?

It seems that we updated `Netty Notice` at Apache Spark `3.0.0-preview` with `Netty 4.1.30.Final`.
- #25544

Since there are many changes like the following, we need to update it by simply copying and pasting.
- Netty 4.1.38.Final
  - netty/netty#9344
- Netty 4.1.44.Final
  - netty/netty#9161
- Netty 4.1.54.Final
  - netty/netty#10773
- Netty 4.1.66.Final
  - netty/netty#11256
  - netty/netty#11437
- Netty 4.1.108.Final
  - netty/netty#13864
- Netty 4.2.1.Final
  - netty/netty#14979
- Netty 4.2.7.Final
  - netty/netty#15658

Additionally, I also double-checked newly added transitive license through ASF [LEGAL-700](https://issues.apache.org/jira/browse/LEGAL-700). We are good to go.

- **Apple Public Source License 2.0**
  - https://spdx.org/licenses/APSL-2.0.html

### Does this PR introduce _any_ user-facing change?

No behavior change.

### How was this patch tested?

Manual review.

### Was this patch authored or co-authored using generative AI tooling?

No.

Closes #53335 from dongjoon-hyun/SPARK-54602.

Authored-by: Dongjoon Hyun <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
dongjoon-hyun added a commit to apache/spark that referenced this pull request Dec 5, 2025
…` license

### What changes were proposed in this pull request?

This PR aims to update `NOTICE-binary` with `Netty` 4.2.7 license.
- https://github.com/netty/netty/blob/netty-4.2.7.Final/NOTICE.txt

### Why are the changes needed?

It seems that we updated `Netty Notice` at Apache Spark `3.0.0-preview` with `Netty 4.1.30.Final`.
- #25544

Since there are many changes like the following, we need to update it by simply copying and pasting.
- Netty 4.1.38.Final
  - netty/netty#9344
- Netty 4.1.44.Final
  - netty/netty#9161
- Netty 4.1.54.Final
  - netty/netty#10773
- Netty 4.1.66.Final
  - netty/netty#11256
  - netty/netty#11437
- Netty 4.1.108.Final
  - netty/netty#13864
- Netty 4.2.1.Final
  - netty/netty#14979
- Netty 4.2.7.Final
  - netty/netty#15658

Additionally, I also double-checked newly added transitive license through ASF [LEGAL-700](https://issues.apache.org/jira/browse/LEGAL-700). We are good to go.

- **Apple Public Source License 2.0**
  - https://spdx.org/licenses/APSL-2.0.html

### Does this PR introduce _any_ user-facing change?

No behavior change.

### How was this patch tested?

Manual review.

### Was this patch authored or co-authored using generative AI tooling?

No.

Closes #53335 from dongjoon-hyun/SPARK-54602.

Authored-by: Dongjoon Hyun <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
(cherry picked from commit 191ce4c)
Signed-off-by: Dongjoon Hyun <[email protected]>
normanmaurer added a commit that referenced this pull request Nov 5, 2026
#17726)

Motivation:

`HttpContentCompressor.determineEncoding(...)` parses the qvalue of
every coding in `Accept-Encoding` correctly, but then only compares each
coding with the next one in the list `br, zstd, snappy, gzip, deflate`
(`brQ >= zstdQ`, `zstdQ >= snappyQ`, ...). That pairwise rule was right
when only gzip and deflate existed (`determineWrapper(...)`), but with
five codings it picks a coding the client ranked lower, or did not
accept at all:

| `Accept-Encoding` | configured | selected now | expected |
|---|---|---|---|
| `br;q=0, gzip` | default, Brotli on the class path | `br` | `gzip` |
| `gzip, br;q=0` | default, Brotli on the class path | `br` | `gzip` |
| `zstd;q=0, gzip` | default, zstd-jni on the class path | `zstd` |
`gzip` |
| `snappy;q=0, deflate` | default | `snappy` | `deflate` |
| `br;q=0, zstd;q=0, snappy;q=0, gzip;q=0, deflate` | default, Brotli on
the class path | `br` | `deflate` |
| `br;q=0.1, gzip` | default, Brotli on the class path | `br` | `gzip` |

[RFC 9110, Section
12.4.2](https://www.rfc-editor.org/rfc/rfc9110#section-12.4.2): "a value
of 0 means "not acceptable"", and [Section
12.5.3](https://www.rfc-editor.org/rfc/rfc9110#section-12.5.3): a listed
content coding is acceptable "unless it is accompanied by a qvalue of
0". A client that sends `br;q=0` (for example because it cannot decode
Brotli) gets a Brotli body it cannot decode.

The pairwise comparisons came with the additions of br (#11256), zstd
(#11470) and snappy (#13529); the existing tests only cover single
codings and equal qvalues.

Modification:

- Select the configured coding with the highest qvalue above 0. If
qvalues are equal, keep the existing order `br, zstd, snappy, gzip,
deflate` (so `"gzip, br"` still gives `br`). The `*` handling and the
parsing are unchanged.
- Add `testDetermineEncodingHonoursQValues` (gzip, deflate and snappy
configured, runs everywhere) and
`testDetermineEncodingHonoursQValuesBrotli`
(`@EnabledIf("isBrotliAvailable")`) as `@ParameterizedTest`s.
- Update `HttpContentCompressorOptionsTest`: `"br; q=0, deflate"` (and
the same for zstd and snappy) now selects `deflate`, as the listed
coding has qvalue 0.

Result:

The compressor no longer uses a content coding the client marked as not
acceptable, and prefers the coding with the higher qvalue. Requests
whose explicitly listed codings have equal qvalues, and requests that
only use `*`, give the same result as before.

---------

Co-authored-by: Norman Maurer <[email protected]>
chrisvest pushed a commit that referenced this pull request Nov 5, 2026
…content encoding (#18285)

Auto-port of #17726 to 5.0
Cherry-picked commit: 5a638e4

---
Motivation:

`HttpContentCompressor.determineEncoding(...)` parses the qvalue of
every coding in `Accept-Encoding` correctly, but then only compares each
coding with the next one in the list `br, zstd, snappy, gzip, deflate`
(`brQ >= zstdQ`, `zstdQ >= snappyQ`, ...). That pairwise rule was right
when only gzip and deflate existed (`determineWrapper(...)`), but with
five codings it picks a coding the client ranked lower, or did not
accept at all:

| `Accept-Encoding` | configured | selected now | expected |
|---|---|---|---|
| `br;q=0, gzip` | default, Brotli on the class path | `br` | `gzip` |
| `gzip, br;q=0` | default, Brotli on the class path | `br` | `gzip` |
| `zstd;q=0, gzip` | default, zstd-jni on the class path | `zstd` |
`gzip` |
| `snappy;q=0, deflate` | default | `snappy` | `deflate` |
| `br;q=0, zstd;q=0, snappy;q=0, gzip;q=0, deflate` | default, Brotli on
the class path | `br` | `deflate` |
| `br;q=0.1, gzip` | default, Brotli on the class path | `br` | `gzip` |

[RFC 9110, Section
12.4.2](https://www.rfc-editor.org/rfc/rfc9110#section-12.4.2): "a value
of 0 means "not acceptable"", and [Section
12.5.3](https://www.rfc-editor.org/rfc/rfc9110#section-12.5.3): a listed
content coding is acceptable "unless it is accompanied by a qvalue of
0". A client that sends `br;q=0` (for example because it cannot decode
Brotli) gets a Brotli body it cannot decode.

The pairwise comparisons came with the additions of br (#11256), zstd
(#11470) and snappy (#13529); the existing tests only cover single
codings and equal qvalues.

Modification:

- Select the configured coding with the highest qvalue above 0. If
qvalues are equal, keep the existing order `br, zstd, snappy, gzip,
deflate` (so `"gzip, br"` still gives `br`). The `*` handling and the
parsing are unchanged.
- Add `testDetermineEncodingHonoursQValues` (gzip, deflate and snappy
configured, runs everywhere) and
`testDetermineEncodingHonoursQValuesBrotli`
(`@EnabledIf("isBrotliAvailable")`) as `@ParameterizedTest`s.
- Update `HttpContentCompressorOptionsTest`: `"br; q=0, deflate"` (and
the same for zstd and snappy) now selects `deflate`, as the listed
coding has qvalue 0.

Result:

The compressor no longer uses a content coding the client marked as not
acceptable, and prefers the coding with the higher qvalue. Requests
whose explicitly listed codings have equal qvalues, and requests that
only use `*`, give the same result as before.

- `HttpContentCompressorTest`, `HttpContentEncoderTest`,
`HttpContentDecompressorTest` pass (63 tests), and
`HttpContentCompressorOptionsTest` passes (4). Without the change to
`HttpContentCompressor`, 7 of the 13 new cases fail; the others pass
both ways.
- Checkstyle and revapi pass.

The selection was also modelled in Lean 4 against the RFC 9110 rule: for
every combination of qvalues from {absent, 0, 0.5, 1} for the five
codings and `*`, and every subset of configured codings with qvalues
from {absent, 0, 1}, the new selection only returns an acceptable coding
with the highest qvalue, and it returns the same coding as before
whenever the current result already satisfied that. The change was
prepared with Claude Code (Claude Opus 5.5), and the commit has a
`Co-Authored-By` trailer.

_Prepared with Claude Code on behalf of allthingssecurity._

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: allthingssecurity <[email protected]>
Co-authored-by: Norman Maurer <[email protected]>
ikpil added a commit to ikpil/Netty.NET that referenced this pull request Nov 29, 2026
…ent encoding (netty/netty#17726)

Motivation:

`HttpContentCompressor.determineEncoding(...)` parses the qvalue of
every coding in `Accept-Encoding` correctly, but then only compares each
coding with the next one in the list `br, zstd, snappy, gzip, deflate`
(`brQ >= zstdQ`, `zstdQ >= snappyQ`, ...). That pairwise rule was right
when only gzip and deflate existed (`determineWrapper(...)`), but with
five codings it picks a coding the client ranked lower, or did not
accept at all:

| `Accept-Encoding` | configured | selected now | expected |
|---|---|---|---|
| `br;q=0, gzip` | default, Brotli on the class path | `br` | `gzip` |
| `gzip, br;q=0` | default, Brotli on the class path | `br` | `gzip` |
| `zstd;q=0, gzip` | default, zstd-jni on the class path | `zstd` |
`gzip` |
| `snappy;q=0, deflate` | default | `snappy` | `deflate` |
| `br;q=0, zstd;q=0, snappy;q=0, gzip;q=0, deflate` | default, Brotli on
the class path | `br` | `deflate` |
| `br;q=0.1, gzip` | default, Brotli on the class path | `br` | `gzip` |

[RFC 9110, Section
12.4.2](https://www.rfc-editor.org/rfc/rfc9110#section-12.4.2): "a value
of 0 means "not acceptable"", and [Section
12.5.3](https://www.rfc-editor.org/rfc/rfc9110#section-12.5.3): a listed
content coding is acceptable "unless it is accompanied by a qvalue of
0". A client that sends `br;q=0` (for example because it cannot decode
Brotli) gets a Brotli body it cannot decode.

The pairwise comparisons came with the additions of br (netty/netty#11256), zstd
(netty/netty#11470) and snappy (netty/netty#13529); the existing tests only cover single
codings and equal qvalues.

Modification:

- Select the configured coding with the highest qvalue above 0. If
qvalues are equal, keep the existing order `br, zstd, snappy, gzip,
deflate` (so `"gzip, br"` still gives `br`). The `*` handling and the
parsing are unchanged.
- Add `testDetermineEncodingHonoursQValues` (gzip, deflate and snappy
configured, runs everywhere) and
`testDetermineEncodingHonoursQValuesBrotli`
(`@EnabledIf("isBrotliAvailable")`) as `@ParameterizedTest`s.
- Update `HttpContentCompressorOptionsTest`: `"br; q=0, deflate"` (and
the same for zstd and snappy) now selects `deflate`, as the listed
coding has qvalue 0.

Result:

The compressor no longer uses a content coding the client marked as not
acceptable, and prefers the coding with the higher qvalue. Requests
whose explicitly listed codings have equal qvalues, and requests that
only use `*`, give the same result as before.

---------

Co-authored-by: Norman Maurer <[email protected]>

Upstream-Commit: netty/netty@5a638e4
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.

Support Brotli

8 participants