Search in sources :

Example 6 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 handhshake 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 7 with CodecException

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

the class HttpContentDecoder method decode.

@Override
protected void decode(ChannelHandlerContext ctx, HttpObject msg, List<Object> out) throws Exception {
    if (msg instanceof HttpResponse && ((HttpResponse) msg).status().code() == 100) {
        if (!(msg instanceof LastHttpContent)) {
            continueResponse = true;
        }
        // 100-continue response must be passed through.
        out.add(ReferenceCountUtil.retain(msg));
        return;
    }
    if (continueResponse) {
        if (msg instanceof LastHttpContent) {
            continueResponse = false;
        }
        // 100-continue response must be passed through.
        out.add(ReferenceCountUtil.retain(msg));
        return;
    }
    if (msg instanceof HttpMessage) {
        cleanup();
        final HttpMessage message = (HttpMessage) msg;
        final HttpHeaders headers = message.headers();
        // Determine the content encoding.
        String contentEncoding = headers.get(HttpHeaderNames.CONTENT_ENCODING);
        if (contentEncoding != null) {
            contentEncoding = contentEncoding.trim();
        } else {
            contentEncoding = IDENTITY;
        }
        decoder = newContentDecoder(contentEncoding);
        if (decoder == null) {
            if (message instanceof HttpContent) {
                ((HttpContent) message).retain();
            }
            out.add(message);
            return;
        }
        // Otherwise, rely on LastHttpContent message.
        if (headers.contains(HttpHeaderNames.CONTENT_LENGTH)) {
            headers.remove(HttpHeaderNames.CONTENT_LENGTH);
            headers.set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
        }
        // Either it is already chunked or EOF terminated.
        // See https://github.com/netty/netty/issues/5892
        // set new content encoding,
        CharSequence targetContentEncoding = getTargetContentEncoding(contentEncoding);
        if (HttpHeaderValues.IDENTITY.contentEquals(targetContentEncoding)) {
            // Do NOT set the 'Content-Encoding' header if the target encoding is 'identity'
            // as per: http://tools.ietf.org/html/rfc2616#section-14.11
            headers.remove(HttpHeaderNames.CONTENT_ENCODING);
        } else {
            headers.set(HttpHeaderNames.CONTENT_ENCODING, targetContentEncoding);
        }
        if (message instanceof HttpContent) {
            // If message is a full request or response object (headers + data), don't copy data part into out.
            // Output headers only; data part will be decoded below.
            // Note: "copy" object must not be an instance of LastHttpContent class,
            // as this would (erroneously) indicate the end of the HttpMessage to other handlers.
            HttpMessage copy;
            if (message instanceof HttpRequest) {
                // HttpRequest or FullHttpRequest
                HttpRequest r = (HttpRequest) message;
                copy = new DefaultHttpRequest(r.protocolVersion(), r.method(), r.uri());
            } else if (message instanceof HttpResponse) {
                // HttpResponse or FullHttpResponse
                HttpResponse r = (HttpResponse) message;
                copy = new DefaultHttpResponse(r.protocolVersion(), r.status());
            } else {
                throw new CodecException("Object of class " + message.getClass().getName() + " is not a HttpRequest or HttpResponse");
            }
            copy.headers().set(message.headers());
            copy.setDecoderResult(message.decoderResult());
            out.add(copy);
        } else {
            out.add(message);
        }
    }
    if (msg instanceof HttpContent) {
        final HttpContent c = (HttpContent) msg;
        if (decoder == null) {
            out.add(c.retain());
        } else {
            decodeContent(c, out);
        }
    }
}
Also used : CodecException(io.netty.handler.codec.CodecException)

Aggregations

CodecException (io.netty.handler.codec.CodecException)7 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)5 ByteBuf (io.netty.buffer.ByteBuf)4 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)2 PrematureChannelClosureException (io.netty.handler.codec.PrematureChannelClosureException)2 BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)2 ContinuationWebSocketFrame (io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame)2 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)2 WebSocketFrame (io.netty.handler.codec.http.websocketx.WebSocketFrame)2 Test (org.junit.Test)2 Channel (io.netty.channel.Channel)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 HttpResponse (io.netty.handler.codec.http.HttpResponse)1 IllegalReferenceCountException (io.netty.util.IllegalReferenceCountException)1 ArrayList (java.util.ArrayList)1