Skip to content

Commit 0e8bf94

Browse files
committed
Polish "Add properties for Netty HttpDecoderSpec"
See gh-22367
1 parent f068f9f commit 0e8bf94

File tree

4 files changed

+71
-56
lines changed

4 files changed

+71
-56
lines changed

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

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,29 +1397,29 @@ public static class Netty {
13971397
private Duration connectionTimeout;
13981398

13991399
/**
1400-
* The maximum chunk size that can be decoded for the HTTP request.
1400+
* Maximum content length of an H2C upgrade request.
14011401
*/
1402-
private DataSize maxChunkSize;
1402+
private DataSize h2cMaxContentLength = DataSize.ofBytes(0);
14031403

14041404
/**
1405-
* The maximum length that can be decoded for the HTTP request's initial line.
1405+
* Initial buffer size for HTTP request decoding.
14061406
*/
1407-
private DataSize maxInitialLineLength;
1407+
private DataSize initialBufferSize = DataSize.ofBytes(128);
14081408

14091409
/**
1410-
* Configure whether or not to validate headers when decoding requests.
1410+
* Maximum chunk size that can be decoded for an HTTP request.
14111411
*/
1412-
private Boolean validateHeaders;
1412+
private DataSize maxChunkSize = DataSize.ofKilobytes(8);
14131413

14141414
/**
1415-
* The maximum length of the content of the HTTP/2.0 clear-text upgrade request.
1415+
* Maximum length that can be decoded for an HTTP request's initial line.
14161416
*/
1417-
private DataSize h2cMaxContentLength;
1417+
private DataSize maxInitialLineLength = DataSize.ofKilobytes(4);
14181418

14191419
/**
1420-
* The initial buffer size for HTTP request decoding.
1420+
* Whether to validate headers when decoding requests.
14211421
*/
1422-
private DataSize initialBufferSize;
1422+
private boolean validateHeaders = true;
14231423

14241424
public Duration getConnectionTimeout() {
14251425
return this.connectionTimeout;
@@ -1429,6 +1429,22 @@ public void setConnectionTimeout(Duration connectionTimeout) {
14291429
this.connectionTimeout = connectionTimeout;
14301430
}
14311431

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+
14321448
public DataSize getMaxChunkSize() {
14331449
return this.maxChunkSize;
14341450
}
@@ -1445,30 +1461,14 @@ public void setMaxInitialLineLength(DataSize maxInitialLineLength) {
14451461
this.maxInitialLineLength = maxInitialLineLength;
14461462
}
14471463

1448-
public Boolean getValidateHeaders() {
1464+
public boolean isValidateHeaders() {
14491465
return this.validateHeaders;
14501466
}
14511467

1452-
public void setValidateHeaders(Boolean validateHeaders) {
1468+
public void setValidateHeaders(boolean validateHeaders) {
14531469
this.validateHeaders = validateHeaders;
14541470
}
14551471

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-
14721472
}
14731473

14741474
/**

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
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;
2220

2321
import io.netty.channel.ChannelOption;
2422

@@ -62,13 +60,7 @@ public void customize(NettyReactiveWebServerFactory factory) {
6260
ServerProperties.Netty nettyProperties = this.serverProperties.getNetty();
6361
propertyMapper.from(nettyProperties::getConnectionTimeout).whenNonNull()
6462
.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-
}
63+
customizeRequestDecoder(factory, propertyMapper);
7264
}
7365

7466
private boolean getOrDeduceUseForwardHeaders() {
@@ -79,6 +71,11 @@ private boolean getOrDeduceUseForwardHeaders() {
7971
return this.serverProperties.getForwardHeadersStrategy().equals(ServerProperties.ForwardHeadersStrategy.NATIVE);
8072
}
8173

74+
private void customizeConnectionTimeout(NettyReactiveWebServerFactory factory, Duration connectionTimeout) {
75+
factory.addServerCustomizers((httpServer) -> httpServer.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
76+
(int) connectionTimeout.toMillis()));
77+
}
78+
8279
private void customizeRequestDecoder(NettyReactiveWebServerFactory factory, PropertyMapper propertyMapper) {
8380
factory.addServerCustomizers((httpServer) -> httpServer.httpRequestDecoder((httpRequestDecoderSpec) -> {
8481
propertyMapper.from(this.serverProperties.getMaxHttpHeaderSize()).whenNonNull()
@@ -95,15 +92,10 @@ private void customizeRequestDecoder(NettyReactiveWebServerFactory factory, Prop
9592
.h2cMaxContentLength((int) h2cMaxContentLength.toBytes()));
9693
propertyMapper.from(nettyProperties.getInitialBufferSize()).whenNonNull().to(
9794
(initialBufferSize) -> httpRequestDecoderSpec.initialBufferSize((int) initialBufferSize.toBytes()));
98-
propertyMapper.from(nettyProperties.getValidateHeaders()).whenNonNull()
95+
propertyMapper.from(nettyProperties.isValidateHeaders()).whenNonNull()
9996
.to(httpRequestDecoderSpec::validateHeaders);
10097
return httpRequestDecoderSpec;
10198
}));
10299
}
103100

104-
private void customizeConnectionTimeout(NettyReactiveWebServerFactory factory, Duration connectionTimeout) {
105-
factory.addServerCustomizers((httpServer) -> httpServer.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
106-
(int) connectionTimeout.toMillis()));
107-
}
108-
109101
}

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: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ void setConnectionTimeout() {
110110
}
111111

112112
@Test
113-
void setHttpRequestDecoder() {
113+
void configureHttpRequestDecoder() {
114114
ServerProperties.Netty nettyProperties = this.serverProperties.getNetty();
115-
nettyProperties.setValidateHeaders(true);
115+
nettyProperties.setValidateHeaders(false);
116116
nettyProperties.setInitialBufferSize(DataSize.ofBytes(512));
117117
nettyProperties.setH2cMaxContentLength(DataSize.ofKilobytes(1));
118118
nettyProperties.setMaxChunkSize(DataSize.ofKilobytes(16));
@@ -123,28 +123,20 @@ void setHttpRequestDecoder() {
123123
NettyServerCustomizer serverCustomizer = this.customizerCaptor.getValue();
124124
HttpServer httpServer = serverCustomizer.apply(HttpServer.create());
125125
HttpRequestDecoderSpec decoder = httpServer.configuration().decoder();
126-
assertThat(decoder.validateHeaders()).isTrue();
126+
assertThat(decoder.validateHeaders()).isFalse();
127127
assertThat(decoder.initialBufferSize()).isEqualTo(nettyProperties.getInitialBufferSize().toBytes());
128128
assertThat(decoder.h2cMaxContentLength()).isEqualTo(nettyProperties.getH2cMaxContentLength().toBytes());
129129
assertThat(decoder.maxChunkSize()).isEqualTo(nettyProperties.getMaxChunkSize().toBytes());
130130
assertThat(decoder.maxInitialLineLength()).isEqualTo(nettyProperties.getMaxInitialLineLength().toBytes());
131131
}
132132

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-
141133
private void verifyConnectionTimeout(NettyReactiveWebServerFactory factory, Integer expected) {
142134
if (expected == null) {
143135
verify(factory, never()).addServerCustomizers(any(NettyServerCustomizer.class));
144136
return;
145137
}
146-
verify(factory, times(1)).addServerCustomizers(this.customizerCaptor.capture());
147-
NettyServerCustomizer serverCustomizer = this.customizerCaptor.getValue();
138+
verify(factory, times(2)).addServerCustomizers(this.customizerCaptor.capture());
139+
NettyServerCustomizer serverCustomizer = this.customizerCaptor.getAllValues().get(0);
148140
HttpServer httpServer = serverCustomizer.apply(HttpServer.create());
149141
Map<ChannelOption<?>, ?> options = httpServer.configuration().options();
150142
assertThat(options.get(ChannelOption.CONNECT_TIMEOUT_MILLIS)).isEqualTo(expected);

0 commit comments

Comments
 (0)