Configure max HTTP header size when using HTTP2 with Tomcat

Closes gh-31322
pull/31676/head
Andy Wilkinson 2 years ago
parent ed897fc922
commit e02803d341

@ -280,6 +280,11 @@ public class TomcatWebServerFactoryCustomizer
if (handler instanceof AbstractHttp11Protocol) { if (handler instanceof AbstractHttp11Protocol) {
AbstractHttp11Protocol protocol = (AbstractHttp11Protocol) handler; AbstractHttp11Protocol protocol = (AbstractHttp11Protocol) handler;
protocol.setMaxHttpHeaderSize(maxHttpHeaderSize); protocol.setMaxHttpHeaderSize(maxHttpHeaderSize);
for (UpgradeProtocol upgradeProtocol : protocol.findUpgradeProtocols()) {
if (upgradeProtocol instanceof Http2Protocol) {
((Http2Protocol) upgradeProtocol).setMaxHeaderSize(maxHttpHeaderSize);
}
}
} }
}); });
} }

@ -182,6 +182,19 @@ class TomcatWebServerFactoryCustomizerTests {
.getMaxHttpHeaderSize()).isEqualTo(DataSize.ofKilobytes(1).toBytes())); .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 @Test
void customMaxHttpHeaderSizeIgnoredIfNegative() { void customMaxHttpHeaderSizeIgnoredIfNegative() {
bind("server.max-http-header-size=-1"); bind("server.max-http-header-size=-1");

Loading…
Cancel
Save