From e7b5d956816a77f57d22b57c2058bcbfda90217c Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Sun, 12 Aug 2018 13:21:29 +0200 Subject: [PATCH] Adapt ServerProperties with a default size to DataSize See gh-13974 --- .../boot/autoconfigure/web/ServerProperties.java | 9 +++++---- .../web/embedded/TomcatWebServerFactoryCustomizer.java | 4 +++- .../TomcatWebServerFactoryCustomizerTests.java | 10 ++++++---- .../main/asciidoc/appendix-application-properties.adoc | 2 +- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index c872465e470..4a344584a02 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -37,6 +37,7 @@ import org.springframework.boot.web.server.Ssl; import org.springframework.boot.web.servlet.server.Jsp; import org.springframework.boot.web.servlet.server.Session; import org.springframework.util.StringUtils; +import org.springframework.util.unit.DataSize; /** * {@link ConfigurationProperties} for a web server (e.g. port and path settings). @@ -330,9 +331,9 @@ public class ServerProperties { private int maxHttpHeaderSize = 0; /** - * Maximum amount of request body bytes to swallow. + * Maximum amount of request body to swallow. */ - private int maxSwallowSize = 2097152; + private DataSize maxSwallowSize = DataSize.ofMegaBytes(2); /** * Whether requests to the context root should be redirected by appending a / to @@ -496,11 +497,11 @@ public class ServerProperties { return this.maxHttpHeaderSize; } - public int getMaxSwallowSize() { + public DataSize getMaxSwallowSize() { return this.maxSwallowSize; } - public void setMaxSwallowSize(int maxSwallowSize) { + public void setMaxSwallowSize(DataSize maxSwallowSize) { this.maxSwallowSize = maxSwallowSize; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java index b2f70db2f62..35636961122 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java @@ -37,6 +37,7 @@ import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.core.Ordered; import org.springframework.core.env.Environment; import org.springframework.util.StringUtils; +import org.springframework.util.unit.DataSize; /** * Customization for Tomcat-specific features common for both Servlet and Reactive @@ -86,7 +87,8 @@ public class TomcatWebServerFactoryCustomizer implements propertyMapper.from(() -> determineMaxHttpHeaderSize()).when(this::isPositive) .to((maxHttpHeaderSize) -> customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize)); - propertyMapper.from(tomcatProperties::getMaxSwallowSize) + propertyMapper.from(tomcatProperties::getMaxSwallowSize).whenNonNull() + .asInt(DataSize::toBytes) .to((maxSwallowSize) -> customizeMaxSwallowSize(factory, maxSwallowSize)); propertyMapper.from(tomcatProperties::getMaxHttpPostSize) .when((maxHttpPostSize) -> maxHttpPostSize != 0) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java index d5b58abaefc..165f99f3e88 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java @@ -41,6 +41,7 @@ import org.springframework.boot.web.embedded.tomcat.TomcatWebServer; import org.springframework.mock.env.MockEnvironment; import org.springframework.test.context.support.TestPropertySourceUtils; import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.util.unit.DataSize; import static org.assertj.core.api.Assertions.assertThat; @@ -74,8 +75,9 @@ public class TomcatWebServerFactoryCustomizerTests { public void defaultsAreConsistent() { customizeAndRunServer((server) -> { assertThat(((AbstractHttp11Protocol) server.getTomcat().getConnector() - .getProtocolHandler()).getMaxSwallowSize()).isEqualTo( - this.serverProperties.getTomcat().getMaxSwallowSize()); + .getProtocolHandler()).getMaxSwallowSize()) + .isEqualTo(this.serverProperties.getTomcat() + .getMaxSwallowSize().toBytes()); }); } @@ -121,10 +123,10 @@ public class TomcatWebServerFactoryCustomizerTests { @Test public void customMaxSwallowSize() { - bind("server.tomcat.max-swallow-size=10"); + bind("server.tomcat.max-swallow-size=10MB"); customizeAndRunServer((server) -> assertThat(((AbstractHttp11Protocol) server .getTomcat().getConnector().getProtocolHandler()).getMaxSwallowSize()) - .isEqualTo(10)); + .isEqualTo(DataSize.ofMegaBytes(10).toBytes())); } @Test diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index e19e1b98239..e6aa62eb333 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -260,7 +260,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=2097152 # Maximum amount of request body bytes to swallow. + server.tomcat.max-swallow-size=2MB # Maximum amount of request body 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.