Search in sources :

Example 1 with UpgradeCodecFactory

use of io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodecFactory in project netty by netty.

the class HttpServerUpgradeHandlerTest method upgradeFail.

@Test
public void upgradeFail() {
    final HttpServerCodec httpServerCodec = new HttpServerCodec();
    final UpgradeCodecFactory factory = new UpgradeCodecFactory() {

        @Override
        public UpgradeCodec newUpgradeCodec(CharSequence protocol) {
            return new TestUpgradeCodec();
        }
    };
    HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(httpServerCodec, factory);
    EmbeddedChannel channel = new EmbeddedChannel(httpServerCodec, upgradeHandler);
    // Build a h2c upgrade request, but without connection header.
    String upgradeString = "GET / HTTP/1.1\r\n" + "Host: example.com\r\n" + "Upgrade: h2c\r\n\r\n";
    ByteBuf upgrade = Unpooled.copiedBuffer(upgradeString, CharsetUtil.US_ASCII);
    assertTrue(channel.writeInbound(upgrade));
    assertNotNull(channel.pipeline().get(HttpServerCodec.class));
    // Should not be removed.
    assertNotNull(channel.pipeline().get(HttpServerUpgradeHandler.class));
    assertNull(channel.pipeline().get("marker"));
    HttpRequest req = channel.readInbound();
    assertEquals(HttpVersion.HTTP_1_1, req.protocolVersion());
    assertTrue(req.headers().contains(HttpHeaderNames.UPGRADE, "h2c", false));
    assertFalse(req.headers().contains(HttpHeaderNames.CONNECTION));
    ReferenceCountUtil.release(req);
    assertNull(channel.readInbound());
    // No response should be written because we're just passing through.
    channel.flushOutbound();
    assertNull(channel.readOutbound());
    assertFalse(channel.finishAndReleaseAll());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) UpgradeCodecFactory(io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodecFactory) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 2 with UpgradeCodecFactory

use of io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodecFactory in project netty by netty.

the class HttpServerUpgradeHandlerTest method upgradesPipelineInSameMethodInvocation.

@Test
public void upgradesPipelineInSameMethodInvocation() {
    final HttpServerCodec httpServerCodec = new HttpServerCodec();
    final UpgradeCodecFactory factory = new UpgradeCodecFactory() {

        @Override
        public UpgradeCodec newUpgradeCodec(CharSequence protocol) {
            return new TestUpgradeCodec();
        }
    };
    ChannelHandler testInStackFrame = new ChannelDuplexHandler() {

        // marker boolean to signal that we're in the `channelRead` method
        private boolean inReadCall;

        private boolean writeUpgradeMessage;

        private boolean writeFlushed;

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            assertFalse(inReadCall);
            assertFalse(writeUpgradeMessage);
            inReadCall = true;
            try {
                super.channelRead(ctx, msg);
                // All in the same call stack, the upgrade codec should receive the message,
                // written the upgrade response, and upgraded the pipeline.
                assertTrue(writeUpgradeMessage);
                assertFalse(writeFlushed);
                assertNull(ctx.pipeline().get(HttpServerCodec.class));
                assertNotNull(ctx.pipeline().get("marker"));
            } finally {
                inReadCall = false;
            }
        }

        @Override
        public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) {
            // We ensure that we're in the read call and defer the write so we can
            // make sure the pipeline was reformed irrespective of the flush completing.
            assertTrue(inReadCall);
            writeUpgradeMessage = true;
            ctx.channel().eventLoop().execute(new Runnable() {

                @Override
                public void run() {
                    ctx.write(msg, promise);
                }
            });
            promise.addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) {
                    writeFlushed = true;
                }
            });
        }
    };
    HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(httpServerCodec, factory);
    EmbeddedChannel channel = new EmbeddedChannel(testInStackFrame, httpServerCodec, upgradeHandler);
    String upgradeString = "GET / HTTP/1.1\r\n" + "Host: example.com\r\n" + "Connection: Upgrade, HTTP2-Settings\r\n" + "Upgrade: nextprotocol\r\n" + "HTTP2-Settings: AAMAAABkAAQAAP__\r\n\r\n";
    ByteBuf upgrade = Unpooled.copiedBuffer(upgradeString, CharsetUtil.US_ASCII);
    assertFalse(channel.writeInbound(upgrade));
    assertNull(channel.pipeline().get(HttpServerCodec.class));
    assertNotNull(channel.pipeline().get("marker"));
    channel.flushOutbound();
    ByteBuf upgradeMessage = channel.readOutbound();
    String expectedHttpResponse = "HTTP/1.1 101 Switching Protocols\r\n" + "connection: upgrade\r\n" + "upgrade: nextprotocol\r\n\r\n";
    assertEquals(expectedHttpResponse, upgradeMessage.toString(CharsetUtil.US_ASCII));
    assertTrue(upgradeMessage.release());
    assertFalse(channel.finishAndReleaseAll());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) UpgradeCodecFactory(io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodecFactory) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ChannelHandler(io.netty.channel.ChannelHandler) ByteBuf(io.netty.buffer.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) Test(org.junit.jupiter.api.Test)

Example 3 with UpgradeCodecFactory

use of io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodecFactory in project grpc-java by grpc.

the class ProtocolNegotiatorsTest method plaintextUpgradeNegotiator.

@Test
public void plaintextUpgradeNegotiator() throws Exception {
    LocalAddress addr = new LocalAddress("plaintextUpgradeNegotiator");
    UpgradeCodecFactory ucf = new UpgradeCodecFactory() {

        @Override
        public UpgradeCodec newUpgradeCodec(CharSequence protocol) {
            return new Http2ServerUpgradeCodec(FakeGrpcHttp2ConnectionHandler.newHandler());
        }
    };
    final HttpServerCodec serverCodec = new HttpServerCodec();
    final HttpServerUpgradeHandler serverUpgradeHandler = new HttpServerUpgradeHandler(serverCodec, ucf);
    Channel serverChannel = new ServerBootstrap().group(group).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            ch.pipeline().addLast(serverCodec, serverUpgradeHandler);
        }
    }).bind(addr).sync().channel();
    FakeGrpcHttp2ConnectionHandler gh = FakeGrpcHttp2ConnectionHandler.newHandler();
    ProtocolNegotiator nego = ProtocolNegotiators.plaintextUpgrade();
    ChannelHandler ch = nego.newHandler(gh);
    WriteBufferingAndExceptionHandler wbaeh = new WriteBufferingAndExceptionHandler(ch);
    Channel channel = new Bootstrap().group(group).channel(LocalChannel.class).handler(wbaeh).register().sync().channel();
    ChannelFuture write = channel.writeAndFlush(NettyClientHandler.NOOP_MESSAGE);
    channel.connect(serverChannel.localAddress());
    boolean completed = gh.negotiated.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    if (!completed) {
        assertTrue("failed to negotiated", write.await(TIMEOUT_SECONDS, TimeUnit.SECONDS));
        // sync should fail if we are in this block.
        write.sync();
        throw new AssertionError("neither wrote nor negotiated");
    }
    channel.close().sync();
    serverChannel.close();
    assertThat(gh.securityInfo).isNull();
    assertThat(gh.attrs.get(GrpcAttributes.ATTR_SECURITY_LEVEL)).isEqualTo(SecurityLevel.NONE);
    assertThat(gh.attrs.get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR)).isEqualTo(addr);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) LocalAddress(io.netty.channel.local.LocalAddress) Http2ServerUpgradeCodec(io.netty.handler.codec.http2.Http2ServerUpgradeCodec) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Channel(io.netty.channel.Channel) LocalChannel(io.netty.channel.local.LocalChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) UpgradeCodecFactory(io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodecFactory) ChannelHandler(io.netty.channel.ChannelHandler) HttpServerUpgradeHandler(io.netty.handler.codec.http.HttpServerUpgradeHandler) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ClientTlsProtocolNegotiator(io.grpc.netty.ProtocolNegotiators.ClientTlsProtocolNegotiator) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) Test(org.junit.Test)

Example 4 with UpgradeCodecFactory

use of io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodecFactory in project netty by netty.

the class HttpServerUpgradeHandlerTest method skippedUpgrade.

@Test
public void skippedUpgrade() {
    final HttpServerCodec httpServerCodec = new HttpServerCodec();
    final UpgradeCodecFactory factory = new UpgradeCodecFactory() {

        @Override
        public UpgradeCodec newUpgradeCodec(CharSequence protocol) {
            fail("Should never be invoked");
            return null;
        }
    };
    HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(httpServerCodec, factory) {

        @Override
        protected boolean shouldHandleUpgradeRequest(HttpRequest req) {
            return !req.headers().contains(HttpHeaderNames.UPGRADE, "do-not-upgrade", false);
        }
    };
    EmbeddedChannel channel = new EmbeddedChannel(httpServerCodec, upgradeHandler);
    String upgradeString = "GET / HTTP/1.1\r\n" + "Host: example.com\r\n" + "Connection: Upgrade\r\n" + "Upgrade: do-not-upgrade\r\n\r\n";
    ByteBuf upgrade = Unpooled.copiedBuffer(upgradeString, CharsetUtil.US_ASCII);
    // The upgrade request should not be passed to the next handler without any processing.
    assertTrue(channel.writeInbound(upgrade));
    assertNotNull(channel.pipeline().get(HttpServerCodec.class));
    assertNull(channel.pipeline().get("marker"));
    HttpRequest req = channel.readInbound();
    // Should not be aggregated.
    assertFalse(req instanceof FullHttpRequest);
    assertTrue(req.headers().contains(HttpHeaderNames.CONNECTION, "Upgrade", false));
    assertTrue(req.headers().contains(HttpHeaderNames.UPGRADE, "do-not-upgrade", false));
    assertTrue(channel.readInbound() instanceof LastHttpContent);
    assertNull(channel.readInbound());
    // No response should be written because we're just passing through.
    channel.flushOutbound();
    assertNull(channel.readOutbound());
    assertFalse(channel.finishAndReleaseAll());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) UpgradeCodecFactory(io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodecFactory) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 5 with UpgradeCodecFactory

use of io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodecFactory in project netty by netty.

the class CleartextHttp2ServerUpgradeHandlerTest method setUpServerChannel.

private void setUpServerChannel() {
    frameListener = mock(Http2FrameListener.class);
    http2ConnectionHandler = new Http2ConnectionHandlerBuilder().frameListener(frameListener).build();
    UpgradeCodecFactory upgradeCodecFactory = new UpgradeCodecFactory() {

        @Override
        public UpgradeCodec newUpgradeCodec(CharSequence protocol) {
            return new Http2ServerUpgradeCodec(http2ConnectionHandler);
        }
    };
    userEvents = new ArrayList<Object>();
    HttpServerCodec httpServerCodec = new HttpServerCodec();
    HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(httpServerCodec, upgradeCodecFactory);
    CleartextHttp2ServerUpgradeHandler handler = new CleartextHttp2ServerUpgradeHandler(httpServerCodec, upgradeHandler, http2ConnectionHandler);
    channel = new EmbeddedChannel(handler, new ChannelInboundHandlerAdapter() {

        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
            userEvents.add(evt);
        }
    });
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) UpgradeCodecFactory(io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodecFactory) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpServerUpgradeHandler(io.netty.handler.codec.http.HttpServerUpgradeHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Aggregations

EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)6 UpgradeCodecFactory (io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodecFactory)6 ByteBuf (io.netty.buffer.ByteBuf)4 Test (org.junit.jupiter.api.Test)4 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)3 HttpServerUpgradeHandler (io.netty.handler.codec.http.HttpServerUpgradeHandler)3 Channel (io.netty.channel.Channel)2 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelHandler (io.netty.channel.ChannelHandler)2 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)2 ChannelInitializer (io.netty.channel.ChannelInitializer)2 ClientTlsProtocolNegotiator (io.grpc.netty.ProtocolNegotiators.ClientTlsProtocolNegotiator)1 Bootstrap (io.netty.bootstrap.Bootstrap)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 ChannelDuplexHandler (io.netty.channel.ChannelDuplexHandler)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelPromise (io.netty.channel.ChannelPromise)1 LocalAddress (io.netty.channel.local.LocalAddress)1 LocalChannel (io.netty.channel.local.LocalChannel)1