Adapt ServerProperties with a default size to DataSize

See gh-13974
This commit is contained in:
Stephane Nicoll 2018-08-12 13:21:29 +02:00
parent 6734e11222
commit e7b5d95681
4 changed files with 15 additions and 10 deletions

View File

@ -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.Jsp;
import org.springframework.boot.web.servlet.server.Session; import org.springframework.boot.web.servlet.server.Session;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.util.unit.DataSize;
/** /**
* {@link ConfigurationProperties} for a web server (e.g. port and path settings). * {@link ConfigurationProperties} for a web server (e.g. port and path settings).
@ -330,9 +331,9 @@ public class ServerProperties {
private int maxHttpHeaderSize = 0; 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 * Whether requests to the context root should be redirected by appending a / to
@ -496,11 +497,11 @@ public class ServerProperties {
return this.maxHttpHeaderSize; return this.maxHttpHeaderSize;
} }
public int getMaxSwallowSize() { public DataSize getMaxSwallowSize() {
return this.maxSwallowSize; return this.maxSwallowSize;
} }
public void setMaxSwallowSize(int maxSwallowSize) { public void setMaxSwallowSize(DataSize maxSwallowSize) {
this.maxSwallowSize = maxSwallowSize; this.maxSwallowSize = maxSwallowSize;
} }

View File

@ -37,6 +37,7 @@ import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.util.unit.DataSize;
/** /**
* Customization for Tomcat-specific features common for both Servlet and Reactive * 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) propertyMapper.from(() -> determineMaxHttpHeaderSize()).when(this::isPositive)
.to((maxHttpHeaderSize) -> customizeMaxHttpHeaderSize(factory, .to((maxHttpHeaderSize) -> customizeMaxHttpHeaderSize(factory,
maxHttpHeaderSize)); maxHttpHeaderSize));
propertyMapper.from(tomcatProperties::getMaxSwallowSize) propertyMapper.from(tomcatProperties::getMaxSwallowSize).whenNonNull()
.asInt(DataSize::toBytes)
.to((maxSwallowSize) -> customizeMaxSwallowSize(factory, maxSwallowSize)); .to((maxSwallowSize) -> customizeMaxSwallowSize(factory, maxSwallowSize));
propertyMapper.from(tomcatProperties::getMaxHttpPostSize) propertyMapper.from(tomcatProperties::getMaxHttpPostSize)
.when((maxHttpPostSize) -> maxHttpPostSize != 0) .when((maxHttpPostSize) -> maxHttpPostSize != 0)

View File

@ -41,6 +41,7 @@ import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
import org.springframework.mock.env.MockEnvironment; import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.context.support.TestPropertySourceUtils; import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.unit.DataSize;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -74,8 +75,9 @@ public class TomcatWebServerFactoryCustomizerTests {
public void defaultsAreConsistent() { public void defaultsAreConsistent() {
customizeAndRunServer((server) -> { customizeAndRunServer((server) -> {
assertThat(((AbstractHttp11Protocol<?>) server.getTomcat().getConnector() assertThat(((AbstractHttp11Protocol<?>) server.getTomcat().getConnector()
.getProtocolHandler()).getMaxSwallowSize()).isEqualTo( .getProtocolHandler()).getMaxSwallowSize())
this.serverProperties.getTomcat().getMaxSwallowSize()); .isEqualTo(this.serverProperties.getTomcat()
.getMaxSwallowSize().toBytes());
}); });
} }
@ -121,10 +123,10 @@ public class TomcatWebServerFactoryCustomizerTests {
@Test @Test
public void customMaxSwallowSize() { public void customMaxSwallowSize() {
bind("server.tomcat.max-swallow-size=10"); bind("server.tomcat.max-swallow-size=10MB");
customizeAndRunServer((server) -> assertThat(((AbstractHttp11Protocol<?>) server customizeAndRunServer((server) -> assertThat(((AbstractHttp11Protocol<?>) server
.getTomcat().getConnector().getProtocolHandler()).getMaxSwallowSize()) .getTomcat().getConnector().getProtocolHandler()).getMaxSwallowSize())
.isEqualTo(10)); .isEqualTo(DataSize.ofMegaBytes(10).toBytes()));
} }
@Test @Test

View File

@ -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-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-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-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.max-threads=0 # Maximum number of worker threads.
server.tomcat.min-spare-threads=0 # Minimum 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. server.tomcat.port-header=X-Forwarded-Port # Name of the HTTP header used to override the original port value.