Skip to content

Commit dc62e5f

Browse files
committed
Polish "Set max request header size on Netty when using HTTP/2"
See gh-36766
1 parent ee5b23b commit dc62e5f

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +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-
propertyMapper.from(this.serverProperties.getMaxHttpRequestHeaderSize())
70-
.to((maxHttpRequestHeaderSize) -> customizeHttp2MaxHeaderSize(factory, maxHttpRequestHeaderSize.toBytes()));
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+
}
7174
customizeRequestDecoder(factory, propertyMapper);
7275
}
7376

@@ -120,9 +123,9 @@ private void customizeMaxKeepAliveRequests(NettyReactiveWebServerFactory factory
120123
factory.addServerCustomizers((httpServer) -> httpServer.maxKeepAliveRequests(maxKeepAliveRequests));
121124
}
122125

123-
private void customizeHttp2MaxHeaderSize(NettyReactiveWebServerFactory factory, long maxHttpRequestHeaderSize) {
124-
factory.addServerCustomizers(((httpServer) -> httpServer.http2Settings(
125-
(http2SettingsSpecBuilder) -> http2SettingsSpecBuilder.maxHeaderListSize(maxHttpRequestHeaderSize))));
126+
private void customizeHttp2MaxHeaderSize(NettyReactiveWebServerFactory factory, long size) {
127+
factory.addServerCustomizers(
128+
((httpServer) -> httpServer.http2Settings((settings) -> settings.maxHeaderListSize(size))));
126129
}
127130

128131
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ void setMaxKeepAliveRequests() {
130130
@Test
131131
void setHttp2MaxRequestHeaderSize() {
132132
DataSize headerSize = DataSize.ofKilobytes(24);
133+
this.serverProperties.getHttp2().setEnabled(true);
133134
this.serverProperties.setMaxHttpHeaderSize(headerSize);
134135
NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class);
135136
this.customizer.customize(factory);
@@ -147,8 +148,8 @@ void configureHttpRequestDecoder() {
147148
nettyProperties.setMaxInitialLineLength(DataSize.ofKilobytes(32));
148149
NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class);
149150
this.customizer.customize(factory);
150-
then(factory).should(times(2)).addServerCustomizers(this.customizerCaptor.capture());
151-
NettyServerCustomizer serverCustomizer = this.customizerCaptor.getAllValues().get(1);
151+
then(factory).should().addServerCustomizers(this.customizerCaptor.capture());
152+
NettyServerCustomizer serverCustomizer = this.customizerCaptor.getAllValues().get(0);
152153
HttpServer httpServer = serverCustomizer.apply(HttpServer.create());
153154
HttpRequestDecoderSpec decoder = httpServer.configuration().decoder();
154155
assertThat(decoder.validateHeaders()).isFalse();
@@ -164,7 +165,7 @@ private void verifyConnectionTimeout(NettyReactiveWebServerFactory factory, Inte
164165
then(factory).should(never()).addServerCustomizers(any(NettyServerCustomizer.class));
165166
return;
166167
}
167-
then(factory).should(times(3)).addServerCustomizers(this.customizerCaptor.capture());
168+
then(factory).should(times(2)).addServerCustomizers(this.customizerCaptor.capture());
168169
NettyServerCustomizer serverCustomizer = this.customizerCaptor.getAllValues().get(0);
169170
HttpServer httpServer = serverCustomizer.apply(HttpServer.create());
170171
Map<ChannelOption<?>, ?> options = httpServer.configuration().options();
@@ -176,15 +177,15 @@ private void verifyIdleTimeout(NettyReactiveWebServerFactory factory, Duration e
176177
then(factory).should(never()).addServerCustomizers(any(NettyServerCustomizer.class));
177178
return;
178179
}
179-
then(factory).should(times(3)).addServerCustomizers(this.customizerCaptor.capture());
180+
then(factory).should(times(2)).addServerCustomizers(this.customizerCaptor.capture());
180181
NettyServerCustomizer serverCustomizer = this.customizerCaptor.getAllValues().get(0);
181182
HttpServer httpServer = serverCustomizer.apply(HttpServer.create());
182183
Duration idleTimeout = httpServer.configuration().idleTimeout();
183184
assertThat(idleTimeout).isEqualTo(expected);
184185
}
185186

186187
private void verifyMaxKeepAliveRequests(NettyReactiveWebServerFactory factory, int expected) {
187-
then(factory).should(times(3)).addServerCustomizers(this.customizerCaptor.capture());
188+
then(factory).should(times(2)).addServerCustomizers(this.customizerCaptor.capture());
188189
NettyServerCustomizer serverCustomizer = this.customizerCaptor.getAllValues().get(0);
189190
HttpServer httpServer = serverCustomizer.apply(HttpServer.create());
190191
int maxKeepAliveRequests = httpServer.configuration().maxKeepAliveRequests();

0 commit comments

Comments
 (0)