diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/ContentType.java b/httpcore5/src/main/java/org/apache/hc/core5/http/ContentType.java index 31566b519..94cb335cd 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/ContentType.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/ContentType.java @@ -29,6 +29,7 @@ import java.io.Serializable; import java.nio.charset.Charset; +import java.nio.charset.IllegalCharsetNameException; import java.nio.charset.StandardCharsets; import java.nio.charset.UnsupportedCharsetException; import java.util.ArrayList; @@ -375,7 +376,7 @@ private static ContentType create(final String mimeType, final NameValuePair[] p if (!TextUtils.isBlank(s)) { try { charset = Charset.forName(s); - } catch (final UnsupportedCharsetException ex) { + } catch (final UnsupportedCharsetException | IllegalCharsetNameException ex) { if (strict) { throw ex; } diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/TestContentType.java b/httpcore5/src/test/java/org/apache/hc/core5/http/TestContentType.java index c0e5c00f8..29e413cd9 100644 --- a/httpcore5/src/test/java/org/apache/hc/core5/http/TestContentType.java +++ b/httpcore5/src/test/java/org/apache/hc/core5/http/TestContentType.java @@ -191,4 +191,22 @@ void testWithParams() throws Exception { Assertions.assertEquals("text/blah; charset=ISO-8859-1; p=blah", contentType.toString()); } + @Test + void testParseLenientIgnoresIllegalCharsetName() { + final ContentType contentType = ContentType.parseLenient("text/event-stream;charset=ISO-8859-1'"); + Assertions.assertNotNull(contentType); + Assertions.assertEquals("text/event-stream", contentType.getMimeType()); + Assertions.assertNull(contentType.getCharset()); + Assertions.assertEquals("ISO-8859-1'", contentType.getParameter("charset")); + } + + @Test + void testParseLenientIgnoresUnsupportedCharset() { + final ContentType contentType = ContentType.parseLenient("text/plain; charset=__no_such_charset__"); + Assertions.assertNotNull(contentType); + Assertions.assertNull(contentType.getCharset()); + } + + + }