Polish "Make "MaxSwallowSize" more easily configurable"

Closes gh-13966
pull/13983/head
Stephane Nicoll 6 years ago
parent 0d40c5aecc
commit a8b9718073

@ -52,7 +52,6 @@ import org.springframework.util.StringUtils;
* @author Aurélien Leboulanger
* @author Brian Clozel
* @author Olivier Lamy
* @author Artsiom Yudovin
*/
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties {
@ -330,6 +329,11 @@ public class ServerProperties {
*/
private int maxHttpHeaderSize = 0;
/**
* Maximum amount of request body bytes to swallow.
*/
private int maxSwallowSize = 4096;
/**
* Whether requests to the context root should be redirected by appending a / to
* the path.
@ -360,11 +364,6 @@ public class ServerProperties {
*/
private int acceptCount = 0;
/**
* Maximum amount of request body to swallow.
*/
private int maxSwallowSize = 0;
/**
* Comma-separated list of additional patterns that match jars to ignore for TLD
* scanning. The special '?' and '*' characters can be used in the pattern to
@ -497,6 +496,14 @@ public class ServerProperties {
return this.maxHttpHeaderSize;
}
public int getMaxSwallowSize() {
return this.maxSwallowSize;
}
public void setMaxSwallowSize(int maxSwallowSize) {
this.maxSwallowSize = maxSwallowSize;
}
public void setMaxHttpHeaderSize(int maxHttpHeaderSize) {
this.maxHttpHeaderSize = maxHttpHeaderSize;
}
@ -509,14 +516,6 @@ public class ServerProperties {
this.acceptCount = acceptCount;
}
public int getMaxSwallowSize() {
return this.maxSwallowSize;
}
public void setMaxSwallowSize(int maxSwallowSize) {
this.maxSwallowSize = maxSwallowSize;
}
public List<String> getAdditionalTldSkipPatterns() {
return this.additionalTldSkipPatterns;
}

@ -86,6 +86,8 @@ public class TomcatWebServerFactoryCustomizer implements
propertyMapper.from(() -> determineMaxHttpHeaderSize()).when(this::isPositive)
.to((maxHttpHeaderSize) -> customizeMaxHttpHeaderSize(factory,
maxHttpHeaderSize));
propertyMapper.from(tomcatProperties::getMaxSwallowSize)
.to((maxSwallowSize) -> customizeMaxSwallowSize(factory, maxSwallowSize));
propertyMapper.from(tomcatProperties::getMaxHttpPostSize)
.when((maxHttpPostSize) -> maxHttpPostSize != 0)
.to((maxHttpPostSize) -> customizeMaxHttpPostSize(factory,
@ -102,9 +104,6 @@ public class TomcatWebServerFactoryCustomizer implements
.to((maxConnections) -> customizeMaxConnections(factory, maxConnections));
propertyMapper.from(tomcatProperties::getAcceptCount).when(this::isPositive)
.to((acceptCount) -> customizeAcceptCount(factory, acceptCount));
propertyMapper.from(tomcatProperties::getMaxSwallowSize)
.when((maxSwallowSize) -> maxSwallowSize != 0)
.to((maxSwallowSize) -> customizeMaxSwallowSize(factory, maxSwallowSize));
customizeStaticResources(factory);
customizeErrorReportValve(properties.getError(), factory);
}
@ -220,6 +219,17 @@ public class TomcatWebServerFactoryCustomizer implements
});
}
private void customizeMaxSwallowSize(ConfigurableTomcatWebServerFactory factory,
int maxSwallowSize) {
factory.addConnectorCustomizers((connector) -> {
ProtocolHandler handler = connector.getProtocolHandler();
if (handler instanceof AbstractHttp11Protocol) {
AbstractHttp11Protocol<?> protocol = (AbstractHttp11Protocol<?>) handler;
protocol.setMaxSwallowSize(maxSwallowSize);
}
});
}
private void customizeMaxHttpPostSize(ConfigurableTomcatWebServerFactory factory,
int maxHttpPostSize) {
factory.addConnectorCustomizers(
@ -270,16 +280,4 @@ public class TomcatWebServerFactoryCustomizer implements
}
}
@SuppressWarnings("rawtypes")
private void customizeMaxSwallowSize(ConfigurableTomcatWebServerFactory factory,
int maxSwallowSize) {
factory.addConnectorCustomizers((connector) -> {
ProtocolHandler handler = connector.getProtocolHandler();
if (handler instanceof AbstractHttp11Protocol) {
AbstractHttp11Protocol protocol = (AbstractHttp11Protocol) handler;
protocol.setMaxSwallowSize(maxSwallowSize);
}
});
}
}

@ -42,7 +42,6 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Eddú Meléndez
* @author Quinten De Swaef
* @author Venil Noronha
* @author Artsiom Yudovin
*/
public class ServerPropertiesTests {
@ -92,7 +91,6 @@ public class ServerPropertiesTests {
map.put("server.tomcat.remote-ip-header", "Remote-Ip");
map.put("server.tomcat.internal-proxies", "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
map.put("server.tomcat.background-processor-delay", "10");
map.put("server.tomcat.max-swallow-size", "2");
bind(map);
ServerProperties.Tomcat tomcat = this.properties.getTomcat();
assertThat(tomcat.getAccesslog().getPattern()).isEqualTo("%h %t '%r' %s %b");
@ -107,7 +105,6 @@ public class ServerPropertiesTests {
.isEqualTo("10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
assertThat(tomcat.getBackgroundProcessorDelay())
.isEqualTo(Duration.ofSeconds(10));
assertThat(tomcat.getMaxSwallowSize()).isEqualTo(2);
}
@Test

@ -51,6 +51,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Phillip Webb
* @author Rob Tompkins
* @author Artsiom Yudovin
* @author Stephane Nicoll
*/
public class TomcatWebServerFactoryCustomizerTests {
@ -70,11 +71,12 @@ public class TomcatWebServerFactoryCustomizerTests {
}
@Test
public void customMaxSwallowSize() {
bind("server.tomcat.max-swallow-size=10");
customizeAndRunServer((server) -> assertThat(((AbstractHttp11Protocol<?>) server
.getTomcat().getConnector().getProtocolHandler()).getMaxSwallowSize())
.isEqualTo(10));
public void defaultsAreConsistent() {
customizeAndRunServer((server) -> {
assertThat(((AbstractHttp11Protocol<?>) server.getTomcat().getConnector()
.getProtocolHandler()).getMaxSwallowSize()).isEqualTo(
this.serverProperties.getTomcat().getMaxSwallowSize());
});
}
@Test
@ -117,6 +119,14 @@ public class TomcatWebServerFactoryCustomizerTests {
.isEqualTo(10000));
}
@Test
public void customMaxSwallowSize() {
bind("server.tomcat.max-swallow-size=10");
customizeAndRunServer((server) -> assertThat(((AbstractHttp11Protocol<?>) server
.getTomcat().getConnector().getProtocolHandler()).getMaxSwallowSize())
.isEqualTo(10));
}
@Test
public void customRemoteIpValve() {
bind("server.tomcat.remote-ip-header=x-my-remote-ip-header",

@ -252,6 +252,7 @@ content into your application. Rather, pick only the properties that you need.
server.tomcat.max-connections=0 # Maximum number of connections that the server accepts and processes at any given time.
server.tomcat.max-http-header-size=0 # Maximum size, in bytes, of the HTTP message header.
server.tomcat.max-http-post-size=0 # Maximum size, in bytes, of the HTTP post content.
server.tomcat.max-swallow-size=4096 # Maximum amount of request body bytes to swallow.
server.tomcat.max-threads=0 # Maximum number of worker threads.
server.tomcat.min-spare-threads=0 # Minimum number of worker threads.
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.
server.tomcat.resource.cache-ttl= # Time-to-live of the static resource cache.
server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI.
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.
server.tomcat.max-swallow-size= # Maximum amount of request body to swallow.
server.undertow.accesslog.dir= # Undertow access log directory.
server.undertow.accesslog.enabled=false # Whether to enable the access log.
server.undertow.accesslog.pattern=common # Format pattern for access logs.

Loading…
Cancel
Save