[camel] branch master updated: CAMEL-13966: Netty-HTTP proxy mode refactor

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

[camel] branch master updated: CAMEL-13966: Netty-HTTP proxy mode refactor

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

zregvart pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new ee0520c  CAMEL-13966: Netty-HTTP proxy mode refactor
ee0520c is described below

commit ee0520caca3c2f8cf5a97446086ef8f35c6aebdd
Author: Zoran Regvart <[hidden email]>
AuthorDate: Wed Sep 11 10:21:19 2019 +0200

    CAMEL-13966: Netty-HTTP proxy mode refactor
   
    This removes the caching of the origin request in the Exchange property
    and instead tries to compare the message and message body to see if the
    message still contains the origin request.
---
 .../component/netty/http/DefaultNettyHttpBinding.java  | 13 +++----------
 .../camel/component/netty/http/NettyHttpConstants.java |  1 -
 .../netty/http/handlers/HttpServerChannelHandler.java  |  2 --
 .../camel/component/netty/http/ProxyProtocolTest.java  | 18 ++++++++++++++++--
 4 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
index 2ef3db5..c4d7601 100644
--- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
+++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
@@ -530,22 +530,15 @@ public class DefaultNettyHttpBinding implements NettyHttpBinding, Cloneable {
             httpMethod = HttpMethod.valueOf(headerMethod);
         }
 
-        final Exchange exchange = message.getExchange();
-        final Object proxyRequest;
-        if (exchange != null) {
-            proxyRequest = exchange.getProperty(NettyHttpConstants.PROXY_REQUEST);
-        } else {
-            proxyRequest = null;
-        }
-
         HttpRequest request = null;
         if (message instanceof NettyHttpMessage) {
             // if the request is already given we should set the values
             // from message headers and pass on the same request
             final FullHttpRequest givenRequest = ((NettyHttpMessage) message).getHttpRequest();
             // we need to make sure that the givenRequest is the original
-            // request received by the proxy
-            if (givenRequest != null && proxyRequest == givenRequest) {
+            // request received by the proxy, only when the body wasn't
+            // modified by a processor on route
+            if (givenRequest != null && givenRequest.content() == body) {
                 request = givenRequest
                         .setProtocolVersion(protocol)
                         .setMethod(httpMethod)
diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConstants.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConstants.java
index 2ad5784..3dc3a1f 100644
--- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConstants.java
+++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConstants.java
@@ -28,7 +28,6 @@ public final class NettyHttpConstants {
     @Deprecated
     public static final String HTTP_RESPONSE_TEXT = Exchange.HTTP_RESPONSE_TEXT;
     public static final String HTTP_AUTHENTICATION = "CamelHttpAuthentication";
-    public static final String PROXY_REQUEST = "NettyHttpProxyRequest";
 
     private NettyHttpConstants() {
     }
diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java
index 20016ee..888bede 100644
--- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java
+++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java
@@ -41,7 +41,6 @@ import org.apache.camel.component.netty.NettyHelper;
 import org.apache.camel.component.netty.handlers.ServerChannelHandler;
 import org.apache.camel.component.netty.http.HttpPrincipal;
 import org.apache.camel.component.netty.http.NettyHttpConfiguration;
-import org.apache.camel.component.netty.http.NettyHttpConstants;
 import org.apache.camel.component.netty.http.NettyHttpConsumer;
 import org.apache.camel.component.netty.http.NettyHttpSecurityConfiguration;
 import org.apache.camel.component.netty.http.SecurityAuthenticator;
@@ -283,7 +282,6 @@ public class HttpServerChannelHandler extends ServerChannelHandler {
 
         final Message in = exchange.getIn();
         if (configuration.isHttpProxy()) {
-            exchange.setProperty(NettyHttpConstants.PROXY_REQUEST, request);
             in.removeHeader("Proxy-Connection");
         }
     }
diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/ProxyProtocolTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/ProxyProtocolTest.java
index 759e6f3..967b487 100644
--- a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/ProxyProtocolTest.java
+++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/ProxyProtocolTest.java
@@ -31,6 +31,8 @@ import java.util.Locale;
 import java.util.concurrent.TimeUnit;
 import java.util.function.Function;
 
+import io.netty.buffer.ByteBuf;
+
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
 import org.apache.camel.builder.RouteBuilder;
@@ -131,14 +133,17 @@ public class ProxyProtocolTest {
     @Parameters
     public static Iterable<Object[]> routeOptions() {
         final Function<RouteBuilder, RouteDefinition> single = r -> r.from("netty-http:proxy://localhost:" + PROXY_PORT)
+            .process(ProxyProtocolTest::uppercase)
             .to("netty-http:http://localhost:" + ORIGIN_PORT)
             .process(ProxyProtocolTest::uppercase);
 
         final Function<RouteBuilder, RouteDefinition> dynamicPath = r -> r.from("netty-http:proxy://localhost:" + PROXY_PORT)
+            .process(ProxyProtocolTest::uppercase)
             .toD("netty-http:http://localhost:" + ORIGIN_PORT + "/${headers." + Exchange.HTTP_PATH + "}")
             .process(ProxyProtocolTest::uppercase);
 
         final Function<RouteBuilder, RouteDefinition> dynamicUrl = r -> r.from("netty-http:proxy://localhost:" + PROXY_PORT)
+            .process(ProxyProtocolTest::uppercase)
             .toD("netty-http:"
                 + "${headers." + Exchange.HTTP_SCHEME + "}://"
                 + "${headers." + Exchange.HTTP_HOST + "}:"
@@ -158,6 +163,12 @@ public class ProxyProtocolTest {
         final String q = message.getHeader("q", String.class);
         final String body = message.getBody(String.class);
 
+        if ("text/plain".equals(message.getHeader(Exchange.CONTENT_TYPE))) {
+            // when we send text/plain message we're using the route with
+            // uppercase processor before the netty-http producer
+            assertThat(body).isUpperCase();
+        }
+
         if (ObjectHelper.isEmpty(q) && ObjectHelper.isEmpty(body)) {
             message.setBody("origin server");
         } else if (ObjectHelper.isEmpty(body)) {
@@ -201,8 +212,11 @@ public class ProxyProtocolTest {
 
     private static void uppercase(final Exchange exchange) {
         final Message message = exchange.getMessage();
-        final String body = message.getBody(String.class);
+        final ByteBuf body = message.getBody(ByteBuf.class);
 
-        message.setBody(body.toUpperCase(Locale.US));
+        if (body.capacity() != 0) {
+            // only if we received a payload we'll uppercase it
+            message.setBody(body.toString(StandardCharsets.US_ASCII).toUpperCase(Locale.US));
+        }
     }
 }