Polish contribution

This commit is contained in:
Phillip Webb
2016-04-10 10:22:40 -07:00
parent 6d2f88ed9b
commit ea44ae6a35
4 changed files with 108 additions and 32 deletions

View File

@@ -32,7 +32,6 @@ import javax.validation.constraints.NotNull;
import io.undertow.Undertow.Builder;
import io.undertow.UndertowOptions;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.valves.AccessLogValve;
@@ -655,10 +654,11 @@ public class ServerProperties
/**
* Get the max http header size.
* @return the max http header size.
* @deprecated in favor of {@code server.maxHttpHeaderSize}
* @deprecated as of 1.4 in favor of
* {@link ServerProperties#getMaxHttpHeaderSize()}
*/
@Deprecated
@DeprecatedConfigurationProperty(replacement = "server.maxHttpHeaderSize")
@DeprecatedConfigurationProperty(replacement = "server.max-http-header-size")
public int getMaxHttpHeaderSize() {
return this.maxHttpHeaderSize;
}
@@ -666,7 +666,8 @@ public class ServerProperties
/**
* Set the max http header size.
* @param maxHttpHeaderSize the max http header size.
* @deprecated in favor of {@code server.maxHttpHeaderSize}
* @deprecated as of 1.4 in favor of
* {@link ServerProperties#setMaxHttpHeaderSize(int)}
*/
@Deprecated
public void setMaxHttpHeaderSize(int maxHttpHeaderSize) {
@@ -754,11 +755,10 @@ public class ServerProperties
if (this.minSpareThreads > 0) {
customizeMinThreads(factory);
}
if (serverProperties.getMaxHttpHeaderSize() > 0) {
customizeMaxHttpHeaderSize(factory, serverProperties.getMaxHttpHeaderSize());
}
else if (this.maxHttpHeaderSize > 0) {
customizeMaxHttpHeaderSize(factory, this.maxHttpHeaderSize);
int maxHttpHeaderSize = (serverProperties.getMaxHttpHeaderSize() > 0
? serverProperties.getMaxHttpHeaderSize() : this.maxHttpHeaderSize);
if (maxHttpHeaderSize > 0) {
customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize);
}
if (serverProperties.getMaxHttpPostSize() > 0) {
customizeMaxHttpPostSize(factory, serverProperties.getMaxHttpPostSize());
@@ -841,7 +841,8 @@ public class ServerProperties
@SuppressWarnings("rawtypes")
private void customizeMaxHttpHeaderSize(
TomcatEmbeddedServletContainerFactory factory, final int maxHttpHeaderSize) {
TomcatEmbeddedServletContainerFactory factory,
final int maxHttpHeaderSize) {
factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
@Override
@@ -857,12 +858,15 @@ public class ServerProperties
}
private void customizeMaxHttpPostSize(
TomcatEmbeddedServletContainerFactory factory, final int maxHttpPostSize) {
TomcatEmbeddedServletContainerFactory factory,
final int maxHttpPostSize) {
factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
@Override
public void customize(Connector connector) {
connector.setMaxPostSize(maxHttpPostSize);
}
});
}
@@ -952,7 +956,8 @@ public class ServerProperties
JettyEmbeddedServletContainerFactory factory) {
factory.setUseForwardHeaders(serverProperties.getOrDeduceUseForwardHeaders());
if (serverProperties.getMaxHttpHeaderSize() > 0) {
customizeMaxHttpHeaderSize(factory, serverProperties.getMaxHttpHeaderSize());
customizeMaxHttpHeaderSize(factory,
serverProperties.getMaxHttpHeaderSize());
}
if (serverProperties.getMaxHttpPostSize() > 0) {
customizeMaxHttpPostSize(factory, serverProperties.getMaxHttpPostSize());
@@ -960,23 +965,31 @@ public class ServerProperties
}
private void customizeMaxHttpHeaderSize(
JettyEmbeddedServletContainerFactory factory, final int maxHttpHeaderSize) {
JettyEmbeddedServletContainerFactory factory,
final int maxHttpHeaderSize) {
factory.addServerCustomizers(new JettyServerCustomizer() {
@Override
public void customize(Server server) {
org.eclipse.jetty.server.Connector[] connectors = server.getConnectors();
for (org.eclipse.jetty.server.Connector connector : connectors) {
for (ConnectionFactory connectionFactory : connector.getConnectionFactories()) {
for (org.eclipse.jetty.server.Connector connector : server
.getConnectors()) {
for (ConnectionFactory connectionFactory : connector
.getConnectionFactories()) {
if (connectionFactory instanceof HttpConfiguration.ConnectionFactory) {
HttpConfiguration httpConfig =
((HttpConfiguration.ConnectionFactory) connectionFactory)
.getHttpConfiguration();
httpConfig.setRequestHeaderSize(maxHttpHeaderSize);
httpConfig.setResponseHeaderSize(maxHttpHeaderSize);
customize(
(HttpConfiguration.ConnectionFactory) connectionFactory);
}
}
}
}
private void customize(HttpConfiguration.ConnectionFactory factory) {
HttpConfiguration configuration = factory.getHttpConfiguration();
configuration.setRequestHeaderSize(maxHttpHeaderSize);
configuration.setResponseHeaderSize(maxHttpHeaderSize);
}
});
}
@@ -993,7 +1006,8 @@ public class ServerProperties
Handler... handlers) {
for (Handler handler : handlers) {
if (handler instanceof ContextHandler) {
((ContextHandler) handler).setMaxFormContentSize(maxHttpPostSize);
((ContextHandler) handler)
.setMaxFormContentSize(maxHttpPostSize);
}
else if (handler instanceof HandlerWrapper) {
setHandlerMaxHttpPostSize(maxHttpPostSize,
@@ -1112,7 +1126,8 @@ public class ServerProperties
}
factory.setUseForwardHeaders(serverProperties.getOrDeduceUseForwardHeaders());
if (serverProperties.getMaxHttpHeaderSize() > 0) {
customizeMaxHttpHeaderSize(factory, serverProperties.getMaxHttpHeaderSize());
customizeMaxHttpHeaderSize(factory,
serverProperties.getMaxHttpHeaderSize());
}
if (serverProperties.getMaxHttpPostSize() > 0) {
customizeMaxHttpPostSize(factory, serverProperties.getMaxHttpPostSize());
@@ -1120,22 +1135,30 @@ public class ServerProperties
}
private void customizeMaxHttpHeaderSize(
UndertowEmbeddedServletContainerFactory factory, final int maxHttpHeaderSize) {
UndertowEmbeddedServletContainerFactory factory,
final int maxHttpHeaderSize) {
factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {
@Override
public void customize(Builder builder) {
builder.setServerOption(UndertowOptions.MAX_HEADER_SIZE, maxHttpHeaderSize);
builder.setServerOption(UndertowOptions.MAX_HEADER_SIZE,
maxHttpHeaderSize);
}
});
}
private void customizeMaxHttpPostSize(
UndertowEmbeddedServletContainerFactory factory, final int maxHttpPostSize) {
UndertowEmbeddedServletContainerFactory factory,
final int maxHttpPostSize) {
factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {
@Override
public void customize(Builder builder) {
builder.setServerOption(UndertowOptions.MAX_ENTITY_SIZE, (long) maxHttpPostSize);
builder.setServerOption(UndertowOptions.MAX_ENTITY_SIZE,
(long) maxHttpPostSize);
}
});
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.web;
import org.springframework.util.Assert;
/**
* A size threshold that can be specified in
*
* @author Phillip Webb
*/
class SizeThreshold {
private long inBytes;
private SizeThreshold(long inBytes) {
this.inBytes = inBytes;
}
public long getInBytes() {
return this.inBytes;
}
private static SizeThreshold parse(String size) {
Assert.hasLength(size, "Size must not be empty");
size = size.toUpperCase();
if (size.endsWith("KB")) {
return new SizeThreshold(
Long.valueOf(size.substring(0, size.length() - 2)) * 1024);
}
if (size.endsWith("MB")) {
return new SizeThreshold(
Long.valueOf(size.substring(0, size.length() - 2)) * 1024 * 1024);
}
return new SizeThreshold(Long.valueOf(size));
}
}

View File

@@ -253,11 +253,11 @@ public class ServerPropertiesTests {
}
@Test
public void testCustomizeTomcatHeaderSize() throws Exception {
public void testCustomizeHeaderSize() throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put("server.tomcat.maxHttpHeaderSize", "9999");
map.put("server.maxHttpHeaderSize", "9999");
bindProperties(map);
assertThat(this.properties.getTomcat().getMaxHttpHeaderSize()).isEqualTo(9999);
assertThat(this.properties.getMaxHttpHeaderSize()).isEqualTo(9999);
}
@Test