Browse Source

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
pull/26157/head
Arjen Poutsma 4 years ago
parent
commit
dea2029e94
  1. 4
      spring-web/src/main/java/org/springframework/http/codec/multipart/MultipartHttpMessageWriter.java
  2. 4
      spring-web/src/main/java/org/springframework/http/codec/multipart/MultipartWriterSupport.java
  3. 2
      spring-web/src/test/java/org/springframework/http/codec/multipart/MultipartHttpMessageWriterTests.java

4
spring-web/src/main/java/org/springframework/http/codec/multipart/MultipartHttpMessageWriter.java

@ -149,7 +149,9 @@ public class MultipartHttpMessageWriter extends MultipartWriterSupport @@ -149,7 +149,9 @@ public class MultipartHttpMessageWriter extends MultipartWriterSupport
/**
* 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");

4
spring-web/src/main/java/org/springframework/http/codec/multipart/MultipartWriterSupport.java

@ -102,7 +102,11 @@ public class MultipartWriterSupport extends LoggingCodecSupport { @@ -102,7 +102,11 @@ public class MultipartWriterSupport extends LoggingCodecSupport {
params.putAll(mediaType.getParameters());
}
params.put("boundary", new String(boundary, StandardCharsets.US_ASCII));
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);

2
spring-web/src/test/java/org/springframework/http/codec/multipart/MultipartHttpMessageWriterTests.java

@ -190,7 +190,7 @@ public class MultipartHttpMessageWriterTests extends AbstractLeakCheckingTests { @@ -190,7 +190,7 @@ public class MultipartHttpMessageWriterTests extends AbstractLeakCheckingTests {
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);

Loading…
Cancel
Save