diff --git a/jetty-core/jetty-compression/jetty-compression-server/src/main/java/org/eclipse/jetty/compression/server/CompressionConfig.java b/jetty-core/jetty-compression/jetty-compression-server/src/main/java/org/eclipse/jetty/compression/server/CompressionConfig.java index 7f8825ebfc4..001a68c8e63 100644 --- a/jetty-core/jetty-compression/jetty-compression-server/src/main/java/org/eclipse/jetty/compression/server/CompressionConfig.java +++ b/jetty-core/jetty-compression/jetty-compression-server/src/main/java/org/eclipse/jetty/compression/server/CompressionConfig.java @@ -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} * diff --git a/jetty-core/jetty-compression/jetty-compression-server/src/main/java/org/eclipse/jetty/compression/server/CompressionHandler.java b/jetty-core/jetty-compression/jetty-compression-server/src/main/java/org/eclipse/jetty/compression/server/CompressionHandler.java index 6da11a8ca2b..101e425ab6a 100644 --- a/jetty-core/jetty-compression/jetty-compression-server/src/main/java/org/eclipse/jetty/compression/server/CompressionHandler.java +++ b/jetty-core/jetty-compression/jetty-compression-server/src/main/java/org/eclipse/jetty/compression/server/CompressionHandler.java @@ -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(); diff --git a/jetty-core/jetty-compression/jetty-compression-tests/src/test/java/org/eclipse/jetty/compression/CompressionHandlerTest.java b/jetty-core/jetty-compression/jetty-compression-tests/src/test/java/org/eclipse/jetty/compression/CompressionHandlerTest.java index 9f419dc0b75..c5d96fe1ad4 100644 --- a/jetty-core/jetty-compression/jetty-compression-tests/src/test/java/org/eclipse/jetty/compression/CompressionHandlerTest.java +++ b/jetty-core/jetty-compression/jetty-compression-tests/src/test/java/org/eclipse/jetty/compression/CompressionHandlerTest.java @@ -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 = """