Search in sources :

Example 1 with TooLongFrameException

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.TooLongFrameException in project vert.x by eclipse.

the class Http1xTest method testInvalidTrailersInHttpClientResponse.

@Test
public void testInvalidTrailersInHttpClientResponse() throws Exception {
    server.requestHandler(req -> {
        NetSocket so = req.netSocket();
        so.write("HTTP/1.1 200 OK\r\n");
        so.write("Transfer-Encoding: chunked\r\n");
        so.write("\r\n");
        so.write("0\r\n");
        for (int i = 0; i < 2000; i++) {
            so.write("01234567");
        }
    });
    AtomicInteger status = new AtomicInteger();
    testHttpClientResponseDecodeError(err -> {
        switch(status.incrementAndGet()) {
            case 1:
                assertTrue(err instanceof TooLongFrameException);
                break;
            case 2:
                assertTrue(err instanceof VertxException);
                assertTrue(err.getMessage().equals("Connection was closed"));
                testComplete();
                break;
        }
    });
}
Also used : TooLongFrameException(io.netty.handler.codec.TooLongFrameException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 2 with TooLongFrameException

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.TooLongFrameException in project elasticsearch by elastic.

the class Netty4SizeHeaderFrameDecoder method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    try {
        boolean continueProcessing = TcpTransport.validateMessageHeader(Netty4Utils.toBytesReference(in));
        final ByteBuf message = in.skipBytes(TcpHeader.MARKER_BYTES_SIZE + TcpHeader.MESSAGE_LENGTH_SIZE);
        if (!continueProcessing)
            return;
        out.add(message);
    } catch (IllegalArgumentException ex) {
        throw new TooLongFrameException(ex);
    } catch (IllegalStateException ex) {
    /* decode will be called until the ByteBuf is fully consumed; when it is fully
             * consumed, transport#validateMessageHeader will throw an IllegalStateException which
             * is okay, it means we have finished consuming the ByteBuf and we can get out
             */
    }
}
Also used : TooLongFrameException(io.netty.handler.codec.TooLongFrameException) ByteBuf(io.netty.buffer.ByteBuf)

Example 3 with TooLongFrameException

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.TooLongFrameException in project riposte by Nike-Inc.

the class BackstopperRiposteFrameworkErrorHandlerListenerTest method shouldHandleInvalidHttpRequestExceptionWithNonNullCause.

@DataProvider(value = { "true", "false" })
@Test
public void shouldHandleInvalidHttpRequestExceptionWithNonNullCause(boolean useTooLongFrameExceptionAsCause) {
    // given
    Throwable cause = (useTooLongFrameExceptionAsCause) ? new TooLongFrameException("TooLongFrameException occurred") : new RuntimeException("runtime exception");
    String expectedCauseMetadataMessage = (useTooLongFrameExceptionAsCause) ? listener.TOO_LONG_FRAME_LINE_METADATA_MESSAGE : "Invalid HTTP request";
    String outerExceptionMessage = "message - " + UUID.randomUUID().toString();
    ApiError expectedApiErrorBase = (useTooLongFrameExceptionAsCause) ? listener.TOO_LONG_FRAME_LINE_API_ERROR_BASE : testProjectApiErrors.getMalformedRequestApiError();
    // when
    ApiExceptionHandlerListenerResult result = listener.shouldHandleException(new InvalidHttpRequestException(outerExceptionMessage, cause));
    // then
    assertThat(result.shouldHandleResponse).isTrue();
    assertThat(result.errors).isEqualTo(singletonError(new ApiErrorWithMetadata(expectedApiErrorBase, Pair.of("cause", expectedCauseMetadataMessage))));
    assertThat(result.extraDetailsForLogging.get(0).getLeft()).isEqualTo("exception_message");
    assertThat(result.extraDetailsForLogging.get(0).getRight()).isEqualTo(outerExceptionMessage);
    assertThat(result.extraDetailsForLogging.get(1).getLeft()).isEqualTo("exception_cause_details");
    assertThat(result.extraDetailsForLogging.get(1).getRight()).isEqualTo(cause.toString());
}
Also used : TooLongFrameException(io.netty.handler.codec.TooLongFrameException) ApiErrorWithMetadata(com.nike.backstopper.apierror.ApiErrorWithMetadata) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) ApiError(com.nike.backstopper.apierror.ApiError) InvalidHttpRequestException(com.nike.riposte.server.error.exception.InvalidHttpRequestException) ApiExceptionHandlerListenerResult(com.nike.backstopper.handler.listener.ApiExceptionHandlerListenerResult) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 4 with TooLongFrameException

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.TooLongFrameException in project reactor-netty by reactor.

the class HttpServerHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    // read message and track if it was keepAlive
    if (msg instanceof HttpRequest) {
        final HttpRequest request = (HttpRequest) msg;
        DecoderResult decoderResult = request.decoderResult();
        if (decoderResult.isFailure()) {
            Throwable cause = decoderResult.cause();
            HttpServerOperations.log.debug("Decoding failed: " + msg + " : ", cause);
            HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, cause instanceof TooLongFrameException ? HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE : HttpResponseStatus.BAD_REQUEST);
            response.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, 0).set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
            ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
            return;
        }
        if (persistentConnection) {
            pendingResponses += 1;
            if (HttpServerOperations.log.isDebugEnabled()) {
                HttpServerOperations.log.debug("Increasing pending responses, now " + "{}", pendingResponses);
            }
            persistentConnection = isKeepAlive(request);
        } else {
            if (HttpServerOperations.log.isDebugEnabled()) {
                HttpServerOperations.log.debug("dropping pipelined HTTP request, " + "previous response requested connection close");
            }
            ReferenceCountUtil.release(msg);
            return;
        }
        if (pendingResponses > 1) {
            if (HttpServerOperations.log.isDebugEnabled()) {
                HttpServerOperations.log.debug("buffering pipelined HTTP request, " + "pending response count: {}, queue: {}", pendingResponses, pipelined != null ? pipelined.size() : 0);
            }
            overflow = true;
            doPipeline(ctx, msg);
            return;
        } else {
            overflow = false;
            parentContext.createOperations(ctx.channel(), msg);
            if (!(msg instanceof FullHttpRequest)) {
                return;
            }
        }
    } else if (persistentConnection && pendingResponses == 0) {
        if (HttpServerOperations.log.isDebugEnabled()) {
            HttpServerOperations.log.debug("Dropped HTTP content, " + "Since response has been sent already:{}", msg);
        }
        if (msg instanceof LastHttpContent) {
            ctx.fireChannelRead(msg);
        } else {
            ReferenceCountUtil.release(msg);
        }
        ctx.read();
        return;
    } else if (overflow) {
        if (HttpServerOperations.log.isDebugEnabled()) {
            HttpServerOperations.log.debug("buffering pipelined HTTP content, " + "pending response count: {}, pending pipeline:{}", pendingResponses, pipelined != null ? pipelined.size() : 0);
        }
        doPipeline(ctx, msg);
        return;
    }
    ctx.fireChannelRead(msg);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DecoderResult(io.netty.handler.codec.DecoderResult) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Example 5 with TooLongFrameException

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.TooLongFrameException in project teiid by teiid.

the class ObjectDecoder method decode.

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
    if (result == null) {
        ByteBuf frame = null;
        try {
            frame = (ByteBuf) super.decode(ctx, buffer);
        } catch (TooLongFrameException e) {
            throw new IOException(RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40166), e);
        }
        if (frame == null) {
            return null;
        }
        CompactObjectInputStream cois = new CompactObjectInputStream(new ByteBufInputStream(frame), classLoader);
        result = cois.readObject();
        streams = ExternalizeUtil.readList(cois, StreamFactoryReference.class);
        streamIndex = 0;
    }
    while (streamIndex < streams.size()) {
        // read the new chunk size
        if (streamDataToRead == -1) {
            if (buffer.readableBytes() < 2) {
                return null;
            }
            streamDataToRead = buffer.readUnsignedShort();
        }
        if (stream == null) {
            // $NON-NLS-1$
            store = storageManager.createFileStore("temp-stream");
            StreamFactoryReference sfr = streams.get(streamIndex);
            sfr.setStreamFactory(new FileStoreInputStreamFactory(store, Streamable.ENCODING));
            this.stream = new BufferedOutputStream(store.createOutputStream());
        }
        // end of stream
        if (streamDataToRead == 0) {
            stream.close();
            stream = null;
            streamIndex++;
            streamDataToRead = -1;
            continue;
        }
        if (store.getLength() + streamDataToRead > maxLobSize) {
            if (error == null) {
                error = new StreamCorruptedException(// $NON-NLS-1$ //$NON-NLS-2$
                "lob too big: " + (store.getLength() + streamDataToRead) + " (max: " + maxLobSize + ')');
            }
        }
        int toRead = Math.min(buffer.readableBytes(), streamDataToRead);
        if (toRead == 0) {
            return null;
        }
        if (error == null) {
            buffer.readBytes(this.stream, toRead);
        } else {
            buffer.skipBytes(toRead);
        }
        streamDataToRead -= toRead;
        if (streamDataToRead == 0) {
            // get the next chunk
            streamDataToRead = -1;
        }
    }
    Object toReturn = result;
    result = null;
    streams = null;
    stream = null;
    store = null;
    if (error != null) {
        StreamCorruptedException sce = error;
        error = null;
        throw sce;
    }
    return toReturn;
}
Also used : CompactObjectInputStream(org.teiid.netty.handler.codec.serialization.CompactObjectInputStream) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) StreamFactoryReference(org.teiid.core.types.InputStreamFactory.StreamFactoryReference) ByteBuf(io.netty.buffer.ByteBuf) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

TooLongFrameException (io.netty.handler.codec.TooLongFrameException)31 ByteBuf (io.netty.buffer.ByteBuf)14 Test (org.junit.Test)9 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)7 Channel (io.netty.channel.Channel)5 Buffer (io.vertx.core.buffer.Buffer)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 TestUtils (io.vertx.test.core.TestUtils)4 java.util (java.util)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Test (org.junit.jupiter.api.Test)4 ChannelHandler (io.netty.channel.ChannelHandler)2 EventLoop (io.netty.channel.EventLoop)2 CorruptedFrameException (io.netty.handler.codec.CorruptedFrameException)2 DecoderException (io.netty.handler.codec.DecoderException)2 DelimiterBasedFrameDecoder (io.netty.handler.codec.DelimiterBasedFrameDecoder)2 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)2 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)2 io.vertx.core (io.vertx.core)2 Future (io.vertx.core.Future)2