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.
This commit is contained in:
@@ -173,7 +173,7 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
|
||||
}
|
||||
}
|
||||
if (headers.getContentLength() == -1) {
|
||||
Long contentLength = getContentLength(t, contentType);
|
||||
Long contentLength = getContentLength(t, headers.getContentType());
|
||||
if (contentLength != null) {
|
||||
headers.setContentLength(contentLength);
|
||||
}
|
||||
|
||||
@@ -85,4 +85,22 @@ public class StringHttpMessageConverterTests {
|
||||
outputMessage.getHeaders().getContentLength());
|
||||
assertFalse("Invalid accept-charset", outputMessage.getHeaders().getAcceptCharset().isEmpty());
|
||||
}
|
||||
|
||||
// SPR-8867
|
||||
|
||||
@Test
|
||||
public void writeOverrideRequestedContentType() throws IOException {
|
||||
Charset utf8 = Charset.forName("UTF-8");
|
||||
MediaType requestedContentType = new MediaType("text", "html");
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
MediaType contentType = new MediaType("text", "plain", utf8);
|
||||
outputMessage.getHeaders().setContentType(contentType);
|
||||
String body = "H\u00e9llo W\u00f6rld";
|
||||
converter.write(body, requestedContentType, outputMessage);
|
||||
assertEquals("Invalid result", body, outputMessage.getBodyAsString(utf8));
|
||||
assertEquals("Invalid content-type", contentType, outputMessage.getHeaders().getContentType());
|
||||
assertEquals("Invalid content-length", body.getBytes(utf8).length,
|
||||
outputMessage.getHeaders().getContentLength());
|
||||
assertFalse("Invalid accept-charset", outputMessage.getHeaders().getAcceptCharset().isEmpty());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user