Search in sources :

Example 76 with ChannelHandler

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler in project grpc-java by grpc.

the class SdsProtocolNegotiatorsTest method clientSdsProtocolNegotiatorNewHandler_noTlsContextAttribute.

@Test
public void clientSdsProtocolNegotiatorNewHandler_noTlsContextAttribute() {
    ChannelHandler mockChannelHandler = mock(ChannelHandler.class);
    ProtocolNegotiator mockProtocolNegotiator = mock(ProtocolNegotiator.class);
    when(mockProtocolNegotiator.newHandler(grpcHandler)).thenReturn(mockChannelHandler);
    ClientSdsProtocolNegotiator pn = new ClientSdsProtocolNegotiator(mockProtocolNegotiator);
    ChannelHandler newHandler = pn.newHandler(grpcHandler);
    assertThat(newHandler).isNotNull();
    assertThat(newHandler).isSameInstanceAs(mockChannelHandler);
}
Also used : ProtocolNegotiator(io.grpc.netty.InternalProtocolNegotiator.ProtocolNegotiator) ClientSdsProtocolNegotiator(io.grpc.xds.internal.sds.SdsProtocolNegotiators.ClientSdsProtocolNegotiator) ClientSdsProtocolNegotiator(io.grpc.xds.internal.sds.SdsProtocolNegotiators.ClientSdsProtocolNegotiator) ChannelHandler(io.netty.channel.ChannelHandler) Test(org.junit.Test)

Example 77 with ChannelHandler

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler in project grpc-java by grpc.

the class SdsProtocolNegotiatorsTest method serverSdsHandler_nullTlsContext_expectFallbackProtocolNegotiator.

@Test
public void serverSdsHandler_nullTlsContext_expectFallbackProtocolNegotiator() {
    ChannelHandler mockChannelHandler = mock(ChannelHandler.class);
    ProtocolNegotiator mockProtocolNegotiator = mock(ProtocolNegotiator.class);
    when(mockProtocolNegotiator.newHandler(grpcHandler)).thenReturn(mockChannelHandler);
    SdsProtocolNegotiators.HandlerPickerHandler handlerPickerHandler = new SdsProtocolNegotiators.HandlerPickerHandler(grpcHandler, mockProtocolNegotiator);
    pipeline.addLast(handlerPickerHandler);
    channelHandlerCtx = pipeline.context(handlerPickerHandler);
    // should find HandlerPickerHandler
    assertThat(channelHandlerCtx).isNotNull();
    // kick off protocol negotiation
    pipeline.fireUserEventTriggered(InternalProtocolNegotiationEvent.getDefault());
    channelHandlerCtx = pipeline.context(handlerPickerHandler);
    assertThat(channelHandlerCtx).isNull();
    // need this for tasks to execute on eventLoop
    channel.runPendingTasks();
    Iterator<Map.Entry<String, ChannelHandler>> iterator = pipeline.iterator();
    assertThat(iterator.next().getValue()).isSameInstanceAs(mockChannelHandler);
    // no more handlers in the pipeline
    assertThat(iterator.hasNext()).isFalse();
}
Also used : ProtocolNegotiator(io.grpc.netty.InternalProtocolNegotiator.ProtocolNegotiator) ClientSdsProtocolNegotiator(io.grpc.xds.internal.sds.SdsProtocolNegotiators.ClientSdsProtocolNegotiator) ChannelHandler(io.netty.channel.ChannelHandler) Test(org.junit.Test)

Example 78 with ChannelHandler

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler in project grpc-java by grpc.

the class SdsProtocolNegotiatorsTest method clientSdsProtocolNegotiatorNewHandler_withTlsContextAttribute.

@Test
public void clientSdsProtocolNegotiatorNewHandler_withTlsContextAttribute() {
    UpstreamTlsContext upstreamTlsContext = CommonTlsContextTestsUtil.buildUpstreamTlsContext(CommonTlsContext.newBuilder().build());
    ClientSdsProtocolNegotiator pn = new ClientSdsProtocolNegotiator(InternalProtocolNegotiators.plaintext());
    GrpcHttp2ConnectionHandler mockHandler = mock(GrpcHttp2ConnectionHandler.class);
    ChannelLogger logger = mock(ChannelLogger.class);
    doNothing().when(logger).log(any(ChannelLogLevel.class), anyString());
    when(mockHandler.getNegotiationLogger()).thenReturn(logger);
    TlsContextManager mockTlsContextManager = mock(TlsContextManager.class);
    when(mockHandler.getEagAttributes()).thenReturn(Attributes.newBuilder().set(InternalXdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER, new SslContextProviderSupplier(upstreamTlsContext, mockTlsContextManager)).build());
    ChannelHandler newHandler = pn.newHandler(mockHandler);
    assertThat(newHandler).isNotNull();
    assertThat(newHandler).isInstanceOf(ClientSdsHandler.class);
}
Also used : ClientSdsProtocolNegotiator(io.grpc.xds.internal.sds.SdsProtocolNegotiators.ClientSdsProtocolNegotiator) UpstreamTlsContext(io.grpc.xds.EnvoyServerProtoData.UpstreamTlsContext) TlsContextManager(io.grpc.xds.TlsContextManager) ChannelLogger(io.grpc.ChannelLogger) NoopChannelLogger(io.grpc.internal.TestUtils.NoopChannelLogger) ChannelHandler(io.netty.channel.ChannelHandler) GrpcHttp2ConnectionHandler(io.grpc.netty.GrpcHttp2ConnectionHandler) ChannelLogLevel(io.grpc.ChannelLogger.ChannelLogLevel) Test(org.junit.Test)

Example 79 with ChannelHandler

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler in project vert.x by eclipse.

the class Http1xUpgradeToH2CHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof HttpRequest) {
        HttpRequest request = (HttpRequest) msg;
        if (request.headers().contains(io.vertx.core.http.HttpHeaders.UPGRADE, Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, true)) {
            String connection = request.headers().get(io.vertx.core.http.HttpHeaders.CONNECTION);
            int found = 0;
            if (connection != null && connection.length() > 0) {
                StringBuilder buff = new StringBuilder();
                int pos = 0;
                int len = connection.length();
                while (pos < len) {
                    char c = connection.charAt(pos++);
                    if (c != ' ' && c != ',') {
                        buff.append(Character.toLowerCase(c));
                    }
                    if (c == ',' || pos == len) {
                        if (buff.indexOf("upgrade") == 0 && buff.length() == 7) {
                            found |= 1;
                        } else if (buff.indexOf("http2-settings") == 0 && buff.length() == 14) {
                            found |= 2;
                        }
                        buff.setLength(0);
                    }
                }
            }
            if (found == 3) {
                String settingsHeader = request.headers().get(Http2CodecUtil.HTTP_UPGRADE_SETTINGS_HEADER);
                if (settingsHeader != null) {
                    Http2Settings settings = HttpUtils.decodeSettings(settingsHeader);
                    if (settings != null) {
                        if (initializer.context.isEventLoopContext()) {
                            ChannelPipeline pipeline = ctx.pipeline();
                            DefaultFullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, SWITCHING_PROTOCOLS, Unpooled.EMPTY_BUFFER, false);
                            res.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE);
                            res.headers().add(HttpHeaderNames.UPGRADE, Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME);
                            ctx.writeAndFlush(res);
                            pipeline.remove("httpEncoder");
                            if (isCompressionSupported) {
                                pipeline.remove("deflater");
                                pipeline.remove("chunkedWriter");
                            }
                            if (isDecompressionSupported) {
                                pipeline.remove("inflater");
                            }
                            handler = initializer.buildHttp2ConnectionHandler(initializer.context, initializer.connectionHandler);
                            pipeline.addLast("handler", handler);
                            handler.serverUpgrade(ctx, settings, request);
                            DefaultHttp2Headers headers = new DefaultHttp2Headers();
                            headers.method(request.method().name());
                            headers.path(request.uri());
                            headers.authority(request.headers().get("host"));
                            headers.scheme("http");
                            request.headers().remove("http2-settings");
                            request.headers().remove("host");
                            request.headers().forEach(header -> headers.set(header.getKey().toLowerCase(), header.getValue()));
                            ctx.fireChannelRead(new DefaultHttp2HeadersFrame(headers, false));
                        } else {
                            HttpServerImpl.log.warn("Cannot perform HTTP/2 upgrade in a worker verticle");
                        }
                    }
                }
            }
            if (handler == null) {
                DefaultFullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST, Unpooled.EMPTY_BUFFER, false);
                res.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
                ctx.writeAndFlush(res);
            }
        } else {
            initializer.configureHttp1(ctx.pipeline());
            ctx.fireChannelRead(msg);
            ctx.pipeline().remove(this);
        }
    } else {
        if (handler != null) {
            if (msg instanceof HttpContent) {
                HttpContent content = (HttpContent) msg;
                ByteBuf buf = VertxHandler.safeBuffer(content.content());
                boolean end = msg instanceof LastHttpContent;
                ctx.fireChannelRead(new DefaultHttp2DataFrame(buf, end, 0));
                if (end) {
                    ChannelPipeline pipeline = ctx.pipeline();
                    for (Map.Entry<String, ChannelHandler> handler : pipeline) {
                        if (handler.getValue() instanceof Http2ConnectionHandler) {
                        // Continue
                        } else {
                            pipeline.remove(handler.getKey());
                        }
                    }
                    initializer.configureHttp2(pipeline);
                }
            } else {
                // We might have left over buffer sent when removing the HTTP decoder that needs to be propagated to the HTTP handler
                super.channelRead(ctx, msg);
            }
        }
    }
}
Also used : ChannelHandler(io.netty.channel.ChannelHandler) ByteBuf(io.netty.buffer.ByteBuf) ChannelPipeline(io.netty.channel.ChannelPipeline) Map(java.util.Map)

Example 80 with ChannelHandler

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler in project vert.x by eclipse.

the class WebSocketHandshakeInboundHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof HttpResponse) {
        HttpResponse resp = (HttpResponse) msg;
        response = new DefaultFullHttpResponse(resp.protocolVersion(), resp.status());
        response.headers().add(resp.headers());
    }
    if (msg instanceof HttpContent) {
        HttpContent content = (HttpContent) msg;
        try {
            if (response != null) {
                response.content().writeBytes(content.content());
                if (msg instanceof LastHttpContent) {
                    response.trailingHeaders().add(((LastHttpContent) msg).trailingHeaders());
                    ChannelPipeline pipeline = chctx.pipeline();
                    pipeline.remove(WebSocketHandshakeInboundHandler.this);
                    ChannelHandler handler = pipeline.get(HttpContentDecompressor.class);
                    if (handler != null) {
                        // remove decompressor as its not needed anymore once connection was upgraded to WebSocket
                        ctx.pipeline().remove(handler);
                    }
                    Future<HeadersAdaptor> fut = handshakeComplete(response);
                    wsHandler.handle(fut);
                }
            }
        } finally {
            content.release();
        }
    }
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HeadersAdaptor(io.vertx.core.http.impl.headers.HeadersAdaptor) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) ChannelHandler(io.netty.channel.ChannelHandler) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) ChannelPipeline(io.netty.channel.ChannelPipeline)

Aggregations

ChannelHandler (io.netty.channel.ChannelHandler)216 Test (org.junit.Test)88 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)44 Channel (io.netty.channel.Channel)27 ChannelPipeline (io.netty.channel.ChannelPipeline)26 SslHandler (io.netty.handler.ssl.SslHandler)25 Test (org.junit.jupiter.api.Test)24 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)23 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)21 FilterChainMatchingHandler (io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingHandler)20 ChannelFuture (io.netty.channel.ChannelFuture)20 FilterChainSelector (io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingHandler.FilterChainSelector)19 ChannelHandlerAdapter (io.netty.channel.ChannelHandlerAdapter)18 InetSocketAddress (java.net.InetSocketAddress)18 DownstreamTlsContext (io.grpc.xds.EnvoyServerProtoData.DownstreamTlsContext)17 FilterChain (io.grpc.xds.EnvoyServerProtoData.FilterChain)17 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)16 AtomicReference (java.util.concurrent.atomic.AtomicReference)16 TcpIngester (com.wavefront.ingester.TcpIngester)15 ByteBuf (io.netty.buffer.ByteBuf)13