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 da53f2f2c57 CAMEL-21840: camel-http producer fix issue with HTTP_QUERY
and non ascii chars
da53f2f2c57 is described below
commit da53f2f2c5704b5e8b3b43ef8c2c337752dff479
Author: Dennis Bohnstedt <[email protected]>
AuthorDate: Thu Mar 6 17:21:29 2025 +0100
CAMEL-21840: camel-http producer fix issue with HTTP_QUERY and non ascii
chars
* Added unit-test, showing problem in camel-http path encoding, when using
Exchange.HTTP_QUERY
* Fixed problem in camel-http path encoding, by using Ascii encoded url
instead of uri when creating HttpUriRequest
* Changed testcase representation of danish characters to unicode encoded
hex
---
.../apache/camel/component/http/HttpProducer.java | 2 +-
.../apache/camel/component/http/HttpQueryTest.java | 43 ++++++++++++++++++++++
.../http/handler/BasicValidationHandler.java | 16 ++++++++
3 files changed, 60 insertions(+), 1 deletion(-)
diff --git
a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
index e5c93af69f4..fd6ba0673b6 100644
---
a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
+++
b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
@@ -645,7 +645,7 @@ public class HttpProducer extends DefaultProducer
implements LineNumberAware {
// create http holder objects for the request
HttpMethods methodToUse = HttpMethodHelper.createMethod(exchange,
getEndpoint());
- HttpUriRequest method = methodToUse.createMethod(uri);
+ HttpUriRequest method = methodToUse.createMethod(url);
// special for HTTP DELETE/GET if the message body should be included
if (getEndpoint().isDeleteWithBody() &&
"DELETE".equals(method.getMethod())
diff --git
a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpQueryTest.java
b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpQueryTest.java
index c4804311ba1..7e4a5a49039 100644
---
a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpQueryTest.java
+++
b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpQueryTest.java
@@ -29,6 +29,8 @@ public class HttpQueryTest extends BaseHttpTest {
private HttpServer localServer;
private String baseUrl;
+
+ private final String DANISH_CHARACTERS_UNICODE =
"\u00e6\u00f8\u00e5\u00C6\u00D8\u00C5";
@Override
public void setupResources() throws Exception {
@@ -40,6 +42,12 @@ public class HttpQueryTest extends BaseHttpTest {
.register("/test/", new BasicValidationHandler(GET.name(),
"my=@+camel", null, getExpectedContent()))
.register("/user/pass",
new BasicValidationHandler(GET.name(),
"password=baa&username=foo", null, getExpectedContent()))
+ .register("/user/passwd",
+ new BasicValidationHandler(
+ GET.name(),
"password='PasswordWithCharsThatNeedEscaping!≥≤!'&username=NotFromTheUSofA",
null,
+ getExpectedContent()))
+ .register("/danish-accepted",
+ new BasicValidationHandler(GET.name(), "characters='"+
DANISH_CHARACTERS_UNICODE +"'", null, getExpectedContent()))
.create();
localServer.start();
@@ -85,4 +93,39 @@ public class HttpQueryTest extends BaseHttpTest {
assertExchange(exchange);
}
+
+ @Test
+ public void httpQueryWithPasswordContainingNonAsciiCharacter() {
+ Exchange exchange = template.request(
+ baseUrl +
"/user/passwd?password='PasswordWithCharsThatNeedEscaping!≥≤!'&username=NotFromTheUSofA",
+ exchange1 -> {
+ });
+
+ assertExchange(exchange);
+ }
+
+ @Test
+ public void
httpQueryWithPasswordContainingNonAsciiCharacterAsQueryParams() {
+ Exchange exchange = template.request(baseUrl + "/user/passwd",
+ exchange1 -> exchange1.getIn().setHeader(Exchange.HTTP_QUERY,
+
"password='PasswordWithCharsThatNeedEscaping!≥≤!'&username=NotFromTheUSofA"));
+
+ assertExchange(exchange);
+ }
+
+ @Test
+ public void httpDanishCharactersAcceptedInBaseURL() {
+ Exchange exchange = template.request(baseUrl +
"/danish-accepted?characters='"+ DANISH_CHARACTERS_UNICODE +"'", exchange1 -> {
+ });
+
+ assertExchange(exchange);
+ }
+
+ @Test
+ public void httpDanishCharactersAcceptedAsQueryParams() {
+ Exchange exchange = template.request(baseUrl + "/danish-accepted",
+ exchange1 -> exchange1.getIn().setHeader(Exchange.HTTP_QUERY,
"characters='"+ DANISH_CHARACTERS_UNICODE +"'"));
+
+ assertExchange(exchange);
+ }
}
diff --git
a/components/camel-http/src/test/java/org/apache/camel/component/http/handler/BasicValidationHandler.java
b/components/camel-http/src/test/java/org/apache/camel/component/http/handler/BasicValidationHandler.java
index 680cffa7d5f..2974f793f47 100644
---
a/components/camel-http/src/test/java/org/apache/camel/component/http/handler/BasicValidationHandler.java
+++
b/components/camel-http/src/test/java/org/apache/camel/component/http/handler/BasicValidationHandler.java
@@ -17,6 +17,7 @@
package org.apache.camel.component.http.handler;
import java.io.IOException;
+import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
@@ -76,6 +77,12 @@ public class BasicValidationHandler implements
HttpRequestHandler {
return;
}
+ if (!validateRequestEncoding(request)) {
+ response.setCode(HttpStatus.SC_BAD_REQUEST);
+ response.setReasonPhrase("Request URI not encoded correctly!");
+ return;
+ }
+
if (expectedContent != null) {
HttpEntity entity = request.getEntity();
String content = EntityUtils.toString(entity);
@@ -105,6 +112,15 @@ public class BasicValidationHandler implements
HttpRequestHandler {
return true;
}
+ protected boolean validateRequestEncoding(ClassicHttpRequest request)
throws IOException {
+ try {
+ String encodedRequestPath = new
URI(request.getPath()).toASCIIString();
+ return request.getPath().equals(encodedRequestPath); // Did
request.path contain un-encoded characters?
+ } catch (URISyntaxException e) {
+ throw new IOException(e);
+ }
+ }
+
protected String buildResponse(ClassicHttpRequest request) {
return responseContent;
}