Skip to content

Commit

Permalink
Fix HttpUtil.getCharset to not throw illegal charset exception (netty…
Browse files Browse the repository at this point in the history
…#9439)

Motivation:

If the HttpUtil.getCharset method is called with an illegal charset like
"charset=!illegal!" it throws an IllegalCharsetNameException. But the javadoc
states, that defaultCharset is returned if incorrect header value. Since the
client sending the request sets the header value this should not crash.

Modification:

HttpUtil.getCharset catches the IllegalCharsetNameException and returns the
defualt value.

Result:

HttpUtil.getCharset does not throw IllegalCharsetNameException any more.
  • Loading branch information
noSim authored and normanmaurer committed Aug 10, 2019
1 parent 3f0f322 commit cbc8fc4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
import java.util.Iterator;
Expand Down Expand Up @@ -391,15 +392,14 @@ public static Charset getCharset(CharSequence contentTypeValue, Charset defaultC
if (charsetCharSequence != null) {
try {
return Charset.forName(charsetCharSequence.toString());
} catch (IllegalCharsetNameException ignored) {
// just return the default charset
} catch (UnsupportedCharsetException ignored) {
return defaultCharset;
// just return the default charset
}
} else {
return defaultCharset;
}
} else {
return defaultCharset;
}
return defaultCharset;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public void testGetCharsetIfNotLastParameter() {
public void testGetCharset_defaultValue() {
final String SIMPLE_CONTENT_TYPE = "text/html";
final String CONTENT_TYPE_WITH_INCORRECT_CHARSET = "text/html; charset=UTFFF";
final String CONTENT_TYPE_WITH_ILLEGAL_CHARSET_NAME = "text/html; charset=!illegal!";

HttpMessage message = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
message.headers().set(HttpHeaderNames.CONTENT_TYPE, SIMPLE_CONTENT_TYPE);
Expand All @@ -127,6 +128,15 @@ public void testGetCharset_defaultValue() {
assertEquals(CharsetUtil.UTF_8, HttpUtil.getCharset(message, StandardCharsets.UTF_8));
assertEquals(CharsetUtil.UTF_8,
HttpUtil.getCharset(CONTENT_TYPE_WITH_INCORRECT_CHARSET, StandardCharsets.UTF_8));

message.headers().set(HttpHeaderNames.CONTENT_TYPE, CONTENT_TYPE_WITH_ILLEGAL_CHARSET_NAME);
assertEquals(CharsetUtil.ISO_8859_1, HttpUtil.getCharset(message));
assertEquals(CharsetUtil.ISO_8859_1, HttpUtil.getCharset(CONTENT_TYPE_WITH_ILLEGAL_CHARSET_NAME));

message.headers().set(HttpHeaderNames.CONTENT_TYPE, CONTENT_TYPE_WITH_ILLEGAL_CHARSET_NAME);
assertEquals(CharsetUtil.UTF_8, HttpUtil.getCharset(message, StandardCharsets.UTF_8));
assertEquals(CharsetUtil.UTF_8,
HttpUtil.getCharset(CONTENT_TYPE_WITH_ILLEGAL_CHARSET_NAME, StandardCharsets.UTF_8));
}

@Test
Expand Down

0 comments on commit cbc8fc4

Please sign in to comment.