Java Code Examples for io.netty.handler.codec.http2.Http2DataFrame#isEndStream()

The following examples show how to use io.netty.handler.codec.http2.Http2DataFrame#isEndStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractH2DuplexHandler.java    From servicetalk with Apache License 2.0 6 votes vote down vote up
final void readDataFrame(ChannelHandlerContext ctx, Object msg) {
    Object toRelease = msg;
    try {
        Http2DataFrame dataFrame = (Http2DataFrame) msg;
        if (dataFrame.content().isReadable()) {
            // Copy to unpooled memory before passing to the user
            Buffer data = allocator.newBuffer(dataFrame.content().readableBytes());
            ByteBuf nettyData = toByteBuf(data);
            nettyData.writeBytes(dataFrame.content());
            toRelease = release(dataFrame);
            ctx.fireChannelRead(data);
        } else {
            toRelease = release(dataFrame);
        }
        if (dataFrame.isEndStream()) {
            ctx.fireChannelRead(headersFactory.newEmptyTrailers());
        }
    } finally {
        if (toRelease != null) {
            ReferenceCountUtil.release(toRelease);
        }
    }
}
 
Example 2
Source File: WindowSizeTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (!(msg instanceof Http2Frame)) {
        ctx.fireChannelRead(msg);
        return;
    }

    Http2Frame frame = (Http2Frame) msg;
    receivedFrames.add(frame);
    if (frame instanceof Http2DataFrame) {
        Http2DataFrame dataFrame = (Http2DataFrame) frame;
        if (dataFrame.isEndStream()) {
            Http2HeadersFrame respHeaders = new DefaultHttp2HeadersFrame(
                    new DefaultHttp2Headers().status("204"), true)
                    .stream(dataFrame.stream());
            ctx.writeAndFlush(respHeaders);
        }
    }
    ReferenceCountUtil.release(frame);
}
 
Example 3
Source File: HelloWorldHttp2Handler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * If receive a frame with end-of-stream set, send a pre-canned response.
 */
private static void onDataRead(ChannelHandlerContext ctx, Http2DataFrame data) throws Exception {
    if (data.isEndStream()) {
        sendResponse(ctx, data.content());
    } else {
        // We do not send back the response to the remote-peer, so we need to release it.
        data.release();
    }
}
 
Example 4
Source File: HelloWorldHttp2Handler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * If receive a frame with end-of-stream set, send a pre-canned response.
 */
private static void onDataRead(ChannelHandlerContext ctx, Http2DataFrame data) throws Exception {
    Http2FrameStream stream = data.stream();

    if (data.isEndStream()) {
        sendResponse(ctx, stream, data.content());
    } else {
        // We do not send back the response to the remote-peer, so we need to release it.
        data.release();
    }

    // Update the flowcontroller
    ctx.write(new DefaultHttp2WindowUpdateFrame(data.initialFlowControlledBytes()).stream(stream));
}
 
Example 5
Source File: Http2ToHttpInboundAdapter.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private void onDataRead(Http2DataFrame dataFrame, ChannelHandlerContext ctx) throws Http2Exception {
    ByteBuf data = dataFrame.content();
    data.retain();
    if (!dataFrame.isEndStream()) {
        ctx.fireChannelRead(new DefaultHttpContent(data));
    } else {
        ctx.fireChannelRead(new DefaultLastHttpContent(data));
    }
}
 
Example 6
Source File: ReadTimeoutTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (!(msg instanceof Http2Frame)) {
        ctx.fireChannelRead(msg);
        return;
    }

    Http2Frame frame = (Http2Frame) msg;
    if (frame instanceof Http2DataFrame) {
        Http2DataFrame dataFrame = (Http2DataFrame) frame;
        ReferenceCountUtil.release(frame);
        if (dataFrame.isEndStream()) {
            Http2HeadersFrame respHeaders = new DefaultHttp2HeadersFrame(
                    new DefaultHttp2Headers().status("204"), true)
                    .stream(dataFrame.stream());
            ctx.writeAndFlush(respHeaders);
        }

        if (sleeps > 0) {
            --sleeps;
            // Simulate a server that's slow to read data. Since our
            // window size is equal to the max frame size, the client
            // shouldn't be able to send more data until we update our
            // window
            try {
                Thread.sleep(500);
            } catch (InterruptedException ie) {
            }
        }
        ctx.writeAndFlush(new DefaultHttp2WindowUpdateFrame(dataFrame.initialFlowControlledBytes())
                .stream(dataFrame.stream()));
    }
}
 
Example 7
Source File: PingTimeoutTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, Http2Frame http2Frame) throws Exception {
    if (http2Frame instanceof Http2DataFrame) {
        Http2DataFrame dataFrame = (Http2DataFrame) http2Frame;
        if (dataFrame.isEndStream()) {
            Http2Headers headers = new DefaultHttp2Headers().status("200");
            ctx.writeAndFlush(new DefaultHttp2HeadersFrame(headers, false));
            ctx.executor().scheduleAtFixedRate(() -> {
                DefaultHttp2DataFrame respData = new DefaultHttp2DataFrame(Unpooled.wrappedBuffer("hello".getBytes()), false);
                ctx.writeAndFlush(respData);
            }, 0, 2, TimeUnit.SECONDS);
        }
    }
}
 
Example 8
Source File: AccessLogHandlerH2.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
	boolean lastContent = false;
	if (msg instanceof Http2HeadersFrame) {
		final Http2HeadersFrame responseHeaders = (Http2HeadersFrame) msg;
		final Http2Headers headers = responseHeaders.headers();
		lastContent = responseHeaders.isEndStream();

		accessLog.status(headers.status())
		         .chunked(true);
	}
	if (msg instanceof Http2DataFrame) {
		final Http2DataFrame data = (Http2DataFrame) msg;
		lastContent = data.isEndStream();

		accessLog.increaseContentLength(data.content().readableBytes());
	}
	if (lastContent) {
		ctx.write(msg, promise.unvoid())
		   .addListener(future -> {
		       if (future.isSuccess()) {
		           accessLog.log();
		       }
		   });
		return;
	}
	//"FutureReturnValueIgnored" this is deliberate
	ctx.write(msg, promise);
}