Skip to content

Commit e02803d

Browse files
committed
Configure max HTTP header size when using HTTP2 with Tomcat
Closes gh-31322
1 parent ed897fc commit e02803d

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,11 @@ private void customizeMaxHttpHeaderSize(ConfigurableTomcatWebServerFactory facto
280280
if (handler instanceof AbstractHttp11Protocol) {
281281
AbstractHttp11Protocol protocol = (AbstractHttp11Protocol) handler;
282282
protocol.setMaxHttpHeaderSize(maxHttpHeaderSize);
283+
for (UpgradeProtocol upgradeProtocol : protocol.findUpgradeProtocols()) {
284+
if (upgradeProtocol instanceof Http2Protocol) {
285+
((Http2Protocol) upgradeProtocol).setMaxHeaderSize(maxHttpHeaderSize);
286+
}
287+
}
283288
}
284289
});
285290
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,19 @@ void customMaxHttpHeaderSize() {
182182
.getMaxHttpHeaderSize()).isEqualTo(DataSize.ofKilobytes(1).toBytes()));
183183
}
184184

185+
@Test
186+
void customMaxHttpHeaderSizeWithHttp2() {
187+
bind("server.max-http-header-size=1KB", "server.http2.enabled=true");
188+
customizeAndRunServer((server) -> {
189+
AbstractHttp11Protocol<?> protocolHandler = (AbstractHttp11Protocol<?>) server.getTomcat().getConnector()
190+
.getProtocolHandler();
191+
long expectedSize = DataSize.ofKilobytes(1).toBytes();
192+
assertThat(protocolHandler.getMaxHttpHeaderSize()).isEqualTo(expectedSize);
193+
assertThat(((Http2Protocol) protocolHandler.getUpgradeProtocol("h2c")).getMaxHeaderSize())
194+
.isEqualTo(expectedSize);
195+
});
196+
}
197+
185198
@Test
186199
void customMaxHttpHeaderSizeIgnoredIfNegative() {
187200
bind("server.max-http-header-size=-1");

0 commit comments

Comments
 (0)