Skip to content

Commit

Permalink
support zstd compression
Browse files Browse the repository at this point in the history
  • Loading branch information
sullis committed Apr 1, 2024
1 parent 6aee54c commit 7c0d0f4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.compression.Brotli;
import io.netty.handler.codec.compression.Zstd;
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 @@ -66,6 +67,7 @@
import static org.asynchttpclient.util.HttpUtils.ACCEPT_ALL_HEADER_VALUE;
import static org.asynchttpclient.util.HttpUtils.GZIP_DEFLATE;
import static org.asynchttpclient.util.HttpUtils.filterOutBrotliFromAcceptEncoding;
import static org.asynchttpclient.util.HttpUtils.filterOutZstdFromAcceptEncoding;
import static org.asynchttpclient.util.HttpUtils.hostHeader;
import static org.asynchttpclient.util.HttpUtils.originHeader;
import static org.asynchttpclient.util.HttpUtils.urlEncodeFormParams;
Expand Down Expand Up @@ -179,13 +181,21 @@ public NettyRequest newNettyRequest(Request request, boolean performConnectReque
// For manual decompression by user, any encoding may suite, so leave untouched
headers.set(ACCEPT_ENCODING, filterOutBrotliFromAcceptEncoding(userDefinedAcceptEncoding));
}
if (!Zstd.isAvailable()) {
// zstd is not available.
// For manual decompression by user, any encoding may suit, so leave untouched
headers.set(ACCEPT_ENCODING, filterOutZstdFromAcceptEncoding(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);
}
if (Zstd.isAvailable()) {
headers.add(ACCEPT_ENCODING, HttpHeaderValues.ZSTD);
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions client/src/main/java/org/asynchttpclient/util/HttpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public final class HttpUtils {
private static final String CONTENT_TYPE_CHARSET_ATTRIBUTE = "charset=";
private static final String CONTENT_TYPE_BOUNDARY_ATTRIBUTE = "boundary=";
private static final String BROTLY_ACCEPT_ENCODING_SUFFIX = ", br";
private static final String ZSTD_ACCEPT_ENCODING_SUFFIX = ", zstd";

private HttpUtils() {
// Prevent outside initialization
Expand Down Expand Up @@ -173,4 +174,12 @@ public static CharSequence filterOutBrotliFromAcceptEncoding(String acceptEncodi
}
return acceptEncoding;
}

public static CharSequence filterOutZstdFromAcceptEncoding(String acceptEncoding) {
// we don't support zstd ATM
if (acceptEncoding.endsWith(ZSTD_ACCEPT_ENCODING_SUFFIX)) {
return acceptEncoding.subSequence(0, acceptEncoding.length() - ZSTD_ACCEPT_ENCODING_SUFFIX.length());
}
return acceptEncoding;
}
}
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 @@ -3,6 +3,7 @@
import io.netty.channel.epoll.Epoll;
import io.netty.channel.kqueue.KQueue;
import io.netty.handler.codec.compression.Brotli;
import io.netty.handler.codec.compression.Zstd;
import io.netty.incubator.channel.uring.IOUring;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
Expand Down Expand Up @@ -40,4 +41,16 @@ public void brotliIsAvailableOnLinux() {
public void brotliIsAvailableOnMac() {
assertTrue(Brotli.isAvailable());
}

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

@Test
@EnabledOnOs(value = OS.MAC)
public void zstdIsAvailableOnMac() {
assertTrue(Zstd.isAvailable());
}
}
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<netty.version>4.1.108.Final</netty.version>
<netty.iouring>0.0.25.Final</netty.iouring>
<brotli4j.version>1.16.0</brotli4j.version>
<zstd-jni.version>1.5.6-1</zstd-jni.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 @@ -224,6 +225,13 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.github.luben</groupId>
<artifactId>zstd-jni</artifactId>
<version>${zstd-jni.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>brotli4j</artifactId>
Expand Down

0 comments on commit 7c0d0f4

Please sign in to comment.