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 10:53:39 UTC

[camel] branch main updated: CAMEL-19171: camel-http - Prevent duplicating slashes in generated URI (#9580)

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

nfilotto 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 f6baf0f2475 CAMEL-19171: camel-http - Prevent duplicating slashes in generated URI (#9580)
f6baf0f2475 is described below

commit f6baf0f2475dcf932d49f104ba850ed7177825df
Author: Nicolas Filotto <es...@users.noreply.github.com>
AuthorDate: Mon Mar 20 11:53:31 2023 +0100

    CAMEL-19171: camel-http - Prevent duplicating slashes in generated URI (#9580)
    
    ## Motivation
    
    When the endpoint URI has a trailing slash and the path starts with a slash, the generated URI contains 2 slashes instead of only one which was accepted by HttpComponents v4 but it is rejected by HttpComponents v5.
    
    ## Modifications:
    
    * Skip one slash one, when the endpoint URI has a trailing slash and the path to append starts with a slash
    * Add a unit test to cover the use case
---
 .../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(