Search in sources :

Example 6 with Http2FrameStream

use of io.netty.handler.codec.http2.Http2FrameStream in project netty by netty.

the class HelloWorldHttp2Handler method sendResponse.

/**
 * Sends a "Hello World" DATA frame to the client.
 */
private static void sendResponse(ChannelHandlerContext ctx, Http2FrameStream stream, ByteBuf payload) {
    // Send a frame for the response status
    Http2Headers headers = new DefaultHttp2Headers().status(OK.codeAsText());
    ctx.write(new DefaultHttp2HeadersFrame(headers).stream(stream));
    ctx.write(new DefaultHttp2DataFrame(payload, true).stream(stream));
}
Also used : DefaultHttp2HeadersFrame(io.netty.handler.codec.http2.DefaultHttp2HeadersFrame) DefaultHttp2DataFrame(io.netty.handler.codec.http2.DefaultHttp2DataFrame) Http2Headers(io.netty.handler.codec.http2.Http2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers)

Example 7 with Http2FrameStream

use of io.netty.handler.codec.http2.Http2FrameStream in project netty by netty.

the class HelloWorldHttp2Handler method onDataRead.

/**
 * 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));
}
Also used : DefaultHttp2WindowUpdateFrame(io.netty.handler.codec.http2.DefaultHttp2WindowUpdateFrame) Http2FrameStream(io.netty.handler.codec.http2.Http2FrameStream)

Example 8 with Http2FrameStream

use of io.netty.handler.codec.http2.Http2FrameStream in project netty by netty.

the class Http2MultiplexHandler method exceptionCaught.

@Override
public void exceptionCaught(ChannelHandlerContext ctx, final Throwable cause) throws Exception {
    if (cause instanceof Http2FrameStreamException) {
        Http2FrameStreamException exception = (Http2FrameStreamException) cause;
        Http2FrameStream stream = exception.stream();
        AbstractHttp2StreamChannel childChannel = (AbstractHttp2StreamChannel) ((DefaultHttp2FrameStream) stream).attachment;
        try {
            childChannel.pipeline().fireExceptionCaught(cause.getCause());
        } finally {
            childChannel.unsafe().closeForcibly();
        }
        return;
    }
    if (cause.getCause() instanceof SSLException) {
        forEachActiveStream(new Http2FrameStreamVisitor() {

            @Override
            public boolean visit(Http2FrameStream stream) {
                AbstractHttp2StreamChannel childChannel = (AbstractHttp2StreamChannel) ((DefaultHttp2FrameStream) stream).attachment;
                childChannel.pipeline().fireExceptionCaught(cause);
                return true;
            }
        });
    }
    ctx.fireExceptionCaught(cause);
}
Also used : DefaultHttp2FrameStream(io.netty.handler.codec.http2.Http2FrameCodec.DefaultHttp2FrameStream) DefaultHttp2FrameStream(io.netty.handler.codec.http2.Http2FrameCodec.DefaultHttp2FrameStream) SSLException(javax.net.ssl.SSLException)

Example 9 with Http2FrameStream

use of io.netty.handler.codec.http2.Http2FrameStream in project netty by netty.

the class Http2FrameCodecTest method multipleNewOutboundStreamsShouldBeBuffered.

@Test
public void multipleNewOutboundStreamsShouldBeBuffered() throws Exception {
    // We use a limit of 1 and then increase it step by step.
    setUp(Http2FrameCodecBuilder.forServer().encoderEnforceMaxConcurrentStreams(true), new Http2Settings().maxConcurrentStreams(1));
    Http2FrameStream stream1 = frameCodec.newStream();
    Http2FrameStream stream2 = frameCodec.newStream();
    Http2FrameStream stream3 = frameCodec.newStream();
    ChannelPromise promise1 = channel.newPromise();
    ChannelPromise promise2 = channel.newPromise();
    ChannelPromise promise3 = channel.newPromise();
    channel.writeAndFlush(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers()).stream(stream1), promise1);
    channel.writeAndFlush(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers()).stream(stream2), promise2);
    channel.writeAndFlush(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers()).stream(stream3), promise3);
    assertTrue(isStreamIdValid(stream1.id()));
    channel.runPendingTasks();
    assertTrue(isStreamIdValid(stream2.id()));
    assertTrue(promise1.syncUninterruptibly().isSuccess());
    assertFalse(promise2.isDone());
    assertFalse(promise3.isDone());
    // Increase concurrent streams limit to 2
    frameInboundWriter.writeInboundSettings(new Http2Settings().maxConcurrentStreams(2));
    channel.flush();
    // As we increased the limit to 2 we should have also succeed the second frame.
    assertTrue(promise2.syncUninterruptibly().isSuccess());
    assertFalse(promise3.isDone());
    frameInboundWriter.writeInboundSettings(new Http2Settings().maxConcurrentStreams(3));
    channel.flush();
    // With the max streams of 3 all streams should be succeed now.
    assertTrue(promise3.syncUninterruptibly().isSuccess());
    assertFalse(channel.finishAndReleaseAll());
}
Also used : ChannelPromise(io.netty.channel.ChannelPromise) Http2TestUtil.anyChannelPromise(io.netty.handler.codec.http2.Http2TestUtil.anyChannelPromise) Http2TestUtil.anyHttp2Settings(io.netty.handler.codec.http2.Http2TestUtil.anyHttp2Settings) Test(org.junit.jupiter.api.Test)

Example 10 with Http2FrameStream

use of io.netty.handler.codec.http2.Http2FrameStream in project netty by netty.

the class Http2FrameCodecTest method streamShouldBeOpenInListener.

@Test
public void streamShouldBeOpenInListener() {
    final Http2FrameStream stream2 = frameCodec.newStream();
    assertEquals(State.IDLE, stream2.state());
    final AtomicBoolean listenerExecuted = new AtomicBoolean();
    channel.writeAndFlush(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers()).stream(stream2)).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            assertTrue(future.isSuccess());
            assertEquals(State.OPEN, stream2.state());
            listenerExecuted.set(true);
        }
    });
    assertTrue(listenerExecuted.get());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ChannelFutureListener(io.netty.channel.ChannelFutureListener) StreamException(io.netty.handler.codec.http2.Http2Exception.StreamException) UnsupportedMessageTypeException(io.netty.handler.codec.UnsupportedMessageTypeException) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)7 ChannelPromise (io.netty.channel.ChannelPromise)5 Http2TestUtil.anyChannelPromise (io.netty.handler.codec.http2.Http2TestUtil.anyChannelPromise)5 Http2TestUtil.anyHttp2Settings (io.netty.handler.codec.http2.Http2TestUtil.anyHttp2Settings)3 ByteBuf (io.netty.buffer.ByteBuf)2 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)2 UnsupportedMessageTypeException (io.netty.handler.codec.UnsupportedMessageTypeException)2 StreamException (io.netty.handler.codec.http2.Http2Exception.StreamException)2 DefaultHttp2DataFrame (io.netty.handler.codec.http2.DefaultHttp2DataFrame)1 DefaultHttp2Headers (io.netty.handler.codec.http2.DefaultHttp2Headers)1 DefaultHttp2HeadersFrame (io.netty.handler.codec.http2.DefaultHttp2HeadersFrame)1 DefaultHttp2WindowUpdateFrame (io.netty.handler.codec.http2.DefaultHttp2WindowUpdateFrame)1 DefaultHttp2FrameStream (io.netty.handler.codec.http2.Http2FrameCodec.DefaultHttp2FrameStream)1 Http2FrameStream (io.netty.handler.codec.http2.Http2FrameStream)1 Http2Headers (io.netty.handler.codec.http2.Http2Headers)1 DefaultPromise (io.netty.util.concurrent.DefaultPromise)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 SSLException (javax.net.ssl.SSLException)1 Timeout (org.junit.jupiter.api.Timeout)1