Skip to content

Commit f068f9f

Browse files
Julien Eyraudwilkinsona
authored andcommitted
Add properties for Netty HttpDecoderSpec
See gh-22367
1 parent 530a267 commit f068f9f

File tree

3 files changed

+125
-6
lines changed

3 files changed

+125
-6
lines changed

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,31 @@ public static class Netty {
13961396
*/
13971397
private Duration connectionTimeout;
13981398

1399+
/**
1400+
* The maximum chunk size that can be decoded for the HTTP request.
1401+
*/
1402+
private DataSize maxChunkSize;
1403+
1404+
/**
1405+
* The maximum length that can be decoded for the HTTP request's initial line.
1406+
*/
1407+
private DataSize maxInitialLineLength;
1408+
1409+
/**
1410+
* Configure whether or not to validate headers when decoding requests.
1411+
*/
1412+
private Boolean validateHeaders;
1413+
1414+
/**
1415+
* The maximum length of the content of the HTTP/2.0 clear-text upgrade request.
1416+
*/
1417+
private DataSize h2cMaxContentLength;
1418+
1419+
/**
1420+
* The initial buffer size for HTTP request decoding.
1421+
*/
1422+
private DataSize initialBufferSize;
1423+
13991424
public Duration getConnectionTimeout() {
14001425
return this.connectionTimeout;
14011426
}
@@ -1404,6 +1429,46 @@ public void setConnectionTimeout(Duration connectionTimeout) {
14041429
this.connectionTimeout = connectionTimeout;
14051430
}
14061431

1432+
public DataSize getMaxChunkSize() {
1433+
return this.maxChunkSize;
1434+
}
1435+
1436+
public void setMaxChunkSize(DataSize maxChunkSize) {
1437+
this.maxChunkSize = maxChunkSize;
1438+
}
1439+
1440+
public DataSize getMaxInitialLineLength() {
1441+
return this.maxInitialLineLength;
1442+
}
1443+
1444+
public void setMaxInitialLineLength(DataSize maxInitialLineLength) {
1445+
this.maxInitialLineLength = maxInitialLineLength;
1446+
}
1447+
1448+
public Boolean getValidateHeaders() {
1449+
return this.validateHeaders;
1450+
}
1451+
1452+
public void setValidateHeaders(Boolean validateHeaders) {
1453+
this.validateHeaders = validateHeaders;
1454+
}
1455+
1456+
public DataSize getH2cMaxContentLength() {
1457+
return this.h2cMaxContentLength;
1458+
}
1459+
1460+
public void setH2cMaxContentLength(DataSize h2cMaxContentLength) {
1461+
this.h2cMaxContentLength = h2cMaxContentLength;
1462+
}
1463+
1464+
public DataSize getInitialBufferSize() {
1465+
return this.initialBufferSize;
1466+
}
1467+
1468+
public void setInitialBufferSize(DataSize initialBufferSize) {
1469+
this.initialBufferSize = initialBufferSize;
1470+
}
1471+
14071472
}
14081473

14091474
/**

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

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.springframework.boot.autoconfigure.web.embedded;
1818

1919
import java.time.Duration;
20+
import java.util.Objects;
21+
import java.util.stream.Stream;
2022

2123
import io.netty.channel.ChannelOption;
2224

@@ -27,7 +29,6 @@
2729
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
2830
import org.springframework.core.Ordered;
2931
import org.springframework.core.env.Environment;
30-
import org.springframework.util.unit.DataSize;
3132

3233
/**
3334
* Customization for Netty-specific features.
@@ -58,11 +59,16 @@ public int getOrder() {
5859
public void customize(NettyReactiveWebServerFactory factory) {
5960
factory.setUseForwardHeaders(getOrDeduceUseForwardHeaders());
6061
PropertyMapper propertyMapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
61-
propertyMapper.from(this.serverProperties::getMaxHttpHeaderSize)
62-
.to((maxHttpRequestHeaderSize) -> customizeMaxHttpHeaderSize(factory, maxHttpRequestHeaderSize));
6362
ServerProperties.Netty nettyProperties = this.serverProperties.getNetty();
6463
propertyMapper.from(nettyProperties::getConnectionTimeout).whenNonNull()
6564
.to((connectionTimeout) -> customizeConnectionTimeout(factory, connectionTimeout));
65+
if (Stream
66+
.of(this.serverProperties.getMaxHttpHeaderSize(), nettyProperties.getH2cMaxContentLength(),
67+
nettyProperties.getMaxChunkSize(), nettyProperties.getMaxInitialLineLength(),
68+
nettyProperties.getValidateHeaders(), nettyProperties.getInitialBufferSize())
69+
.anyMatch(Objects::nonNull)) {
70+
customizeRequestDecoder(factory, propertyMapper);
71+
}
6672
}
6773

6874
private boolean getOrDeduceUseForwardHeaders() {
@@ -73,9 +79,26 @@ private boolean getOrDeduceUseForwardHeaders() {
7379
return this.serverProperties.getForwardHeadersStrategy().equals(ServerProperties.ForwardHeadersStrategy.NATIVE);
7480
}
7581

76-
private void customizeMaxHttpHeaderSize(NettyReactiveWebServerFactory factory, DataSize maxHttpHeaderSize) {
77-
factory.addServerCustomizers((httpServer) -> httpServer.httpRequestDecoder(
78-
(httpRequestDecoderSpec) -> httpRequestDecoderSpec.maxHeaderSize((int) maxHttpHeaderSize.toBytes())));
82+
private void customizeRequestDecoder(NettyReactiveWebServerFactory factory, PropertyMapper propertyMapper) {
83+
factory.addServerCustomizers((httpServer) -> httpServer.httpRequestDecoder((httpRequestDecoderSpec) -> {
84+
propertyMapper.from(this.serverProperties.getMaxHttpHeaderSize()).whenNonNull()
85+
.to((maxHttpRequestHeader) -> httpRequestDecoderSpec
86+
.maxHeaderSize((int) maxHttpRequestHeader.toBytes()));
87+
ServerProperties.Netty nettyProperties = this.serverProperties.getNetty();
88+
propertyMapper.from(nettyProperties.getMaxChunkSize()).whenNonNull()
89+
.to((maxChunkSize) -> httpRequestDecoderSpec.maxChunkSize((int) maxChunkSize.toBytes()));
90+
propertyMapper.from(nettyProperties.getMaxInitialLineLength()).whenNonNull()
91+
.to((maxInitialLineLength) -> httpRequestDecoderSpec
92+
.maxInitialLineLength((int) maxInitialLineLength.toBytes()));
93+
propertyMapper.from(nettyProperties.getH2cMaxContentLength()).whenNonNull()
94+
.to((h2cMaxContentLength) -> httpRequestDecoderSpec
95+
.h2cMaxContentLength((int) h2cMaxContentLength.toBytes()));
96+
propertyMapper.from(nettyProperties.getInitialBufferSize()).whenNonNull().to(
97+
(initialBufferSize) -> httpRequestDecoderSpec.initialBufferSize((int) initialBufferSize.toBytes()));
98+
propertyMapper.from(nettyProperties.getValidateHeaders()).whenNonNull()
99+
.to(httpRequestDecoderSpec::validateHeaders);
100+
return httpRequestDecoderSpec;
101+
}));
79102
}
80103

81104
private void customizeConnectionTimeout(NettyReactiveWebServerFactory factory, Duration connectionTimeout) {

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.mockito.ArgumentCaptor;
2626
import org.mockito.Captor;
2727
import org.mockito.MockitoAnnotations;
28+
import reactor.netty.http.server.HttpRequestDecoderSpec;
2829
import reactor.netty.http.server.HttpServer;
2930

3031
import org.springframework.boot.autoconfigure.web.ServerProperties;
@@ -33,6 +34,7 @@
3334
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
3435
import org.springframework.boot.web.embedded.netty.NettyServerCustomizer;
3536
import org.springframework.mock.env.MockEnvironment;
37+
import org.springframework.util.unit.DataSize;
3638

3739
import static org.assertj.core.api.Assertions.assertThat;
3840
import static org.mockito.ArgumentMatchers.any;
@@ -107,6 +109,35 @@ void setConnectionTimeout() {
107109
verifyConnectionTimeout(factory, 1000);
108110
}
109111

112+
@Test
113+
void setHttpRequestDecoder() {
114+
ServerProperties.Netty nettyProperties = this.serverProperties.getNetty();
115+
nettyProperties.setValidateHeaders(true);
116+
nettyProperties.setInitialBufferSize(DataSize.ofBytes(512));
117+
nettyProperties.setH2cMaxContentLength(DataSize.ofKilobytes(1));
118+
nettyProperties.setMaxChunkSize(DataSize.ofKilobytes(16));
119+
nettyProperties.setMaxInitialLineLength(DataSize.ofKilobytes(32));
120+
NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class);
121+
this.customizer.customize(factory);
122+
verify(factory, times(1)).addServerCustomizers(this.customizerCaptor.capture());
123+
NettyServerCustomizer serverCustomizer = this.customizerCaptor.getValue();
124+
HttpServer httpServer = serverCustomizer.apply(HttpServer.create());
125+
HttpRequestDecoderSpec decoder = httpServer.configuration().decoder();
126+
assertThat(decoder.validateHeaders()).isTrue();
127+
assertThat(decoder.initialBufferSize()).isEqualTo(nettyProperties.getInitialBufferSize().toBytes());
128+
assertThat(decoder.h2cMaxContentLength()).isEqualTo(nettyProperties.getH2cMaxContentLength().toBytes());
129+
assertThat(decoder.maxChunkSize()).isEqualTo(nettyProperties.getMaxChunkSize().toBytes());
130+
assertThat(decoder.maxInitialLineLength()).isEqualTo(nettyProperties.getMaxInitialLineLength().toBytes());
131+
}
132+
133+
@Test
134+
void shouldNotSetAnyHttpRequestDecoderProperties() {
135+
this.serverProperties.setMaxHttpHeaderSize(null);
136+
NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class);
137+
this.customizer.customize(factory);
138+
verify(factory, never()).addServerCustomizers(this.customizerCaptor.capture());
139+
}
140+
110141
private void verifyConnectionTimeout(NettyReactiveWebServerFactory factory, Integer expected) {
111142
if (expected == null) {
112143
verify(factory, never()).addServerCustomizers(any(NettyServerCustomizer.class));

0 commit comments

Comments
 (0)