Short circuit caching request body if already done.

Fixes gh-2969
This commit is contained in:
sgibb
2024-03-20 23:00:27 -04:00
parent c79e9ad2b3
commit cc62626997

View File

@@ -361,6 +361,14 @@ public final class ServerWebExchangeUtils {
*/
private static <T> Mono<T> cacheRequestBody(ServerWebExchange exchange, boolean cacheDecoratedRequest,
Function<ServerHttpRequest, Mono<T>> function) {
// don't cache if body is already cached
Object cachedDataBuffer = exchange.getAttribute(CACHED_REQUEST_BODY_ATTR);
if (cachedDataBuffer instanceof DataBuffer) {
if (log.isTraceEnabled()) {
log.trace("body already in exchange attribute, short circuiting");
}
return Mono.just(exchange.getRequest()).flatMap(function);
}
ServerHttpResponse response = exchange.getResponse();
DataBufferFactory factory = response.bufferFactory();
// Join all the DataBuffers so we have a single DataBuffer for the body