From 8771b34c159c85f5eb2b480dc3e0176a6de5398b Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 4 Sep 2018 10:51:54 +0200 Subject: [PATCH] Polish "Align max HTTP header size configuration" Closes gh-14234 --- .../autoconfigure/web/ServerProperties.java | 16 +++++++++------- .../TomcatWebServerFactoryCustomizer.java | 1 + .../web/ServerPropertiesTests.java | 11 +++++++++-- .../TomcatWebServerFactoryCustomizerTests.java | 18 ++++++++++++++++++ .../appendix-application-properties.adoc | 1 - 5 files changed, 37 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 5ec885ad78..ccefe488db 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 @@ -29,6 +29,7 @@ import java.util.Map; import java.util.TimeZone; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.DeprecatedConfigurationProperty; import org.springframework.boot.context.properties.NestedConfigurationProperty; import org.springframework.boot.convert.DurationUnit; import org.springframework.boot.web.server.Compression; @@ -84,7 +85,7 @@ public class ServerProperties { /** * Maximum size of the HTTP message header. */ - private DataSize maxHttpHeaderSize = DataSize.ofKiloBytes(8L); + private DataSize maxHttpHeaderSize = DataSize.ofKiloBytes(8); /** * Time that connectors wait for another HTTP request before closing the connection. @@ -328,9 +329,7 @@ public class ServerProperties { /** * Maximum size, in bytes, of the HTTP message header. - * @deprecated since 2.1.0 in favor of {@link ServerProperties#maxHttpHeaderSize} */ - @Deprecated private int maxHttpHeaderSize = 0; /** @@ -496,10 +495,17 @@ public class ServerProperties { this.maxConnections = maxConnections; } + @Deprecated + @DeprecatedConfigurationProperty(replacement = "server.max-http-header-size") public int getMaxHttpHeaderSize() { return this.maxHttpHeaderSize; } + @Deprecated + public void setMaxHttpHeaderSize(int maxHttpHeaderSize) { + this.maxHttpHeaderSize = maxHttpHeaderSize; + } + public DataSize getMaxSwallowSize() { return this.maxSwallowSize; } @@ -508,10 +514,6 @@ public class ServerProperties { this.maxSwallowSize = maxSwallowSize; } - public void setMaxHttpHeaderSize(int maxHttpHeaderSize) { - this.maxHttpHeaderSize = maxHttpHeaderSize; - } - public int getAcceptCount() { return this.acceptCount; } 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 72a166dd29..3bf75348d2 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 @@ -116,6 +116,7 @@ public class TomcatWebServerFactoryCustomizer implements return value > 0; } + @SuppressWarnings("deprecation") private DataSize determineMaxHttpHeaderSize() { return isPositive(this.serverProperties.getTomcat().getMaxHttpHeaderSize()) ? DataSize diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java index 592d474213..0b4b3276dc 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java @@ -136,9 +136,16 @@ public class ServerPropertiesTests { @Test public void testCustomizeHeaderSize() { - bind("server.max-http-header-size", "9999"); + bind("server.max-http-header-size", "1MB"); assertThat(this.properties.getMaxHttpHeaderSize()) - .isEqualTo(DataSize.ofBytes(9999)); + .isEqualTo(DataSize.ofMegaBytes(1)); + } + + @Test + public void testCustomizeHeaderSizeUseBytesByDefault() { + bind("server.max-http-header-size", "1024"); + assertThat(this.properties.getMaxHttpHeaderSize()) + .isEqualTo(DataSize.ofKiloBytes(1)); } @Test 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 165f99f3e8..60687e555b 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 @@ -121,6 +121,24 @@ public class TomcatWebServerFactoryCustomizerTests { .isEqualTo(10000)); } + @Test + public void customMaxHttpHeaderSize() { + bind("server.max-http-header-size=1KB"); + customizeAndRunServer((server) -> assertThat(((AbstractHttp11Protocol) server + .getTomcat().getConnector().getProtocolHandler()).getMaxHttpHeaderSize()) + .isEqualTo(DataSize.ofKiloBytes(1).toBytes())); + } + + @Test + @Deprecated + public void customMaxHttpHeaderSizeWithDeprecatedProperty() { + bind("server.max-http-header-size=4KB", + "server.tomcat.max-http-header-size=1024"); + customizeAndRunServer((server) -> assertThat(((AbstractHttp11Protocol) server + .getTomcat().getConnector().getProtocolHandler()).getMaxHttpHeaderSize()) + .isEqualTo(DataSize.ofKiloBytes(1).toBytes())); + } + @Test public void customMaxSwallowSize() { bind("server.tomcat.max-swallow-size=10MB"); 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 56873a0e15..ad6cdfe920 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 @@ -265,7 +265,6 @@ content into your application. Rather, pick only the properties that you need. 172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\ 172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3} # Regular expression matching trusted IP addresses. 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. Deprecated, use server.max-http-header-size instead. server.tomcat.max-http-post-size=0 # Maximum size, in bytes, of the HTTP post content. server.tomcat.max-swallow-size=2MB # Maximum amount of request body to swallow. server.tomcat.max-threads=0 # Maximum number of worker threads.