Adding CompressionConfig.Builder.from(MimeTypes)

This commit is contained in:
Joakim Erdfelt 2024-09-26 12:59:57 -05:00
parent 38d501a698
commit b46d075ad3
No known key found for this signature in database
GPG Key ID: 2D0E1FB8FE4B68B4
3 changed files with 56 additions and 3 deletions

View File

@ -16,9 +16,11 @@ package org.eclipse.jetty.compression.server;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.http.PreEncodedHttpField;
import org.eclipse.jetty.http.pathmap.PathSpecSet;
import org.eclipse.jetty.server.Request;
@ -181,7 +183,7 @@ public class CompressionConfig extends AbstractLifeCycle
if (matchedEncoding == null)
return null;
if (!compressMimeTypes.test(request.getMethod()))
if (!compressMethods.test(request.getMethod()))
return null;
if (!compressPaths.test(pathInContext))
@ -586,6 +588,53 @@ public class CompressionConfig extends AbstractLifeCycle
return this;
}
/**
* Setup MimeType exclusion and path exclusion from the provided {@link MimeTypes} configuration.
*
* @param mimeTypes the mime types to iterate.
* @return this builder.
*/
public Builder from(MimeTypes mimeTypes)
{
for (String type : mimeTypes.getMimeMap().values())
{
if ("image/svg+xml".equals(type))
{
compressMimeTypeExclude(type);
decompressMimeTypeExclude(type);
compressPathExclude("*.svgz");
decompressPathExclude("*.svgz");
}
else if (type.startsWith("image/") ||
type.startsWith("audio/") ||
type.startsWith("video/"))
{
compressMimeTypeExclude(type);
decompressMimeTypeExclude(type);
}
}
Stream.of("application/compress",
"application/zip",
"application/gzip",
"application/x-bzip2",
"application/brotli",
"application/x-br",
"application/x-xz",
"application/x-rar-compressed",
"application/vnd.bzip3",
"application/zstd",
// It is possible to use SSE with CompressionHandler, but only if you use `gzip` encoding with syncFlush to true which will impact performance.
"text/event-stream"
).forEach((type) ->
{
compressMimeTypeExclude(type);
decompressMimeTypeExclude(type);
});
return this;
}
/**
* Initialize builder with existing {@link CompressionConfig}
*

View File

@ -24,6 +24,7 @@ import org.eclipse.jetty.http.EtagUtils;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.http.pathmap.MatchedResource;
import org.eclipse.jetty.http.pathmap.PathMappings;
import org.eclipse.jetty.http.pathmap.PathSpec;
@ -224,7 +225,10 @@ public class CompressionHandler extends Handler.Wrapper
if (pathConfigs.isEmpty())
{
// add default configuration if no paths have been configured.
pathConfigs.put("/", CompressionConfig.builder().build());
pathConfigs.put("/",
CompressionConfig.builder()
.from(MimeTypes.DEFAULTS)
.build());
}
super.doStart();

View File

@ -435,7 +435,7 @@ public class CompressionHandlerTest extends AbstractCompressionTest
/**
* Testing how CompressionHandler acts with a single compression implementation added.
* Configuration is only using {@code decompressMethods} excluding {@code PUT}, and including both
* {@code GET} and {@code POST}
* {@code GET} and {@code POST}. This is focused on the decompression of request bodies.
*/
@ParameterizedTest
@CsvSource(textBlock = """