Skip to content

Commit

Permalink
Merge pull request apache#1166 from jboss-fuse/ENTESB-12179-2.23.x
Browse files Browse the repository at this point in the history
Entesb 12179 2.23.x
  • Loading branch information
oscerd committed Nov 19, 2019
2 parents a017b31 + c1d31b4 commit eb3b144
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
Expand Up @@ -78,7 +78,7 @@ with the following path and query parameters:
|===


==== Query Parameters (23 parameters):
==== Query Parameters (24 parameters):


[width="100%",cols="2,5,^1,2",options="header"]
Expand All @@ -95,6 +95,7 @@ with the following path and query parameters:
| *cookieHandler* (producer) | Configure a cookie handler to maintain a HTTP session | | CookieHandler
| *keepAlive* (producer) | Setting to ensure socket is not closed due to inactivity | true | Boolean
| *options* (producer) | Sets additional channel options. The options that can be used are defined in org.xnio.Options. To configure from endpoint uri, then prefix each option with option., such as option.close-abort=true&option.send-buffer=8192 | | Map
| *preserveHostHeader* (producer) | 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. | true | boolean
| *reuseAddresses* (producer) | Setting to facilitate socket multiplexing | true | Boolean
| *tcpNoDelay* (producer) | Setting to improve TCP protocol performance | true | Boolean
| *throwExceptionOnFailure* (producer) | Option to disable throwing the HttpOperationFailedException in case of failed responses from the remote server. This allows you to get all responses regardless of the HTTP status code. | true | Boolean
Expand Down
Expand Up @@ -108,7 +108,12 @@ 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;
Expand Down Expand Up @@ -397,6 +402,12 @@ public boolean isFireWebSocketChannelEvents() {
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 {
Expand Down
Expand Up @@ -133,8 +133,11 @@ public boolean process(final Exchange camelExchange, final AsyncCallback callbac
// 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);
});
Expand Down
@@ -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));
}
};
}
}

0 comments on commit eb3b144

Please sign in to comment.