Skip to content

Commit dea2029

Browse files
committed
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
1 parent 77d6f8b commit dea2029

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ public HttpMessageWriter<MultiValueMap<String, String>> getFormWriter() {
149149
/**
150150
* Set the character set to use for part headers such as
151151
* "Content-Disposition" (and its filename parameter).
152-
* <p>By default this is set to "UTF-8".
152+
* <p>By default this is set to "UTF-8". If changed from this default,
153+
* the "Content-Type" header will have a "charset" parameter that specifies
154+
* the character set used.
153155
*/
154156
public void setCharset(Charset charset) {
155157
Assert.notNull(charset, "Charset must not be null");

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ protected MediaType getMultipartMediaType(@Nullable MediaType mediaType, byte[]
102102
params.putAll(mediaType.getParameters());
103103
}
104104
params.put("boundary", new String(boundary, StandardCharsets.US_ASCII));
105-
params.put("charset", getCharset().name());
105+
Charset charset = getCharset();
106+
if (!charset.equals(StandardCharsets.UTF_8) &&
107+
!charset.equals(StandardCharsets.US_ASCII) ) {
108+
params.put("charset", getCharset().name());
109+
}
106110

107111
mediaType = (mediaType != null ? mediaType : MediaType.MULTIPART_FORM_DATA);
108112
mediaType = new MediaType(mediaType, params);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public void writeMultipartRelated() {
190190
assertThat(contentType.isCompatibleWith(mediaType)).isTrue();
191191
assertThat(contentType.getParameter("type")).isEqualTo("foo");
192192
assertThat(contentType.getParameter("boundary")).isNotEmpty();
193-
assertThat(contentType.getParameter("charset")).isEqualTo("UTF-8");
193+
assertThat(contentType.getParameter("charset")).isNull();
194194

195195
MultiValueMap<String, Part> requestParts = parse(this.response, hints);
196196
assertThat(requestParts.size()).isEqualTo(2);

0 commit comments

Comments
 (0)