Search in sources :

Example 31 with TooLongFrameException

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

the class LengthFieldBasedFrameDecoderTest method testFailSlowTooLongFrameRecovery.

@Test
public void testFailSlowTooLongFrameRecovery() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new LengthFieldBasedFrameDecoder(5, 0, 4, 0, 4, false));
    for (int i = 0; i < 2; i++) {
        assertFalse(ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0, 0, 2 })));
        try {
            assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0 })));
            fail(DecoderException.class.getSimpleName() + " must be raised.");
        } catch (TooLongFrameException e) {
        // Expected
        }
        ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0, 0, 1, 'A' }));
        ByteBuf buf = ch.readInbound();
        assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1));
        buf.release();
    }
}
Also used : TooLongFrameException(io.netty.handler.codec.TooLongFrameException) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) Test(org.junit.jupiter.api.Test)

Example 32 with TooLongFrameException

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

the class WebSocket00FrameDecoder method decodeTextFrame.

private WebSocketFrame decodeTextFrame(ChannelHandlerContext ctx, ByteBuf buffer) {
    int ridx = buffer.readerIndex();
    int rbytes = actualReadableBytes();
    int delimPos = buffer.indexOf(ridx, ridx + rbytes, (byte) 0xFF);
    if (delimPos == -1) {
        // Frame delimiter (0xFF) not found
        if (rbytes > maxFrameSize) {
            // Frame length exceeded the maximum
            throw new TooLongFrameException();
        } else {
            // Wait until more data is received
            return null;
        }
    }
    int frameSize = delimPos - ridx;
    if (frameSize > maxFrameSize) {
        throw new TooLongFrameException();
    }
    ByteBuf binaryData = readBytes(ctx.alloc(), buffer, frameSize);
    buffer.skipBytes(1);
    int ffDelimPos = binaryData.indexOf(binaryData.readerIndex(), binaryData.writerIndex(), (byte) 0xFF);
    if (ffDelimPos >= 0) {
        binaryData.release();
        throw new IllegalArgumentException("a text frame should not contain 0xFF.");
    }
    return new TextWebSocketFrame(binaryData);
}
Also used : TooLongFrameException(io.netty.handler.codec.TooLongFrameException) ByteBuf(io.netty.buffer.ByteBuf)

Example 33 with TooLongFrameException

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

the class JsonObjectDecoder method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (state == ST_CORRUPTED) {
        in.skipBytes(in.readableBytes());
        return;
    }
    if (this.idx > in.readerIndex() && lastReaderIndex != in.readerIndex()) {
        this.idx = in.readerIndex() + (idx - lastReaderIndex);
    }
    // index of next byte to process.
    int idx = this.idx;
    int wrtIdx = in.writerIndex();
    if (wrtIdx > maxObjectLength) {
        // buffer size exceeded maxObjectLength; discarding the complete buffer.
        in.skipBytes(in.readableBytes());
        reset();
        throw new TooLongFrameException("object length exceeds " + maxObjectLength + ": " + wrtIdx + " bytes discarded");
    }
    for (; /* use current idx */
    idx < wrtIdx; idx++) {
        byte c = in.getByte(idx);
        if (state == ST_DECODING_NORMAL) {
            decodeByte(c, in, idx);
            // that the JSON object/array is complete.
            if (openBraces == 0) {
                ByteBuf json = extractObject(ctx, in, in.readerIndex(), idx + 1 - in.readerIndex());
                if (json != null) {
                    out.add(json);
                }
                // The JSON object/array was extracted => discard the bytes from
                // the input buffer.
                in.readerIndex(idx + 1);
                // Reset the object state to get ready for the next JSON object/text
                // coming along the byte stream.
                reset();
            }
        } else if (state == ST_DECODING_ARRAY_STREAM) {
            decodeByte(c, in, idx);
            if (!insideString && (openBraces == 1 && c == ',' || openBraces == 0 && c == ']')) {
                // because the byte at position idx is not a whitespace.
                for (int i = in.readerIndex(); Character.isWhitespace(in.getByte(i)); i++) {
                    in.skipBytes(1);
                }
                // skip trailing spaces.
                int idxNoSpaces = idx - 1;
                while (idxNoSpaces >= in.readerIndex() && Character.isWhitespace(in.getByte(idxNoSpaces))) {
                    idxNoSpaces--;
                }
                ByteBuf json = extractObject(ctx, in, in.readerIndex(), idxNoSpaces + 1 - in.readerIndex());
                if (json != null) {
                    out.add(json);
                }
                in.readerIndex(idx + 1);
                if (c == ']') {
                    reset();
                }
            }
        // JSON object/array detected. Accumulate bytes until all braces/brackets are closed.
        } else if (c == '{' || c == '[') {
            initDecoding(c);
            if (state == ST_DECODING_ARRAY_STREAM) {
                // Discard the array bracket
                in.skipBytes(1);
            }
        // Discard leading spaces in front of a JSON object/array.
        } else if (Character.isWhitespace(c)) {
            in.skipBytes(1);
        } else {
            state = ST_CORRUPTED;
            throw new CorruptedFrameException("invalid JSON received at byte position " + idx + ": " + ByteBufUtil.hexDump(in));
        }
    }
    if (in.readableBytes() == 0) {
        this.idx = 0;
    } else {
        this.idx = idx;
    }
    this.lastReaderIndex = in.readerIndex();
}
Also used : TooLongFrameException(io.netty.handler.codec.TooLongFrameException) CorruptedFrameException(io.netty.handler.codec.CorruptedFrameException) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

TooLongFrameException (io.netty.handler.codec.TooLongFrameException)33 ByteBuf (io.netty.buffer.ByteBuf)15 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 CorruptedFrameException (io.netty.handler.codec.CorruptedFrameException)4 TestUtils (io.vertx.test.core.TestUtils)4 Test (org.junit.jupiter.api.Test)4 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)3 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)3 ChannelHandler (io.netty.channel.ChannelHandler)2 EventLoop (io.netty.channel.EventLoop)2 DecoderException (io.netty.handler.codec.DecoderException)2 DelimiterBasedFrameDecoder (io.netty.handler.codec.DelimiterBasedFrameDecoder)2 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)2 HttpRequest (io.netty.handler.codec.http.HttpRequest)2 HttpResponse (io.netty.handler.codec.http.HttpResponse)2 io.vertx.core (io.vertx.core)2