Search in sources :

Example 26 with TooLongFrameException

use of 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 27 with TooLongFrameException

use of 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 28 with TooLongFrameException

use of 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)

Example 29 with TooLongFrameException

use of io.netty.handler.codec.TooLongFrameException in project vert.x by eclipse.

the class Http2Test method testClearTextUpgradeWithBodyTooLongFrameResponse.

@Test
public void testClearTextUpgradeWithBodyTooLongFrameResponse() throws Exception {
    server.close();
    Buffer buffer = TestUtils.randomBuffer(1024);
    server = vertx.createHttpServer().requestHandler(req -> {
        req.response().setChunked(true);
        vertx.setPeriodic(1, id -> {
            req.response().write(buffer);
        });
    });
    startServer(testAddress);
    client.close();
    client = vertx.createHttpClient(new HttpClientOptions().setProtocolVersion(HttpVersion.HTTP_2));
    client.request(new RequestOptions(requestOptions).setSsl(false)).onComplete(onSuccess(req -> {
        req.response(onFailure(err -> {
        }));
        req.setChunked(true);
        req.exceptionHandler(err -> {
            if (err instanceof TooLongFrameException) {
                testComplete();
            }
        });
        req.sendHead();
    }));
    await();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) java.util(java.util) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) ConnectionBase(io.vertx.core.net.impl.ConnectionBase) Context(io.vertx.core.Context) PfxOptions(io.vertx.core.net.PfxOptions) BooleanSupplier(java.util.function.BooleanSupplier) OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) ChannelPromise(io.netty.channel.ChannelPromise) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DuplexChannel(io.netty.channel.socket.DuplexChannel) Http2ServerConnection(io.vertx.core.http.impl.Http2ServerConnection) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) Promise(io.vertx.core.Promise) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) Test(org.junit.Test) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Repeat(io.vertx.test.core.Repeat) Stream(java.util.stream.Stream) Buffer(io.vertx.core.buffer.Buffer) Ignore(org.junit.Ignore) AsyncTestBase(io.vertx.test.core.AsyncTestBase) Cert(io.vertx.test.tls.Cert) Http2CodecUtil(io.netty.handler.codec.http2.Http2CodecUtil) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) Test(org.junit.Test)

Example 30 with TooLongFrameException

use of io.netty.handler.codec.TooLongFrameException in project cxf by apache.

the class NettyHttpServletHandler method exceptionCaught.

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    LOG.log(Level.SEVERE, "UNEXPECTED_EXCEPCTION_IN_NETTY_SERVLET_HANDLER", cause);
    interceptOnRequestFailed(ctx, cause);
    Channel ch = ctx.channel();
    if (cause instanceof IllegalArgumentException) {
        ch.close();
    } else {
        if (cause instanceof TooLongFrameException) {
            sendError(ctx, HttpResponseStatus.BAD_REQUEST);
            return;
        }
        if (ch.isActive()) {
            sendError(ctx, HttpResponseStatus.INTERNAL_SERVER_ERROR);
        }
    }
    ctx.close();
}
Also used : TooLongFrameException(io.netty.handler.codec.TooLongFrameException) Channel(io.netty.channel.Channel)

Aggregations

TooLongFrameException (io.netty.handler.codec.TooLongFrameException)31 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 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 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