Skip to content

Commit ef78d4b

Browse files
committed
Merge pull request #22367 from Julien-Eyraud
* gh-22367: Polish "Add properties for Netty HttpDecoderSpec" Add properties for Netty HttpDecoderSpec Closes gh-22367
2 parents 530a267 + 0e8bf94 commit ef78d4b

File tree

4 files changed

+144
-10
lines changed

4 files changed

+144
-10
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+
* Maximum content length of an H2C upgrade request.
1401+
*/
1402+
private DataSize h2cMaxContentLength = DataSize.ofBytes(0);
1403+
1404+
/**
1405+
* Initial buffer size for HTTP request decoding.
1406+
*/
1407+
private DataSize initialBufferSize = DataSize.ofBytes(128);
1408+
1409+
/**
1410+
* Maximum chunk size that can be decoded for an HTTP request.
1411+
*/
1412+
private DataSize maxChunkSize = DataSize.ofKilobytes(8);
1413+
1414+
/**
1415+
* Maximum length that can be decoded for an HTTP request's initial line.
1416+
*/
1417+
private DataSize maxInitialLineLength = DataSize.ofKilobytes(4);
1418+
1419+
/**
1420+
* Whether to validate headers when decoding requests.
1421+
*/
1422+
private boolean validateHeaders = true;
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 getH2cMaxContentLength() {
1433+
return this.h2cMaxContentLength;
1434+
}
1435+
1436+
public void setH2cMaxContentLength(DataSize h2cMaxContentLength) {
1437+
this.h2cMaxContentLength = h2cMaxContentLength;
1438+
}
1439+
1440+
public DataSize getInitialBufferSize() {
1441+
return this.initialBufferSize;
1442+
}
1443+
1444+
public void setInitialBufferSize(DataSize initialBufferSize) {
1445+
this.initialBufferSize = initialBufferSize;
1446+
}
1447+
1448+
public DataSize getMaxChunkSize() {
1449+
return this.maxChunkSize;
1450+
}
1451+
1452+
public void setMaxChunkSize(DataSize maxChunkSize) {
1453+
this.maxChunkSize = maxChunkSize;
1454+
}
1455+
1456+
public DataSize getMaxInitialLineLength() {
1457+
return this.maxInitialLineLength;
1458+
}
1459+
1460+
public void setMaxInitialLineLength(DataSize maxInitialLineLength) {
1461+
this.maxInitialLineLength = maxInitialLineLength;
1462+
}
1463+
1464+
public boolean isValidateHeaders() {
1465+
return this.validateHeaders;
1466+
}
1467+
1468+
public void setValidateHeaders(boolean validateHeaders) {
1469+
this.validateHeaders = validateHeaders;
1470+
}
1471+
14071472
}
14081473

14091474
/**

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
2828
import org.springframework.core.Ordered;
2929
import org.springframework.core.env.Environment;
30-
import org.springframework.util.unit.DataSize;
3130

3231
/**
3332
* Customization for Netty-specific features.
@@ -58,11 +57,10 @@ public int getOrder() {
5857
public void customize(NettyReactiveWebServerFactory factory) {
5958
factory.setUseForwardHeaders(getOrDeduceUseForwardHeaders());
6059
PropertyMapper propertyMapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
61-
propertyMapper.from(this.serverProperties::getMaxHttpHeaderSize)
62-
.to((maxHttpRequestHeaderSize) -> customizeMaxHttpHeaderSize(factory, maxHttpRequestHeaderSize));
6360
ServerProperties.Netty nettyProperties = this.serverProperties.getNetty();
6461
propertyMapper.from(nettyProperties::getConnectionTimeout).whenNonNull()
6562
.to((connectionTimeout) -> customizeConnectionTimeout(factory, connectionTimeout));
63+
customizeRequestDecoder(factory, propertyMapper);
6664
}
6765

6866
private boolean getOrDeduceUseForwardHeaders() {
@@ -73,14 +71,31 @@ private boolean getOrDeduceUseForwardHeaders() {
7371
return this.serverProperties.getForwardHeadersStrategy().equals(ServerProperties.ForwardHeadersStrategy.NATIVE);
7472
}
7573

76-
private void customizeMaxHttpHeaderSize(NettyReactiveWebServerFactory factory, DataSize maxHttpHeaderSize) {
77-
factory.addServerCustomizers((httpServer) -> httpServer.httpRequestDecoder(
78-
(httpRequestDecoderSpec) -> httpRequestDecoderSpec.maxHeaderSize((int) maxHttpHeaderSize.toBytes())));
79-
}
80-
8174
private void customizeConnectionTimeout(NettyReactiveWebServerFactory factory, Duration connectionTimeout) {
8275
factory.addServerCustomizers((httpServer) -> httpServer.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
8376
(int) connectionTimeout.toMillis()));
8477
}
8578

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

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import org.eclipse.jetty.server.Server;
4444
import org.eclipse.jetty.util.thread.ThreadPool;
4545
import org.junit.jupiter.api.Test;
46+
import reactor.netty.http.HttpDecoderSpec;
47+
import reactor.netty.http.server.HttpRequestDecoderSpec;
4648

4749
import org.springframework.boot.autoconfigure.web.ServerProperties.Tomcat.Accesslog;
4850
import org.springframework.boot.context.properties.bind.Bindable;
@@ -533,6 +535,35 @@ void undertowMaxHttpPostSizeMatchesDefault() {
533535
.isEqualTo(UndertowOptions.DEFAULT_MAX_ENTITY_SIZE);
534536
}
535537

538+
@Test
539+
void nettyMaxChunkSizeMatchesHttpDecoderSpecDefault() {
540+
assertThat(this.properties.getNetty().getMaxChunkSize().toBytes())
541+
.isEqualTo(HttpDecoderSpec.DEFAULT_MAX_CHUNK_SIZE);
542+
}
543+
544+
@Test
545+
void nettyMaxInitialLineLenghtMatchesHttpDecoderSpecDefault() {
546+
assertThat(this.properties.getNetty().getMaxInitialLineLength().toBytes())
547+
.isEqualTo(HttpDecoderSpec.DEFAULT_MAX_INITIAL_LINE_LENGTH);
548+
}
549+
550+
@Test
551+
void nettyValidateHeadersMatchesHttpDecoderSpecDefault() {
552+
assertThat(this.properties.getNetty().isValidateHeaders()).isEqualTo(HttpDecoderSpec.DEFAULT_VALIDATE_HEADERS);
553+
}
554+
555+
@Test
556+
void nettyH2cMaxContentLengthMatchesHttpDecoderSpecDefault() {
557+
assertThat(this.properties.getNetty().getH2cMaxContentLength().toBytes())
558+
.isEqualTo(HttpRequestDecoderSpec.DEFAULT_H2C_MAX_CONTENT_LENGTH);
559+
}
560+
561+
@Test
562+
void nettyInitialBufferSizeMatchesHttpDecoderSpecDefault() {
563+
assertThat(this.properties.getNetty().getInitialBufferSize().toBytes())
564+
.isEqualTo(HttpDecoderSpec.DEFAULT_INITIAL_BUFFER_SIZE);
565+
}
566+
536567
private Connector getDefaultConnector() throws Exception {
537568
return new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
538569
}

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

Lines changed: 25 additions & 2 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,13 +109,34 @@ void setConnectionTimeout() {
107109
verifyConnectionTimeout(factory, 1000);
108110
}
109111

112+
@Test
113+
void configureHttpRequestDecoder() {
114+
ServerProperties.Netty nettyProperties = this.serverProperties.getNetty();
115+
nettyProperties.setValidateHeaders(false);
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()).isFalse();
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+
110133
private void verifyConnectionTimeout(NettyReactiveWebServerFactory factory, Integer expected) {
111134
if (expected == null) {
112135
verify(factory, never()).addServerCustomizers(any(NettyServerCustomizer.class));
113136
return;
114137
}
115-
verify(factory, times(1)).addServerCustomizers(this.customizerCaptor.capture());
116-
NettyServerCustomizer serverCustomizer = this.customizerCaptor.getValue();
138+
verify(factory, times(2)).addServerCustomizers(this.customizerCaptor.capture());
139+
NettyServerCustomizer serverCustomizer = this.customizerCaptor.getAllValues().get(0);
117140
HttpServer httpServer = serverCustomizer.apply(HttpServer.create());
118141
Map<ChannelOption<?>, ?> options = httpServer.configuration().options();
119142
assertThat(options.get(ChannelOption.CONNECT_TIMEOUT_MILLIS)).isEqualTo(expected);

0 commit comments

Comments
 (0)