Skip to content

Commit

Permalink
feature(proxy): support brotli compression
Browse files Browse the repository at this point in the history
  • Loading branch information
sullis committed Mar 13, 2024
1 parent 01036ea commit 0cede14
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 0 deletions.
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,18 @@
package com.hotels.styx.proxy;

import io.netty.handler.codec.compression.Brotli;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;

import static org.junit.jupiter.api.Assertions.assertTrue;


public class BrotliTest {
@Test
@EnabledOnOs(value = { OS.LINUX, OS.MAC })
public void brotliCompressionIsAvailable() throws Throwable {
Brotli.ensureAvailability();
assertTrue(Brotli.isAvailable());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
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.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.mockito.Answers.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;


public class HttpCompressorTest {
private HttpCompressor compressor;
private ChannelHandlerContext ctx;


@BeforeEach
public void setUp() throws Exception {
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", "application/json", "br"),
Arguments.of("br", "image/jpeg", null),
Arguments.of("gzip", "application/json", "gzip"),
Arguments.of("gzip", "image/jpeg", null),
Arguments.of("br, gzip", "application/json", "br"),
Arguments.of("gzip, br", "application/json", "br"),
Arguments.of("", "application/json", null),
Arguments.of("", "image/jpeg", null),
Arguments.of("br, garbage", "application/json", "br"),
Arguments.of("gzip, garbage", "application/json", "gzip"),
Arguments.of("garbage", "application/json", null),
Arguments.of("?", "application/json", null),
Arguments.of(",", "application/json", 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

0 comments on commit 0cede14

Please sign in to comment.