Add RFC5987 support for HTTP header field params

This commit adds support for HTTP header field parameters encoding, as
described in RFC5987.
Note that the default implementation still relies on US-ASCII encoding,
as the latest rfc7230 Section 3.2.4 says that:

> Newly defined header fields SHOULD limit their field values to
  US-ASCII octets

Issue: SPR-14547
Cherry-picked from: f2faf84f31
This commit is contained in:
Brian Clozel
2016-08-25 14:39:07 +02:00
parent 026473280b
commit 9b91b9db8c
4 changed files with 86 additions and 3 deletions

View File

@@ -672,12 +672,32 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
* @param filename the filename (may be {@code null})
*/
public void setContentDispositionFormData(String name, String filename) {
setContentDispositionFormData(name, filename, null);
}
/**
* Set the (new) value of the {@code Content-Disposition} header
* for {@code form-data}, optionally encoding the filename using the rfc5987.
* <p>Only the US-ASCII, UTF-8 and ISO-8859-1 charsets are supported.
* @param name the control name
* @param filename the filename (may be {@code null})
* @param charset the charset used for the filename (may be {@code null})
* @see <a href="https://tools.ietf.org/html/rfc7230#section-3.2.4">rfc7230 Section 3.2.4</a>
* @since 5.0
*/
public void setContentDispositionFormData(String name, String filename, Charset charset) {
Assert.notNull(name, "'name' must not be null");
StringBuilder builder = new StringBuilder("form-data; name=\"");
builder.append(name).append('\"');
if (filename != null) {
builder.append("; filename=\"");
builder.append(filename).append('\"');
if(charset == null || Charset.forName("US-ASCII").equals(charset)) {
builder.append("; filename=\"");
builder.append(filename).append('\"');
}
else {
builder.append("; filename*=");
builder.append(StringUtils.encodeHttpHeaderFieldParam(filename, charset));
}
}
set(CONTENT_DISPOSITION, builder.toString());
}