This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 77a480c962de CAMEL-25355: camel-util, camel-support, camel-http-common 
- treat an empty charset parameter of the Content-Type as no charset (#27530)
77a480c962de is described below

commit 77a480c962de91d9ebd041540f4147473228069d
Author: allthingssecurity <[email protected]>
AuthorDate: Thu Oct 8 18:35:47 2026 +0530

    CAMEL-25355: camel-util, camel-support, camel-http-common - treat an empty 
charset parameter of the Content-Type as no charset (#27530)
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../camel/http/common/DefaultHttpBinding.java      | 10 +--
 .../HttpProducerEmptyCharsetParameterTest.java     | 73 +++++++++++++++++++++
 .../jetty/JettyEmptyCharsetParameterTest.java      | 63 ++++++++++++++++++
 .../servlet/ServletEmptyCharsetParameterTest.java  | 76 ++++++++++++++++++++++
 .../org/apache/camel/support/http/HttpUtil.java    |  5 +-
 .../apache/camel/support/http/HttpUtilTest.java    | 34 ++++++++++
 .../main/java/org/apache/camel/util/IOHelper.java  | 11 ++--
 .../java/org/apache/camel/util/IOHelperTest.java   | 10 +++
 8 files changed, 273 insertions(+), 9 deletions(-)

diff --git 
a/components/camel-http-common/src/main/java/org/apache/camel/http/common/DefaultHttpBinding.java
 
b/components/camel-http-common/src/main/java/org/apache/camel/http/common/DefaultHttpBinding.java
index 64e1dbb23afb..aa242fe85d65 100644
--- 
a/components/camel-http-common/src/main/java/org/apache/camel/http/common/DefaultHttpBinding.java
+++ 
b/components/camel-http-common/src/main/java/org/apache/camel/http/common/DefaultHttpBinding.java
@@ -190,9 +190,11 @@ public class DefaultHttpBinding implements HttpBinding {
             }
         }
 
-        if (request.getCharacterEncoding() != null) {
-            headers.put(Exchange.HTTP_CHARACTER_ENCODING, 
request.getCharacterEncoding());
-            
message.getExchange().setProperty(ExchangePropertyKey.CHARSET_NAME, 
request.getCharacterEncoding());
+        String characterEncoding = request.getCharacterEncoding();
+        // servlet containers may return an empty charset parameter (charset=) 
as an empty name, which is no charset
+        if (characterEncoding != null && !characterEncoding.isBlank()) {
+            headers.put(Exchange.HTTP_CHARACTER_ENCODING, characterEncoding);
+            
message.getExchange().setProperty(ExchangePropertyKey.CHARSET_NAME, 
characterEncoding);
         }
 
         try {
@@ -277,7 +279,7 @@ public class DefaultHttpBinding implements HttpBinding {
                     && 
request.getContentType().startsWith(HttpConstants.CONTENT_TYPE_WWW_FORM_URLENCODED)
                     && !skipWwwFormUrlEncoding) {
                 String charset = request.getCharacterEncoding();
-                if (charset == null) {
+                if (charset == null || charset.isBlank()) {
                     charset = "UTF-8";
                 }
 
diff --git 
a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProducerEmptyCharsetParameterTest.java
 
b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProducerEmptyCharsetParameterTest.java
new file mode 100644
index 000000000000..bb3127132de5
--- /dev/null
+++ 
b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProducerEmptyCharsetParameterTest.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.http;
+
+import java.nio.charset.StandardCharsets;
+
+import org.apache.camel.Exchange;
+import org.apache.hc.core5.http.HttpHeaders;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
+import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
+import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+/**
+ * A response with an empty charset parameter in the Content-Type has no 
charset, so the exchange charset is not set to
+ * an empty name and the body is read with the default charset (UTF-8).
+ */
+class HttpProducerEmptyCharsetParameterTest extends BaseHttpTest {
+
+    private static final String TEXT = "Grüße aus Köln";
+
+    private HttpServer localServer;
+
+    @Override
+    public void setupResources() throws Exception {
+        localServer = ServerBootstrap.bootstrap()
+                
.setCanonicalHostName("localhost").setHttpProcessor(getBasicHttpProcessor())
+                
.setConnectionReuseStrategy(getConnectionReuseStrategy()).setResponseFactory(getHttpResponseFactory())
+                .setSslContext(getSSLContext())
+                .register("/text", (request, response, context) -> {
+                    response.setHeader(HttpHeaders.CONTENT_TYPE, 
request.getFirstHeader("X-Response-Content-Type").getValue());
+                    response.setEntity(new 
ByteArrayEntity(TEXT.getBytes(StandardCharsets.UTF_8), null));
+                    response.setCode(HttpStatus.SC_OK);
+                }).create();
+        localServer.start();
+    }
+
+    @Override
+    public void cleanupResources() throws Exception {
+        if (localServer != null) {
+            localServer.stop();
+        }
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = { "text/plain; charset=", "text/plain; 
charset=\"\"", "text/plain; charset= ;format=flowed" })
+    void responseWithEmptyCharsetParameter(String contentType) {
+        Exchange exchange = template.request("http://localhost:"; + 
localServer.getLocalPort() + "/text",
+                e -> e.getIn().setHeader("X-Response-Content-Type", 
contentType));
+
+        assertNull(exchange.getException());
+        assertEquals(TEXT, exchange.getMessage().getBody(String.class));
+    }
+}
diff --git 
a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyEmptyCharsetParameterTest.java
 
b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyEmptyCharsetParameterTest.java
new file mode 100644
index 000000000000..f121fa7979b9
--- /dev/null
+++ 
b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyEmptyCharsetParameterTest.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.jetty;
+
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * A request with an empty charset parameter in the Content-Type has no 
charset, so the exchange charset is not set to
+ * an empty name and the body is read with the default charset (UTF-8).
+ */
+class JettyEmptyCharsetParameterTest extends BaseJettyTest {
+
+    private static final String TEXT = "Grüße aus Köln";
+
+    @Test
+    void requestWithEmptyCharsetParameter() throws Exception {
+        // Jetty itself rejects other empty forms such as charset="" 
(IllegalCharsetNameException in getContentType)
+        HttpRequest request = 
HttpRequest.newBuilder(URI.create("http://localhost:"; + getPort() + "/echo"))
+                .header("Content-Type", "text/plain; charset=")
+                .POST(HttpRequest.BodyPublishers.ofString(TEXT, 
StandardCharsets.UTF_8))
+                .build();
+        HttpResponse<String> response = HttpClient.newHttpClient()
+                .send(request, 
HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
+
+        assertEquals(200, response.statusCode(), response.body());
+        assertEquals("Hello " + TEXT, response.body());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("jetty:http://localhost:{{port}}/echo";)
+                        .convertBodyTo(String.class)
+                        .setBody(simple("Hello ${body}"));
+            }
+        };
+    }
+}
diff --git 
a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletEmptyCharsetParameterTest.java
 
b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletEmptyCharsetParameterTest.java
new file mode 100644
index 000000000000..e9a8eb9886b7
--- /dev/null
+++ 
b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletEmptyCharsetParameterTest.java
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.servlet;
+
+import java.io.ByteArrayInputStream;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * A request with an empty charset parameter in the Content-Type has no 
charset, so the exchange charset is not set to
+ * an empty name and the body is read with the default charset (UTF-8).
+ */
+class ServletEmptyCharsetParameterTest extends ServletCamelRouterTestSupport {
+
+    private static final String TEXT = "Grüße aus Köln";
+
+    // the embedded Undertow fails itself on a charset= at the very end of the 
header, so it is followed by a parameter
+    @ParameterizedTest
+    @ValueSource(strings = {
+            "text/plain; charset=; format=flowed", "text/plain; charset=\"\"", 
"text/plain; charset= ;format=flowed" })
+    void requestWithEmptyCharsetParameter(String contentType) throws Exception 
{
+        WebRequest req = new PostMethodWebRequest(
+                contextUrl + "/services/echo", new 
ByteArrayInputStream(TEXT.getBytes(StandardCharsets.UTF_8)), contentType);
+        WebResponse response = query(req, false);
+
+        assertEquals(200, response.getResponseCode(), 
response.getText(StandardCharsets.UTF_8));
+        assertEquals("Hello " + TEXT, 
response.getText(StandardCharsets.UTF_8));
+    }
+
+    @Test
+    void formWithEmptyCharsetParameter() throws Exception {
+        WebRequest req = new PostMethodWebRequest(
+                contextUrl + "/services/form",
+                new 
ByteArrayInputStream("greeting=Gr%C3%BC%C3%9Fe+aus+K%C3%B6ln".getBytes(StandardCharsets.US_ASCII)),
+                "application/x-www-form-urlencoded; charset=\"\"");
+        WebResponse response = query(req, false);
+
+        assertEquals(200, response.getResponseCode(), 
response.getText(StandardCharsets.UTF_8));
+        assertEquals(TEXT, response.getText(StandardCharsets.UTF_8));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("servlet:/echo")
+                        .convertBodyTo(String.class)
+                        .setBody(simple("Hello ${body}"));
+
+                from("servlet:/form")
+                        .setBody(header("greeting"));
+            }
+        };
+    }
+}
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/http/HttpUtil.java 
b/core/camel-support/src/main/java/org/apache/camel/support/http/HttpUtil.java
index 952334d9f031..4a5604561bc1 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/http/HttpUtil.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/http/HttpUtil.java
@@ -29,6 +29,7 @@ import org.apache.camel.Message;
 import org.apache.camel.TypeConverter;
 import org.apache.camel.spi.HeaderFilterStrategy;
 import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.URISupport;
 
@@ -83,7 +84,9 @@ public final class HttpUtil {
                 if (charset.contains(";")) {
                     charset = StringHelper.before(charset, ";");
                 }
-                return IOHelper.normalizeCharset(charset);
+                charset = IOHelper.normalizeCharset(charset);
+                // an empty charset parameter is no charset
+                return ObjectHelper.isEmpty(charset) ? null : charset;
             }
         }
         return null;
diff --git 
a/core/camel-support/src/test/java/org/apache/camel/support/http/HttpUtilTest.java
 
b/core/camel-support/src/test/java/org/apache/camel/support/http/HttpUtilTest.java
new file mode 100644
index 000000000000..b20b5f4b3f25
--- /dev/null
+++ 
b/core/camel-support/src/test/java/org/apache/camel/support/http/HttpUtilTest.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.support.http;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+@SuppressWarnings("deprecation")
+class HttpUtilTest {
+
+    @Test
+    void testGetCharsetFromContentTypeEmptyCharset() {
+        // an empty charset parameter is no charset
+        assertNull(HttpUtil.getCharsetFromContentType("text/plain; charset="));
+        assertNull(HttpUtil.getCharsetFromContentType("text/plain; 
charset=\"\""));
+        assertNull(HttpUtil.getCharsetFromContentType("text/plain; charset= 
"));
+        assertNull(HttpUtil.getCharsetFromContentType("text/plain; charset=; 
format=flowed"));
+    }
+}
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java 
b/core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java
index 58ea4874c69d..7e9911e1a5c1 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java
@@ -628,7 +628,7 @@ public final class IOHelper {
      * Get the charset name from the content type string
      *
      * @param  contentType the content type
-     * @return             the charset name, or <tt>UTF-8</tt> if no found
+     * @return             the charset name, or <tt>UTF-8</tt> if no found (or 
the charset parameter is empty)
      */
     public static String getCharsetNameFromContentType(String contentType) {
         // try optimized for direct match without using splitting
@@ -651,7 +651,9 @@ public final class IOHelper {
             } else {
                 charset = contentType.substring(pos + 8);
             }
-            return normalizeCharset(charset);
+            charset = normalizeCharset(charset);
+            // an empty charset parameter is no charset
+            return charset.isEmpty() ? "UTF-8" : charset;
         }
 
         String[] values = contentType.split(";");
@@ -661,8 +663,9 @@ public final class IOHelper {
             String prefix = "charset=";
             if (value.regionMatches(true, 0, prefix, 0, prefix.length())) {
                 // Take the charset name
-                String charset = value.substring(8);
-                return normalizeCharset(charset);
+                String charset = normalizeCharset(value.substring(8));
+                // an empty charset parameter is no charset
+                return charset.isEmpty() ? "UTF-8" : charset;
             }
         }
         // use UTF-8 as default
diff --git 
a/core/camel-util/src/test/java/org/apache/camel/util/IOHelperTest.java 
b/core/camel-util/src/test/java/org/apache/camel/util/IOHelperTest.java
index 5303e411ef81..8e652e88753b 100644
--- a/core/camel-util/src/test/java/org/apache/camel/util/IOHelperTest.java
+++ b/core/camel-util/src/test/java/org/apache/camel/util/IOHelperTest.java
@@ -55,6 +55,16 @@ public class IOHelperTest {
         assertEquals("iso-8859-1", 
IOHelper.getCharsetNameFromContentType("application/json; charset=iso-8859-1"));
     }
 
+    @Test
+    public void testCharsetEmpty() {
+        // an empty charset parameter is no charset, so the default is used
+        assertEquals("UTF-8", 
IOHelper.getCharsetNameFromContentType("text/plain; charset="));
+        assertEquals("UTF-8", 
IOHelper.getCharsetNameFromContentType("text/plain; charset=\"\""));
+        assertEquals("UTF-8", 
IOHelper.getCharsetNameFromContentType("text/plain; charset= "));
+        assertEquals("UTF-8", 
IOHelper.getCharsetNameFromContentType("text/plain; charset=; format=flowed"));
+        assertEquals("UTF-8", 
IOHelper.getCharsetNameFromContentType("text/plain; Charset="));
+    }
+
     @Test
     public void testCopyMaxSize() throws IOException {
         byte[] data = new byte[100];

Reply via email to