From e2cff150668e03ae2ab5ea31ce55b2611f164075 Mon Sep 17 00:00:00 2001 From: Sharang Gupta Date: Thu, 12 Nov 2026 02:37:08 +0000 Subject: [PATCH] Match ContentType parameter names ignoring case withParameters() compared parameter names case-sensitively, unlike the rest of the class. For "text/plain; Charset=UTF-8" it added a second charset parameter, and a new "charset" value did not replace the existing one. --- .../java/org/apache/hc/core5/http/ContentType.java | 11 ++++++----- .../org/apache/hc/core5/http/TestContentType.java | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) 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 9f02a8bc4..60a330734 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 @@ -527,21 +527,22 @@ public ContentType withParameters( if (params.length == 0) { return this; } - final Map paramMap = new LinkedHashMap<>(); + // parameter names are case-insensitive + final Map paramMap = new LinkedHashMap<>(); if (this.params != null) { for (final NameValuePair param: this.params) { - paramMap.put(param.getName(), param.getValue()); + paramMap.put(TextUtils.toLowerCase(param.getName()), param); } } for (final NameValuePair param: params) { - paramMap.put(param.getName(), param.getValue()); + paramMap.put(TextUtils.toLowerCase(param.getName()), param); } final List newParams = new ArrayList<>(paramMap.size() + 1); if (this.charset != null && !paramMap.containsKey(CHARSET)) { newParams.add(new BasicNameValuePair(CHARSET, this.charset.name())); } - for (final Map.Entry entry: paramMap.entrySet()) { - newParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + for (final NameValuePair param: paramMap.values()) { + newParams.add(new BasicNameValuePair(param.getName(), param.getValue())); } return create(this.getMimeType(), newParams.toArray(EMPTY_NAME_VALUE_PAIR_ARRAY), true); } 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 83e754f31..89f9d55f6 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 @@ -198,6 +198,20 @@ void testWithParams() throws Exception { Assertions.assertThat(contentType.toString()).isEqualTo("text/blah; charset=ISO-8859-1; p=blah"); } + @Test + void testWithParamsNameCaseInsensitive() { + final ContentType contentType = ContentType.parse("text/plain; Charset=UTF-8"); + Assertions.assertThat(contentType.getCharset()).isEqualTo(StandardCharsets.UTF_8); + + final ContentType added = contentType.withParameters(new BasicNameValuePair("p", "blah")); + Assertions.assertThat(added.getCharset()).isEqualTo(StandardCharsets.UTF_8); + Assertions.assertThat(added.toString()).isEqualTo("text/plain; Charset=UTF-8; p=blah"); + + final ContentType replaced = contentType.withParameters(new BasicNameValuePair("charset", "US-ASCII")); + Assertions.assertThat(replaced.getCharset()).isEqualTo(StandardCharsets.US_ASCII); + Assertions.assertThat(replaced.toString()).isEqualTo("text/plain; charset=US-ASCII"); + } + @Test void testParseLenientIgnoresIllegalCharsetName() { final ContentType contentType = ContentType.parseLenient("text/event-stream;charset=ISO-8859-1'");