Skip to content

Commit

Permalink
instead of using ProtocolNegotiator interface, mimic the behavior to …
Browse files Browse the repository at this point in the history
…avoid adding other handlers such as WaitUntilActive and KickProtocolNegotiationHandler (no longer exists)
  • Loading branch information
creamsoup committed Sep 19, 2019
1 parent 157d0f5 commit 45f305c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
1 change: 1 addition & 0 deletions netty/build.gradle
Expand Up @@ -13,6 +13,7 @@ configurations {
alpnagent
}

evaluationDependsOn(':grpc-core')
dependencies {
compile project(':grpc-core'),
libraries.netty,
Expand Down
28 changes: 14 additions & 14 deletions netty/src/main/java/io/grpc/netty/ProtocolNegotiators.java
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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);
}
}
}
Expand Down

0 comments on commit 45f305c

Please sign in to comment.