Skip to content

Commit

Permalink
Only write non-default charset in FormHttpMessageConverter
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'.

See gh-25885
Closes gh-26290
  • Loading branch information
poutsma committed Jan 8, 2021
1 parent 69ce7d3 commit ce1ae2f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,10 @@ private void writeMultipart(

byte[] boundary = generateMultipartBoundary();
if (!isFilenameCharsetSet()) {
parameters.put("charset", this.charset.name());
if (!this.charset.equals(StandardCharsets.UTF_8) &&
!this.charset.equals(StandardCharsets.US_ASCII)) {
parameters.put("charset", this.charset.name());
}
}
parameters.put("boundary", new String(boundary, StandardCharsets.US_ASCII));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,29 @@ public void writeMultipartOrder() throws Exception {
.endsWith("><string>foo</string></MyBean>");
}

@Test
public void writeMultipartCharset() throws Exception {
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
parts.add("logo", logo);

MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
this.converter.write(parts, MULTIPART_FORM_DATA, outputMessage);

MediaType contentType = outputMessage.getHeaders().getContentType();
Map<String, String> parameters = contentType.getParameters();
assertThat(parameters).containsOnlyKeys("boundary");

this.converter.setCharset(StandardCharsets.ISO_8859_1);

outputMessage = new MockHttpOutputMessage();
this.converter.write(parts, MULTIPART_FORM_DATA, outputMessage);

parameters = outputMessage.getHeaders().getContentType().getParameters();
assertThat(parameters).containsOnlyKeys("boundary", "charset");
assertThat(parameters).containsEntry("charset", "ISO-8859-1");
}

private void assertCanRead(MediaType mediaType) {
assertCanRead(MultiValueMap.class, mediaType);
}
Expand Down

0 comments on commit ce1ae2f

Please sign in to comment.