Skip to content

Commit 0e13fd0

Browse files
committed
Configure HTTP compression for HTTP/2 with Tomcat
This commit ensures that the compression options are also applied on the `Http2Protocol` when http/2 is enabled with Tomcat. Closes gh-15591
1 parent 7330b8b commit 0e13fd0

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/CompressionConnectorCustomizer.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,9 @@
1818

1919
import org.apache.catalina.connector.Connector;
2020
import org.apache.coyote.ProtocolHandler;
21+
import org.apache.coyote.UpgradeProtocol;
2122
import org.apache.coyote.http11.AbstractHttp11Protocol;
23+
import org.apache.coyote.http2.Http2Protocol;
2224

2325
import org.springframework.boot.web.server.Compression;
2426
import org.springframework.util.StringUtils;
@@ -44,6 +46,24 @@ public void customize(Connector connector) {
4446
if (handler instanceof AbstractHttp11Protocol) {
4547
customize((AbstractHttp11Protocol<?>) handler);
4648
}
49+
for (UpgradeProtocol upgradeProtocol : connector.findUpgradeProtocols()) {
50+
if (upgradeProtocol instanceof Http2Protocol) {
51+
customize((Http2Protocol) upgradeProtocol);
52+
}
53+
}
54+
}
55+
}
56+
57+
private void customize(Http2Protocol upgradeProtocol) {
58+
Compression compression = this.compression;
59+
upgradeProtocol.setCompression("on");
60+
upgradeProtocol.setCompressionMinSize(compression.getMinResponseSize());
61+
upgradeProtocol.setCompressibleMimeType(
62+
StringUtils.arrayToCommaDelimitedString(compression.getMimeTypes()));
63+
if (this.compression.getExcludedUserAgents() != null) {
64+
upgradeProtocol
65+
.setNoCompressionUserAgents(StringUtils.arrayToCommaDelimitedString(
66+
this.compression.getExcludedUserAgents()));
4767
}
4868
}
4969

0 commit comments

Comments
 (0)