From e02803d341b2be03718a15339f66eda729ce0277 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 10 Jun 2022 11:32:57 +0100 Subject: [PATCH] Configure max HTTP header size when using HTTP2 with Tomcat Closes gh-31322 --- .../embedded/TomcatWebServerFactoryCustomizer.java | 5 +++++ .../TomcatWebServerFactoryCustomizerTests.java | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java index e53729ab1c..deb41dff14 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java @@ -280,6 +280,11 @@ public class TomcatWebServerFactoryCustomizer if (handler instanceof AbstractHttp11Protocol) { AbstractHttp11Protocol protocol = (AbstractHttp11Protocol) handler; protocol.setMaxHttpHeaderSize(maxHttpHeaderSize); + for (UpgradeProtocol upgradeProtocol : protocol.findUpgradeProtocols()) { + if (upgradeProtocol instanceof Http2Protocol) { + ((Http2Protocol) upgradeProtocol).setMaxHeaderSize(maxHttpHeaderSize); + } + } } }); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java index 48af84f5e5..a12d66738b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java @@ -182,6 +182,19 @@ class TomcatWebServerFactoryCustomizerTests { .getMaxHttpHeaderSize()).isEqualTo(DataSize.ofKilobytes(1).toBytes())); } + @Test + void customMaxHttpHeaderSizeWithHttp2() { + bind("server.max-http-header-size=1KB", "server.http2.enabled=true"); + customizeAndRunServer((server) -> { + AbstractHttp11Protocol protocolHandler = (AbstractHttp11Protocol) server.getTomcat().getConnector() + .getProtocolHandler(); + long expectedSize = DataSize.ofKilobytes(1).toBytes(); + assertThat(protocolHandler.getMaxHttpHeaderSize()).isEqualTo(expectedSize); + assertThat(((Http2Protocol) protocolHandler.getUpgradeProtocol("h2c")).getMaxHeaderSize()) + .isEqualTo(expectedSize); + }); + } + @Test void customMaxHttpHeaderSizeIgnoredIfNegative() { bind("server.max-http-header-size=-1");