Adds max initial line length configuration.

fixes gh-1554
This commit is contained in:
suntiancheng
2020-02-04 18:09:20 +08:00
committed by Spencer Gibb
parent 3f5c0b26ce
commit 070c4be396
5 changed files with 71 additions and 0 deletions

View File

@@ -609,6 +609,11 @@ public class GatewayAutoConfiguration {
spec.maxHeaderSize(
(int) properties.getMaxHeaderSize().toBytes());
}
if (properties.getMaxInitialLineLength() != null) {
// cast to int is ok, since @Max is Integer.MAX_VALUE
spec.maxInitialLineLength(
(int) properties.getMaxInitialLineLength().toBytes());
}
return spec;
}).tcpConfiguration(tcpClient -> {

View File

@@ -42,11 +42,13 @@ import org.springframework.boot.web.server.WebServerException;
import org.springframework.core.style.ToStringCreator;
import org.springframework.util.ResourceUtils;
import org.springframework.util.unit.DataSize;
import org.springframework.validation.annotation.Validated;
/**
* Configuration properties for the Netty {@link reactor.netty.http.client.HttpClient}.
*/
@ConfigurationProperties("spring.cloud.gateway.httpclient")
@Validated
public class HttpClientProperties {
/** The connect timeout in millis, the default is 45s. */
@@ -58,6 +60,9 @@ public class HttpClientProperties {
/** The max response header size. */
private DataSize maxHeaderSize;
/** The max initial line length. */
private DataSize maxInitialLineLength;
/** Pool configuration for Netty HttpClient. */
private Pool pool = new Pool();
@@ -98,6 +103,15 @@ public class HttpClientProperties {
this.maxHeaderSize = maxHeaderSize;
}
@Max(Integer.MAX_VALUE)
public DataSize getMaxInitialLineLength() {
return maxInitialLineLength;
}
public void setMaxInitialLineLength(DataSize maxInitialLineLength) {
this.maxInitialLineLength = maxInitialLineLength;
}
public Pool getPool() {
return pool;
}
@@ -145,6 +159,7 @@ public class HttpClientProperties {
.append("connectTimeout", connectTimeout)
.append("responseTimeout", responseTimeout)
.append("maxHeaderSize", maxHeaderSize)
.append("maxInitialLineLength", maxInitialLineLength)
.append("pool", pool)
.append("proxy", proxy)
.append("ssl", ssl)

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2013-2020 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
*
* https://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.cloud.gateway.config;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.constraints.Max;
import org.springframework.util.unit.DataSize;
// https://in.relation.to/2017/03/02/adding-custom-constraint-definitions-via-the-java-service-loader/
public class MaxDataSizeValidator implements ConstraintValidator<Max, DataSize> {
private long maxValue;
@Override
public boolean isValid(DataSize value, ConstraintValidatorContext context) {
// null values are valid
if (value == null) {
return true;
}
return value.toBytes() <= maxValue;
}
@Override
public void initialize(Max maxValue) {
this.maxValue = maxValue.value();
}
}

View File

@@ -91,11 +91,17 @@ public class GatewayAutoConfigurationTests {
"spring.cloud.gateway.httpclient.connect-timeout=10",
"spring.cloud.gateway.httpclient.response-timeout=10s",
"spring.cloud.gateway.httpclient.pool.type=fixed",
// greather than integer max value
"spring.cloud.gateway.httpclient.max-initial-line-length=2147483647",
"spring.cloud.gateway.httpclient.proxy.host=myhost",
"spring.cloud.gateway.httpclient.websocket.max-frame-payload-length=1024")
.run(context -> {
assertThat(context).hasSingleBean(HttpClient.class);
HttpClient httpClient = context.getBean(HttpClient.class);
HttpClientProperties properties = context
.getBean(HttpClientProperties.class);
assertThat(properties.getMaxInitialLineLength().toBytes())
.isLessThanOrEqualTo(Integer.MAX_VALUE);
/*
* FIXME: 2.1.0 HttpClientOptions options = httpClient.options();
*

View File

@@ -0,0 +1 @@
org.springframework.cloud.gateway.config.MaxDataSizeValidator