From ea274fc5f8ec926d37480c4256ea07b57ff867db Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Sat, 14 Feb 2026 18:03:55 +0100 Subject: [PATCH] HTTPCORE-794 - Fix parseLenient to ignore illegal charset names --- .../org/apache/hc/core5/http/ContentType.java | 3 ++- .../apache/hc/core5/http/TestContentType.java | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) 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 31566b5191..94cb335cd4 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 c0e5c00f80..29e413cd92 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()); + } + + + }