Skip to content

Commit cc62626

Browse files
committed
Short circuit caching request body if already done.
Fixes gh-2969
1 parent c79e9ad commit cc62626

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/support/ServerWebExchangeUtils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,14 @@ public static <T> Mono<T> cacheRequestBody(ServerWebExchange exchange,
361361
*/
362362
private static <T> Mono<T> cacheRequestBody(ServerWebExchange exchange, boolean cacheDecoratedRequest,
363363
Function<ServerHttpRequest, Mono<T>> function) {
364+
// don't cache if body is already cached
365+
Object cachedDataBuffer = exchange.getAttribute(CACHED_REQUEST_BODY_ATTR);
366+
if (cachedDataBuffer instanceof DataBuffer) {
367+
if (log.isTraceEnabled()) {
368+
log.trace("body already in exchange attribute, short circuiting");
369+
}
370+
return Mono.just(exchange.getRequest()).flatMap(function);
371+
}
364372
ServerHttpResponse response = exchange.getResponse();
365373
DataBufferFactory factory = response.bufferFactory();
366374
// Join all the DataBuffers so we have a single DataBuffer for the body

0 commit comments

Comments
 (0)