shashank created CAMEL-25248:
--------------------------------

             Summary: camel-undertow - a String body is always sent as UTF-8, 
even when the Content-Type declares another charset
                 Key: CAMEL-25248
                 URL: https://issues.apache.org/jira/browse/CAMEL-25248
             Project: Camel
          Issue Type: Bug
          Components: camel-undertow
            Reporter: shashank


The Undertow consumer writes a response body with

{code:java}
ByteBuffer bodyAsByteBuffer = tc.mandatoryConvertTo(ByteBuffer.class, body);   
// UndertowConsumer.sendResponse
{code}

and the producer a request body with {{tc.tryConvertTo(ByteBuffer.class, 
body)}} ({{UndertowProducer.process}}). Both convert *without the exchange*, so 
{{NIOConverter.toByteBuffer(String, Exchange)}} gets {{null}} and encodes a 
{{String}} in the default charset (UTF-8), whatever the message says:

* a route that sets {{Content-Type: text/plain; charset=ISO-8859-1}} and a 
{{String}} body: {{Grüße aus Köln}} (14 characters) is sent as 17 bytes of 
UTF-8 labelled ISO-8859-1, and the client reads {{Grü...}};
* an echo, or any route that keeps the request {{Content-Type}}: the consumer 
reads an ISO-8859-1 request correctly (it sets {{CamelCharsetName}} from the 
request charset), but the {{String}} reply under the same {{Content-Type}} is 
UTF-8;
* the producer: {{to("undertow:http://...";)}} with a {{String}} body and 
{{Content-Type: text/plain; charset=ISO-8859-1}} sends UTF-8 bytes under that 
header.

{{byte[]}}, {{InputStream}} and other non-{{String}} bodies, and ASCII text, 
are not affected. The other HTTP components honour the charset: 
camel-servlet/camel-jetty ({{DefaultHttpBinding}}) write a {{String}} in 
{{CamelCharsetName}}, camel-http uses {{CamelCharsetName}} or the 
{{Content-Type}} charset, camel-netty-http converts with the exchange, and 
camel-platform-http-vertx uses the {{Content-Type}} charset since CAMEL-25217.

h3. Reproduction

{code:java}
from("undertow:http://localhost:8080/latin1";)
    .setHeader(Exchange.CONTENT_TYPE, constant("text/plain; 
charset=ISO-8859-1"))
    .setBody(constant("Grüße aus Köln"));

from("undertow:http://localhost:8080/echo";)
    .convertBodyTo(String.class);
{code}

{{GET /latin1}} returns 17 bytes (UTF-8) instead of 14; {{POST /echo}} with 
{{Content-Type: text/plain; charset=ISO-8859-1}} and the 14 ISO-8859-1 bytes 
returns 17 bytes; 
{{template.sendBodyAndHeader("undertow:http://localhost:8080/received";, "Grüße 
aus Köln", "Content-Type", "text/plain; charset=ISO-8859-1")}} delivers 17 
bytes. A unit test with these three cases fails on main ({{array lengths 
differ, expected: <14> but was: <17>}}); a control with {{Content-Type: 
text/plain}} (UTF-8 expected) passes.

h3. Proposed fix

A {{String}} body is encoded in the charset declared by the {{Content-Type}} of 
the request or response being sent (new {{UndertowHelper.toByteBuffer(String, 
String)}}, used by the consumer and the producer), as CAMEL-25217 does for 
camel-platform-http-vertx. Without a declared charset (or with an unknown one) 
the body is converted as today (UTF-8), and other bodies are converted as 
today. {{CamelCharsetName}} is deliberately not used as a fallback: Undertow 
cannot declare it in the header afterwards (camel-servlet does, with 
{{setCharacterEncoding}}), so a response to an ISO-8859-1 request with 
{{Content-Type: application/json}} would be written in ISO-8859-1 while the 
client reads UTF-8.

With the fix the new test (5 cases, including that last one) and the whole 
camel-undertow suite pass (196 tests, 1 skipped). Only messages that declare a 
charset other than UTF-8 change, so the change gets an upgrade guide note.

Affected: 4.14.x, 4.18.x (same code) and main. The fix does not read 
{{CamelCharsetName}}, so it can be backported on its own (on 4.14.x/4.18.x the 
consumer still sets it to ISO-8859-1 for a request without charset; 892be63447 
changed that on main).

Duplicate check (2026-10-01): JIRA component camel-undertow with "charset", 
"encoding" or "UTF-8", and summary "undertow" with 
"charset"/"encoding"/"ISO-8859-1": only CAMEL-11392 (ByteBuffer overflow with 
multibyte characters, fixed 2017), CAMEL-12247, CAMEL-10803; nothing about the 
charset of a String body. GitHub pull requests "undertow charset": none.

_Filed with Claude Code on behalf of allthingssecurity._




--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to