Skip to content

Commit 2482a83

Browse files
committed
ContentCachingResponseWrapper skips contentLength for chunked responses
Closes gh-26182
1 parent a88093c commit 2482a83

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import javax.servlet.http.HttpServletResponse;
2828
import javax.servlet.http.HttpServletResponseWrapper;
2929

30+
import org.springframework.http.HttpHeaders;
3031
import org.springframework.lang.Nullable;
3132
import org.springframework.util.FastByteArrayOutputStream;
3233

@@ -223,7 +224,9 @@ protected void copyBodyToResponse(boolean complete) throws IOException {
223224
if (this.content.size() > 0) {
224225
HttpServletResponse rawResponse = (HttpServletResponse) getResponse();
225226
if ((complete || this.contentLength != null) && !rawResponse.isCommitted()) {
226-
rawResponse.setContentLength(complete ? this.content.size() : this.contentLength);
227+
if (rawResponse.getHeader(HttpHeaders.TRANSFER_ENCODING) == null) {
228+
rawResponse.setContentLength(complete ? this.content.size() : this.contentLength);
229+
}
227230
this.contentLength = null;
228231
}
229232
this.content.writeTo(rawResponse.getOutputStream());
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2002-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.web.filter;
17+
18+
import java.nio.charset.StandardCharsets;
19+
20+
import javax.servlet.http.HttpServletResponse;
21+
22+
import org.junit.Test;
23+
24+
import org.springframework.http.HttpHeaders;
25+
import org.springframework.mock.web.test.MockHttpServletResponse;
26+
import org.springframework.util.FileCopyUtils;
27+
import org.springframework.web.util.ContentCachingResponseWrapper;
28+
29+
import static org.junit.Assert.assertArrayEquals;
30+
import static org.junit.Assert.assertEquals;
31+
import static org.junit.Assert.assertNull;
32+
import static org.junit.Assert.assertTrue;
33+
34+
/**
35+
* Unit tests for {@link ContentCachingResponseWrapper}.
36+
* @author Rossen Stoyanchev
37+
*/
38+
public class ContentCachingResponseWrapperTests {
39+
40+
@Test
41+
public void copyBodyToResponse() throws Exception {
42+
byte[] responseBody = "Hello World".getBytes(StandardCharsets.UTF_8);
43+
MockHttpServletResponse response = new MockHttpServletResponse();
44+
45+
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
46+
responseWrapper.setStatus(HttpServletResponse.SC_OK);
47+
FileCopyUtils.copy(responseBody, responseWrapper.getOutputStream());
48+
responseWrapper.copyBodyToResponse();
49+
50+
assertEquals(200, response.getStatus());
51+
assertTrue(response.getContentLength() > 0);
52+
assertArrayEquals(responseBody, response.getContentAsByteArray());
53+
}
54+
55+
@Test
56+
public void copyBodyToResponseWithTransferEncoding() throws Exception {
57+
byte[] responseBody = "6\r\nHello 5\r\nWorld0\r\n\r\n".getBytes(StandardCharsets.UTF_8);
58+
MockHttpServletResponse response = new MockHttpServletResponse();
59+
60+
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
61+
responseWrapper.setStatus(HttpServletResponse.SC_OK);
62+
responseWrapper.setHeader(HttpHeaders.TRANSFER_ENCODING, "chunked");
63+
FileCopyUtils.copy(responseBody, responseWrapper.getOutputStream());
64+
responseWrapper.copyBodyToResponse();
65+
66+
assertEquals(200, response.getStatus());
67+
assertEquals("chunked", response.getHeader(HttpHeaders.TRANSFER_ENCODING));
68+
assertNull(response.getHeader(HttpHeaders.CONTENT_LENGTH));
69+
assertArrayEquals(responseBody, response.getContentAsByteArray());
70+
}
71+
72+
}

0 commit comments

Comments
 (0)