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'");