From dc62e5fbc35c860008e47394f53086e036b4566c Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Thu, 17 Aug 2023 11:46:08 +0200 Subject: [PATCH] Polish "Set max request header size on Netty when using HTTP/2" See gh-36766 --- .../embedded/NettyWebServerFactoryCustomizer.java | 13 ++++++++----- .../NettyWebServerFactoryCustomizerTests.java | 11 ++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizer.java index 36ee77d7e8..1574e1973c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizer.java @@ -66,8 +66,11 @@ public class NettyWebServerFactoryCustomizer .to((idleTimeout) -> customizeIdleTimeout(factory, idleTimeout)); propertyMapper.from(nettyProperties::getMaxKeepAliveRequests) .to((maxKeepAliveRequests) -> customizeMaxKeepAliveRequests(factory, maxKeepAliveRequests)); - propertyMapper.from(this.serverProperties.getMaxHttpRequestHeaderSize()) - .to((maxHttpRequestHeaderSize) -> customizeHttp2MaxHeaderSize(factory, maxHttpRequestHeaderSize.toBytes())); + if (this.serverProperties.getHttp2() != null && this.serverProperties.getHttp2().isEnabled()) { + propertyMapper.from(this.serverProperties.getMaxHttpHeaderSize()) + .whenNonNull() + .to((size) -> customizeHttp2MaxHeaderSize(factory, size.toBytes())); + } customizeRequestDecoder(factory, propertyMapper); } @@ -120,9 +123,9 @@ public class NettyWebServerFactoryCustomizer factory.addServerCustomizers((httpServer) -> httpServer.maxKeepAliveRequests(maxKeepAliveRequests)); } - private void customizeHttp2MaxHeaderSize(NettyReactiveWebServerFactory factory, long maxHttpRequestHeaderSize) { - factory.addServerCustomizers(((httpServer) -> httpServer.http2Settings( - (http2SettingsSpecBuilder) -> http2SettingsSpecBuilder.maxHeaderListSize(maxHttpRequestHeaderSize)))); + private void customizeHttp2MaxHeaderSize(NettyReactiveWebServerFactory factory, long size) { + factory.addServerCustomizers( + ((httpServer) -> httpServer.http2Settings((settings) -> settings.maxHeaderListSize(size)))); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizerTests.java index 81ab1ebace..65795b60bd 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizerTests.java @@ -130,6 +130,7 @@ class NettyWebServerFactoryCustomizerTests { @Test void setHttp2MaxRequestHeaderSize() { DataSize headerSize = DataSize.ofKilobytes(24); + this.serverProperties.getHttp2().setEnabled(true); this.serverProperties.setMaxHttpHeaderSize(headerSize); NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class); this.customizer.customize(factory); @@ -147,8 +148,8 @@ class NettyWebServerFactoryCustomizerTests { nettyProperties.setMaxInitialLineLength(DataSize.ofKilobytes(32)); NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class); this.customizer.customize(factory); - then(factory).should(times(2)).addServerCustomizers(this.customizerCaptor.capture()); - NettyServerCustomizer serverCustomizer = this.customizerCaptor.getAllValues().get(1); + then(factory).should().addServerCustomizers(this.customizerCaptor.capture()); + NettyServerCustomizer serverCustomizer = this.customizerCaptor.getAllValues().get(0); HttpServer httpServer = serverCustomizer.apply(HttpServer.create()); HttpRequestDecoderSpec decoder = httpServer.configuration().decoder(); assertThat(decoder.validateHeaders()).isFalse(); @@ -164,7 +165,7 @@ class NettyWebServerFactoryCustomizerTests { then(factory).should(never()).addServerCustomizers(any(NettyServerCustomizer.class)); return; } - then(factory).should(times(3)).addServerCustomizers(this.customizerCaptor.capture()); + then(factory).should(times(2)).addServerCustomizers(this.customizerCaptor.capture()); NettyServerCustomizer serverCustomizer = this.customizerCaptor.getAllValues().get(0); HttpServer httpServer = serverCustomizer.apply(HttpServer.create()); Map, ?> options = httpServer.configuration().options(); @@ -176,7 +177,7 @@ class NettyWebServerFactoryCustomizerTests { then(factory).should(never()).addServerCustomizers(any(NettyServerCustomizer.class)); return; } - then(factory).should(times(3)).addServerCustomizers(this.customizerCaptor.capture()); + then(factory).should(times(2)).addServerCustomizers(this.customizerCaptor.capture()); NettyServerCustomizer serverCustomizer = this.customizerCaptor.getAllValues().get(0); HttpServer httpServer = serverCustomizer.apply(HttpServer.create()); Duration idleTimeout = httpServer.configuration().idleTimeout(); @@ -184,7 +185,7 @@ class NettyWebServerFactoryCustomizerTests { } private void verifyMaxKeepAliveRequests(NettyReactiveWebServerFactory factory, int expected) { - then(factory).should(times(3)).addServerCustomizers(this.customizerCaptor.capture()); + then(factory).should(times(2)).addServerCustomizers(this.customizerCaptor.capture()); NettyServerCustomizer serverCustomizer = this.customizerCaptor.getAllValues().get(0); HttpServer httpServer = serverCustomizer.apply(HttpServer.create()); int maxKeepAliveRequests = httpServer.configuration().maxKeepAliveRequests();