Search in sources :

Example 41 with Http2Exception

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

the class Http2ConnectionRoundtripTest method headersUsingHigherValuedStreamIdPreventsUsingLowerStreamId.

@Test
public void headersUsingHigherValuedStreamIdPreventsUsingLowerStreamId() throws Exception {
    bootstrapEnv(1, 1, 1, 0);
    final Http2Headers headers = dummyHeaders();
    runInChannel(clientChannel, new Http2Runnable() {

        @Override
        public void run() throws Http2Exception {
            http2Client.encoder().writeHeaders(ctx(), 5, headers, 0, (short) 16, false, 0, false, newPromise());
            http2Client.encoder().frameWriter().writeHeaders(ctx(), 3, headers, 0, (short) 16, false, 0, false, newPromise());
            http2Client.flush(ctx());
        }
    });
    assertTrue(serverSettingsAckLatch.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
    assertTrue(requestLatch.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
    verify(serverListener).onHeadersRead(any(ChannelHandlerContext.class), eq(5), eq(headers), eq(0), eq((short) 16), eq(false), eq(0), eq(false));
    verify(serverListener, never()).onHeadersRead(any(ChannelHandlerContext.class), eq(3), any(Http2Headers.class), anyInt(), anyShort(), anyBoolean(), anyInt(), anyBoolean());
    // Client should receive a RST_STREAM for stream 3, but there is not Http2Stream object so the listener is never
    // notified.
    verify(serverListener, never()).onGoAwayRead(any(ChannelHandlerContext.class), anyInt(), anyLong(), any(ByteBuf.class));
    verify(serverListener, never()).onRstStreamRead(any(ChannelHandlerContext.class), anyInt(), anyLong());
    verify(clientListener, never()).onGoAwayRead(any(ChannelHandlerContext.class), anyInt(), anyLong(), any(ByteBuf.class));
    verify(clientListener, never()).onRstStreamRead(any(ChannelHandlerContext.class), anyInt(), anyLong());
}
Also used : Http2Runnable(io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 42 with Http2Exception

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

the class Http2ConnectionRoundtripTest method inflightFrameAfterStreamResetShouldNotMakeConnectionUnusable.

@Test
public void inflightFrameAfterStreamResetShouldNotMakeConnectionUnusable() throws Exception {
    bootstrapEnv(1, 1, 2, 1);
    final CountDownLatch latch = new CountDownLatch(1);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            ChannelHandlerContext ctx = invocationOnMock.getArgument(0);
            http2Server.encoder().writeHeaders(ctx, (Integer) invocationOnMock.getArgument(1), (Http2Headers) invocationOnMock.getArgument(2), 0, false, ctx.newPromise());
            http2Server.flush(ctx);
            return null;
        }
    }).when(serverListener).onHeadersRead(any(ChannelHandlerContext.class), anyInt(), any(Http2Headers.class), anyInt(), anyShort(), anyBoolean(), anyInt(), anyBoolean());
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            latch.countDown();
            return null;
        }
    }).when(clientListener).onHeadersRead(any(ChannelHandlerContext.class), eq(5), any(Http2Headers.class), anyInt(), anyShort(), anyBoolean(), anyInt(), anyBoolean());
    // Create a single stream by sending a HEADERS frame to the server.
    final short weight = 16;
    final Http2Headers headers = dummyHeaders();
    runInChannel(clientChannel, new Http2Runnable() {

        @Override
        public void run() throws Http2Exception {
            http2Client.encoder().writeHeaders(ctx(), 3, headers, 0, weight, false, 0, false, newPromise());
            http2Client.flush(ctx());
            http2Client.encoder().writeRstStream(ctx(), 3, Http2Error.INTERNAL_ERROR.code(), newPromise());
            http2Client.flush(ctx());
        }
    });
    runInChannel(clientChannel, new Http2Runnable() {

        @Override
        public void run() throws Http2Exception {
            http2Client.encoder().writeHeaders(ctx(), 5, headers, 0, weight, false, 0, false, newPromise());
            http2Client.flush(ctx());
        }
    });
    assertTrue(latch.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Http2Runnable(io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 43 with Http2Exception

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

the class Http2ConnectionHandlerTest method serverShouldSend431OnHeaderSizeErrorWhenDecodingInitialHeaders.

@Test
public void serverShouldSend431OnHeaderSizeErrorWhenDecodingInitialHeaders() throws Exception {
    int padding = 0;
    handler = newHandler();
    Http2Exception e = new Http2Exception.HeaderListSizeException(STREAM_ID, PROTOCOL_ERROR, "Header size exceeded max allowed size 8196", true);
    when(stream.id()).thenReturn(STREAM_ID);
    when(connection.isServer()).thenReturn(true);
    when(stream.isHeadersSent()).thenReturn(false);
    when(remote.lastStreamCreated()).thenReturn(STREAM_ID);
    when(frameWriter.writeRstStream(eq(ctx), eq(STREAM_ID), eq(PROTOCOL_ERROR.code()), eq(promise))).thenReturn(future);
    handler.exceptionCaught(ctx, e);
    ArgumentCaptor<Http2Headers> captor = ArgumentCaptor.forClass(Http2Headers.class);
    verify(encoder).writeHeaders(eq(ctx), eq(STREAM_ID), captor.capture(), eq(padding), eq(true), eq(promise));
    Http2Headers headers = captor.getValue();
    assertEquals(HttpResponseStatus.REQUEST_HEADER_FIELDS_TOO_LARGE.codeAsText(), headers.status());
    verify(frameWriter).writeRstStream(ctx, STREAM_ID, PROTOCOL_ERROR.code(), promise);
}
Also used : ShutdownHint(io.netty.handler.codec.http2.Http2Exception.ShutdownHint) Test(org.junit.jupiter.api.Test)

Example 44 with Http2Exception

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

the class Http2ConnectionHandlerTest method serverShouldNeverSend431IfHeadersAlreadySent.

@Test
public void serverShouldNeverSend431IfHeadersAlreadySent() throws Exception {
    int padding = 0;
    handler = newHandler();
    Http2Exception e = new Http2Exception.HeaderListSizeException(STREAM_ID, PROTOCOL_ERROR, "Header size exceeded max allowed size 8196", true);
    when(stream.id()).thenReturn(STREAM_ID);
    when(connection.isServer()).thenReturn(true);
    when(stream.isHeadersSent()).thenReturn(true);
    when(remote.lastStreamCreated()).thenReturn(STREAM_ID);
    when(frameWriter.writeRstStream(eq(ctx), eq(STREAM_ID), eq(PROTOCOL_ERROR.code()), eq(promise))).thenReturn(future);
    handler.exceptionCaught(ctx, e);
    verify(encoder, never()).writeHeaders(eq(ctx), eq(STREAM_ID), any(Http2Headers.class), eq(padding), eq(true), eq(promise));
    verify(frameWriter).writeRstStream(ctx, STREAM_ID, PROTOCOL_ERROR.code(), promise);
}
Also used : ShutdownHint(io.netty.handler.codec.http2.Http2Exception.ShutdownHint) Test(org.junit.jupiter.api.Test)

Example 45 with Http2Exception

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

the class Http2ConnectionHandlerTest method serverShouldNeverSend431HeaderSizeErrorWhenEncoding.

@Test
public void serverShouldNeverSend431HeaderSizeErrorWhenEncoding() throws Exception {
    int padding = 0;
    handler = newHandler();
    Http2Exception e = new Http2Exception.HeaderListSizeException(STREAM_ID, PROTOCOL_ERROR, "Header size exceeded max allowed size 8196", false);
    when(stream.id()).thenReturn(STREAM_ID);
    when(connection.isServer()).thenReturn(true);
    when(stream.isHeadersSent()).thenReturn(false);
    when(remote.lastStreamCreated()).thenReturn(STREAM_ID);
    when(frameWriter.writeRstStream(eq(ctx), eq(STREAM_ID), eq(PROTOCOL_ERROR.code()), eq(promise))).thenReturn(future);
    handler.exceptionCaught(ctx, e);
    verify(encoder, never()).writeHeaders(eq(ctx), eq(STREAM_ID), any(Http2Headers.class), eq(padding), eq(true), eq(promise));
    verify(frameWriter).writeRstStream(ctx, STREAM_ID, PROTOCOL_ERROR.code(), promise);
}
Also used : ShutdownHint(io.netty.handler.codec.http2.Http2Exception.ShutdownHint) Test(org.junit.jupiter.api.Test)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)109 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)100 ChannelFuture (io.netty.channel.ChannelFuture)92 Test (org.junit.Test)89 Http2Exception (io.netty.handler.codec.http2.Http2Exception)85 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)81 AtomicReference (java.util.concurrent.atomic.AtomicReference)78 ByteArrayOutputStream (java.io.ByteArrayOutputStream)76 Channel (io.netty.channel.Channel)75 ChannelPipeline (io.netty.channel.ChannelPipeline)75 ArrayList (java.util.ArrayList)75 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)75 ChannelInitializer (io.netty.channel.ChannelInitializer)74 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)74 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)74 DefaultHttp2Headers (io.netty.handler.codec.http2.DefaultHttp2Headers)74 Http2Headers (io.netty.handler.codec.http2.Http2Headers)74 ApplicationProtocolNames (io.netty.handler.ssl.ApplicationProtocolNames)74 ApplicationProtocolNegotiationHandler (io.netty.handler.ssl.ApplicationProtocolNegotiationHandler)74 SslHandler (io.netty.handler.ssl.SslHandler)74