Search in sources :

Example 1 with UpgradeEvent

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

the class Http2FrameCodec method userEventTriggered.

/**
     * Handles the cleartext HTTP upgrade event. If an upgrade occurred, sends a simple response via
     * HTTP/2 on stream 1 (the stream specifically reserved for cleartext HTTP upgrade).
     */
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (!(evt instanceof UpgradeEvent)) {
        super.userEventTriggered(ctx, evt);
        return;
    }
    UpgradeEvent upgrade = (UpgradeEvent) evt;
    ctx.fireUserEventTriggered(upgrade.retain());
    try {
        Http2Stream stream = http2Handler.connection().stream(Http2CodecUtil.HTTP_UPGRADE_STREAM_ID);
        // TODO: improve handler/stream lifecycle so that stream isn't active before handler added.
        // The stream was already made active, but ctx may have been null so it wasn't initialized.
        // https://github.com/netty/netty/issues/4942
        new ConnectionListener().onStreamActive(stream);
        upgrade.upgradeRequest().headers().setInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(), Http2CodecUtil.HTTP_UPGRADE_STREAM_ID);
        new InboundHttpToHttp2Adapter(http2Handler.connection(), http2Handler.decoder().frameListener()).channelRead(ctx, upgrade.upgradeRequest().retain());
    } finally {
        upgrade.release();
    }
}
Also used : UpgradeEvent(io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeEvent)

Example 2 with UpgradeEvent

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

the class CleartextHttp2ServerUpgradeHandlerTest method validateClearTextUpgrade.

private void validateClearTextUpgrade(String upgradeString) {
    setUpServerChannel();
    ByteBuf upgrade = Unpooled.copiedBuffer(upgradeString, CharsetUtil.US_ASCII);
    assertFalse(channel.writeInbound(upgrade));
    assertEquals(1, userEvents.size());
    Object userEvent = userEvents.get(0);
    assertTrue(userEvent instanceof UpgradeEvent);
    assertEquals("h2c", ((UpgradeEvent) userEvent).protocol());
    ReferenceCountUtil.release(userEvent);
    assertEquals(100, http2ConnectionHandler.connection().local().maxActiveStreams());
    assertEquals(65535, http2ConnectionHandler.connection().local().flowController().initialWindowSize());
    assertEquals(1, http2ConnectionHandler.connection().numActiveStreams());
    assertNotNull(http2ConnectionHandler.connection().stream(1));
    Http2Stream stream = http2ConnectionHandler.connection().stream(1);
    assertEquals(State.HALF_CLOSED_REMOTE, stream.state());
    assertFalse(stream.isHeadersSent());
    String expectedHttpResponse = "HTTP/1.1 101 Switching Protocols\r\n" + "connection: upgrade\r\n" + "upgrade: h2c\r\n\r\n";
    ByteBuf responseBuffer = channel.readOutbound();
    assertEquals(expectedHttpResponse, responseBuffer.toString(CharsetUtil.UTF_8));
    responseBuffer.release();
    // Check that the preface was send (a.k.a the settings frame)
    ByteBuf settingsBuffer = channel.readOutbound();
    assertNotNull(settingsBuffer);
    settingsBuffer.release();
    assertNull(channel.readOutbound());
}
Also used : PriorKnowledgeUpgradeEvent(io.netty.handler.codec.http2.CleartextHttp2ServerUpgradeHandler.PriorKnowledgeUpgradeEvent) UpgradeEvent(io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeEvent) ByteBuf(io.netty.buffer.ByteBuf)

Example 3 with UpgradeEvent

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

the class Http2FrameCodecTest method upgradeWithoutFlowControlling.

@Test
public void upgradeWithoutFlowControlling() throws Exception {
    channel.pipeline().addAfter(frameCodec.ctx.name(), null, new ChannelInboundHandlerAdapter() {

        @Override
        public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
            if (msg instanceof Http2DataFrame) {
                // Simulate consuming the frame and update the flow-controller.
                Http2DataFrame data = (Http2DataFrame) msg;
                ctx.writeAndFlush(new DefaultHttp2WindowUpdateFrame(data.initialFlowControlledBytes()).stream(data.stream())).addListener(new ChannelFutureListener() {

                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                        Throwable cause = future.cause();
                        if (cause != null) {
                            ctx.fireExceptionCaught(cause);
                        }
                    }
                });
            }
            ReferenceCountUtil.release(msg);
        }
    });
    frameInboundWriter.writeInboundHeaders(Http2CodecUtil.HTTP_UPGRADE_STREAM_ID, request, 31, false);
    // Using reflect as the constructor is package-private and the class is final.
    Constructor<UpgradeEvent> constructor = UpgradeEvent.class.getDeclaredConstructor(CharSequence.class, FullHttpRequest.class);
    // Check if we could make it accessible which may fail on java9.
    Assumptions.assumeTrue(ReflectionUtil.trySetAccessible(constructor, true) == null);
    String longString = new String(new char[70000]).replace("\0", "*");
    DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/", bb(longString));
    HttpServerUpgradeHandler.UpgradeEvent upgradeEvent = constructor.newInstance("HTTP/2", request);
    channel.pipeline().fireUserEventTriggered(upgradeEvent);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) AsciiString(io.netty.util.AsciiString) HttpServerUpgradeHandler(io.netty.handler.codec.http.HttpServerUpgradeHandler) ChannelFutureListener(io.netty.channel.ChannelFutureListener) StreamException(io.netty.handler.codec.http2.Http2Exception.StreamException) UnsupportedMessageTypeException(io.netty.handler.codec.UnsupportedMessageTypeException) UpgradeEvent(io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeEvent) UpgradeEvent(io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeEvent) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 4 with UpgradeEvent

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

the class Http2FrameCodecTest method upgradeEventNoRefCntError.

@Test
public void upgradeEventNoRefCntError() throws Exception {
    frameInboundWriter.writeInboundHeaders(Http2CodecUtil.HTTP_UPGRADE_STREAM_ID, request, 31, false);
    // Using reflect as the constructor is package-private and the class is final.
    Constructor<UpgradeEvent> constructor = UpgradeEvent.class.getDeclaredConstructor(CharSequence.class, FullHttpRequest.class);
    // Check if we could make it accessible which may fail on java9.
    Assumptions.assumeTrue(ReflectionUtil.trySetAccessible(constructor, true) == null);
    HttpServerUpgradeHandler.UpgradeEvent upgradeEvent = constructor.newInstance("HTTP/2", new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"));
    channel.pipeline().fireUserEventTriggered(upgradeEvent);
    assertEquals(1, upgradeEvent.refCnt());
}
Also used : UpgradeEvent(io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeEvent) UpgradeEvent(io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeEvent) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpServerUpgradeHandler(io.netty.handler.codec.http.HttpServerUpgradeHandler) Test(org.junit.jupiter.api.Test)

Aggregations

UpgradeEvent (io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeEvent)4 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)2 HttpServerUpgradeHandler (io.netty.handler.codec.http.HttpServerUpgradeHandler)2 Test (org.junit.jupiter.api.Test)2 ByteBuf (io.netty.buffer.ByteBuf)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)1 UnsupportedMessageTypeException (io.netty.handler.codec.UnsupportedMessageTypeException)1 PriorKnowledgeUpgradeEvent (io.netty.handler.codec.http2.CleartextHttp2ServerUpgradeHandler.PriorKnowledgeUpgradeEvent)1 StreamException (io.netty.handler.codec.http2.Http2Exception.StreamException)1 AsciiString (io.netty.util.AsciiString)1