HttpUtil.getCharset() fails for charset in double-quotes (#11373)
__Motivation__ As described in https://github.com/netty/netty/issues/11370 we should support quoted charset values __Modification__ Modify `HttpUtil.getCharset(CharSequence contentTypeValue, Charset defaultCharset)` to trim the double-quotes if present. __Result__ `HttpUtil.getCharset()` now supports quoted charsets. Fixes https://github.com/netty/netty/issues/11370
This commit is contained in:
parent
e69107ceaf
commit
625a7a1075
@ -394,10 +394,15 @@ public final class HttpUtil {
|
|||||||
*/
|
*/
|
||||||
public static Charset getCharset(CharSequence contentTypeValue, Charset defaultCharset) {
|
public static Charset getCharset(CharSequence contentTypeValue, Charset defaultCharset) {
|
||||||
if (contentTypeValue != null) {
|
if (contentTypeValue != null) {
|
||||||
CharSequence charsetCharSequence = getCharsetAsSequence(contentTypeValue);
|
CharSequence charsetRaw = getCharsetAsSequence(contentTypeValue);
|
||||||
if (charsetCharSequence != null) {
|
if (charsetRaw != null) {
|
||||||
|
if (charsetRaw.length() > 2) { // at least contains 2 quotes(")
|
||||||
|
if (charsetRaw.charAt(0) == '"' && charsetRaw.charAt(charsetRaw.length() - 1) == '"') {
|
||||||
|
charsetRaw = charsetRaw.subSequence(1, charsetRaw.length() - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
return Charset.forName(charsetCharSequence.toString());
|
return Charset.forName(charsetRaw.toString());
|
||||||
} catch (IllegalCharsetNameException ignored) {
|
} catch (IllegalCharsetNameException ignored) {
|
||||||
// just return the default charset
|
// just return the default charset
|
||||||
} catch (UnsupportedCharsetException ignored) {
|
} catch (UnsupportedCharsetException ignored) {
|
||||||
|
@ -76,19 +76,66 @@ public class HttpUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetCharset() {
|
public void testGetCharset() {
|
||||||
String NORMAL_CONTENT_TYPE = "text/html; charset=utf-8";
|
testGetCharsetUtf8("text/html; charset=utf-8");
|
||||||
String UPPER_CASE_NORMAL_CONTENT_TYPE = "TEXT/HTML; CHARSET=UTF-8";
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetCharsetNoSpace() {
|
||||||
|
testGetCharsetUtf8("text/html;charset=utf-8");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetCharsetQuoted() {
|
||||||
|
testGetCharsetUtf8("text/html; charset=\"utf-8\"");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetCharsetNoSpaceQuoted() {
|
||||||
|
testGetCharsetUtf8("text/html;charset=\"utf-8\"");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void testGetCharsetUtf8(String contentType) {
|
||||||
|
String UPPER_CASE_NORMAL_CONTENT_TYPE = contentType.toUpperCase();
|
||||||
|
|
||||||
HttpMessage message = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
|
HttpMessage message = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
|
||||||
message.headers().set(HttpHeaderNames.CONTENT_TYPE, NORMAL_CONTENT_TYPE);
|
message.headers().set(HttpHeaderNames.CONTENT_TYPE, contentType);
|
||||||
assertEquals(CharsetUtil.UTF_8, HttpUtil.getCharset(message));
|
assertEquals(CharsetUtil.UTF_8, HttpUtil.getCharset(message));
|
||||||
assertEquals(CharsetUtil.UTF_8, HttpUtil.getCharset(NORMAL_CONTENT_TYPE));
|
assertEquals(CharsetUtil.UTF_8, HttpUtil.getCharset(contentType));
|
||||||
|
|
||||||
message.headers().set(HttpHeaderNames.CONTENT_TYPE, UPPER_CASE_NORMAL_CONTENT_TYPE);
|
message.headers().set(HttpHeaderNames.CONTENT_TYPE, UPPER_CASE_NORMAL_CONTENT_TYPE);
|
||||||
assertEquals(CharsetUtil.UTF_8, HttpUtil.getCharset(message));
|
assertEquals(CharsetUtil.UTF_8, HttpUtil.getCharset(message));
|
||||||
assertEquals(CharsetUtil.UTF_8, HttpUtil.getCharset(UPPER_CASE_NORMAL_CONTENT_TYPE));
|
assertEquals(CharsetUtil.UTF_8, HttpUtil.getCharset(UPPER_CASE_NORMAL_CONTENT_TYPE));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetCharsetNoLeadingQuotes() {
|
||||||
|
testGetCharsetInvalidQuotes("text/html;charset=utf-8\"");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetCharsetNoTrailingQuotes() {
|
||||||
|
testGetCharsetInvalidQuotes("text/html;charset=\"utf-8");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetCharsetOnlyQuotes() {
|
||||||
|
testGetCharsetInvalidQuotes("text/html;charset=\"\"");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void testGetCharsetInvalidQuotes(String contentType) {
|
||||||
|
String UPPER_CASE_NORMAL_CONTENT_TYPE = contentType.toUpperCase();
|
||||||
|
|
||||||
|
HttpMessage message = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
|
||||||
|
message.headers().set(HttpHeaderNames.CONTENT_TYPE, contentType);
|
||||||
|
assertEquals(CharsetUtil.ISO_8859_1, HttpUtil.getCharset(message, CharsetUtil.ISO_8859_1));
|
||||||
|
assertEquals(CharsetUtil.ISO_8859_1, HttpUtil.getCharset(contentType, CharsetUtil.ISO_8859_1));
|
||||||
|
|
||||||
|
message.headers().set(HttpHeaderNames.CONTENT_TYPE, UPPER_CASE_NORMAL_CONTENT_TYPE);
|
||||||
|
assertEquals(CharsetUtil.ISO_8859_1, HttpUtil.getCharset(message, CharsetUtil.ISO_8859_1));
|
||||||
|
assertEquals(CharsetUtil.ISO_8859_1, HttpUtil.getCharset(UPPER_CASE_NORMAL_CONTENT_TYPE,
|
||||||
|
CharsetUtil.ISO_8859_1));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetCharsetIfNotLastParameter() {
|
public void testGetCharsetIfNotLastParameter() {
|
||||||
String NORMAL_CONTENT_TYPE_WITH_PARAMETERS = "application/soap-xml; charset=utf-8; "
|
String NORMAL_CONTENT_TYPE_WITH_PARAMETERS = "application/soap-xml; charset=utf-8; "
|
||||||
|
Loading…
x
Reference in New Issue
Block a user