Skip to content

Commit efb87fd

Browse files
committed
Merge pull request #36766 from NersesAM
* pr/36766: Polish "Set max request header size on Netty when using HTTP/2" Set max request header size on Netty when using HTTP/2 Closes gh-36766
2 parents da7eea4 + dc62e5f commit efb87fd

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public void customize(NettyReactiveWebServerFactory factory) {
6666
.to((idleTimeout) -> customizeIdleTimeout(factory, idleTimeout));
6767
propertyMapper.from(nettyProperties::getMaxKeepAliveRequests)
6868
.to((maxKeepAliveRequests) -> customizeMaxKeepAliveRequests(factory, maxKeepAliveRequests));
69+
if (this.serverProperties.getHttp2() != null && this.serverProperties.getHttp2().isEnabled()) {
70+
propertyMapper.from(this.serverProperties.getMaxHttpHeaderSize())
71+
.whenNonNull()
72+
.to((size) -> customizeHttp2MaxHeaderSize(factory, size.toBytes()));
73+
}
6974
customizeRequestDecoder(factory, propertyMapper);
7075
}
7176

@@ -118,4 +123,9 @@ private void customizeMaxKeepAliveRequests(NettyReactiveWebServerFactory factory
118123
factory.addServerCustomizers((httpServer) -> httpServer.maxKeepAliveRequests(maxKeepAliveRequests));
119124
}
120125

126+
private void customizeHttp2MaxHeaderSize(NettyReactiveWebServerFactory factory, long size) {
127+
factory.addServerCustomizers(
128+
((httpServer) -> httpServer.http2Settings((settings) -> settings.maxHeaderListSize(size))));
129+
}
130+
121131
}

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.mockito.ArgumentCaptor;
2727
import org.mockito.Captor;
2828
import org.mockito.junit.jupiter.MockitoExtension;
29+
import reactor.netty.http.Http2SettingsSpec;
2930
import reactor.netty.http.server.HttpRequestDecoderSpec;
3031
import reactor.netty.http.server.HttpServer;
3132

@@ -126,9 +127,20 @@ void setMaxKeepAliveRequests() {
126127
verifyMaxKeepAliveRequests(factory, 100);
127128
}
128129

130+
@Test
131+
void setHttp2MaxRequestHeaderSize() {
132+
DataSize headerSize = DataSize.ofKilobytes(24);
133+
this.serverProperties.getHttp2().setEnabled(true);
134+
this.serverProperties.setMaxHttpHeaderSize(headerSize);
135+
NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class);
136+
this.customizer.customize(factory);
137+
verifyHttp2MaxHeaderSize(factory, headerSize.toBytes());
138+
}
139+
129140
@Test
130141
void configureHttpRequestDecoder() {
131142
ServerProperties.Netty nettyProperties = this.serverProperties.getNetty();
143+
this.serverProperties.setMaxHttpHeaderSize(DataSize.ofKilobytes(24));
132144
nettyProperties.setValidateHeaders(false);
133145
nettyProperties.setInitialBufferSize(DataSize.ofBytes(512));
134146
nettyProperties.setH2cMaxContentLength(DataSize.ofKilobytes(1));
@@ -137,10 +149,11 @@ void configureHttpRequestDecoder() {
137149
NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class);
138150
this.customizer.customize(factory);
139151
then(factory).should().addServerCustomizers(this.customizerCaptor.capture());
140-
NettyServerCustomizer serverCustomizer = this.customizerCaptor.getValue();
152+
NettyServerCustomizer serverCustomizer = this.customizerCaptor.getAllValues().get(0);
141153
HttpServer httpServer = serverCustomizer.apply(HttpServer.create());
142154
HttpRequestDecoderSpec decoder = httpServer.configuration().decoder();
143155
assertThat(decoder.validateHeaders()).isFalse();
156+
assertThat(decoder.maxHeaderSize()).isEqualTo(this.serverProperties.getMaxHttpHeaderSize().toBytes());
144157
assertThat(decoder.initialBufferSize()).isEqualTo(nettyProperties.getInitialBufferSize().toBytes());
145158
assertThat(decoder.h2cMaxContentLength()).isEqualTo(nettyProperties.getH2cMaxContentLength().toBytes());
146159
assertThat(decoder.maxChunkSize()).isEqualTo(nettyProperties.getMaxChunkSize().toBytes());
@@ -179,4 +192,12 @@ private void verifyMaxKeepAliveRequests(NettyReactiveWebServerFactory factory, i
179192
assertThat(maxKeepAliveRequests).isEqualTo(expected);
180193
}
181194

195+
private void verifyHttp2MaxHeaderSize(NettyReactiveWebServerFactory factory, long expected) {
196+
then(factory).should(times(2)).addServerCustomizers(this.customizerCaptor.capture());
197+
NettyServerCustomizer serverCustomizer = this.customizerCaptor.getAllValues().get(0);
198+
HttpServer httpServer = serverCustomizer.apply(HttpServer.create());
199+
Http2SettingsSpec decoder = httpServer.configuration().http2SettingsSpec();
200+
assertThat(decoder.maxHeaderListSize()).isEqualTo(expected);
201+
}
202+
182203
}

0 commit comments

Comments
 (0)