Skip to content

Commit

Permalink
Only write non-default charset in MultipartWriterSupport
Browse files Browse the repository at this point in the history
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 spring-projectsgh-25885
  • Loading branch information
poutsma committed Nov 23, 2020
1 parent 7c47f55 commit f230624
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ public HttpMessageWriter<MultiValueMap<String, String>> getFormWriter() {
/**
* Set the character set to use for part headers such as
* "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) {
Assert.notNull(charset, "Charset must not be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ protected MediaType getMultipartMediaType(@Nullable MediaType mediaType, byte[]
params.putAll(mediaType.getParameters());
}
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 = new MediaType(mediaType, params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public void writeMultipartRelated() {
assertThat(contentType.isCompatibleWith(mediaType)).isTrue();
assertThat(contentType.getParameter("type")).isEqualTo("foo");
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);
assertThat(requestParts.size()).isEqualTo(2);
Expand Down

0 comments on commit f230624

Please sign in to comment.