Skip to content

Commit 0d40c5a

Browse files
artsiomsnicoll
authored andcommitted
Make "MaxSwallowSize" more easily configurable
See gh-13966
1 parent 1548dd5 commit 0d40c5a

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
* @author Aurélien Leboulanger
5353
* @author Brian Clozel
5454
* @author Olivier Lamy
55+
* @author Artsiom Yudovin
5556
*/
5657
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
5758
public class ServerProperties {
@@ -359,6 +360,11 @@ public static class Tomcat {
359360
*/
360361
private int acceptCount = 0;
361362

363+
/**
364+
* Maximum amount of request body to swallow.
365+
*/
366+
private int maxSwallowSize = 0;
367+
362368
/**
363369
* Comma-separated list of additional patterns that match jars to ignore for TLD
364370
* scanning. The special '?' and '*' characters can be used in the pattern to
@@ -503,6 +509,14 @@ public void setAcceptCount(int acceptCount) {
503509
this.acceptCount = acceptCount;
504510
}
505511

512+
public int getMaxSwallowSize() {
513+
return this.maxSwallowSize;
514+
}
515+
516+
public void setMaxSwallowSize(int maxSwallowSize) {
517+
this.maxSwallowSize = maxSwallowSize;
518+
}
519+
506520
public List<String> getAdditionalTldSkipPatterns() {
507521
return this.additionalTldSkipPatterns;
508522
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* @author Yulin Qin
4747
* @author Stephane Nicoll
4848
* @author Phillip Webb
49+
* @author Artsiom Yudovin
4950
* @since 2.0.0
5051
*/
5152
public class TomcatWebServerFactoryCustomizer implements
@@ -101,6 +102,9 @@ public void customize(ConfigurableTomcatWebServerFactory factory) {
101102
.to((maxConnections) -> customizeMaxConnections(factory, maxConnections));
102103
propertyMapper.from(tomcatProperties::getAcceptCount).when(this::isPositive)
103104
.to((acceptCount) -> customizeAcceptCount(factory, acceptCount));
105+
propertyMapper.from(tomcatProperties::getMaxSwallowSize)
106+
.when((maxSwallowSize) -> maxSwallowSize != 0)
107+
.to((maxSwallowSize) -> customizeMaxSwallowSize(factory, maxSwallowSize));
104108
customizeStaticResources(factory);
105109
customizeErrorReportValve(properties.getError(), factory);
106110
}
@@ -266,4 +270,16 @@ private void customizeErrorReportValve(ErrorProperties error,
266270
}
267271
}
268272

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+
269285
}

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

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

@@ -91,6 +92,7 @@ public void testTomcatBinding() {
9192
map.put("server.tomcat.remote-ip-header", "Remote-Ip");
9293
map.put("server.tomcat.internal-proxies", "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
9394
map.put("server.tomcat.background-processor-delay", "10");
95+
map.put("server.tomcat.max-swallow-size", "2");
9496
bind(map);
9597
ServerProperties.Tomcat tomcat = this.properties.getTomcat();
9698
assertThat(tomcat.getAccesslog().getPattern()).isEqualTo("%h %t '%r' %s %b");
@@ -105,6 +107,7 @@ public void testTomcatBinding() {
105107
.isEqualTo("10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
106108
assertThat(tomcat.getBackgroundProcessorDelay())
107109
.isEqualTo(Duration.ofSeconds(10));
110+
assertThat(tomcat.getMaxSwallowSize()).isEqualTo(2);
108111
}
109112

110113
@Test

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.catalina.valves.RemoteIpValve;
2929
import org.apache.catalina.webresources.StandardRoot;
3030
import org.apache.coyote.AbstractProtocol;
31+
import org.apache.coyote.http11.AbstractHttp11Protocol;
3132
import org.junit.Before;
3233
import org.junit.Test;
3334

@@ -49,6 +50,7 @@
4950
* @author Brian Clozel
5051
* @author Phillip Webb
5152
* @author Rob Tompkins
53+
* @author Artsiom Yudovin
5254
*/
5355
public class TomcatWebServerFactoryCustomizerTests {
5456

@@ -67,6 +69,14 @@ public void setup() {
6769
this.serverProperties);
6870
}
6971

72+
@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));
78+
}
79+
7080
@Test
7181
public void customAcceptCount() {
7282
bind("server.tomcat.accept-count=10");

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ content into your application. Rather, pick only the properties that you need.
263263
server.tomcat.resource.cache-ttl= # Time-to-live of the static resource cache.
264264
server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI.
265265
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.
266267
server.undertow.accesslog.dir= # Undertow access log directory.
267268
server.undertow.accesslog.enabled=false # Whether to enable the access log.
268269
server.undertow.accesslog.pattern=common # Format pattern for access logs.

0 commit comments

Comments
 (0)