|
|
|
@ -27,6 +27,7 @@ import org.apache.coyote.http11.AbstractHttp11Protocol;
|
|
|
|
|
|
|
|
|
|
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
|
|
|
|
import org.springframework.boot.cloud.CloudPlatform;
|
|
|
|
|
import org.springframework.boot.context.properties.PropertyMapper;
|
|
|
|
|
import org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory;
|
|
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
@ -36,6 +37,8 @@ import org.springframework.util.StringUtils;
|
|
|
|
|
* servers.
|
|
|
|
|
*
|
|
|
|
|
* @author Brian Clozel
|
|
|
|
|
* @author Yulin Qin
|
|
|
|
|
* @author Stephane Nicoll
|
|
|
|
|
* @since 2.0.0
|
|
|
|
|
*/
|
|
|
|
|
public final class TomcatCustomizer {
|
|
|
|
@ -46,47 +49,52 @@ public final class TomcatCustomizer {
|
|
|
|
|
public static void customizeTomcat(ServerProperties serverProperties,
|
|
|
|
|
Environment environment, ConfigurableTomcatWebServerFactory factory) {
|
|
|
|
|
ServerProperties.Tomcat tomcatProperties = serverProperties.getTomcat();
|
|
|
|
|
if (tomcatProperties.getBasedir() != null) {
|
|
|
|
|
factory.setBaseDirectory(tomcatProperties.getBasedir());
|
|
|
|
|
}
|
|
|
|
|
if (tomcatProperties.getBackgroundProcessorDelay() != null) {
|
|
|
|
|
factory.setBackgroundProcessorDelay(
|
|
|
|
|
(int) tomcatProperties.getBackgroundProcessorDelay().getSeconds());
|
|
|
|
|
}
|
|
|
|
|
PropertyMapper propertyMapper = PropertyMapper.get();
|
|
|
|
|
propertyMapper.from(tomcatProperties::getBasedir).whenNonNull()
|
|
|
|
|
.to(factory::setBaseDirectory);
|
|
|
|
|
propertyMapper.from(tomcatProperties::getBackgroundProcessorDelay).whenNonNull()
|
|
|
|
|
.as(Duration::getSeconds).as(Long::intValue)
|
|
|
|
|
.to(factory::setBackgroundProcessorDelay);
|
|
|
|
|
customizeRemoteIpValve(serverProperties, environment, factory);
|
|
|
|
|
if (tomcatProperties.getMaxThreads() > 0) {
|
|
|
|
|
customizeMaxThreads(factory, tomcatProperties.getMaxThreads());
|
|
|
|
|
}
|
|
|
|
|
if (tomcatProperties.getMinSpareThreads() > 0) {
|
|
|
|
|
customizeMinThreads(factory, tomcatProperties.getMinSpareThreads());
|
|
|
|
|
}
|
|
|
|
|
int maxHttpHeaderSize = (serverProperties.getMaxHttpHeaderSize() > 0
|
|
|
|
|
? serverProperties.getMaxHttpHeaderSize()
|
|
|
|
|
: tomcatProperties.getMaxHttpHeaderSize());
|
|
|
|
|
if (maxHttpHeaderSize > 0) {
|
|
|
|
|
customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize);
|
|
|
|
|
}
|
|
|
|
|
if (tomcatProperties.getMaxHttpPostSize() != 0) {
|
|
|
|
|
customizeMaxHttpPostSize(factory, tomcatProperties.getMaxHttpPostSize());
|
|
|
|
|
}
|
|
|
|
|
if (tomcatProperties.getAccesslog().isEnabled()) {
|
|
|
|
|
customizeAccessLog(tomcatProperties, factory);
|
|
|
|
|
}
|
|
|
|
|
if (tomcatProperties.getUriEncoding() != null) {
|
|
|
|
|
factory.setUriEncoding(tomcatProperties.getUriEncoding());
|
|
|
|
|
}
|
|
|
|
|
if (serverProperties.getConnectionTimeout() != null) {
|
|
|
|
|
customizeConnectionTimeout(factory, serverProperties.getConnectionTimeout());
|
|
|
|
|
}
|
|
|
|
|
if (tomcatProperties.getMaxConnections() > 0) {
|
|
|
|
|
customizeMaxConnections(factory, tomcatProperties.getMaxConnections());
|
|
|
|
|
}
|
|
|
|
|
if (tomcatProperties.getAcceptCount() > 0) {
|
|
|
|
|
customizeAcceptCount(factory, tomcatProperties.getAcceptCount());
|
|
|
|
|
}
|
|
|
|
|
propertyMapper.from(tomcatProperties::getMaxThreads)
|
|
|
|
|
.when(TomcatCustomizer::isPositive)
|
|
|
|
|
.to(maxThreads -> customizeMaxThreads(factory, tomcatProperties.getMaxThreads()));
|
|
|
|
|
propertyMapper.from(tomcatProperties::getMinSpareThreads)
|
|
|
|
|
.when(TomcatCustomizer::isPositive)
|
|
|
|
|
.to(minSpareThreads -> customizeMinThreads(factory, minSpareThreads));
|
|
|
|
|
propertyMapper.from(() -> determineMaxHttpHeaderSize(serverProperties, tomcatProperties))
|
|
|
|
|
.when(TomcatCustomizer::isPositive)
|
|
|
|
|
.to(maxHttpHeaderSize -> customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize));
|
|
|
|
|
propertyMapper.from(tomcatProperties::getMaxHttpPostSize)
|
|
|
|
|
.when(maxHttpPostSize -> maxHttpPostSize != 0)
|
|
|
|
|
.to(maxHttpPostSize -> customizeMaxHttpPostSize(factory, maxHttpPostSize));
|
|
|
|
|
propertyMapper.from(tomcatProperties::getAccesslog)
|
|
|
|
|
.when(ServerProperties.Tomcat.Accesslog::isEnabled)
|
|
|
|
|
.to(enabled -> customizeAccessLog(tomcatProperties, factory));
|
|
|
|
|
propertyMapper.from(tomcatProperties::getUriEncoding).whenNonNull()
|
|
|
|
|
.to(factory::setUriEncoding);
|
|
|
|
|
propertyMapper.from(serverProperties::getConnectionTimeout).whenNonNull()
|
|
|
|
|
.to(connectionTimeout -> customizeConnectionTimeout(factory, connectionTimeout));
|
|
|
|
|
propertyMapper.from(tomcatProperties::getMaxConnections)
|
|
|
|
|
.when(TomcatCustomizer::isPositive)
|
|
|
|
|
.to(maxConnections -> customizeMaxConnections(factory, maxConnections));
|
|
|
|
|
propertyMapper.from(tomcatProperties::getAcceptCount)
|
|
|
|
|
.when(TomcatCustomizer::isPositive)
|
|
|
|
|
.to(acceptCount -> customizeAcceptCount(factory, acceptCount));
|
|
|
|
|
customizeStaticResources(serverProperties.getTomcat().getResource(), factory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static boolean isPositive(int value) {
|
|
|
|
|
return value > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static int determineMaxHttpHeaderSize(ServerProperties serverProperties,
|
|
|
|
|
ServerProperties.Tomcat tomcatProperties) {
|
|
|
|
|
return serverProperties.getMaxHttpHeaderSize() > 0
|
|
|
|
|
? serverProperties.getMaxHttpHeaderSize()
|
|
|
|
|
: tomcatProperties.getMaxHttpHeaderSize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void customizeAcceptCount(ConfigurableTomcatWebServerFactory factory,
|
|
|
|
|
int acceptCount) {
|
|
|
|
|
factory.addConnectorCustomizers((connector) -> {
|
|
|
|
|