Search in sources :

Example 1 with UnsupportedMessageTypeException

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

the class Http2MultiplexCodec method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (!(msg instanceof Http2Frame)) {
        ctx.fireChannelRead(msg);
        return;
    }
    if (msg instanceof Http2StreamFrame) {
        Http2StreamFrame frame = (Http2StreamFrame) msg;
        int streamId = frame.streamId();
        Http2StreamChannel childChannel = childChannels.get(streamId);
        if (childChannel == null) {
            // TODO: Combine with DefaultHttp2ConnectionDecoder.shouldIgnoreHeadersOrDataFrame logic.
            ReferenceCountUtil.release(msg);
            throw new StreamException(streamId, STREAM_CLOSED, format("Received %s frame for an unknown stream %d", frame.name(), streamId));
        }
        fireChildReadAndRegister(childChannel, frame);
    } else if (msg instanceof Http2GoAwayFrame) {
        Http2GoAwayFrame goAwayFrame = (Http2GoAwayFrame) msg;
        for (PrimitiveEntry<Http2StreamChannel> entry : childChannels.entries()) {
            Http2StreamChannel childChannel = entry.value();
            int streamId = entry.key();
            if (streamId > goAwayFrame.lastStreamId() && isOutboundStream(server, streamId)) {
                childChannel.pipeline().fireUserEventTriggered(goAwayFrame.retainedDuplicate());
            }
        }
        goAwayFrame.release();
    } else {
        // It's safe to release, as UnsupportedMessageTypeException just calls msg.getClass()
        ReferenceCountUtil.release(msg);
        throw new UnsupportedMessageTypeException(msg);
    }
}
Also used : PrimitiveEntry(io.netty.util.collection.IntObjectMap.PrimitiveEntry) StreamException(io.netty.handler.codec.http2.Http2Exception.StreamException) UnsupportedMessageTypeException(io.netty.handler.codec.UnsupportedMessageTypeException)

Example 2 with UnsupportedMessageTypeException

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

the class SpdyHttpEncoder method encode.

@Override
protected void encode(ChannelHandlerContext ctx, HttpObject msg, List<Object> out) throws Exception {
    boolean valid = false;
    boolean last = false;
    if (msg instanceof HttpRequest) {
        HttpRequest httpRequest = (HttpRequest) msg;
        SpdySynStreamFrame spdySynStreamFrame = createSynStreamFrame(httpRequest);
        out.add(spdySynStreamFrame);
        last = spdySynStreamFrame.isLast() || spdySynStreamFrame.isUnidirectional();
        valid = true;
    }
    if (msg instanceof HttpResponse) {
        HttpResponse httpResponse = (HttpResponse) msg;
        SpdyHeadersFrame spdyHeadersFrame = createHeadersFrame(httpResponse);
        out.add(spdyHeadersFrame);
        last = spdyHeadersFrame.isLast();
        valid = true;
    }
    if (msg instanceof HttpContent && !last) {
        HttpContent chunk = (HttpContent) msg;
        chunk.content().retain();
        SpdyDataFrame spdyDataFrame = new DefaultSpdyDataFrame(currentStreamId, chunk.content());
        if (chunk instanceof LastHttpContent) {
            LastHttpContent trailer = (LastHttpContent) chunk;
            HttpHeaders trailers = trailer.trailingHeaders();
            if (trailers.isEmpty()) {
                spdyDataFrame.setLast(true);
                out.add(spdyDataFrame);
            } else {
                // Create SPDY HEADERS frame out of trailers
                SpdyHeadersFrame spdyHeadersFrame = new DefaultSpdyHeadersFrame(currentStreamId, validateHeaders);
                spdyHeadersFrame.setLast(true);
                Iterator<Entry<CharSequence, CharSequence>> itr = trailers.iteratorCharSequence();
                while (itr.hasNext()) {
                    Map.Entry<CharSequence, CharSequence> entry = itr.next();
                    final CharSequence headerName = headersToLowerCase ? AsciiString.of(entry.getKey()).toLowerCase() : entry.getKey();
                    spdyHeadersFrame.headers().add(headerName, entry.getValue());
                }
                // Write DATA frame and append HEADERS frame
                out.add(spdyDataFrame);
                out.add(spdyHeadersFrame);
            }
        } else {
            out.add(spdyDataFrame);
        }
        valid = true;
    }
    if (!valid) {
        throw new UnsupportedMessageTypeException(msg);
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) HttpResponse(io.netty.handler.codec.http.HttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) Entry(java.util.Map.Entry) Map(java.util.Map) HttpContent(io.netty.handler.codec.http.HttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) UnsupportedMessageTypeException(io.netty.handler.codec.UnsupportedMessageTypeException)

Example 3 with UnsupportedMessageTypeException

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

the class SpdyFrameCodec method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    ByteBuf frame;
    if (msg instanceof SpdyDataFrame) {
        SpdyDataFrame spdyDataFrame = (SpdyDataFrame) msg;
        frame = spdyFrameEncoder.encodeDataFrame(ctx.alloc(), spdyDataFrame.streamId(), spdyDataFrame.isLast(), spdyDataFrame.content());
        spdyDataFrame.release();
        ctx.write(frame, promise);
    } else if (msg instanceof SpdySynStreamFrame) {
        SpdySynStreamFrame spdySynStreamFrame = (SpdySynStreamFrame) msg;
        ByteBuf headerBlock = spdyHeaderBlockEncoder.encode(ctx.alloc(), spdySynStreamFrame);
        try {
            frame = spdyFrameEncoder.encodeSynStreamFrame(ctx.alloc(), spdySynStreamFrame.streamId(), spdySynStreamFrame.associatedStreamId(), spdySynStreamFrame.priority(), spdySynStreamFrame.isLast(), spdySynStreamFrame.isUnidirectional(), headerBlock);
        } finally {
            headerBlock.release();
        }
        ctx.write(frame, promise);
    } else if (msg instanceof SpdySynReplyFrame) {
        SpdySynReplyFrame spdySynReplyFrame = (SpdySynReplyFrame) msg;
        ByteBuf headerBlock = spdyHeaderBlockEncoder.encode(ctx.alloc(), spdySynReplyFrame);
        try {
            frame = spdyFrameEncoder.encodeSynReplyFrame(ctx.alloc(), spdySynReplyFrame.streamId(), spdySynReplyFrame.isLast(), headerBlock);
        } finally {
            headerBlock.release();
        }
        ctx.write(frame, promise);
    } else if (msg instanceof SpdyRstStreamFrame) {
        SpdyRstStreamFrame spdyRstStreamFrame = (SpdyRstStreamFrame) msg;
        frame = spdyFrameEncoder.encodeRstStreamFrame(ctx.alloc(), spdyRstStreamFrame.streamId(), spdyRstStreamFrame.status().code());
        ctx.write(frame, promise);
    } else if (msg instanceof SpdySettingsFrame) {
        SpdySettingsFrame spdySettingsFrame = (SpdySettingsFrame) msg;
        frame = spdyFrameEncoder.encodeSettingsFrame(ctx.alloc(), spdySettingsFrame);
        ctx.write(frame, promise);
    } else if (msg instanceof SpdyPingFrame) {
        SpdyPingFrame spdyPingFrame = (SpdyPingFrame) msg;
        frame = spdyFrameEncoder.encodePingFrame(ctx.alloc(), spdyPingFrame.id());
        ctx.write(frame, promise);
    } else if (msg instanceof SpdyGoAwayFrame) {
        SpdyGoAwayFrame spdyGoAwayFrame = (SpdyGoAwayFrame) msg;
        frame = spdyFrameEncoder.encodeGoAwayFrame(ctx.alloc(), spdyGoAwayFrame.lastGoodStreamId(), spdyGoAwayFrame.status().code());
        ctx.write(frame, promise);
    } else if (msg instanceof SpdyHeadersFrame) {
        SpdyHeadersFrame spdyHeadersFrame = (SpdyHeadersFrame) msg;
        ByteBuf headerBlock = spdyHeaderBlockEncoder.encode(ctx.alloc(), spdyHeadersFrame);
        try {
            frame = spdyFrameEncoder.encodeHeadersFrame(ctx.alloc(), spdyHeadersFrame.streamId(), spdyHeadersFrame.isLast(), headerBlock);
        } finally {
            headerBlock.release();
        }
        ctx.write(frame, promise);
    } else if (msg instanceof SpdyWindowUpdateFrame) {
        SpdyWindowUpdateFrame spdyWindowUpdateFrame = (SpdyWindowUpdateFrame) msg;
        frame = spdyFrameEncoder.encodeWindowUpdateFrame(ctx.alloc(), spdyWindowUpdateFrame.streamId(), spdyWindowUpdateFrame.deltaWindowSize());
        ctx.write(frame, promise);
    } else {
        throw new UnsupportedMessageTypeException(msg);
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) UnsupportedMessageTypeException(io.netty.handler.codec.UnsupportedMessageTypeException)

Example 4 with UnsupportedMessageTypeException

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

the class RtspEncoder method encodeInitialLine.

@Override
protected void encodeInitialLine(final ByteBuf buf, final HttpMessage message) throws Exception {
    if (message instanceof HttpRequest) {
        HttpRequest request = (HttpRequest) message;
        ByteBufUtil.copy(request.method().asciiName(), buf);
        buf.writeByte(SP);
        buf.writeCharSequence(request.uri(), CharsetUtil.UTF_8);
        buf.writeByte(SP);
        buf.writeCharSequence(request.protocolVersion().toString(), CharsetUtil.US_ASCII);
        ByteBufUtil.writeShortBE(buf, CRLF_SHORT);
    } else if (message instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) message;
        buf.writeCharSequence(response.protocolVersion().toString(), CharsetUtil.US_ASCII);
        buf.writeByte(SP);
        ByteBufUtil.copy(response.status().codeAsText(), buf);
        buf.writeByte(SP);
        buf.writeCharSequence(response.status().reasonPhrase(), CharsetUtil.US_ASCII);
        ByteBufUtil.writeShortBE(buf, CRLF_SHORT);
    } else {
        throw new UnsupportedMessageTypeException("Unsupported type " + StringUtil.simpleClassName(message));
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpResponse(io.netty.handler.codec.http.HttpResponse) UnsupportedMessageTypeException(io.netty.handler.codec.UnsupportedMessageTypeException)

Example 5 with UnsupportedMessageTypeException

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

the class Http2FrameCodec method write.

/**
 * Processes all {@link Http2Frame}s. {@link Http2StreamFrame}s may only originate in child
 * streams.
 */
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
    if (msg instanceof Http2DataFrame) {
        Http2DataFrame dataFrame = (Http2DataFrame) msg;
        encoder().writeData(ctx, dataFrame.stream().id(), dataFrame.content(), dataFrame.padding(), dataFrame.isEndStream(), promise);
    } else if (msg instanceof Http2HeadersFrame) {
        writeHeadersFrame(ctx, (Http2HeadersFrame) msg, promise);
    } else if (msg instanceof Http2WindowUpdateFrame) {
        Http2WindowUpdateFrame frame = (Http2WindowUpdateFrame) msg;
        Http2FrameStream frameStream = frame.stream();
        // to set the Http2FrameStream so we assume if it is null the WINDOW_UPDATE is for the connection stream.
        try {
            if (frameStream == null) {
                increaseInitialConnectionWindow(frame.windowSizeIncrement());
            } else {
                consumeBytes(frameStream.id(), frame.windowSizeIncrement());
            }
            promise.setSuccess();
        } catch (Throwable t) {
            promise.setFailure(t);
        }
    } else if (msg instanceof Http2ResetFrame) {
        Http2ResetFrame rstFrame = (Http2ResetFrame) msg;
        int id = rstFrame.stream().id();
        // stream in an invalid state and cause a connection error.
        if (connection().streamMayHaveExisted(id)) {
            encoder().writeRstStream(ctx, rstFrame.stream().id(), rstFrame.errorCode(), promise);
        } else {
            ReferenceCountUtil.release(rstFrame);
            promise.setFailure(Http2Exception.streamError(rstFrame.stream().id(), Http2Error.PROTOCOL_ERROR, "Stream never existed"));
        }
    } else if (msg instanceof Http2PingFrame) {
        Http2PingFrame frame = (Http2PingFrame) msg;
        encoder().writePing(ctx, frame.ack(), frame.content(), promise);
    } else if (msg instanceof Http2SettingsFrame) {
        encoder().writeSettings(ctx, ((Http2SettingsFrame) msg).settings(), promise);
    } else if (msg instanceof Http2SettingsAckFrame) {
        // In the event of manual SETTINGS ACK, it is assumed the encoder will apply the earliest received but not
        // yet ACKed settings.
        encoder().writeSettingsAck(ctx, promise);
    } else if (msg instanceof Http2GoAwayFrame) {
        writeGoAwayFrame(ctx, (Http2GoAwayFrame) msg, promise);
    } else if (msg instanceof Http2PushPromiseFrame) {
        Http2PushPromiseFrame pushPromiseFrame = (Http2PushPromiseFrame) msg;
        writePushPromise(ctx, pushPromiseFrame, promise);
    } else if (msg instanceof Http2PriorityFrame) {
        Http2PriorityFrame priorityFrame = (Http2PriorityFrame) msg;
        encoder().writePriority(ctx, priorityFrame.stream().id(), priorityFrame.streamDependency(), priorityFrame.weight(), priorityFrame.exclusive(), promise);
    } else if (msg instanceof Http2UnknownFrame) {
        Http2UnknownFrame unknownFrame = (Http2UnknownFrame) msg;
        encoder().writeFrame(ctx, unknownFrame.frameType(), unknownFrame.stream().id(), unknownFrame.flags(), unknownFrame.content(), promise);
    } else if (!(msg instanceof Http2Frame)) {
        ctx.write(msg, promise);
    } else {
        ReferenceCountUtil.release(msg);
        throw new UnsupportedMessageTypeException(msg);
    }
}
Also used : UnsupportedMessageTypeException(io.netty.handler.codec.UnsupportedMessageTypeException)

Aggregations

UnsupportedMessageTypeException (io.netty.handler.codec.UnsupportedMessageTypeException)6 ByteBuf (io.netty.buffer.ByteBuf)2 HttpRequest (io.netty.handler.codec.http.HttpRequest)2 HttpResponse (io.netty.handler.codec.http.HttpResponse)2 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)1 HttpContent (io.netty.handler.codec.http.HttpContent)1 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)1 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)1 StreamException (io.netty.handler.codec.http2.Http2Exception.StreamException)1 PrimitiveEntry (io.netty.util.collection.IntObjectMap.PrimitiveEntry)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1