[camel] branch camel-2.x updated: CAMEL-14139 Camel Undertow does not provide an option to use the producer as the Host header when bridging two http endpoints (#3338)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[camel] branch camel-2.x updated: CAMEL-14139 Camel Undertow does not provide an option to use the producer as the Host header when bridging two http endpoints (#3338)

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

davsclaus pushed a commit to branch camel-2.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-2.x by this push:
     new 7dfef82  CAMEL-14139 Camel Undertow does not provide an option to use the producer as the Host header when bridging two http endpoints (#3338)
7dfef82 is described below

commit 7dfef8203d85d305fbfedf14b94e8cabbe7718a9
Author: avi5kdonrh <[hidden email]>
AuthorDate: Thu Nov 14 17:20:29 2019 +0530

    CAMEL-14139 Camel Undertow does not provide an option to use the producer as the Host header when bridging two http endpoints (#3338)
---
 .../camel/component/undertow/UndertowEndpoint.java | 14 +++++-
 .../camel/component/undertow/UndertowProducer.java |  7 ++-
 .../UndertowHttpProxyPreserveHostTest.java         | 55 ++++++++++++++++++++++
 3 files changed, 73 insertions(+), 3 deletions(-)

diff --git a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowEndpoint.java b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowEndpoint.java
index 090c8a4..4eb171e 100644
--- a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowEndpoint.java
+++ b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowEndpoint.java
@@ -102,7 +102,13 @@ public class UndertowEndpoint extends DefaultEndpoint implements AsyncEndpoint,
     private Integer sendTimeout = 30000;
     @UriParam(label = "consumer,websocket", defaultValue = "false")
     private boolean fireWebSocketChannelEvents;
-
+    @UriParam(
+            label = "producer", defaultValue = "true",
+            description = "If the option is true, UndertowProducer will set the Host header to the value contained in the current exchange Host header,"
+            + " useful in reverse proxy applications where you want the Host header received by the downstream server to reflect the URL called by the upstream client,"
+            + " this allows applications which use the Host header to generate accurate URL's for a proxied service."
+    )
+    private boolean preserveHostHeader = true;
     public UndertowEndpoint(String uri, UndertowComponent component) {
         super(uri, component);
         this.component = component;
@@ -391,6 +397,12 @@ public class UndertowEndpoint extends DefaultEndpoint implements AsyncEndpoint,
     public void setFireWebSocketChannelEvents(boolean fireWebSocketChannelEvents) {
         this.fireWebSocketChannelEvents = fireWebSocketChannelEvents;
     }
+    public void setPreserveHostHeader(boolean preserveHostHeader) {
+        this.preserveHostHeader = preserveHostHeader;
+    }
+    public boolean isPreserveHostHeader() {
+        return preserveHostHeader;
+    }
 
     @Override
     protected void doStart() throws Exception {
diff --git a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowProducer.java b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowProducer.java
index 2a7a5bd..d2a6c52 100644
--- a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowProducer.java
+++ b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowProducer.java
@@ -133,8 +133,11 @@ public class UndertowProducer extends DefaultAsyncProducer {
         // Set the Host header
         final Message message = camelExchange.getIn();
         final String host = message.getHeader(Headers.HOST_STRING, String.class);
-        requestHeaders.put(Headers.HOST, Optional.ofNullable(host).orElseGet(uri::getAuthority));
-
+        if (endpoint.isPreserveHostHeader()) {
+            requestHeaders.put(Headers.HOST, Optional.ofNullable(host).orElseGet(uri::getAuthority));
+        } else {
+            requestHeaders.put(Headers.HOST, uri.getAuthority());
+        }
         cookieHeaders.forEach((key, values) -> {
             requestHeaders.putAll(HttpString.tryFromString(key), values);
         });
diff --git a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowHttpProxyPreserveHostTest.java b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowHttpProxyPreserveHostTest.java
new file mode 100644
index 0000000..4e33528
--- /dev/null
+++ b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowHttpProxyPreserveHostTest.java
@@ -0,0 +1,55 @@
+/*
+ * 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.undertow;
+import io.undertow.util.Headers;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+public class UndertowHttpProxyPreserveHostTest extends BaseUndertowTest {
+
+    @Test
+    public void preserveHostFalse() {
+        String actual =   template.requestBody("undertow:http://localhost:{{port}}", "&preserveHostHeader=false", String.class);
+        String expected = "localhost:" + getPort();
+        assertNotEquals(expected, actual);
+    }
+    @Test
+    public void preserveHostTrue() {
+        String actual =   template.requestBody("undertow:http://localhost:{{port}}", "&preserveHostHeader=true", String.class);
+        String expected = "localhost:" + getPort();
+        assertEquals(expected, actual);
+    }
+    @Test
+    public void emptyPreseveHost() {
+        String actual =   template.requestBody("undertow:http://localhost:{{port}}", "", String.class);
+        String expected = "localhost:" + getPort();
+        assertEquals(expected, actual);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("undertow:http://localhost:{{port}}")
+                        .toD("undertow:http://localhost:{{port2}}?bridgeEndpoint=true${body}");
+                from("undertow:http://localhost:{{port2}}")
+                        .setBody(exchange -> exchange.getIn().getHeader(Headers.HOST_STRING));
+            }
+        };
+    }
+}