From 0e13fd039d85110ca0bf91f4c59ae7a8bc60f0c1 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Tue, 8 Jan 2019 14:14:10 +0100 Subject: [PATCH] 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 --- .../CompressionConnectorCustomizer.java | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/CompressionConnectorCustomizer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/CompressionConnectorCustomizer.java index e9d1d64d74b..08041743fde 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/CompressionConnectorCustomizer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/CompressionConnectorCustomizer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,9 @@ package org.springframework.boot.web.embedded.tomcat; import org.apache.catalina.connector.Connector; import org.apache.coyote.ProtocolHandler; +import org.apache.coyote.UpgradeProtocol; import org.apache.coyote.http11.AbstractHttp11Protocol; +import org.apache.coyote.http2.Http2Protocol; import org.springframework.boot.web.server.Compression; import org.springframework.util.StringUtils; @@ -44,6 +46,24 @@ class CompressionConnectorCustomizer implements TomcatConnectorCustomizer { if (handler instanceof AbstractHttp11Protocol) { customize((AbstractHttp11Protocol) handler); } + for (UpgradeProtocol upgradeProtocol : connector.findUpgradeProtocols()) { + if (upgradeProtocol instanceof Http2Protocol) { + customize((Http2Protocol) upgradeProtocol); + } + } + } + } + + private void customize(Http2Protocol upgradeProtocol) { + Compression compression = this.compression; + upgradeProtocol.setCompression("on"); + upgradeProtocol.setCompressionMinSize(compression.getMinResponseSize()); + upgradeProtocol.setCompressibleMimeType( + StringUtils.arrayToCommaDelimitedString(compression.getMimeTypes())); + if (this.compression.getExcludedUserAgents() != null) { + upgradeProtocol + .setNoCompressionUserAgents(StringUtils.arrayToCommaDelimitedString( + this.compression.getExcludedUserAgents())); } }