Polish "Align max HTTP header size configuration"

Closes gh-14234
This commit is contained in:
Stephane Nicoll 2018-09-04 10:51:54 +02:00
parent dbbb378650
commit 8771b34c15
5 changed files with 37 additions and 10 deletions

View File

@ -29,6 +29,7 @@ import java.util.Map;
import java.util.TimeZone; import java.util.TimeZone;
import org.springframework.boot.context.properties.ConfigurationProperties; 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.context.properties.NestedConfigurationProperty;
import org.springframework.boot.convert.DurationUnit; import org.springframework.boot.convert.DurationUnit;
import org.springframework.boot.web.server.Compression; import org.springframework.boot.web.server.Compression;
@ -84,7 +85,7 @@ public class ServerProperties {
/** /**
* Maximum size of the HTTP message header. * 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. * 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. * 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; private int maxHttpHeaderSize = 0;
/** /**
@ -496,10 +495,17 @@ public class ServerProperties {
this.maxConnections = maxConnections; this.maxConnections = maxConnections;
} }
@Deprecated
@DeprecatedConfigurationProperty(replacement = "server.max-http-header-size")
public int getMaxHttpHeaderSize() { public int getMaxHttpHeaderSize() {
return this.maxHttpHeaderSize; return this.maxHttpHeaderSize;
} }
@Deprecated
public void setMaxHttpHeaderSize(int maxHttpHeaderSize) {
this.maxHttpHeaderSize = maxHttpHeaderSize;
}
public DataSize getMaxSwallowSize() { public DataSize getMaxSwallowSize() {
return this.maxSwallowSize; return this.maxSwallowSize;
} }
@ -508,10 +514,6 @@ public class ServerProperties {
this.maxSwallowSize = maxSwallowSize; this.maxSwallowSize = maxSwallowSize;
} }
public void setMaxHttpHeaderSize(int maxHttpHeaderSize) {
this.maxHttpHeaderSize = maxHttpHeaderSize;
}
public int getAcceptCount() { public int getAcceptCount() {
return this.acceptCount; return this.acceptCount;
} }

View File

@ -116,6 +116,7 @@ public class TomcatWebServerFactoryCustomizer implements
return value > 0; return value > 0;
} }
@SuppressWarnings("deprecation")
private DataSize determineMaxHttpHeaderSize() { private DataSize determineMaxHttpHeaderSize() {
return isPositive(this.serverProperties.getTomcat().getMaxHttpHeaderSize()) return isPositive(this.serverProperties.getTomcat().getMaxHttpHeaderSize())
? DataSize ? DataSize

View File

@ -136,9 +136,16 @@ public class ServerPropertiesTests {
@Test @Test
public void testCustomizeHeaderSize() { public void testCustomizeHeaderSize() {
bind("server.max-http-header-size", "9999"); bind("server.max-http-header-size", "1MB");
assertThat(this.properties.getMaxHttpHeaderSize()) 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 @Test

View File

@ -121,6 +121,24 @@ public class TomcatWebServerFactoryCustomizerTests {
.isEqualTo(10000)); .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 @Test
public void customMaxSwallowSize() { public void customMaxSwallowSize() {
bind("server.tomcat.max-swallow-size=10MB"); bind("server.tomcat.max-swallow-size=10MB");

View File

@ -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\\.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. 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-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-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-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.