Skip to content

Commit

Permalink
Issue #12397 - Add more default excludes to GzipHandler. (#12414)
Browse files Browse the repository at this point in the history
* Issue #12397 - Add more default excludes to GzipHandler.

+ Exclude more compressed mime-types
+ Exclude more compressed path extensions
+ Add missing compressed file types to mime.properties
  • Loading branch information
joakime authored Oct 23, 2024
1 parent 67d4825 commit 92302d6
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,4 @@ xyz=chemical/x-xyz
xz=application/x-xz
z=application/compress
zip=application/zip
zst=application/zstd
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,27 @@ else if (type.startsWith("image/") ||
type.startsWith("video/"))
_mimeTypes.exclude(type);
}

_mimeTypes.exclude("application/compress");
_paths.exclude("*.z");
_mimeTypes.exclude("application/zip");
_paths.exclude("*.zip");
_mimeTypes.exclude("application/x-gtar");
_paths.exclude("*.tgz");
_mimeTypes.exclude("application/java-archive");
_paths.exclude("*.jar");
_mimeTypes.exclude("application/gzip");
_paths.exclude("*.gz", "*.gzip");
_mimeTypes.exclude("application/x-bzip2");
_paths.exclude("*.bz2", "*.bzip", "*.bz");
_mimeTypes.exclude("application/brotli");
_paths.exclude("*.br", "*.brotli");
_mimeTypes.exclude("application/x-xz");
_paths.exclude("*.xz");
_mimeTypes.exclude("application/x-rar-compressed");
_paths.exclude("*.rar");
_mimeTypes.exclude("application/zstd");
_paths.exclude("*.zst", "*.zstd");

// It is possible to use SSE with GzipHandler but you will need to set _synFlush to true which will impact performance.
_mimeTypes.exclude("text/event-stream");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;

Expand Down Expand Up @@ -1750,6 +1751,68 @@ public void testSimpleCompressedResponse(int fileSize, WorkDir workDir) throws E
assertThat("(Uncompressed) Content Hash", metadata.uncompressedSha1Sum, is(expectedSha1Sum));
}

/**
* Test of default GzipHandler configuration against requests to already compressed content-types.
* @param filename
*/
@ParameterizedTest
@CsvSource(delimiter = '|', textBlock = """
# Filename | Content-Type
example.tar.gz | application/gzip
example.tgz | application/x-gtar
example.zip | application/zip
example.jar | application/java-archive
example.gz | application/gzip
example.bz2 | application/x-bzip2
example.rar | application/x-rar-compressed
example.zst | application/zstd
""")
public void testDoNotRecompressDefault(String filename, String contentType, WorkDir workDir) throws Exception
{
Path tmpPath = workDir.getEmptyPathDir();
Path contextDir = tmpPath.resolve("context");
FS.ensureDirExists(contextDir);

_contextHandler.setBaseResourceAsPath(contextDir);
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setEtags(true);
_contextHandler.setHandler(resourceHandler);

int filesize = 2048;

// Prepare Server File
Path file = Files.write(contextDir.resolve(filename), generateContent(filesize));
String expectedSha1Sum = Sha1Sum.calculate(file);

_server.start();

// Setup request
HttpTester.Request request = HttpTester.newRequest();
request.setMethod("GET");
request.setVersion(HttpVersion.HTTP_1_1);
request.setHeader("Host", "tester");
request.setHeader("Connection", "close");
request.setHeader("Accept-Encoding", "gzip");
request.setURI("/ctx/" + filename);

// Issue request
ByteBuffer rawResponse = _connector.getResponse(request.generate(), 5, TimeUnit.SECONDS);

// Parse response
HttpTester.Response response = HttpTester.parseResponse(rawResponse);

assertThat("Response status", response.getStatus(), is(HttpStatus.OK_200));

// Response Content-Encoding check
assertThat("Response[Content-Encoding]", response.get("Content-Encoding"), nullValue());
assertThat("Response[Vary]", response.get("Vary"), nullValue());

// Response Content checks
UncompressedMetadata metadata = parseResponseContent(response);
assertThat("(Uncompressed) Content Length", metadata.uncompressedSize, is(filesize));
assertThat("(Uncompressed) Content Hash", metadata.uncompressedSha1Sum, is(expectedSha1Sum));
}

protected FilterInputStream newContentEncodingFilterInputStream(String contentEncoding, InputStream inputStream) throws IOException
{
if (contentEncoding == null)
Expand Down

0 comments on commit 92302d6

Please sign in to comment.