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
This commit is contained in:
Brian Clozel
2016-08-25 14:21:25 +02:00
parent 41f7680e20
commit f2faf84f31
4 changed files with 88 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package org.springframework.util;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Locale;
import java.util.Properties;
@@ -700,4 +701,19 @@ public class StringUtilsTests {
assertEquals("Variant containing country code not extracted correctly", variant, locale.getVariant());
}
// SPR-14547
@Test
public void encodeHttpHeaderFieldParam() {
String result = StringUtils.encodeHttpHeaderFieldParam("test.txt", StandardCharsets.US_ASCII);
assertEquals("test.txt", result);
result = StringUtils.encodeHttpHeaderFieldParam("中文.txt", StandardCharsets.UTF_8);
assertEquals("UTF-8''%E4%B8%AD%E6%96%87.txt", result);
}
@Test(expected = IllegalArgumentException.class)
public void encodeHttpHeaderFieldParamInvalidCharset() {
StringUtils.encodeHttpHeaderFieldParam("test", StandardCharsets.UTF_16);
}
}