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

Issue #12397 - Add more default excludes to GzipHandler. #12414

Merged
merged 2 commits into from
Oct 23, 2024
Merged
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
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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems a very broad brush stroke. Also I don't see this mimetype listed on https://www.iana.org/assignments/media-types/media-types.txt

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was taken from our mime.properties file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yikes

_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
Loading