Search in sources :

Example 16 with CodecException

use of io.netty.handler.codec.CodecException in project netty by netty.

the class WebSocketClientExtensionHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;
        if (WebSocketExtensionUtil.isWebsocketUpgrade(response.headers())) {
            String extensionsHeader = response.headers().getAsString(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS);
            if (extensionsHeader != null) {
                List<WebSocketExtensionData> extensions = WebSocketExtensionUtil.extractExtensions(extensionsHeader);
                List<WebSocketClientExtension> validExtensions = new ArrayList<WebSocketClientExtension>(extensions.size());
                int rsv = 0;
                for (WebSocketExtensionData extensionData : extensions) {
                    Iterator<WebSocketClientExtensionHandshaker> extensionHandshakersIterator = extensionHandshakers.iterator();
                    WebSocketClientExtension validExtension = null;
                    while (validExtension == null && extensionHandshakersIterator.hasNext()) {
                        WebSocketClientExtensionHandshaker extensionHandshaker = extensionHandshakersIterator.next();
                        validExtension = extensionHandshaker.handshakeExtension(extensionData);
                    }
                    if (validExtension != null && ((validExtension.rsv() & rsv) == 0)) {
                        rsv = rsv | validExtension.rsv();
                        validExtensions.add(validExtension);
                    } else {
                        throw new CodecException("invalid WebSocket Extension handshake for \"" + extensionsHeader + '"');
                    }
                }
                for (WebSocketClientExtension validExtension : validExtensions) {
                    WebSocketExtensionDecoder decoder = validExtension.newExtensionDecoder();
                    WebSocketExtensionEncoder encoder = validExtension.newExtensionEncoder();
                    ctx.pipeline().addAfter(ctx.name(), decoder.getClass().getName(), decoder);
                    ctx.pipeline().addAfter(ctx.name(), encoder.getClass().getName(), encoder);
                }
            }
            ctx.pipeline().remove(ctx.name());
        }
    }
    super.channelRead(ctx, msg);
}
Also used : ArrayList(java.util.ArrayList) HttpResponse(io.netty.handler.codec.http.HttpResponse) CodecException(io.netty.handler.codec.CodecException)

Example 17 with CodecException

use of io.netty.handler.codec.CodecException in project netty by netty.

the class WebSocketClientExtensionHandlerTest method testIfMainAndFallbackUseRSV1WillFail.

@Test
public void testIfMainAndFallbackUseRSV1WillFail() {
    // initialize
    when(mainHandshakerMock.newRequestData()).thenReturn(new WebSocketExtensionData("main", Collections.<String, String>emptyMap()));
    when(mainHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("main"))).thenReturn(mainExtensionMock);
    when(mainHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("fallback"))).thenReturn(null);
    when(fallbackHandshakerMock.newRequestData()).thenReturn(new WebSocketExtensionData("fallback", Collections.<String, String>emptyMap()));
    when(fallbackHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("main"))).thenReturn(null);
    when(fallbackHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("fallback"))).thenReturn(fallbackExtensionMock);
    when(mainExtensionMock.rsv()).thenReturn(WebSocketExtension.RSV1);
    when(fallbackExtensionMock.rsv()).thenReturn(WebSocketExtension.RSV1);
    // execute
    EmbeddedChannel ch = new EmbeddedChannel(new WebSocketClientExtensionHandler(mainHandshakerMock, fallbackHandshakerMock));
    HttpRequest req = newUpgradeRequest(null);
    ch.writeOutbound(req);
    HttpRequest req2 = ch.readOutbound();
    List<WebSocketExtensionData> reqExts = WebSocketExtensionUtil.extractExtensions(req2.headers().get(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS));
    HttpResponse res = newUpgradeResponse("main, fallback");
    try {
        ch.writeInbound(res);
    } catch (CodecException e) {
        return;
    }
    fail("Expected to encounter a CodecException");
    // test
    assertEquals(2, reqExts.size());
    assertEquals("main", reqExts.get(0).name());
    assertEquals("fallback", reqExts.get(1).name());
    verify(mainHandshakerMock).newRequestData();
    verify(mainHandshakerMock, atLeastOnce()).handshakeExtension(webSocketExtensionDataMatcher("main"));
    verify(mainHandshakerMock, atLeastOnce()).handshakeExtension(webSocketExtensionDataMatcher("fallback"));
    verify(fallbackHandshakerMock).newRequestData();
    verify(fallbackHandshakerMock, atLeastOnce()).handshakeExtension(webSocketExtensionDataMatcher("main"));
    verify(fallbackHandshakerMock, atLeastOnce()).handshakeExtension(webSocketExtensionDataMatcher("fallback"));
    verify(mainExtensionMock, atLeastOnce()).rsv();
    verify(fallbackExtensionMock, atLeastOnce()).rsv();
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpResponse(io.netty.handler.codec.http.HttpResponse) CodecException(io.netty.handler.codec.CodecException) Test(org.junit.jupiter.api.Test)

Example 18 with CodecException

use of io.netty.handler.codec.CodecException in project netty by netty.

the class DeflateDecoder method decompressContent.

private ByteBuf decompressContent(ChannelHandlerContext ctx, WebSocketFrame msg) {
    if (decoder == null) {
        if (!(msg instanceof TextWebSocketFrame) && !(msg instanceof BinaryWebSocketFrame)) {
            throw new CodecException("unexpected initial frame type: " + msg.getClass().getName());
        }
        decoder = new EmbeddedChannel(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.NONE));
    }
    boolean readable = msg.content().isReadable();
    boolean emptyDeflateBlock = EMPTY_DEFLATE_BLOCK.equals(msg.content());
    decoder.writeInbound(msg.content().retain());
    if (appendFrameTail(msg)) {
        decoder.writeInbound(FRAME_TAIL.duplicate());
    }
    CompositeByteBuf compositeDecompressedContent = ctx.alloc().compositeBuffer();
    for (; ; ) {
        ByteBuf partUncompressedContent = decoder.readInbound();
        if (partUncompressedContent == null) {
            break;
        }
        if (!partUncompressedContent.isReadable()) {
            partUncompressedContent.release();
            continue;
        }
        compositeDecompressedContent.addComponent(true, partUncompressedContent);
    }
    // See https://github.com/netty/netty/issues/4348
    if (!emptyDeflateBlock && readable && compositeDecompressedContent.numComponents() <= 0) {
        // May contain left-over data that doesn't affect decompression
        if (!(msg instanceof ContinuationWebSocketFrame)) {
            compositeDecompressedContent.release();
            throw new CodecException("cannot read uncompressed buffer");
        }
    }
    if (msg.isFinalFragment() && noContext) {
        cleanup();
    }
    return compositeDecompressedContent;
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) CodecException(io.netty.handler.codec.CodecException) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 19 with CodecException

use of io.netty.handler.codec.CodecException in project netty by netty.

the class DeflateEncoder method encode.

@Override
protected void encode(ChannelHandlerContext ctx, WebSocketFrame msg, List<Object> out) throws Exception {
    final ByteBuf compressedContent;
    if (msg.content().isReadable()) {
        compressedContent = compressContent(ctx, msg);
    } else if (msg.isFinalFragment()) {
        // Set empty DEFLATE block manually for unknown buffer size
        // https://tools.ietf.org/html/rfc7692#section-7.2.3.6
        compressedContent = EMPTY_DEFLATE_BLOCK.duplicate();
    } else {
        throw new CodecException("cannot compress content buffer");
    }
    final WebSocketFrame outMsg;
    if (msg instanceof TextWebSocketFrame) {
        outMsg = new TextWebSocketFrame(msg.isFinalFragment(), rsv(msg), compressedContent);
    } else if (msg instanceof BinaryWebSocketFrame) {
        outMsg = new BinaryWebSocketFrame(msg.isFinalFragment(), rsv(msg), compressedContent);
    } else if (msg instanceof ContinuationWebSocketFrame) {
        outMsg = new ContinuationWebSocketFrame(msg.isFinalFragment(), rsv(msg), compressedContent);
    } else {
        throw new CodecException("unexpected frame type: " + msg.getClass().getName());
    }
    out.add(outMsg);
}
Also used : ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) CodecException(io.netty.handler.codec.CodecException) 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) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

CodecException (io.netty.handler.codec.CodecException)19 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)7 ByteBuf (io.netty.buffer.ByteBuf)6 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)4 Test (org.junit.jupiter.api.Test)4 BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)3 ContinuationWebSocketFrame (io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame)3 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)2 PrematureChannelClosureException (io.netty.handler.codec.PrematureChannelClosureException)2 HttpResponse (io.netty.handler.codec.http.HttpResponse)2 WebSocketFrame (io.netty.handler.codec.http.websocketx.WebSocketFrame)2 ByteBuffer (org.lanternpowered.server.network.buffer.ByteBuffer)2 Message (org.lanternpowered.server.network.message.Message)2 MessagePlayInOutChannelPayload (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload)2 Vector3i (com.flowpowered.math.vector.Vector3i)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)1