Search in sources :

Example 1 with WebSocketExtensionFilter

use of io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionFilter in project netty by netty.

the class PerMessageDeflateDecoderTest method testIllegalStateWhenDecompressionInProgress.

@Test
public void testIllegalStateWhenDecompressionInProgress() {
    WebSocketExtensionFilter selectivityDecompressionFilter = new WebSocketExtensionFilter() {

        @Override
        public boolean mustSkip(WebSocketFrame frame) {
            return frame.content().readableBytes() < 100;
        }
    };
    EmbeddedChannel encoderChannel = new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.NONE, 9, 15, 8));
    final EmbeddedChannel decoderChannel = new EmbeddedChannel(new PerMessageDeflateDecoder(false, selectivityDecompressionFilter));
    byte[] firstPayload = new byte[200];
    random.nextBytes(firstPayload);
    byte[] finalPayload = new byte[50];
    random.nextBytes(finalPayload);
    assertTrue(encoderChannel.writeOutbound(Unpooled.wrappedBuffer(firstPayload)));
    assertTrue(encoderChannel.writeOutbound(Unpooled.wrappedBuffer(finalPayload)));
    ByteBuf compressedFirstPayload = encoderChannel.readOutbound();
    ByteBuf compressedFinalPayload = encoderChannel.readOutbound();
    assertTrue(encoderChannel.finishAndReleaseAll());
    BinaryWebSocketFrame firstPart = new BinaryWebSocketFrame(false, WebSocketExtension.RSV1, compressedFirstPayload);
    final ContinuationWebSocketFrame finalPart = new ContinuationWebSocketFrame(true, WebSocketExtension.RSV1, compressedFinalPayload);
    assertTrue(decoderChannel.writeInbound(firstPart));
    BinaryWebSocketFrame outboundFirstPart = decoderChannel.readInbound();
    // first part is decompressed
    assertEquals(0, outboundFirstPart.rsv());
    assertArrayEquals(firstPayload, ByteBufUtil.getBytes(outboundFirstPart.content()));
    assertTrue(outboundFirstPart.release());
    // final part throwing exception
    try {
        assertThrows(DecoderException.class, new Executable() {

            @Override
            public void execute() {
                decoderChannel.writeInbound(finalPart);
            }
        });
    } finally {
        assertTrue(finalPart.release());
        assertFalse(encoderChannel.finishAndReleaseAll());
    }
}
Also used : ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) WebSocketExtensionFilter(io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionFilter) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) ByteBuf(io.netty.buffer.ByteBuf) Executable(org.junit.jupiter.api.function.Executable) Test(org.junit.jupiter.api.Test)

Example 2 with WebSocketExtensionFilter

use of io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionFilter in project netty by netty.

the class PerMessageDeflateEncoderTest method testIllegalStateWhenCompressionInProgress.

@Test
public void testIllegalStateWhenCompressionInProgress() {
    WebSocketExtensionFilter selectivityCompressionFilter = new WebSocketExtensionFilter() {

        @Override
        public boolean mustSkip(WebSocketFrame frame) {
            return frame.content().readableBytes() < 100;
        }
    };
    final EmbeddedChannel encoderChannel = new EmbeddedChannel(new PerMessageDeflateEncoder(9, 15, false, selectivityCompressionFilter));
    byte[] firstPayload = new byte[200];
    random.nextBytes(firstPayload);
    byte[] finalPayload = new byte[90];
    random.nextBytes(finalPayload);
    BinaryWebSocketFrame firstPart = new BinaryWebSocketFrame(false, 0, Unpooled.wrappedBuffer(firstPayload));
    final ContinuationWebSocketFrame finalPart = new ContinuationWebSocketFrame(true, 0, Unpooled.wrappedBuffer(finalPayload));
    assertTrue(encoderChannel.writeOutbound(firstPart));
    BinaryWebSocketFrame outboundFirstPart = encoderChannel.readOutbound();
    // first part is compressed
    assertEquals(WebSocketExtension.RSV1, outboundFirstPart.rsv());
    assertFalse(Arrays.equals(firstPayload, ByteBufUtil.getBytes(outboundFirstPart.content())));
    assertTrue(outboundFirstPart.release());
    // final part throwing exception
    try {
        assertThrows(EncoderException.class, new Executable() {

            @Override
            public void execute() throws Throwable {
                encoderChannel.writeOutbound(finalPart);
            }
        });
    } finally {
        assertTrue(finalPart.release());
        assertFalse(encoderChannel.finishAndReleaseAll());
    }
}
Also used : ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) WebSocketExtensionFilter(io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionFilter) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) Executable(org.junit.jupiter.api.function.Executable) Test(org.junit.jupiter.api.Test)

Example 3 with WebSocketExtensionFilter

use of io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionFilter in project netty by netty.

the class PerMessageDeflateEncoderTest method testSelectivityCompressionSkip.

@Test
public void testSelectivityCompressionSkip() {
    WebSocketExtensionFilter selectivityCompressionFilter = new WebSocketExtensionFilter() {

        @Override
        public boolean mustSkip(WebSocketFrame frame) {
            return (frame instanceof TextWebSocketFrame || frame instanceof BinaryWebSocketFrame) && frame.content().readableBytes() < 100;
        }
    };
    EmbeddedChannel encoderChannel = new EmbeddedChannel(new PerMessageDeflateEncoder(9, 15, false, selectivityCompressionFilter));
    EmbeddedChannel decoderChannel = new EmbeddedChannel(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.NONE));
    String textPayload = "not compressed payload";
    byte[] binaryPayload = new byte[101];
    random.nextBytes(binaryPayload);
    WebSocketFrame textFrame = new TextWebSocketFrame(textPayload);
    BinaryWebSocketFrame binaryFrame = new BinaryWebSocketFrame(Unpooled.wrappedBuffer(binaryPayload));
    assertTrue(encoderChannel.writeOutbound(textFrame));
    assertTrue(encoderChannel.writeOutbound(binaryFrame));
    WebSocketFrame outboundTextFrame = encoderChannel.readOutbound();
    // compression skipped for textFrame
    assertEquals(0, outboundTextFrame.rsv());
    assertEquals(textPayload, outboundTextFrame.content().toString(UTF_8));
    assertTrue(outboundTextFrame.release());
    WebSocketFrame outboundBinaryFrame = encoderChannel.readOutbound();
    // compression not skipped for binaryFrame
    assertEquals(WebSocketExtension.RSV1, outboundBinaryFrame.rsv());
    assertTrue(decoderChannel.writeInbound(outboundBinaryFrame.content().retain()));
    ByteBuf uncompressedBinaryPayload = decoderChannel.readInbound();
    assertArrayEquals(binaryPayload, ByteBufUtil.getBytes(uncompressedBinaryPayload));
    assertTrue(outboundBinaryFrame.release());
    assertTrue(uncompressedBinaryPayload.release());
    assertFalse(encoderChannel.finish());
    assertFalse(decoderChannel.finish());
}
Also used : TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) WebSocketExtensionFilter(io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionFilter) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 4 with WebSocketExtensionFilter

use of io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionFilter in project netty by netty.

the class PerMessageDeflateDecoderTest method testSelectivityDecompressionSkip.

@Test
public void testSelectivityDecompressionSkip() {
    WebSocketExtensionFilter selectivityDecompressionFilter = new WebSocketExtensionFilter() {

        @Override
        public boolean mustSkip(WebSocketFrame frame) {
            return frame instanceof TextWebSocketFrame && frame.content().readableBytes() < 100;
        }
    };
    EmbeddedChannel encoderChannel = new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.NONE, 9, 15, 8));
    EmbeddedChannel decoderChannel = new EmbeddedChannel(new PerMessageDeflateDecoder(false, selectivityDecompressionFilter));
    String textPayload = "compressed payload";
    byte[] binaryPayload = new byte[300];
    random.nextBytes(binaryPayload);
    assertTrue(encoderChannel.writeOutbound(Unpooled.wrappedBuffer(textPayload.getBytes(UTF_8))));
    assertTrue(encoderChannel.writeOutbound(Unpooled.wrappedBuffer(binaryPayload)));
    ByteBuf compressedTextPayload = encoderChannel.readOutbound();
    ByteBuf compressedBinaryPayload = encoderChannel.readOutbound();
    TextWebSocketFrame compressedTextFrame = new TextWebSocketFrame(true, WebSocketExtension.RSV1, compressedTextPayload);
    BinaryWebSocketFrame compressedBinaryFrame = new BinaryWebSocketFrame(true, WebSocketExtension.RSV1, compressedBinaryPayload);
    assertTrue(decoderChannel.writeInbound(compressedTextFrame));
    assertTrue(decoderChannel.writeInbound(compressedBinaryFrame));
    TextWebSocketFrame inboundTextFrame = decoderChannel.readInbound();
    BinaryWebSocketFrame inboundBinaryFrame = decoderChannel.readInbound();
    assertEquals(WebSocketExtension.RSV1, inboundTextFrame.rsv());
    assertEquals(compressedTextPayload, inboundTextFrame.content());
    assertTrue(inboundTextFrame.release());
    assertEquals(0, inboundBinaryFrame.rsv());
    assertArrayEquals(binaryPayload, ByteBufUtil.getBytes(inboundBinaryFrame.content()));
    assertTrue(inboundBinaryFrame.release());
    assertTrue(encoderChannel.finishAndReleaseAll());
    assertFalse(decoderChannel.finish());
}
Also used : TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) WebSocketExtensionFilter(io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionFilter) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Aggregations

EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)4 ContinuationWebSocketFrame (io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame)4 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)4 WebSocketFrame (io.netty.handler.codec.http.websocketx.WebSocketFrame)4 WebSocketExtensionFilter (io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionFilter)4 Test (org.junit.jupiter.api.Test)4 ByteBuf (io.netty.buffer.ByteBuf)3 Executable (org.junit.jupiter.api.function.Executable)2