Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/main/java/io/cdap/http/NettyHttpService.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.compression.StandardCompressionOptions;
import io.netty.handler.codec.http.HttpContentCompressor;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.HttpServerKeepAliveHandler;
Expand Down Expand Up @@ -416,7 +417,12 @@ protected void initChannel(SocketChannel ch) throws Exception {
pipeline.addLast("ssl", sslHandlerFactory.create(ch.alloc()));
}
pipeline.addLast("codec", new HttpServerCodec());
pipeline.addLast("compressor", new HttpContentCompressor());
// Only offer gzip and deflate. The no-arg constructor in newer Netty versions also turns on
// zstd (and brotli) whenever those libraries are on the classpath, and fails with a
// NoSuchMethodError when an older zstd-jni is loaded first (for example from a Hadoop
// or Spark runtime classpath).
pipeline.addLast("compressor", new HttpContentCompressor(StandardCompressionOptions.gzip(),
StandardCompressionOptions.deflate()));
pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
pipeline.addLast("keepAlive", new HttpServerKeepAliveHandler());
pipeline.addLast("router", new RequestRouter(resourceHandler, httpChunkLimit, sslHandlerFactory != null));
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/io/cdap/http/HttpServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,25 @@ public void testCompressResponse() throws Exception {
Assert.assertEquals("Testing message chunk", getContent(urlConn));
}

@Test
public void testCompressResponseOnlyGzipOrDeflate() throws Exception {
// Encodings other than gzip and deflate are not offered, so the response is not compressed.
HttpURLConnection urlConn = request("/test/v1/compressResponse?message=Testing+message", HttpMethod.GET);
urlConn.setRequestProperty(HttpHeaderNames.ACCEPT_ENCODING.toString(), "zstd, br, snappy");

Assert.assertEquals(HttpResponseStatus.OK.code(), urlConn.getResponseCode());
Assert.assertNull(urlConn.getHeaderField(HttpHeaderNames.CONTENT_ENCODING.toString()));
Assert.assertEquals("Testing message", getContent(urlConn));

// When gzip is also accepted, gzip is used.
urlConn = request("/test/v1/compressResponse?message=Testing+message", HttpMethod.GET);
urlConn.setRequestProperty(HttpHeaderNames.ACCEPT_ENCODING.toString(), "zstd, br, gzip");

Assert.assertEquals(HttpResponseStatus.OK.code(), urlConn.getResponseCode());
Assert.assertEquals("gzip", urlConn.getHeaderField(HttpHeaderNames.CONTENT_ENCODING.toString()));
Assert.assertEquals("Testing message", getContent(urlConn));
}

@Test
public void testContinueHandler() throws Exception {
// Send two request with BodyConsumer handling with keep-alive and expect continue header
Expand Down
Loading