Skip to content

Commit

Permalink
support brotli (#1938)
Browse files Browse the repository at this point in the history
  • Loading branch information
sullis authored Mar 1, 2024
1 parent a30ffca commit 631c3b0
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.compression.Brotli;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.HttpHeaderValues;
Expand Down Expand Up @@ -173,13 +174,18 @@ public NettyRequest newNettyRequest(Request request, boolean performConnectReque
String userDefinedAcceptEncoding = headers.get(ACCEPT_ENCODING);
if (userDefinedAcceptEncoding != null) {
if (config.isEnableAutomaticDecompression()) {
// we don't support Brotli ATM, for automatic decompression.
// For manual decompression by user, any encoding may suite, so leave untouched
headers.set(ACCEPT_ENCODING, filterOutBrotliFromAcceptEncoding(userDefinedAcceptEncoding));
if (!Brotli.isAvailable()) {
// Brotli is not available.
// For manual decompression by user, any encoding may suite, so leave untouched
headers.set(ACCEPT_ENCODING, filterOutBrotliFromAcceptEncoding(userDefinedAcceptEncoding));
}
}
} else if (config.isCompressionEnforced()) {
// Add Accept Encoding header if compression is enforced
headers.set(ACCEPT_ENCODING, GZIP_DEFLATE);
if (Brotli.isAvailable()) {
headers.add(ACCEPT_ENCODING, HttpHeaderValues.BR);
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions client/src/test/java/org/asynchttpclient/netty/NettyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.netty.channel.epoll.Epoll;
import io.netty.channel.kqueue.KQueue;
import io.netty.handler.codec.compression.Brotli;
import io.netty.incubator.channel.uring.IOUring;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
Expand All @@ -27,4 +28,16 @@ public void ioUringIsAvailableOnLinux() {
public void kqueueIsAvailableOnMac() {
assertTrue(KQueue.isAvailable());
}

@Test
@EnabledOnOs(value = OS.LINUX)
public void brotliIsAvailableOnLinux() {
assertTrue(Brotli.isAvailable());
}

@Test
@EnabledOnOs(value = OS.MAC)
public void brotliIsAvailableOnMac() {
assertTrue(Brotli.isAvailable());
}
}
46 changes: 45 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2023 AsyncHttpClient Project. All rights reserved.
~ Copyright (c) 2024 AsyncHttpClient Project. All rights reserved.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -60,6 +60,7 @@

<netty.version>4.1.107.Final</netty.version>
<netty.iouring>0.0.25.Final</netty.iouring>
<brotli4j.version>1.16.0</brotli4j.version>
<slf4j.version>2.0.12</slf4j.version>
<activation.version>2.0.1</activation.version>
<logback.version>1.4.11</logback.version>
Expand Down Expand Up @@ -223,6 +224,49 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>brotli4j</artifactId>
<version>${brotli4j.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>native-linux-x86_64</artifactId>
<version>${brotli4j.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>native-linux-aarch64</artifactId>
<version>${brotli4j.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>native-linux-riscv64</artifactId>
<version>${brotli4j.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>native-osx-x86_64</artifactId>
<version>${brotli4j.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>native-osx-aarch64</artifactId>
<version>${brotli4j.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>native-windows-x86_64</artifactId>
<version>${brotli4j.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down

0 comments on commit 631c3b0

Please sign in to comment.