Only write non-default charset in MultipartWriterSupport

This commit only writes the 'charset' parameter in the written headers
if it is non-default (not UTF-8), since RFC7578 states that the only
allowed parameter is 'boundary'.

Closes gh-25885
This commit is contained in:
Arjen Poutsma 2020-11-23 16:43:41 +01:00
parent 77d6f8bc00
commit dea2029e94
3 changed files with 9 additions and 3 deletions

View File

@ -149,7 +149,9 @@ public class MultipartHttpMessageWriter extends MultipartWriterSupport
/** /**
* Set the character set to use for part headers such as * Set the character set to use for part headers such as
* "Content-Disposition" (and its filename parameter). * "Content-Disposition" (and its filename parameter).
* <p>By default this is set to "UTF-8". * <p>By default this is set to "UTF-8". If changed from this default,
* the "Content-Type" header will have a "charset" parameter that specifies
* the character set used.
*/ */
public void setCharset(Charset charset) { public void setCharset(Charset charset) {
Assert.notNull(charset, "Charset must not be null"); Assert.notNull(charset, "Charset must not be null");

View File

@ -102,7 +102,11 @@ public class MultipartWriterSupport extends LoggingCodecSupport {
params.putAll(mediaType.getParameters()); params.putAll(mediaType.getParameters());
} }
params.put("boundary", new String(boundary, StandardCharsets.US_ASCII)); params.put("boundary", new String(boundary, StandardCharsets.US_ASCII));
params.put("charset", getCharset().name()); Charset charset = getCharset();
if (!charset.equals(StandardCharsets.UTF_8) &&
!charset.equals(StandardCharsets.US_ASCII) ) {
params.put("charset", getCharset().name());
}
mediaType = (mediaType != null ? mediaType : MediaType.MULTIPART_FORM_DATA); mediaType = (mediaType != null ? mediaType : MediaType.MULTIPART_FORM_DATA);
mediaType = new MediaType(mediaType, params); mediaType = new MediaType(mediaType, params);

View File

@ -190,7 +190,7 @@ public class MultipartHttpMessageWriterTests extends AbstractLeakCheckingTests {
assertThat(contentType.isCompatibleWith(mediaType)).isTrue(); assertThat(contentType.isCompatibleWith(mediaType)).isTrue();
assertThat(contentType.getParameter("type")).isEqualTo("foo"); assertThat(contentType.getParameter("type")).isEqualTo("foo");
assertThat(contentType.getParameter("boundary")).isNotEmpty(); assertThat(contentType.getParameter("boundary")).isNotEmpty();
assertThat(contentType.getParameter("charset")).isEqualTo("UTF-8"); assertThat(contentType.getParameter("charset")).isNull();
MultiValueMap<String, Part> requestParts = parse(this.response, hints); MultiValueMap<String, Part> requestParts = parse(this.response, hints);
assertThat(requestParts.size()).isEqualTo(2); assertThat(requestParts.size()).isEqualTo(2);