-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Request Size Limit filter with test case and review comments
- Loading branch information
1 parent
112a35d
commit 83a32a0
Showing
5 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...ava/org/springframework/cloud/gateway/filter/factory/RequestSizeGatewayFilterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright 2013-2017 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.cloud.gateway.filter.factory; | ||
|
||
import org.springframework.cloud.gateway.filter.GatewayFilter; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.server.reactive.ServerHttpRequest; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* This filter blocks the request, if the request size is more than the permissible size.The default request size is 5 MB. | ||
* @author Arpan | ||
*/ | ||
public class RequestSizeGatewayFilterFactory | ||
extends AbstractGatewayFilterFactory<RequestSizeGatewayFilterFactory.RequestSizeConfig> { | ||
|
||
private static String PREFIX = "kMGTPE"; | ||
private static String ERROR = "Request size is larger than permissible limit." | ||
+ " Request size is %s where permissible limit is %s"; | ||
|
||
public RequestSizeGatewayFilterFactory() { | ||
super(RequestSizeGatewayFilterFactory.RequestSizeConfig.class); | ||
} | ||
|
||
@Override | ||
public GatewayFilter apply(RequestSizeGatewayFilterFactory.RequestSizeConfig requestSizeConfig) { | ||
requestSizeConfig.validate(); | ||
return (exchange, chain) -> { | ||
ServerHttpRequest request = exchange.getRequest(); | ||
String contentLength = request.getHeaders().getFirst("content-length"); | ||
if (!StringUtils.isEmpty(contentLength)) { | ||
Long currentRequestSize = Long.valueOf(contentLength); | ||
if (currentRequestSize > requestSizeConfig.getMaxSize()) { | ||
exchange.getResponse().setStatusCode(HttpStatus.PAYLOAD_TOO_LARGE); | ||
exchange.getResponse().getHeaders().add("errorMessage", | ||
getErrorMessage(currentRequestSize, requestSizeConfig.getMaxSize())); | ||
return exchange.getResponse().setComplete(); | ||
} | ||
} | ||
return chain.filter(exchange); | ||
}; | ||
} | ||
|
||
public static class RequestSizeConfig { | ||
|
||
private Long maxSize = 5000000L; | ||
|
||
public RequestSizeGatewayFilterFactory.RequestSizeConfig setMaxSize(Long maxSize) { | ||
this.maxSize = maxSize; | ||
return this; | ||
} | ||
|
||
public Long getMaxSize() { | ||
return maxSize; | ||
} | ||
|
||
public void validate() { | ||
Assert.isTrue(this.maxSize != null && this.maxSize > 0, "maxSize must be greater than 0"); | ||
Assert.isInstanceOf(Long.class, maxSize, "maxSize must be a number"); | ||
} | ||
} | ||
|
||
private static String getErrorMessage(Long currentRequestSize, Long maxSize) { | ||
return String.format(ERROR, getReadableByteCount(currentRequestSize), getReadableByteCount(maxSize)); | ||
} | ||
|
||
private static String getReadableByteCount(long bytes) { | ||
int unit = 1000; | ||
if (bytes < unit) | ||
return bytes + " B"; | ||
int exp = (int) (Math.log(bytes) / Math.log(unit)); | ||
String pre = Character.toString(PREFIX.charAt(exp - 1)); | ||
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...org/springframework/cloud/gateway/filter/factory/RequestSizeGatewayFilterFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.springframework.cloud.gateway.filter.factory; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.SpringBootConfiguration; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.cloud.gateway.route.RouteLocator; | ||
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; | ||
import org.springframework.cloud.gateway.test.BaseWebClientTests; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; | ||
|
||
/** | ||
* @author Arpan Das | ||
*/ | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(webEnvironment = RANDOM_PORT) | ||
@DirtiesContext | ||
public class RequestSizeGatewayFilterFactoryTest extends BaseWebClientTests { | ||
|
||
private static final String responseMesssage = "Request size is larger than permissible limit. Request size is 6.0 MB where permissible limit is 5.0 MB"; | ||
|
||
@Test | ||
public void setRequestSizeFilterWorks() { | ||
testClient.get().uri("/headers") | ||
.header("Host", "www.setrequestsize.org") | ||
.header("content-length", "6000000") | ||
.exchange().expectStatus().isEqualTo(HttpStatus.PAYLOAD_TOO_LARGE) | ||
.expectHeader().valueMatches("errorMessage", responseMesssage); | ||
} | ||
|
||
@EnableAutoConfiguration | ||
@SpringBootConfiguration | ||
@Import(DefaultTestConfig.class) | ||
public static class TestConfig { | ||
@Value("${test.uri}") | ||
String uri; | ||
|
||
@Bean | ||
public RouteLocator testRouteLocator(RouteLocatorBuilder builder) { | ||
return builder.routes().route("test_request_size", | ||
r -> r.order(-1).host("**.setrequestsize.org").filters(f -> f.setRequestSize(5000000L)).uri(uri)) | ||
.build(); | ||
} | ||
} | ||
} |