Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(proxy): support brotli compression #852

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
25 changes: 25 additions & 0 deletions components/proxy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,31 @@
<classifier>linux-aarch_64</classifier>
</dependency>

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

<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>native-linux-x86_64</artifactId>
</dependency>

<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>native-linux-aarch64</artifactId>
</dependency>

<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>native-osx-x86_64</artifactId>
</dependency>

<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>native-osx-aarch64</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.hotels.styx.proxy;

import io.netty.handler.codec.compression.CompressionOptions;
import io.netty.handler.codec.http.HttpContentCompressor;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpResponse;
Expand Down Expand Up @@ -53,6 +54,14 @@ public class HttpCompressor extends HttpContentCompressor {
"application/json");


public HttpCompressor() {
this(0);
}

public HttpCompressor(int contentSizeThreshold) {
super(contentSizeThreshold, (CompressionOptions[]) null);
}

private boolean shouldCompress(String contentType) {
return ENCODING_TYPES.contains(contentType != null ? contentType.toLowerCase() : "");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Copyright (C) 2013-2024 Expedia Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.proxy;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.compression.Brotli;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.HttpContentEncoder;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;


public class HttpCompressorTest {
private static final String COMPRESSIBLE_MIME_TYPE = "application/json";
private static final String NOT_COMPRESSIBLE_MIME_TYPE = "image/jpeg";
private HttpCompressor compressor;
private ChannelHandlerContext ctx;


@BeforeEach
public void setUp() throws Exception {
assertTrue(Brotli.isAvailable());
compressor = new HttpCompressor();
ctx = mock(ChannelHandlerContext.class, RETURNS_DEEP_STUBS);
compressor.handlerAdded(ctx);
}


@ParameterizedTest
@MethodSource("responseEncodingParameters")
public void validateResponseEncoding(final String acceptEncoding, final String contentType, final String expectedEncoding) throws Exception {
HttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
httpResponse.headers().add("Content-Type", contentType);
HttpContentEncoder.Result result = compressor.beginEncode(httpResponse, acceptEncoding);
if (expectedEncoding == null) {
assertNull(result);
} else {
assertEquals(result.targetContentEncoding(), expectedEncoding);
assertNotNull(result.contentEncoder());
}
}

private static Stream<Arguments> responseEncodingParameters() {
// Note: "br" is brotli compression
return Stream.of(
Arguments.of("br", COMPRESSIBLE_MIME_TYPE, "br"),
Arguments.of("br", NOT_COMPRESSIBLE_MIME_TYPE, null),
Arguments.of("gzip", COMPRESSIBLE_MIME_TYPE, "gzip"),
Arguments.of("gzip", NOT_COMPRESSIBLE_MIME_TYPE, null),
Arguments.of("br, gzip", COMPRESSIBLE_MIME_TYPE, "br"),
Arguments.of("gzip, br", COMPRESSIBLE_MIME_TYPE, "br"),
Arguments.of("", COMPRESSIBLE_MIME_TYPE, null),
Arguments.of("", NOT_COMPRESSIBLE_MIME_TYPE, null),
Arguments.of("br, garbage", COMPRESSIBLE_MIME_TYPE, "br"),
Arguments.of("gzip, garbage", COMPRESSIBLE_MIME_TYPE, "gzip"),
Arguments.of("garbage", COMPRESSIBLE_MIME_TYPE, null),
Arguments.of("?", COMPRESSIBLE_MIME_TYPE, null),
Arguments.of(",", COMPRESSIBLE_MIME_TYPE, null)
);
}
}
31 changes: 31 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
<janino.version>3.1.12</janino.version>
<micrometer.version>1.12.3</micrometer.version>
<netty.version>4.1.107.Final</netty.version>
<brotli4j.version>1.16.0</brotli4j.version>
<okhttp.version>4.12.0</okhttp.version>
<pcollections.version>3.0.5</pcollections.version>
<reactive-streams.version>1.0.4</reactive-streams.version>
Expand Down Expand Up @@ -291,6 +292,36 @@
<version>${guava.version}</version>
</dependency>

<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>brotli4j</artifactId>
<version>${brotli4j.version}</version>
</dependency>

<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>native-linux-x86_64</artifactId>
<version>${brotli4j.version}</version>
</dependency>

<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>native-linux-aarch64</artifactId>
<version>${brotli4j.version}</version>
</dependency>

<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>native-osx-x86_64</artifactId>
<version>${brotli4j.version}</version>
</dependency>

<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>native-osx-aarch64</artifactId>
<version>${brotli4j.version}</version>
</dependency>

<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
Expand Down