Search in sources :

Example 26 with Http2Exception

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

the class Http2StreamCodec method onError.

@Override
public void onError(ChannelHandlerContext ctx, Throwable cause) {
    super.onError(ctx, cause);
    Http2Exception http2Exception = getEmbeddedHttp2Exception(cause);
    if (http2Exception == null) {
        doHandleConnectionException(ctx, cause);
    } else {
        if (http2Exception instanceof Http2Exception.StreamException) {
            Http2Exception.StreamException streamException = (Http2Exception.StreamException) http2Exception;
            doHandleStreamException(connection().stream(streamException.streamId()), ctx, streamException);
        } else if (http2Exception instanceof Http2Exception.CompositeStreamException) {
            Http2Exception.CompositeStreamException compositException = (Http2Exception.CompositeStreamException) http2Exception;
            for (Http2Exception.StreamException streamException : compositException) {
                doHandleStreamException(connection().stream(streamException.streamId()), ctx, streamException);
            }
        } else {
            doHandleConnectionException(ctx, http2Exception);
        }
    }
}
Also used : Http2CodecUtil.getEmbeddedHttp2Exception(io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception) Http2Exception(io.netty.handler.codec.http2.Http2Exception)

Example 27 with Http2Exception

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

the class DefaultHttp2ConnectionTest method removeAllStreamsWhileIteratingActiveStreamsAndExceptionOccurs.

@Test
public void removeAllStreamsWhileIteratingActiveStreamsAndExceptionOccurs() throws Exception {
    final Endpoint<Http2RemoteFlowController> remote = client.remote();
    final Endpoint<Http2LocalFlowController> local = client.local();
    for (int c = 3, s = 2; c < 5000; c += 2, s += 2) {
        local.createStream(c, false);
        remote.createStream(s, false);
    }
    final Promise<Void> promise = group.next().newPromise();
    final CountDownLatch latch = new CountDownLatch(1);
    try {
        client.forEachActiveStream(new Http2StreamVisitor() {

            @Override
            public boolean visit(Http2Stream stream) throws Http2Exception {
                // This close call is basically a noop, because the following statement will throw an exception.
                client.close(promise);
                // Do an invalid operation while iterating.
                remote.createStream(3, false);
                return true;
            }
        });
    } catch (Http2Exception ignored) {
        client.close(promise).addListener(new FutureListener<Void>() {

            @Override
            public void operationComplete(Future<Void> future) throws Exception {
                assertTrue(promise.isDone());
                latch.countDown();
            }
        });
    }
    assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : FutureListener(io.netty.util.concurrent.FutureListener) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(io.netty.handler.codec.http2.Http2Connection.Endpoint) Future(io.netty.util.concurrent.Future) Test(org.junit.jupiter.api.Test)

Example 28 with Http2Exception

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

the class DefaultHttp2ConnectionTest method listenerThrowShouldNotPreventOtherListenersFromBeingNotified.

/**
 * We force {@link #clientListener} methods to all throw a {@link RuntimeException} and verify the following:
 * <ol>
 * <li>all listener methods are called for both {@link #clientListener} and {@link #clientListener2}</li>
 * <li>{@link #clientListener2} is notified after {@link #clientListener}</li>
 * <li>{@link #clientListener2} methods are all still called despite {@link #clientListener}'s
 * method throwing a {@link RuntimeException}</li>
 * </ol>
 */
@Test
public void listenerThrowShouldNotPreventOtherListenersFromBeingNotified() throws Http2Exception {
    final boolean[] calledArray = new boolean[128];
    // The following setup will ensure that clientListener throws exceptions, and marks a value in an array
    // such that clientListener2 will verify that is is set or fail the test.
    int methodIndex = 0;
    doAnswer(new ListenerExceptionThrower(calledArray, methodIndex)).when(clientListener).onStreamAdded(any(Http2Stream.class));
    doAnswer(new ListenerVerifyCallAnswer(calledArray, methodIndex++)).when(clientListener2).onStreamAdded(any(Http2Stream.class));
    doAnswer(new ListenerExceptionThrower(calledArray, methodIndex)).when(clientListener).onStreamActive(any(Http2Stream.class));
    doAnswer(new ListenerVerifyCallAnswer(calledArray, methodIndex++)).when(clientListener2).onStreamActive(any(Http2Stream.class));
    doAnswer(new ListenerExceptionThrower(calledArray, methodIndex)).when(clientListener).onStreamHalfClosed(any(Http2Stream.class));
    doAnswer(new ListenerVerifyCallAnswer(calledArray, methodIndex++)).when(clientListener2).onStreamHalfClosed(any(Http2Stream.class));
    doAnswer(new ListenerExceptionThrower(calledArray, methodIndex)).when(clientListener).onStreamClosed(any(Http2Stream.class));
    doAnswer(new ListenerVerifyCallAnswer(calledArray, methodIndex++)).when(clientListener2).onStreamClosed(any(Http2Stream.class));
    doAnswer(new ListenerExceptionThrower(calledArray, methodIndex)).when(clientListener).onStreamRemoved(any(Http2Stream.class));
    doAnswer(new ListenerVerifyCallAnswer(calledArray, methodIndex++)).when(clientListener2).onStreamRemoved(any(Http2Stream.class));
    doAnswer(new ListenerExceptionThrower(calledArray, methodIndex)).when(clientListener).onGoAwaySent(anyInt(), anyLong(), any(ByteBuf.class));
    doAnswer(new ListenerVerifyCallAnswer(calledArray, methodIndex++)).when(clientListener2).onGoAwaySent(anyInt(), anyLong(), any(ByteBuf.class));
    doAnswer(new ListenerExceptionThrower(calledArray, methodIndex)).when(clientListener).onGoAwayReceived(anyInt(), anyLong(), any(ByteBuf.class));
    doAnswer(new ListenerVerifyCallAnswer(calledArray, methodIndex++)).when(clientListener2).onGoAwayReceived(anyInt(), anyLong(), any(ByteBuf.class));
    doAnswer(new ListenerExceptionThrower(calledArray, methodIndex)).when(clientListener).onStreamAdded(any(Http2Stream.class));
    doAnswer(new ListenerVerifyCallAnswer(calledArray, methodIndex++)).when(clientListener2).onStreamAdded(any(Http2Stream.class));
    // Now we add clientListener2 and exercise all listener functionality
    try {
        client.addListener(clientListener2);
        Http2Stream stream = client.local().createStream(3, false);
        verify(clientListener).onStreamAdded(any(Http2Stream.class));
        verify(clientListener2).onStreamAdded(any(Http2Stream.class));
        verify(clientListener).onStreamActive(any(Http2Stream.class));
        verify(clientListener2).onStreamActive(any(Http2Stream.class));
        Http2Stream reservedStream = client.remote().reservePushStream(2, stream);
        verify(clientListener, never()).onStreamActive(streamEq(reservedStream));
        verify(clientListener2, never()).onStreamActive(streamEq(reservedStream));
        reservedStream.open(false);
        verify(clientListener).onStreamActive(streamEq(reservedStream));
        verify(clientListener2).onStreamActive(streamEq(reservedStream));
        stream.closeLocalSide();
        verify(clientListener).onStreamHalfClosed(any(Http2Stream.class));
        verify(clientListener2).onStreamHalfClosed(any(Http2Stream.class));
        stream.close();
        verify(clientListener).onStreamClosed(any(Http2Stream.class));
        verify(clientListener2).onStreamClosed(any(Http2Stream.class));
        verify(clientListener).onStreamRemoved(any(Http2Stream.class));
        verify(clientListener2).onStreamRemoved(any(Http2Stream.class));
        client.goAwaySent(client.connectionStream().id(), Http2Error.INTERNAL_ERROR.code(), Unpooled.EMPTY_BUFFER);
        verify(clientListener).onGoAwaySent(anyInt(), anyLong(), any(ByteBuf.class));
        verify(clientListener2).onGoAwaySent(anyInt(), anyLong(), any(ByteBuf.class));
        client.goAwayReceived(client.connectionStream().id(), Http2Error.INTERNAL_ERROR.code(), Unpooled.EMPTY_BUFFER);
        verify(clientListener).onGoAwayReceived(anyInt(), anyLong(), any(ByteBuf.class));
        verify(clientListener2).onGoAwayReceived(anyInt(), anyLong(), any(ByteBuf.class));
    } finally {
        client.removeListener(clientListener2);
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) Endpoint(io.netty.handler.codec.http2.Http2Connection.Endpoint) Test(org.junit.jupiter.api.Test)

Example 29 with Http2Exception

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

the class DataCompressionHttp2Test method zstdEncodingSingleEmptyMessage.

@Test
public void zstdEncodingSingleEmptyMessage() throws Exception {
    final String text = "";
    final ByteBuf data = Unpooled.copiedBuffer(text.getBytes());
    bootstrapEnv(data.readableBytes());
    try {
        final Http2Headers headers = new DefaultHttp2Headers().method(POST).path(PATH).set(HttpHeaderNames.CONTENT_ENCODING, HttpHeaderValues.ZSTD);
        runInChannel(clientChannel, new Http2Runnable() {

            @Override
            public void run() throws Http2Exception {
                clientEncoder.writeHeaders(ctxClient(), 3, headers, 0, false, newPromiseClient());
                clientEncoder.writeData(ctxClient(), 3, data.retain(), 0, true, newPromiseClient());
                clientHandler.flush(ctxClient());
            }
        });
        awaitServer();
        assertEquals(text, serverOut.toString(CharsetUtil.UTF_8.name()));
    } finally {
        data.release();
    }
}
Also used : Http2Runnable(io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable) AsciiString(io.netty.util.AsciiString) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 30 with Http2Exception

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

the class DataCompressionHttp2Test method justHeadersNoData.

@Test
public void justHeadersNoData() throws Exception {
    bootstrapEnv(0);
    final Http2Headers headers = new DefaultHttp2Headers().method(GET).path(PATH).set(HttpHeaderNames.CONTENT_ENCODING, HttpHeaderValues.GZIP);
    runInChannel(clientChannel, new Http2Runnable() {

        @Override
        public void run() throws Http2Exception {
            clientEncoder.writeHeaders(ctxClient(), 3, headers, 0, true, newPromiseClient());
            clientHandler.flush(ctxClient());
        }
    });
    awaitServer();
    verify(serverListener).onHeadersRead(any(ChannelHandlerContext.class), eq(3), eq(headers), eq(0), eq(DEFAULT_PRIORITY_WEIGHT), eq(false), eq(0), eq(true));
}
Also used : Http2Runnable(io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) 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