mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-17 16:39:29 +00:00
Allow unlimited caching in ContentCachingRequestWrapper
Prior to this commit the `ContentCachingRequestWrapper(HttpServletRequest)` constructor was deprecated; this variant caches by default an unlimited amount of data. The replacement `ContentCachingRequestWrapper(HttpServletRequest, int)` allows such behavior in 7.0 but that change has not been bacported to 6.2.x. This commit ensures that the replacement constructor can be safely used in 6.2.x, preparing for the 7.0.x upgrade. Fixes gh-36620
This commit is contained in:
+5
-3
@@ -85,7 +85,9 @@ public class ContentCachingRequestWrapper extends HttpServletRequestWrapper {
|
||||
/**
|
||||
* Create a new ContentCachingRequestWrapper for the given servlet request.
|
||||
* @param request the original servlet request
|
||||
* @param contentCacheLimit the maximum number of bytes to cache per request
|
||||
* @param contentCacheLimit the maximum number of bytes to cache per request;
|
||||
* no limit is set if the value is 0 or less. It is recommended to set a
|
||||
* concrete limit in order to avoid using too much memory.
|
||||
* @since 4.3.6
|
||||
* @see #handleContentOverflow(int)
|
||||
*/
|
||||
@@ -93,12 +95,12 @@ public class ContentCachingRequestWrapper extends HttpServletRequestWrapper {
|
||||
super(request);
|
||||
int contentLength = request.getContentLength();
|
||||
if (contentLength > 0) {
|
||||
this.cachedContent = new FastByteArrayOutputStream(Math.min(contentLength, contentCacheLimit));
|
||||
this.cachedContent = new FastByteArrayOutputStream((contentCacheLimit > 0 ? Math.min(contentLength, contentCacheLimit) : contentLength));
|
||||
}
|
||||
else {
|
||||
this.cachedContent = new FastByteArrayOutputStream();
|
||||
}
|
||||
this.contentCacheLimit = contentCacheLimit;
|
||||
this.contentCacheLimit = (contentCacheLimit > 0 ? contentCacheLimit : null);
|
||||
}
|
||||
|
||||
|
||||
|
||||
+8
@@ -74,6 +74,14 @@ class ContentCachingRequestWrapperTests {
|
||||
assertThat(wrapper.getContentAsString()).isEqualTo(new String(response, CHARSET));
|
||||
}
|
||||
|
||||
@Test
|
||||
void cachedContentToByteArrayWithoutLimit() throws Exception {
|
||||
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(createGetRequest("Hello World"), 0);
|
||||
byte[] response = wrapper.getInputStream().readAllBytes();
|
||||
assertThat(response).isEqualTo("Hello World".getBytes(CHARSET));
|
||||
assertThat(wrapper.getContentAsByteArray()).isEqualTo("Hello World".getBytes(CHARSET));
|
||||
}
|
||||
|
||||
@Test
|
||||
void cachedContentToByteArrayWithLimit() throws Exception {
|
||||
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(createGetRequest("Hello World"), CONTENT_CACHE_LIMIT);
|
||||
|
||||
Reference in New Issue
Block a user