From 45f305cf876a1c0ac5ca93ec9191d81dfec9632c Mon Sep 17 00:00:00 2001 From: Jihun Cho Date: Wed, 18 Sep 2019 17:00:04 -0700 Subject: [PATCH] instead of using ProtocolNegotiator interface, mimic the behavior to avoid adding other handlers such as WaitUntilActive and KickProtocolNegotiationHandler (no longer exists) --- netty/build.gradle | 1 + .../io/grpc/netty/ProtocolNegotiators.java | 28 +++++++++---------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/netty/build.gradle b/netty/build.gradle index ad573eaaca76..251a3915174d 100644 --- a/netty/build.gradle +++ b/netty/build.gradle @@ -13,6 +13,7 @@ configurations { alpnagent } +evaluationDependsOn(':grpc-core') dependencies { compile project(':grpc-core'), libraries.netty, diff --git a/netty/src/main/java/io/grpc/netty/ProtocolNegotiators.java b/netty/src/main/java/io/grpc/netty/ProtocolNegotiators.java index 7e9e876cbf84..2705ab8f934b 100644 --- a/netty/src/main/java/io/grpc/netty/ProtocolNegotiators.java +++ b/netty/src/main/java/io/grpc/netty/ProtocolNegotiators.java @@ -34,13 +34,10 @@ import io.grpc.internal.GrpcAttributes; import io.grpc.internal.GrpcUtil; import io.netty.channel.ChannelDuplexHandler; -import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelInboundHandler; import io.netty.channel.ChannelInboundHandlerAdapter; -import io.netty.channel.ChannelPromise; import io.netty.handler.codec.http.DefaultHttpRequest; import io.netty.handler.codec.http.HttpClientCodec; import io.netty.handler.codec.http.HttpClientUpgradeHandler; @@ -60,12 +57,9 @@ import io.netty.util.AsciiString; import io.netty.util.Attribute; import io.netty.util.AttributeMap; -import io.netty.util.ReferenceCountUtil; import java.net.SocketAddress; import java.net.URI; -import java.util.ArrayDeque; import java.util.Arrays; -import java.util.Queue; import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.Nullable; @@ -196,9 +190,9 @@ private void fireProtocolNegotiationEvent(ChannelHandlerContext ctx, SSLSession public static ProtocolNegotiator httpProxy(final SocketAddress proxyAddress, final @Nullable String proxyUsername, final @Nullable String proxyPassword, final ProtocolNegotiator negotiator) { + checkNotNull(negotiator, "negotiator"); + checkNotNull(proxyAddress, "proxyAddress"); final AsciiString scheme = negotiator.scheme(); - Preconditions.checkNotNull(proxyAddress, "proxyAddress"); - Preconditions.checkNotNull(negotiator, "negotiator"); class ProxyNegotiator implements ProtocolNegotiator { @Override public ChannelHandler newHandler(GrpcHttp2ConnectionHandler http2Handler) { @@ -222,25 +216,31 @@ public void close() { return new ProxyNegotiator(); } - static final class ProxyProtocolNegotiationHandler extends ProtocolNegotiationHandler { + /** + * A Proxy handler follows {@link ProtocolNegotiationHandler} pattern. Upon successful proxy + * connection, this handler will install {@code next} handler which should be a handler from + * other type of {@link ProtocolNegotiator} to continue negotiating protocol using proxy. + */ + static final class ProxyProtocolNegotiationHandler extends ChannelDuplexHandler { private final SocketAddress address; @Nullable private final String userName; @Nullable private final String password; + private final ChannelHandler next; public ProxyProtocolNegotiationHandler( SocketAddress address, @Nullable String userName, @Nullable String password, ChannelHandler next) { - super(next); this.address = checkNotNull(address, "address"); this.userName = userName; this.password = password; + this.next = checkNotNull(next, "next"); } @Override - protected void protocolNegotiationEventTriggered(ChannelHandlerContext ctx) { + public void handlerAdded(ChannelHandlerContext ctx) { HttpProxyHandler nettyProxyHandler; if (userName == null || password == null) { nettyProxyHandler = new HttpProxyHandler(address); @@ -251,11 +251,11 @@ protected void protocolNegotiationEventTriggered(ChannelHandlerContext ctx) { } @Override - protected void userEventTriggered0(ChannelHandlerContext ctx, Object evt) throws Exception { + public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof ProxyConnectionEvent) { - fireProtocolNegotiationEvent(ctx); + ctx.pipeline().replace(ctx.name(), /* newName= */ null, next); } else { - super.userEventTriggered0(ctx, evt); + super.userEventTriggered(ctx, evt); } } }