Skip to content

Commit

Permalink
Add configuration property to customize the Tomcat connector's max pa…
Browse files Browse the repository at this point in the history
…rameter

Closes spring-projectsGH-43275
  • Loading branch information
quaff committed Nov 26, 2024
1 parent 32433e8 commit 39c03fc
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
* @author Florian Storz
* @author Michael Weidmann
* @author Lasse Wulff
* @author Yanming Zhou
* @since 1.0.0
*/
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
Expand Down Expand Up @@ -513,13 +514,11 @@ public static class Tomcat {
*/
private DataSize maxHttpResponseHeaderSize = DataSize.ofKilobytes(8);

public DataSize getMaxHttpFormPostSize() {
return this.maxHttpFormPostSize;
}

public void setMaxHttpFormPostSize(DataSize maxHttpFormPostSize) {
this.maxHttpFormPostSize = maxHttpFormPostSize;
}
/**
* Maximum number of parameters (GET plus POST) that will be automatically parsed
* by the container.
*/
private Integer maxParameterCount;

public Accesslog getAccesslog() {
return this.accesslog;
Expand Down Expand Up @@ -669,6 +668,22 @@ public void setMaxHttpResponseHeaderSize(DataSize maxHttpResponseHeaderSize) {
this.maxHttpResponseHeaderSize = maxHttpResponseHeaderSize;
}

public DataSize getMaxHttpFormPostSize() {
return this.maxHttpFormPostSize;
}

public void setMaxHttpFormPostSize(DataSize maxHttpFormPostSize) {
this.maxHttpFormPostSize = maxHttpFormPostSize;
}

public Integer getMaxParameterCount() {
return this.maxParameterCount;
}

public void setMaxParameterCount(Integer maxParameterCount) {
this.maxParameterCount = maxParameterCount;
}

/**
* Tomcat access log properties.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
* @author Parviz Rozikov
* @author Florian Storz
* @author Michael Weidmann
* @author Yanming Zhou
* @since 2.0.0
*/
public class TomcatWebServerFactoryCustomizer
Expand Down Expand Up @@ -119,6 +120,9 @@ public void customize(ConfigurableTomcatWebServerFactory factory) {
.asInt(DataSize::toBytes)
.when((maxHttpFormPostSize) -> maxHttpFormPostSize != 0)
.to((maxHttpFormPostSize) -> customizeMaxHttpFormPostSize(factory, maxHttpFormPostSize));
map.from(properties::getMaxParameterCount)
.whenNonNull()
.to((maxParameterCount) -> customizeMaxParameterCount(factory, maxParameterCount));
map.from(properties::getAccesslog)
.when(ServerProperties.Tomcat.Accesslog::isEnabled)
.to((enabled) -> customizeAccessLog(factory));
Expand Down Expand Up @@ -292,6 +296,10 @@ private void customizeMaxHttpFormPostSize(ConfigurableTomcatWebServerFactory fac
factory.addConnectorCustomizers((connector) -> connector.setMaxPostSize(maxHttpFormPostSize));
}

private void customizeMaxParameterCount(ConfigurableTomcatWebServerFactory factory, int maxParameterCount) {
factory.addConnectorCustomizers((connector) -> connector.setMaxParameterCount(maxParameterCount));
}

private void customizeAccessLog(ConfigurableTomcatWebServerFactory factory) {
ServerProperties.Tomcat tomcatProperties = this.serverProperties.getTomcat();
AccessLogValve valve = new AccessLogValve();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
* @author Chris Bono
* @author Parviz Rozikov
* @author Lasse Wulff
* @author Yanming Zhou
*/
@DirtiesUrlFactories
class ServerPropertiesTests {
Expand Down Expand Up @@ -199,7 +200,7 @@ void testCustomizedMimeMapping() {
}

@Test
void testCustomizeUriEncoding() {
void testCustomizeTomcatUriEncoding() {
bind("server.tomcat.uri-encoding", "US-ASCII");
assertThat(this.properties.getTomcat().getUriEncoding()).isEqualTo(StandardCharsets.US_ASCII);
}
Expand Down Expand Up @@ -235,17 +236,23 @@ void testCustomizeTomcatKeepAliveTimeoutWithInfinite() {
}

@Test
void customizeMaxKeepAliveRequests() {
void testCustomizeTomcatMaxKeepAliveRequests() {
bind("server.tomcat.max-keep-alive-requests", "200");
assertThat(this.properties.getTomcat().getMaxKeepAliveRequests()).isEqualTo(200);
}

@Test
void customizeMaxKeepAliveRequestsWithInfinite() {
void testCustomizeTomcatMaxKeepAliveRequestsWithInfinite() {
bind("server.tomcat.max-keep-alive-requests", "-1");
assertThat(this.properties.getTomcat().getMaxKeepAliveRequests()).isEqualTo(-1);
}

@Test
void testCustomizeTomcatMaxParameterCount() {
bind("server.tomcat.max-parameter-count", "100");
assertThat(this.properties.getTomcat().getMaxParameterCount()).isEqualTo(100);
}

@Test
void testCustomizeTomcatMinSpareThreads() {
bind("server.tomcat.threads.min-spare", "10");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
* @author Victor Mandujano
* @author Parviz Rozikov
* @author Moritz Halbritter
* @author Yanming Zhou
*/
class TomcatWebServerFactoryCustomizerTests {

Expand Down Expand Up @@ -194,6 +195,13 @@ void customMaxHttpRequestHeaderSize() {
.isEqualTo(DataSize.ofMegabytes(10).toBytes()));
}

@Test
void customMaxParameterCount() {
bind("server.tomcat.max-parameter-count=100");
customizeAndRunServer(
(server) -> assertThat(server.getTomcat().getConnector().getMaxParameterCount()).isEqualTo(100));
}

@Test
void customMaxRequestHttpHeaderSizeIgnoredIfNegative() {
bind("server.max-http-request-header-size=-1");
Expand Down

0 comments on commit 39c03fc

Please sign in to comment.