You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2023/03/20 09:40:21 UTC

[camel] 01/01: CAMEL-19171: camel-http - Prevent duplicating slashes in generated URI

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

nfilotto pushed a commit to branch CAMEL-19171/prevent-double-slashes
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 1bd0f1ce69d6cdc390ea68be8369d8a67246c881
Author: Nicolas Filotto <nf...@talend.com>
AuthorDate: Mon Mar 20 10:39:50 2023 +0100

    CAMEL-19171: camel-http - Prevent duplicating slashes in generated URI
---
 .../org/apache/camel/http/common/HttpHelper.java   | 10 ++++--
 .../component/http/helper/HttpHelperTest.java      | 38 ++++++++++++++++++++++
 2 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java
index 9ca91573cbe..38e88fbecb2 100644
--- a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java
+++ b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java
@@ -209,13 +209,17 @@ public final class HttpHelper {
                 // if there are no query params
                 if (idx == -1) {
                     // make sure that there is exactly one "/" between HTTP_URI and HTTP_PATH
-                    uri = uri.endsWith("/") || path.startsWith("/") ? uri : uri + "/";
-                    uri = uri.concat(path);
+                    if (uri.endsWith("/") && path.startsWith("/")) {
+                        uri = uri.concat(path.substring(1));
+                    } else {
+                        uri = uri.endsWith("/") || path.startsWith("/") ? uri : uri + "/";
+                        uri = uri.concat(path);
+                    }
                 } else {
                     // there are query params, so inject the relative path in the right place
                     String base = uri.substring(0, idx);
                     base = base.endsWith("/") ? base : base + "/";
-                    base = base.concat(path);
+                    base = base.concat(path.startsWith("/") ? path.substring(1) : path);
                     uri = base.concat(uri.substring(idx));
                 }
             }
diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/helper/HttpHelperTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/helper/HttpHelperTest.java
index 30be75c9526..5a8445521b6 100644
--- a/components/camel-http/src/test/java/org/apache/camel/component/http/helper/HttpHelperTest.java
+++ b/components/camel-http/src/test/java/org/apache/camel/component/http/helper/HttpHelperTest.java
@@ -87,6 +87,44 @@ public class HttpHelperTest {
         assertEquals("http://camel.apache.org", url);
     }
 
+    @Test
+    public void createURLShouldReturnTheEndpointURIIfBridgeEndpointWithOneSlashOnly() throws URISyntaxException {
+        String url = HttpHelper.createURL(
+                createExchangeWithOptionalCamelHttpUriHeader("http://apache.org", "/"),
+                createHttpEndpoint(true, "http://camel.apache.org/"));
+
+        assertEquals("http://camel.apache.org/", url);
+    }
+
+    @Test
+    public void createURLShouldReturnTheEndpointURIIfBridgeEndpointWithSubPathAndOneSlashOnly() throws URISyntaxException {
+        String url = HttpHelper.createURL(
+                createExchangeWithOptionalCamelHttpUriHeader("http://apache.org", "/somePath/"),
+                createHttpEndpoint(true, "http://camel.apache.org/"));
+
+        assertEquals("http://camel.apache.org/somePath/", url);
+    }
+
+    @Test
+    public void createURLShouldReturnTheEndpointURIIfBridgeEndpointWithQueryParameterSubPathAndOneSlashOnly()
+            throws URISyntaxException {
+        String url = HttpHelper.createURL(
+                createExchangeWithOptionalCamelHttpUriHeader("http://apache.org", "/"),
+                createHttpEndpoint(true, "http://camel.apache.org/?foo=bar"));
+
+        assertEquals("http://camel.apache.org/?foo=bar", url);
+    }
+
+    @Test
+    public void createURLShouldReturnTheEndpointURIIfBridgeEndpointWithQueryParameterAndOneSlashOnly()
+            throws URISyntaxException {
+        String url = HttpHelper.createURL(
+                createExchangeWithOptionalCamelHttpUriHeader("http://apache.org", "/somePath/"),
+                createHttpEndpoint(true, "http://camel.apache.org/?foo=bar"));
+
+        assertEquals("http://camel.apache.org/somePath/?foo=bar", url);
+    }
+
     @Test
     public void createURLShouldReturnTheEndpointURIIfNotBridgeEndpoint() throws URISyntaxException {
         String url = HttpHelper.createURL(