Fix InputStream caching in ContentCachingReqWrapper

Prior to this commit, the ContentCachingRequestWrapper would immediately
consume the wrapped request's InputStream when asked for the cached
content; that caused several issues:

* the request body was read in memory even if it wasn't yet consumed by
the application, leading to inefficiencies.
* when requesting the InputStream, an empty InputStream was returned
since the original was already read.

This case only happened for form POSTs requests.

This commit makes sure that the wrapper does not alter the request
expected behavior:

* when getting the inputstream, it is wrapped in order to cache its
content
* when getting request parameters, the request body is cached and its
inputstream is consumed, as expected

Issue: SPR-12810
This commit is contained in:
Brian Clozel
2015-03-13 10:30:29 +01:00
parent 88a14488a1
commit c6250f5164
2 changed files with 52 additions and 8 deletions

View File

@@ -61,6 +61,23 @@ public class ContentCachingRequestWrapperTests {
// getting request parameters will consume the request body
Assert.assertFalse(wrapper.getParameterMap().isEmpty());
Assert.assertEquals("first=value&second=foo&second=bar", new String(wrapper.getContentAsByteArray()));
// SPR-12810 : inputstream body should be consumed
Assert.assertEquals("", new String(FileCopyUtils.copyToByteArray(wrapper.getInputStream())));
}
// SPR-12810
@Test
public void inputStreamFormPostRequest() throws Exception {
this.request.setMethod("POST");
this.request.setContentType(FORM_CONTENT_TYPE);
this.request.setCharacterEncoding(CHARSET);
this.request.setParameter("first", "value");
this.request.setParameter("second", new String[] {"foo", "bar"});
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request);
byte[] response = FileCopyUtils.copyToByteArray(wrapper.getInputStream());
Assert.assertArrayEquals(response, wrapper.getContentAsByteArray());
}
}