Skip to content

Commit a8b9718

Browse files
committed
Polish "Make "MaxSwallowSize" more easily configurable"
Closes gh-13966
1 parent 0d40c5a commit a8b9718

File tree

5 files changed

+42
-38
lines changed

5 files changed

+42
-38
lines changed

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
* @author Aurélien Leboulanger
5353
* @author Brian Clozel
5454
* @author Olivier Lamy
55-
* @author Artsiom Yudovin
5655
*/
5756
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
5857
public class ServerProperties {
@@ -330,6 +329,11 @@ public static class Tomcat {
330329
*/
331330
private int maxHttpHeaderSize = 0;
332331

332+
/**
333+
* Maximum amount of request body bytes to swallow.
334+
*/
335+
private int maxSwallowSize = 4096;
336+
333337
/**
334338
* Whether requests to the context root should be redirected by appending a / to
335339
* the path.
@@ -360,11 +364,6 @@ public static class Tomcat {
360364
*/
361365
private int acceptCount = 0;
362366

363-
/**
364-
* Maximum amount of request body to swallow.
365-
*/
366-
private int maxSwallowSize = 0;
367-
368367
/**
369368
* Comma-separated list of additional patterns that match jars to ignore for TLD
370369
* scanning. The special '?' and '*' characters can be used in the pattern to
@@ -497,6 +496,14 @@ public int getMaxHttpHeaderSize() {
497496
return this.maxHttpHeaderSize;
498497
}
499498

499+
public int getMaxSwallowSize() {
500+
return this.maxSwallowSize;
501+
}
502+
503+
public void setMaxSwallowSize(int maxSwallowSize) {
504+
this.maxSwallowSize = maxSwallowSize;
505+
}
506+
500507
public void setMaxHttpHeaderSize(int maxHttpHeaderSize) {
501508
this.maxHttpHeaderSize = maxHttpHeaderSize;
502509
}
@@ -509,14 +516,6 @@ public void setAcceptCount(int acceptCount) {
509516
this.acceptCount = acceptCount;
510517
}
511518

512-
public int getMaxSwallowSize() {
513-
return this.maxSwallowSize;
514-
}
515-
516-
public void setMaxSwallowSize(int maxSwallowSize) {
517-
this.maxSwallowSize = maxSwallowSize;
518-
}
519-
520519
public List<String> getAdditionalTldSkipPatterns() {
521520
return this.additionalTldSkipPatterns;
522521
}

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ public void customize(ConfigurableTomcatWebServerFactory factory) {
8686
propertyMapper.from(() -> determineMaxHttpHeaderSize()).when(this::isPositive)
8787
.to((maxHttpHeaderSize) -> customizeMaxHttpHeaderSize(factory,
8888
maxHttpHeaderSize));
89+
propertyMapper.from(tomcatProperties::getMaxSwallowSize)
90+
.to((maxSwallowSize) -> customizeMaxSwallowSize(factory, maxSwallowSize));
8991
propertyMapper.from(tomcatProperties::getMaxHttpPostSize)
9092
.when((maxHttpPostSize) -> maxHttpPostSize != 0)
9193
.to((maxHttpPostSize) -> customizeMaxHttpPostSize(factory,
@@ -102,9 +104,6 @@ public void customize(ConfigurableTomcatWebServerFactory factory) {
102104
.to((maxConnections) -> customizeMaxConnections(factory, maxConnections));
103105
propertyMapper.from(tomcatProperties::getAcceptCount).when(this::isPositive)
104106
.to((acceptCount) -> customizeAcceptCount(factory, acceptCount));
105-
propertyMapper.from(tomcatProperties::getMaxSwallowSize)
106-
.when((maxSwallowSize) -> maxSwallowSize != 0)
107-
.to((maxSwallowSize) -> customizeMaxSwallowSize(factory, maxSwallowSize));
108107
customizeStaticResources(factory);
109108
customizeErrorReportValve(properties.getError(), factory);
110109
}
@@ -220,6 +219,17 @@ private void customizeMaxHttpHeaderSize(ConfigurableTomcatWebServerFactory facto
220219
});
221220
}
222221

222+
private void customizeMaxSwallowSize(ConfigurableTomcatWebServerFactory factory,
223+
int maxSwallowSize) {
224+
factory.addConnectorCustomizers((connector) -> {
225+
ProtocolHandler handler = connector.getProtocolHandler();
226+
if (handler instanceof AbstractHttp11Protocol) {
227+
AbstractHttp11Protocol<?> protocol = (AbstractHttp11Protocol<?>) handler;
228+
protocol.setMaxSwallowSize(maxSwallowSize);
229+
}
230+
});
231+
}
232+
223233
private void customizeMaxHttpPostSize(ConfigurableTomcatWebServerFactory factory,
224234
int maxHttpPostSize) {
225235
factory.addConnectorCustomizers(
@@ -270,16 +280,4 @@ private void customizeErrorReportValve(ErrorProperties error,
270280
}
271281
}
272282

273-
@SuppressWarnings("rawtypes")
274-
private void customizeMaxSwallowSize(ConfigurableTomcatWebServerFactory factory,
275-
int maxSwallowSize) {
276-
factory.addConnectorCustomizers((connector) -> {
277-
ProtocolHandler handler = connector.getProtocolHandler();
278-
if (handler instanceof AbstractHttp11Protocol) {
279-
AbstractHttp11Protocol protocol = (AbstractHttp11Protocol) handler;
280-
protocol.setMaxSwallowSize(maxSwallowSize);
281-
}
282-
});
283-
}
284-
285283
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
* @author Eddú Meléndez
4343
* @author Quinten De Swaef
4444
* @author Venil Noronha
45-
* @author Artsiom Yudovin
4645
*/
4746
public class ServerPropertiesTests {
4847

@@ -92,7 +91,6 @@ public void testTomcatBinding() {
9291
map.put("server.tomcat.remote-ip-header", "Remote-Ip");
9392
map.put("server.tomcat.internal-proxies", "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
9493
map.put("server.tomcat.background-processor-delay", "10");
95-
map.put("server.tomcat.max-swallow-size", "2");
9694
bind(map);
9795
ServerProperties.Tomcat tomcat = this.properties.getTomcat();
9896
assertThat(tomcat.getAccesslog().getPattern()).isEqualTo("%h %t '%r' %s %b");
@@ -107,7 +105,6 @@ public void testTomcatBinding() {
107105
.isEqualTo("10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
108106
assertThat(tomcat.getBackgroundProcessorDelay())
109107
.isEqualTo(Duration.ofSeconds(10));
110-
assertThat(tomcat.getMaxSwallowSize()).isEqualTo(2);
111108
}
112109

113110
@Test

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
* @author Phillip Webb
5252
* @author Rob Tompkins
5353
* @author Artsiom Yudovin
54+
* @author Stephane Nicoll
5455
*/
5556
public class TomcatWebServerFactoryCustomizerTests {
5657

@@ -70,11 +71,12 @@ public void setup() {
7071
}
7172

7273
@Test
73-
public void customMaxSwallowSize() {
74-
bind("server.tomcat.max-swallow-size=10");
75-
customizeAndRunServer((server) -> assertThat(((AbstractHttp11Protocol<?>) server
76-
.getTomcat().getConnector().getProtocolHandler()).getMaxSwallowSize())
77-
.isEqualTo(10));
74+
public void defaultsAreConsistent() {
75+
customizeAndRunServer((server) -> {
76+
assertThat(((AbstractHttp11Protocol<?>) server.getTomcat().getConnector()
77+
.getProtocolHandler()).getMaxSwallowSize()).isEqualTo(
78+
this.serverProperties.getTomcat().getMaxSwallowSize());
79+
});
7880
}
7981

8082
@Test
@@ -117,6 +119,14 @@ public void customMaxHttpPostSize() {
117119
.isEqualTo(10000));
118120
}
119121

122+
@Test
123+
public void customMaxSwallowSize() {
124+
bind("server.tomcat.max-swallow-size=10");
125+
customizeAndRunServer((server) -> assertThat(((AbstractHttp11Protocol<?>) server
126+
.getTomcat().getConnector().getProtocolHandler()).getMaxSwallowSize())
127+
.isEqualTo(10));
128+
}
129+
120130
@Test
121131
public void customRemoteIpValve() {
122132
bind("server.tomcat.remote-ip-header=x-my-remote-ip-header",

spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ content into your application. Rather, pick only the properties that you need.
252252
server.tomcat.max-connections=0 # Maximum number of connections that the server accepts and processes at any given time.
253253
server.tomcat.max-http-header-size=0 # Maximum size, in bytes, of the HTTP message header.
254254
server.tomcat.max-http-post-size=0 # Maximum size, in bytes, of the HTTP post content.
255+
server.tomcat.max-swallow-size=4096 # Maximum amount of request body bytes to swallow.
255256
server.tomcat.max-threads=0 # Maximum number of worker threads.
256257
server.tomcat.min-spare-threads=0 # Minimum number of worker threads.
257258
server.tomcat.port-header=X-Forwarded-Port # Name of the HTTP header used to override the original port value.
@@ -263,7 +264,6 @@ content into your application. Rather, pick only the properties that you need.
263264
server.tomcat.resource.cache-ttl= # Time-to-live of the static resource cache.
264265
server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI.
265266
server.tomcat.use-relative-redirects= # Whether HTTP 1.1 and later location headers generated by a call to sendRedirect will use relative or absolute redirects.
266-
server.tomcat.max-swallow-size= # Maximum amount of request body to swallow.
267267
server.undertow.accesslog.dir= # Undertow access log directory.
268268
server.undertow.accesslog.enabled=false # Whether to enable the access log.
269269
server.undertow.accesslog.pattern=common # Format pattern for access logs.

0 commit comments

Comments
 (0)