Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize initial size in ContentCachingRequestWrapper when contentCacheLimit set #29775

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
*/
public class ContentCachingRequestWrapper extends HttpServletRequestWrapper {

private static final int DEFAULT_INITIAL_BUFFER_SIZE_WHEN_UNKNOWN = 1024;

private final ByteArrayOutputStream cachedContent;

@Nullable
Expand All @@ -74,7 +76,8 @@ public class ContentCachingRequestWrapper extends HttpServletRequestWrapper {
public ContentCachingRequestWrapper(HttpServletRequest request) {
super(request);
int contentLength = request.getContentLength();
this.cachedContent = new ByteArrayOutputStream(contentLength >= 0 ? contentLength : 1024);
this.cachedContent = new ByteArrayOutputStream(contentLength >= 0 ?
contentLength : DEFAULT_INITIAL_BUFFER_SIZE_WHEN_UNKNOWN);
this.contentCacheLimit = null;
}

Expand All @@ -87,7 +90,9 @@ public ContentCachingRequestWrapper(HttpServletRequest request) {
*/
public ContentCachingRequestWrapper(HttpServletRequest request, int contentCacheLimit) {
super(request);
this.cachedContent = new ByteArrayOutputStream(contentCacheLimit);
int contentLength = request.getContentLength();
int guessedInitBufferSize = contentLength >= 0 ? contentLength : DEFAULT_INITIAL_BUFFER_SIZE_WHEN_UNKNOWN;
this.cachedContent = new ByteArrayOutputStream(Math.min(guessedInitBufferSize, contentCacheLimit));
this.contentCacheLimit = contentCacheLimit;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ public class ContentCachingRequestWrapperTests {

@Test
void cachedContent() throws Exception {
byte[] contentPayload = "Hello World".getBytes(CHARSET);
this.request.setMethod(GET);
this.request.setCharacterEncoding(CHARSET);
this.request.setContent("Hello World".getBytes(CHARSET));
this.request.setContent(contentPayload);

ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request);
byte[] response = FileCopyUtils.copyToByteArray(wrapper.getInputStream());
assertThat(wrapper.getContentAsByteArray()).isEqualTo(response);
assertThat(wrapper.getContentAsByteArray()).isEqualTo(contentPayload);
}

@Test
Expand Down Expand Up @@ -117,4 +119,40 @@ void inputStreamFormPostRequest() throws Exception {
assertThat(wrapper.getContentAsByteArray()).isEqualTo(response);
}

/**
* When content limiting, if the content length is known and is lower than the content limit, the internal
* buffer should allocate that size. This exaggerates this scenario by using a very large content limit that
* would cause an OOM Error likely otherwise.
*/
@Test
void cachedContentWithLimitKnownLengthAvoidOverAllocation() throws Exception {
Copy link
Contributor Author

@ryanrupp ryanrupp Jan 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit awkward to test without exposing some internals in a package private method or something (maybe reflection to grab the byte array size). Basically, using a really large content limit before this change could cause an OOM Error if you ran this test with < 2GB heap or so. After this change it wouldn't use the 2GB limit, it would just allow growing to it (but the tests don't exercise growing to it as that's not relevant here).

byte[] contentPayload = "Hello World".getBytes(CHARSET);
this.request.setMethod(POST);
this.request.setCharacterEncoding(CHARSET);
this.request.setContent(contentPayload);

ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request, Integer.MAX_VALUE);

byte[] contents = FileCopyUtils.copyToByteArray(wrapper.getInputStream());
assertThat(contents).isEqualTo(contentPayload);
assertThat(wrapper.getContentAsByteArray()).isEqualTo(contentPayload);
}

/**
* When content limiting, if the content length is unknown, rather than allocating the content limit upfront
* (which may be quite a bit larger than the actual request), a smaller initial buffer should be used that is
* allowed to grow as needed up to the limit. This exaggerates this scenario by using a very large content limit
* that would cause an OOM Error likely otherwise.
*/
@Test
void cachedContentWithLimitUnknownLengthAvoidOverAllocation() throws Exception {
this.request.setMethod(POST);
this.request.setCharacterEncoding(CHARSET);

ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request, Integer.MAX_VALUE);

byte[] contents = FileCopyUtils.copyToByteArray(wrapper.getInputStream());
assertThat(contents).isEmpty();
assertThat(wrapper.getContentAsByteArray()).isEmpty();
}
}