You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2023/12/04 09:08:43 UTC

(camel) branch main updated: CAMEL-20182: camel-http - Allow http/https protocol to be in httpUri option that makes it tooling friendly, and also avoids problems with copy url from web browser to be used. And let this url determine the protocol to use (plain vs secure) if http or https is present. (#12304)

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 780b54611ec CAMEL-20182: camel-http - Allow http/https protocol to be in httpUri option that makes it tooling friendly, and also avoids problems with copy url from web browser to be used. And let this url determine the protocol to use (plain vs secure) if http or https is present. (#12304)
780b54611ec is described below

commit 780b54611ec515ee6bec80c0e180db18760d8981
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Dec 4 10:08:36 2023 +0100

    CAMEL-20182: camel-http - Allow http/https protocol to be in httpUri option that makes it tooling friendly, and also avoids problems with copy url from web browser to be used. And let this url determine the protocol to use (plain vs secure) if http or https is present. (#12304)
---
 .../apache/camel/component/http/HttpComponent.java | 28 +++++-----
 .../apache/camel/component/http/HttpEndpoint.java  |  1 -
 .../apache/camel/component/http/HttpProducer.java  |  1 -
 .../org/apache/camel/component/http/HttpUtil.java  | 13 +++++
 .../camel/component/http/HttpEndpointURLTest.java  | 21 +++----
 .../http/HttpInvalidConfigurationTest.java         | 64 ----------------------
 .../camel/component/http/HttpRedirectTest.java     |  2 -
 7 files changed, 38 insertions(+), 92 deletions(-)

diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
index 779dfdf2cbb..f62149ad775 100644
--- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
+++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
@@ -28,7 +28,6 @@ import javax.net.ssl.HostnameVerifier;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
 import org.apache.camel.Producer;
-import org.apache.camel.ResolveEndpointFailedException;
 import org.apache.camel.SSLContextParametersAware;
 import org.apache.camel.component.extension.ComponentVerifierExtension;
 import org.apache.camel.http.base.HttpHelper;
@@ -359,7 +358,15 @@ public class HttpComponent extends HttpCommonComponent implements RestProducerFa
         HeaderFilterStrategy headerFilterStrategy
                 = resolveAndRemoveReferenceParameter(parameters, "headerFilterStrategy", HeaderFilterStrategy.class);
 
-        boolean secure = HttpHelper.isSecureConnection(uri) || sslContextParameters != null;
+        // the actual protocol if present in the remainder part should take precedence
+        String secureProtocol = uri;
+        if (remaining.startsWith("http:") || remaining.startsWith("https:")) {
+            secureProtocol = remaining;
+        }
+        boolean secure = HttpHelper.isSecureConnection(secureProtocol) || sslContextParameters != null;
+
+        // remaining part should be without protocol as that was how this component was originally created
+        remaining = org.apache.camel.component.http.HttpUtil.removeHttpOrHttpsProtocol(remaining);
 
         // need to set scheme on address uri depending on if its secure or not
         String addressUri = (secure ? "https://" : "http://") + remaining;
@@ -367,23 +374,16 @@ public class HttpComponent extends HttpCommonComponent implements RestProducerFa
         addressUri = UnsafeUriCharactersEncoder.encodeHttpURI(addressUri);
         URI uriHttpUriAddress = new URI(addressUri);
 
-        // validate http uri that end-user did not duplicate the http part that can be a common error
-        int pos = uri.indexOf("//");
-        if (pos != -1) {
-            String part = uri.substring(pos + 2);
-            if (part.startsWith("http:") || part.startsWith("https:")) {
-                throw new ResolveEndpointFailedException(
-                        uri,
-                        "The uri part is not configured correctly. You have duplicated the http(s) protocol.");
-            }
-        }
+        // the endpoint uri should use the component name as scheme, so we need to re-create it once more
+        String scheme = StringHelper.before(uri, "://");
+
+        // uri part should be without protocol as that was how this component was originally created
+        uri = org.apache.camel.component.http.HttpUtil.removeHttpOrHttpsProtocol(uri);
 
         // create the configurer to use for this endpoint
         HttpClientConfigurer configurer = createHttpClientConfigurer(parameters, secure);
         URI endpointUri = URISupport.createRemainingURI(uriHttpUriAddress, httpClientParameters);
 
-        // the endpoint uri should use the component name as scheme, so we need to re-create it once more
-        String scheme = StringHelper.before(uri, "://");
         endpointUri = URISupport.createRemainingURI(
                 new URI(
                         scheme,
diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
index 66be0987401..cec0dea6e4d 100644
--- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
+++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
@@ -18,7 +18,6 @@ package org.apache.camel.component.http;
 
 import java.io.Closeable;
 import java.net.URI;
-import java.net.URISyntaxException;
 import java.util.Map;
 
 import javax.net.ssl.HostnameVerifier;
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 3622a57b8f3..65a8cd77859 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
@@ -56,7 +56,6 @@ import org.apache.camel.support.ObjectHelper;
 import org.apache.camel.support.SynchronizationAdapter;
 import org.apache.camel.support.http.HttpUtil;
 import org.apache.camel.util.IOHelper;
-import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.URISupport;
 import org.apache.camel.util.UnsafeUriCharactersEncoder;
 import org.apache.hc.client5.http.classic.HttpClient;
diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpUtil.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpUtil.java
index 212f7642429..1d1815212b4 100644
--- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpUtil.java
+++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpUtil.java
@@ -32,4 +32,17 @@ public final class HttpUtil {
     public static Optional<String> responseHeaderValue(HttpResponse response, String headerName) {
         return responseHeader(response, headerName).map(Header::getValue);
     }
+
+    public static String removeHttpOrHttpsProtocol(String uri) {
+        if (uri.startsWith("http://")) {
+            uri = uri.substring(7);
+        } else if (uri.startsWith("http:")) {
+            uri = uri.substring(5);
+        } else if (uri.startsWith("https://")) {
+            uri = uri.substring(8);
+        } else if (uri.startsWith("https:")) {
+            uri = uri.substring(6);
+        }
+        return uri;
+    }
 }
diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpEndpointURLTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpEndpointURLTest.java
index 14bbb4f8548..976bbf279ce 100644
--- a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpEndpointURLTest.java
+++ b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpEndpointURLTest.java
@@ -16,7 +16,6 @@
  */
 package org.apache.camel.component.http;
 
-import org.apache.camel.ResolveEndpointFailedException;
 import org.apache.camel.test.junit5.CamelTestSupport;
 import org.apache.camel.util.URISupport;
 import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
@@ -25,7 +24,6 @@ import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
 
 public class HttpEndpointURLTest extends CamelTestSupport {
 
@@ -46,14 +44,17 @@ public class HttpEndpointURLTest extends CamelTestSupport {
         assertEquals("https://www.google.com?test=parameter", http2.getHttpUri().toString(), "Get a wrong HttpUri of http2");
         assertEquals(http2.getHttpUri(), http3.getHttpUri(), "Get a wrong HttpUri of http2 andhttp3");
 
-        try {
-            // need to catch the exception here
-            context.getEndpoint("https://http://www.google.com", HttpEndpoint.class);
-            fail("need to throw an exception here");
-        } catch (ResolveEndpointFailedException ex) {
-            assertTrue(ex.getMessage().indexOf("You have duplicated the http(s) protocol") > 0,
-                    "Get a wrong exception message");
-        }
+        // secure because protocol in remainder is https
+        HttpEndpoint http4 = context.getEndpoint("http://https://www.google.com", HttpEndpoint.class);
+        assertEquals("https://www.google.com", http4.getHttpUri().toString(), "Get a wrong HttpUri of http1");
+
+        // secure because protocol in remainder is https
+        HttpEndpoint http5 = context.getEndpoint("https://https://www.google.com", HttpEndpoint.class);
+        assertEquals("https://www.google.com", http5.getHttpUri().toString(), "Get a wrong HttpUri of http1");
+
+        // not secure because protocol in remainder is plain http
+        HttpEndpoint http6 = context.getEndpoint("https://http://www.google.com", HttpEndpoint.class);
+        assertEquals("http://www.google.com", http6.getHttpUri().toString(), "Get a wrong HttpUri of http1");
     }
 
     @Test
diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpInvalidConfigurationTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpInvalidConfigurationTest.java
deleted file mode 100644
index 5d8e654fdfe..00000000000
--- a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpInvalidConfigurationTest.java
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * 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 org.apache.camel.Exchange;
-import org.apache.camel.FailedToCreateRouteException;
-import org.apache.camel.ResolveEndpointFailedException;
-import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.test.junit5.CamelTestSupport;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-
-import static org.apache.camel.component.http.HttpMethods.POST;
-import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
-
-/**
- * Unit test of invalid configuration
- */
-public class HttpInvalidConfigurationTest extends CamelTestSupport {
-
-    private FailedToCreateRouteException exception;
-
-    @BeforeEach
-    @Override
-    public void setUp() throws Exception {
-        try {
-            super.setUp();
-            fail("Should have thrown ResolveEndpointFailedException");
-        } catch (FailedToCreateRouteException e) {
-            exception = e;
-        }
-    }
-
-    @Test
-    public void testInvalidHostConfiguration() {
-        ResolveEndpointFailedException cause = assertIsInstanceOf(ResolveEndpointFailedException.class, exception.getCause());
-        assertTrue(cause.getMessage().endsWith("You have duplicated the http(s) protocol."));
-    }
-
-    @Override
-    protected RouteBuilder createRouteBuilder() {
-        return new RouteBuilder() {
-            public void configure() {
-                from("direct:start").setHeader(Exchange.HTTP_METHOD, POST).to("http://http://www.google.com");
-            }
-        };
-    }
-}
diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpRedirectTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpRedirectTest.java
index 02818bae9c0..f3f8e7576d3 100644
--- a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpRedirectTest.java
+++ b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpRedirectTest.java
@@ -16,8 +16,6 @@
  */
 package org.apache.camel.component.http;
 
-import java.io.IOException;
-
 import org.apache.camel.Exchange;
 import org.apache.camel.component.http.handler.BasicValidationHandler;
 import org.apache.camel.http.base.HttpOperationFailedException;