mirror of
https://github.com/spring-projects/spring-boot.git
synced 2026-09-17 12:09:16 +00:00
Provide support for adding to the mime types that are compressed
Signed-off-by: jdsalasca <jdsalasc@unal.edu.co> See gh-48930
This commit is contained in:
committed by
Andy Wilkinson
parent
d8dddc9a15
commit
b1e9fdc3ec
+1
-1
@@ -46,7 +46,7 @@ final class JettyHandlerWrappers {
|
||||
gzip.setMinCompressSize((int) compression.getMinResponseSize().toBytes());
|
||||
compressionHandler.putCompression(gzip);
|
||||
Builder configBuilder = CompressionConfig.builder();
|
||||
for (String mimeType : compression.getMimeTypes()) {
|
||||
for (String mimeType : compression.getAllMimeTypes()) {
|
||||
configBuilder.compressIncludeMimeType(mimeType);
|
||||
}
|
||||
for (HttpMethod httpMethod : HttpMethod.values()) {
|
||||
|
||||
+1
-1
@@ -56,7 +56,7 @@ final class CompressionCustomizer implements NettyServerCustomizer {
|
||||
if (!this.compression.getMinResponseSize().isNegative()) {
|
||||
server = server.compress((int) this.compression.getMinResponseSize().toBytes());
|
||||
}
|
||||
CompressionPredicate mimeTypes = getMimeTypesPredicate(this.compression.getMimeTypes());
|
||||
CompressionPredicate mimeTypes = getMimeTypesPredicate(this.compression.getAllMimeTypes());
|
||||
CompressionPredicate excludedUserAgents = getExcludedUserAgentsPredicate(
|
||||
this.compression.getExcludedUserAgents());
|
||||
server = server.compress(mimeTypes.and(excludedUserAgents));
|
||||
|
||||
+1
-1
@@ -63,7 +63,7 @@ public class CompressionConnectorCustomizer implements TomcatConnectorCustomizer
|
||||
}
|
||||
|
||||
private String getMimeTypes(Compression compression) {
|
||||
return StringUtils.arrayToCommaDelimitedString(compression.getMimeTypes());
|
||||
return StringUtils.arrayToCommaDelimitedString(compression.getAllMimeTypes());
|
||||
}
|
||||
|
||||
private String getExcludedUserAgents(Compression compression) {
|
||||
|
||||
+23
@@ -19,6 +19,7 @@ package org.springframework.boot.web.server;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationPropertiesSource;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.util.unit.DataSize;
|
||||
|
||||
/**
|
||||
@@ -43,6 +44,11 @@ public class Compression {
|
||||
private String[] mimeTypes = new String[] { "text/html", "text/xml", "text/plain", "text/css", "text/javascript",
|
||||
"application/javascript", "application/json", "application/xml" };
|
||||
|
||||
/**
|
||||
* Comma-separated list of additional MIME types that should be compressed.
|
||||
*/
|
||||
private String[] additionalMimeTypes = new String[0];
|
||||
|
||||
/**
|
||||
* Comma-separated list of user agents for which responses should not be compressed.
|
||||
*/
|
||||
@@ -77,6 +83,23 @@ public class Compression {
|
||||
this.mimeTypes = mimeTypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the MIME types that should be compressed, including any additional types.
|
||||
* @return the MIME types that should be compressed
|
||||
*/
|
||||
public String[] getAllMimeTypes() {
|
||||
String[] combined = StringUtils.concatenateStringArrays(this.mimeTypes, this.additionalMimeTypes);
|
||||
return (combined != null) ? combined : this.mimeTypes;
|
||||
}
|
||||
|
||||
public String[] getAdditionalMimeTypes() {
|
||||
return this.additionalMimeTypes;
|
||||
}
|
||||
|
||||
public void setAdditionalMimeTypes(String[] additionalMimeTypes) {
|
||||
this.additionalMimeTypes = additionalMimeTypes;
|
||||
}
|
||||
|
||||
public String @Nullable [] getExcludedUserAgents() {
|
||||
return this.excludedUserAgents;
|
||||
}
|
||||
|
||||
+8
@@ -27,6 +27,7 @@ import org.springframework.boot.context.properties.bind.Bindable;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
|
||||
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
|
||||
import org.springframework.boot.web.server.Compression;
|
||||
import org.springframework.boot.web.server.MimeMappings;
|
||||
import org.springframework.boot.web.server.MimeMappings.Mapping;
|
||||
import org.springframework.util.unit.DataSize;
|
||||
@@ -148,6 +149,13 @@ class ServerPropertiesTests {
|
||||
.isEqualTo(new Http11Nio2Protocol().getMaxHttpRequestHeaderSize());
|
||||
}
|
||||
|
||||
@Test
|
||||
void additionalCompressionMimeTypesAreAddedToDefaults() {
|
||||
bind("server.compression.additional-mime-types", "application/zip");
|
||||
assertThat(this.properties.getCompression().getAllMimeTypes()).contains(new Compression().getMimeTypes());
|
||||
assertThat(this.properties.getCompression().getAllMimeTypes()).contains("application/zip");
|
||||
}
|
||||
|
||||
private void bind(String name, String value) {
|
||||
bind(Collections.singletonMap(name, value));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user