Skip to content

Commit 631c3b0

Browse files
authored
support brotli (#1938)
1 parent a30ffca commit 631c3b0

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

Diff for: client/src/main/java/org/asynchttpclient/netty/request/NettyRequestFactory.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import io.netty.buffer.ByteBuf;
1919
import io.netty.buffer.Unpooled;
20+
import io.netty.handler.codec.compression.Brotli;
2021
import io.netty.handler.codec.http.DefaultFullHttpRequest;
2122
import io.netty.handler.codec.http.DefaultHttpRequest;
2223
import io.netty.handler.codec.http.HttpHeaderValues;
@@ -173,13 +174,18 @@ public NettyRequest newNettyRequest(Request request, boolean performConnectReque
173174
String userDefinedAcceptEncoding = headers.get(ACCEPT_ENCODING);
174175
if (userDefinedAcceptEncoding != null) {
175176
if (config.isEnableAutomaticDecompression()) {
176-
// we don't support Brotli ATM, for automatic decompression.
177-
// For manual decompression by user, any encoding may suite, so leave untouched
178-
headers.set(ACCEPT_ENCODING, filterOutBrotliFromAcceptEncoding(userDefinedAcceptEncoding));
177+
if (!Brotli.isAvailable()) {
178+
// Brotli is not available.
179+
// For manual decompression by user, any encoding may suite, so leave untouched
180+
headers.set(ACCEPT_ENCODING, filterOutBrotliFromAcceptEncoding(userDefinedAcceptEncoding));
181+
}
179182
}
180183
} else if (config.isCompressionEnforced()) {
181184
// Add Accept Encoding header if compression is enforced
182185
headers.set(ACCEPT_ENCODING, GZIP_DEFLATE);
186+
if (Brotli.isAvailable()) {
187+
headers.add(ACCEPT_ENCODING, HttpHeaderValues.BR);
188+
}
183189
}
184190
}
185191

Diff for: client/src/test/java/org/asynchttpclient/netty/NettyTest.java

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.netty.channel.epoll.Epoll;
44
import io.netty.channel.kqueue.KQueue;
5+
import io.netty.handler.codec.compression.Brotli;
56
import io.netty.incubator.channel.uring.IOUring;
67
import org.junit.jupiter.api.Test;
78
import org.junit.jupiter.api.condition.EnabledOnOs;
@@ -27,4 +28,16 @@ public void ioUringIsAvailableOnLinux() {
2728
public void kqueueIsAvailableOnMac() {
2829
assertTrue(KQueue.isAvailable());
2930
}
31+
32+
@Test
33+
@EnabledOnOs(value = OS.LINUX)
34+
public void brotliIsAvailableOnLinux() {
35+
assertTrue(Brotli.isAvailable());
36+
}
37+
38+
@Test
39+
@EnabledOnOs(value = OS.MAC)
40+
public void brotliIsAvailableOnMac() {
41+
assertTrue(Brotli.isAvailable());
42+
}
3043
}

Diff for: pom.xml

+45-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
3-
~ Copyright (c) 2023 AsyncHttpClient Project. All rights reserved.
3+
~ Copyright (c) 2024 AsyncHttpClient Project. All rights reserved.
44
~
55
~ Licensed under the Apache License, Version 2.0 (the "License");
66
~ you may not use this file except in compliance with the License.
@@ -60,6 +60,7 @@
6060

6161
<netty.version>4.1.107.Final</netty.version>
6262
<netty.iouring>0.0.25.Final</netty.iouring>
63+
<brotli4j.version>1.16.0</brotli4j.version>
6364
<slf4j.version>2.0.12</slf4j.version>
6465
<activation.version>2.0.1</activation.version>
6566
<logback.version>1.4.11</logback.version>
@@ -223,6 +224,49 @@
223224
<optional>true</optional>
224225
</dependency>
225226

227+
<dependency>
228+
<groupId>com.aayushatharva.brotli4j</groupId>
229+
<artifactId>brotli4j</artifactId>
230+
<version>${brotli4j.version}</version>
231+
<optional>true</optional>
232+
</dependency>
233+
<dependency>
234+
<groupId>com.aayushatharva.brotli4j</groupId>
235+
<artifactId>native-linux-x86_64</artifactId>
236+
<version>${brotli4j.version}</version>
237+
<optional>true</optional>
238+
</dependency>
239+
<dependency>
240+
<groupId>com.aayushatharva.brotli4j</groupId>
241+
<artifactId>native-linux-aarch64</artifactId>
242+
<version>${brotli4j.version}</version>
243+
<optional>true</optional>
244+
</dependency>
245+
<dependency>
246+
<groupId>com.aayushatharva.brotli4j</groupId>
247+
<artifactId>native-linux-riscv64</artifactId>
248+
<version>${brotli4j.version}</version>
249+
<optional>true</optional>
250+
</dependency>
251+
<dependency>
252+
<groupId>com.aayushatharva.brotli4j</groupId>
253+
<artifactId>native-osx-x86_64</artifactId>
254+
<version>${brotli4j.version}</version>
255+
<optional>true</optional>
256+
</dependency>
257+
<dependency>
258+
<groupId>com.aayushatharva.brotli4j</groupId>
259+
<artifactId>native-osx-aarch64</artifactId>
260+
<version>${brotli4j.version}</version>
261+
<optional>true</optional>
262+
</dependency>
263+
<dependency>
264+
<groupId>com.aayushatharva.brotli4j</groupId>
265+
<artifactId>native-windows-x86_64</artifactId>
266+
<version>${brotli4j.version}</version>
267+
<optional>true</optional>
268+
</dependency>
269+
226270
<dependency>
227271
<groupId>org.slf4j</groupId>
228272
<artifactId>slf4j-api</artifactId>

0 commit comments

Comments
 (0)