Moved encodeHttpHeaderFieldParam method to HttpHeaders itself (including tests)
This commit also sets the test source encoding to UTF-8.
Issue: SPR-14547
(cherry picked from commit a8f7f75)
This commit is contained in:
@@ -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,7 +16,6 @@
|
||||
|
||||
package org.springframework.util;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
@@ -1195,44 +1194,4 @@ public abstract class StringUtils {
|
||||
return arrayToDelimitedString(arr, ",");
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the given header field param as describe in the rfc5987.
|
||||
* @param input the header field param
|
||||
* @param charset the charset of the header field param string
|
||||
* @return the encoded header field param
|
||||
* @see <a href="https://tools.ietf.org/html/rfc5987">rfc5987</a>
|
||||
* @since 5.0
|
||||
*/
|
||||
public static String encodeHttpHeaderFieldParam(String input, Charset charset) {
|
||||
Assert.notNull(charset, "charset should not be null");
|
||||
if(Charset.forName("US-ASCII").equals(charset)) {
|
||||
return input;
|
||||
}
|
||||
Assert.isTrue(Charset.forName("UTF-8").equals(charset) || Charset.forName("ISO-8859-1").equals(charset),
|
||||
"charset should be UTF-8 or ISO-8859-1");
|
||||
final byte[] source = input.getBytes(charset);
|
||||
final int len = source.length;
|
||||
final StringBuilder sb = new StringBuilder(len << 1);
|
||||
sb.append(charset.name());
|
||||
sb.append("''");
|
||||
for (byte b : source) {
|
||||
if (isRFC5987AttrChar(b)) {
|
||||
sb.append((char) b);
|
||||
}
|
||||
else {
|
||||
sb.append('%');
|
||||
char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16));
|
||||
char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 16));
|
||||
sb.append(hex1);
|
||||
sb.append(hex2);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static boolean isRFC5987AttrChar(byte c) {
|
||||
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
|
||||
|| c == '!' || c == '#' || c == '$' || c == '&' || c == '+' || c == '-'
|
||||
|| c == '.' || c == '^' || c == '_' || c == '`' || c == '|' || c == '~';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.util;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
import java.util.Locale;
|
||||
import java.util.Properties;
|
||||
@@ -628,8 +627,7 @@ public class StringUtilsTests {
|
||||
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
|
||||
}
|
||||
|
||||
// SPR-3671
|
||||
@Test
|
||||
@Test // SPR-3671
|
||||
public void testParseLocaleWithMultiValuedVariant() throws Exception {
|
||||
final String variant = "proper_northern";
|
||||
final String localeString = "en_GB_" + variant;
|
||||
@@ -637,8 +635,7 @@ public class StringUtilsTests {
|
||||
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
|
||||
}
|
||||
|
||||
// SPR-3671
|
||||
@Test
|
||||
@Test // SPR-3671
|
||||
public void testParseLocaleWithMultiValuedVariantUsingSpacesAsSeparators() throws Exception {
|
||||
final String variant = "proper northern";
|
||||
final String localeString = "en GB " + variant;
|
||||
@@ -646,8 +643,7 @@ public class StringUtilsTests {
|
||||
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
|
||||
}
|
||||
|
||||
// SPR-3671
|
||||
@Test
|
||||
@Test // SPR-3671
|
||||
public void testParseLocaleWithMultiValuedVariantUsingMixtureOfUnderscoresAndSpacesAsSeparators() throws Exception {
|
||||
final String variant = "proper northern";
|
||||
final String localeString = "en_GB_" + variant;
|
||||
@@ -655,8 +651,7 @@ public class StringUtilsTests {
|
||||
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
|
||||
}
|
||||
|
||||
// SPR-3671
|
||||
@Test
|
||||
@Test // SPR-3671
|
||||
public void testParseLocaleWithMultiValuedVariantUsingSpacesAsSeparatorsWithLotsOfLeadingWhitespace() throws Exception {
|
||||
final String variant = "proper northern";
|
||||
final String localeString = "en GB " + variant; // lots of whitespace
|
||||
@@ -664,8 +659,7 @@ public class StringUtilsTests {
|
||||
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
|
||||
}
|
||||
|
||||
// SPR-3671
|
||||
@Test
|
||||
@Test // SPR-3671
|
||||
public void testParseLocaleWithMultiValuedVariantUsingUnderscoresAsSeparatorsWithLotsOfLeadingWhitespace() throws Exception {
|
||||
final String variant = "proper_northern";
|
||||
final String localeString = "en_GB_____" + variant; // lots of underscores
|
||||
@@ -673,8 +667,7 @@ public class StringUtilsTests {
|
||||
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
|
||||
}
|
||||
|
||||
// SPR-7779
|
||||
@Test
|
||||
@Test // SPR-7779
|
||||
public void testParseLocaleWithInvalidCharacters() {
|
||||
try {
|
||||
StringUtils.parseLocaleString("%0D%0AContent-length:30%0D%0A%0D%0A%3Cscript%3Ealert%28123%29%3C/script%3E");
|
||||
@@ -685,15 +678,13 @@ public class StringUtilsTests {
|
||||
}
|
||||
}
|
||||
|
||||
// SPR-9420
|
||||
@Test
|
||||
@Test // SPR-9420
|
||||
public void testParseLocaleWithSameLowercaseTokenForLanguageAndCountry() {
|
||||
assertEquals("tr_TR", StringUtils.parseLocaleString("tr_tr").toString());
|
||||
assertEquals("bg_BG_vnt", StringUtils.parseLocaleString("bg_bg_vnt").toString());
|
||||
}
|
||||
|
||||
// SPR-11806
|
||||
@Test
|
||||
@Test // SPR-11806
|
||||
public void testParseLocaleWithVariantContainingCountryCode() {
|
||||
String variant = "GBtest";
|
||||
String localeString = "en_GB_" + variant;
|
||||
@@ -701,19 +692,4 @@ 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", Charset.forName("US-ASCII"));
|
||||
assertEquals("test.txt", result);
|
||||
|
||||
result = StringUtils.encodeHttpHeaderFieldParam("中文.txt", Charset.forName("UTF-8"));
|
||||
assertEquals("UTF-8''%E4%B8%AD%E6%96%87.txt", result);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void encodeHttpHeaderFieldParamInvalidCharset() {
|
||||
StringUtils.encodeHttpHeaderFieldParam("test", Charset.forName("UTF-16"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user