Skip to content

Commit a9a068e

Browse files
committed
SPR-8867 Fix issue with Content-Length header and UTF-8 charset.
The AbstractHttpMessageConverter was using the requested Content-Type rather than the actual response Content-Type to determine the length of the content. This can lead to a problem when a controller returns a ResponseEntity with a Content-Type header that ignores (overrides) the requested Content-Type. The fix ensures that actual response Content-Type is the one used both to write to the response and to determine the length of the content.
1 parent aa7fcb5 commit a9a068e

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

build-spring-framework/resources/changelog.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ Changes in version 3.1 RC2 (2011-11-15)
2323
* added ignoreDefaultModelOnRedirect attribute to <mvc:annotation-driven/>
2424
* added methods to UriComponentsBuilder for replacing the path or the query
2525
* added ServletUriComponentsBuilder to build a UriComponents instance starting with a ServletRequest
26-
* support UriComponentsBuilder as @Controller method argument
26+
* added support for UriComponentsBuilder as @Controller method argument
2727
* MockHttpServletRequest and MockHttpServletResponse now keep contentType field and Content-Type header in sync
2828
* fixed issue with cache ignoring prototype-scoped controllers in RequestMappingHandlerAdapter
29-
* update Spring MVC configuration section to include MVC Java config and the MVC namespace
29+
* updated Spring MVC configuration section to include MVC Java config and the MVC namespace
30+
* fixed issue with setting Content-Length header depending on the charset of the response
3031

3132
Changes in version 3.1 RC1 (2011-10-11)
3233
---------------------------------------

org.springframework.web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public final void write(T t, MediaType contentType, HttpOutputMessage outputMess
173173
}
174174
}
175175
if (headers.getContentLength() == -1) {
176-
Long contentLength = getContentLength(t, contentType);
176+
Long contentLength = getContentLength(t, headers.getContentType());
177177
if (contentLength != null) {
178178
headers.setContentLength(contentLength);
179179
}

org.springframework.web/src/test/java/org/springframework/http/converter/StringHttpMessageConverterTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,22 @@ public void writeUTF8() throws IOException {
8585
outputMessage.getHeaders().getContentLength());
8686
assertFalse("Invalid accept-charset", outputMessage.getHeaders().getAcceptCharset().isEmpty());
8787
}
88+
89+
// SPR-8867
90+
91+
@Test
92+
public void writeOverrideRequestedContentType() throws IOException {
93+
Charset utf8 = Charset.forName("UTF-8");
94+
MediaType requestedContentType = new MediaType("text", "html");
95+
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
96+
MediaType contentType = new MediaType("text", "plain", utf8);
97+
outputMessage.getHeaders().setContentType(contentType);
98+
String body = "H\u00e9llo W\u00f6rld";
99+
converter.write(body, requestedContentType, outputMessage);
100+
assertEquals("Invalid result", body, outputMessage.getBodyAsString(utf8));
101+
assertEquals("Invalid content-type", contentType, outputMessage.getHeaders().getContentType());
102+
assertEquals("Invalid content-length", body.getBytes(utf8).length,
103+
outputMessage.getHeaders().getContentLength());
104+
assertFalse("Invalid accept-charset", outputMessage.getHeaders().getAcceptCharset().isEmpty());
105+
}
88106
}

0 commit comments

Comments
 (0)