Skip to content

Commit bc98410

Browse files
committed
Merge branch '6.1.x'
2 parents e95e442 + 0ca393c commit bc98410

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ public ContentCachingRequestWrapper(HttpServletRequest request) {
8989
public ContentCachingRequestWrapper(HttpServletRequest request, int contentCacheLimit) {
9090
super(request);
9191
int contentLength = request.getContentLength();
92-
this.cachedContent = (contentLength > 0) ? new FastByteArrayOutputStream(contentLength) : new FastByteArrayOutputStream();
92+
if (contentLength > 0) {
93+
this.cachedContent = new FastByteArrayOutputStream(Math.min(contentLength, contentCacheLimit));
94+
}
95+
else {
96+
this.cachedContent = new FastByteArrayOutputStream();
97+
}
9398
this.contentCacheLimit = contentCacheLimit;
9499
}
95100

spring-web/src/test/java/org/springframework/web/util/ContentCachingRequestWrapperTests.java

+16
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
package org.springframework.web.util;
1818

1919
import java.io.UnsupportedEncodingException;
20+
import java.lang.reflect.Field;
2021
import java.nio.charset.StandardCharsets;
2122

2223
import org.junit.jupiter.api.Test;
2324

2425
import org.springframework.http.HttpMethod;
2526
import org.springframework.http.MediaType;
27+
import org.springframework.util.FastByteArrayOutputStream;
28+
import org.springframework.util.ReflectionUtils;
2629
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
2730

2831
import static org.assertj.core.api.Assertions.assertThat;
@@ -89,6 +92,19 @@ void cachedContentToStringWithLimit() throws Exception {
8992
assertThat(wrapper.getContentAsString()).isEqualTo(new String("Hel".getBytes(CHARSET), CHARSET));
9093
}
9194

95+
@Test
96+
void shouldNotAllocateMoreThanCacheLimit() throws Exception {
97+
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(createGetRequest("Hello World"), CONTENT_CACHE_LIMIT);
98+
Field field = ReflectionUtils.findField(ContentCachingRequestWrapper.class, "cachedContent");
99+
ReflectionUtils.makeAccessible(field);
100+
FastByteArrayOutputStream cachedContent = (FastByteArrayOutputStream) ReflectionUtils.getField(field, wrapper);
101+
field = ReflectionUtils.findField(FastByteArrayOutputStream.class, "initialBlockSize");
102+
ReflectionUtils.makeAccessible(field);
103+
int blockSize = (int) ReflectionUtils.getField(field, cachedContent);
104+
assertThat(blockSize).isEqualTo(CONTENT_CACHE_LIMIT);
105+
}
106+
107+
92108
@Test
93109
void cachedContentWithOverflow() throws Exception {
94110
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(

0 commit comments

Comments
 (0)