Top Description Inners Fields Constructors Methods
io.netty.handler.codec.http2

public Class Http2FrameCodec

extends Http2ConnectionHandler
Class Inheritance
Known Direct Subclasses
io.netty.handler.codec.http2.Http2MultiplexCodec
Annotations
@UnstableApi
Imports
io.netty.buffer.ByteBuf, io.netty.channel.Channel, .ChannelFuture, .ChannelFutureListener, .ChannelHandlerContext, .ChannelInboundHandler, .ChannelPromise, io.netty.handler.codec.UnsupportedMessageTypeException, io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeEvent, io.netty.handler.codec.http2.Http2Connection.PropertyKey, .Http2Stream.State, .StreamBufferingEncoder.Http2ChannelClosedException, .StreamBufferingEncoder.Http2GoAwayException, io.netty.util.ReferenceCountUtil, .ReferenceCounted, io.netty.util.collection.IntObjectHashMap, .IntObjectMap, io.netty.util.internal.UnstableApi, io.netty.util.internal.logging.InternalLogger, .InternalLoggerFactory

This API is very immature. The Http2Connection-based API is currently preferred over this API. This API is targeted to eventually replace or reduce the need for the Http2ConnectionHandler API.

An HTTP/2 handler that maps HTTP/2 frames to Http2Frame objects and vice versa. For every incoming HTTP/2 frame, an Http2Frame object is created and propagated via channelRead. Outbound Http2Frame objects received via write are converted to the HTTP/2 wire format. HTTP/2 frames specific to a stream implement the Http2StreamFrame interface. The Http2FrameCodec is instantiated using the Http2FrameCodecBuilder. It's recommended for channel handlers to inherit from the Http2ChannelDuplexHandler, as it provides additional functionality like iterating over all active streams or creating outbound streams.

Stream Lifecycle

The frame codec delivers and writes frames for active streams. An active stream is closed when either side sends a RST_STREAM frame or both sides send a frame with the END_STREAM flag set. Each Http2StreamFrame has a Http2FrameStream object attached that uniquely identifies a particular stream.

Http2StreamFrames read from the channel always a Http2FrameStream object set, while when writing a Http2StreamFrame the application code needs to set a Http2FrameStream object using Http2StreamFrame#stream(Http2FrameStream).

Flow control

The frame codec automatically increments stream and connection flow control windows.

Incoming flow controlled frames need to be consumed by writing a Http2WindowUpdateFrame with the consumed number of bytes and the corresponding stream identifier set to the frame codec.

The local stream-level flow control window can be changed by writing a Http2SettingsFrame with the Http2Settings#initialWindowSize() set to the targeted value.

The connection-level flow control window can be changed by writing a Http2WindowUpdateFrame with the desired window size increment in bytes and the stream identifier set to 0. By default the initial connection-level flow control window is the same as initial stream-level flow control window.

New inbound Streams

The first frame of an HTTP/2 stream must be an Http2HeadersFrame, which will have an Http2FrameStream object attached.

New outbound Streams

A outbound HTTP/2 stream can be created by first instantiating a new Http2FrameStream object via Http2ChannelDuplexHandler#newStream(), and then writing a Http2HeadersFrame object with the stream attached.

    final Http2Stream2 stream = handler.newStream();
    ctx.write(headersFrame.stream(stream)).addListener(new ChannelFutureListener() {

        

Error Handling

Exceptions and errors are propagated via ChannelInboundHandler#exceptionCaught. Exceptions that apply to a specific HTTP/2 stream are wrapped in a Http2FrameStreamException and have the corresponding Http2FrameStream object attached.

Reference Counting

Some Http2StreamFrames implement the ReferenceCounted interface, as they carry reference counted objects (e.g. ByteBufs). The frame codec will call ReferenceCounted#retain() before propagating a reference counted object through the pipeline, and thus an application handler needs to release such an object after having consumed it. For more information on reference counting take a look at https://netty.io/wiki/reference-counted-objects.html

HTTP Upgrade

Server-side HTTP to HTTP/2 upgrade is supported in conjunction with Http2ServerUpgradeCodec; the necessary HTTP-to-HTTP/2 conversion is performed automatically.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
pack-priv static class
private class
private class

Field Summary

Modifier and TypeField and Description
pack-priv ChannelHandlerContext
private final IntObjectMap<Http2FrameCodec.DefaultHttp2FrameStream>
private final Integer
private static final InternalLogger
private int
numBufferedStreams

Number of buffered streams if the StreamBufferingEncoder is used.

protected final Http2Connection.PropertyKey
private final Http2Connection.PropertyKey

Constructor Summary

AccessConstructor and Description
pack-priv
Http2FrameCodec(Http2ConnectionEncoder encoder, Http2ConnectionDecoder decoder, Http2Settings initialSettings, boolean decoupleCloseAndGoAway, boolean flushPreface)

Method Summary

Modifier and TypeMethod and Description
pack-priv final boolean
consumeBytes(int streamId, int bytes)

pack-priv final void
forEachActiveStream(final Http2FrameStreamVisitor streamVisitor)

Iterates over all active HTTP/2 streams.

private void
handleHeaderFuture(ChannelFuture channelFuture, int streamId)

public final void
handlerAdded(ChannelHandlerContext ctx)

Overrides io.netty.handler.codec.http2.Http2ConnectionHandler.handlerAdded.

Implements io.netty.channel.ChannelHandler.handlerAdded.

Gets called after the ChannelHandler was added to the actual context and it's ready to handle events.
pack-priv void
private void
private boolean
protected final boolean
isGracefulShutdownComplete()

Overrides io.netty.handler.codec.http2.Http2ConnectionHandler.isGracefulShutdownComplete.

Called by the graceful shutdown logic to determine when it is safe to close the connection.
pack-priv Http2FrameCodec.DefaultHttp2FrameStream
newStream()

Creates a new outbound/local stream.

pack-priv int
numInitializingStreams()

Retrieve the number of streams currently in the process of being initialized.

protected void
onConnectionError(ChannelHandlerContext
the channel context
ctx
,
boolean
true if the error was caused by an outbound operation.
outbound
,
Throwable
the exception that was caught
cause
,
Http2Exception
the Http2Exception that is embedded in the causality chain. This may be null if it's an unknown exception.
http2Ex
)

Overrides io.netty.handler.codec.http2.Http2ConnectionHandler.onConnectionError.

Handler for a connection error.
pack-priv void
pack-priv void
pack-priv void
private void
private static void
private void
protected final void
onStreamError(ChannelHandlerContext
the channel context
ctx
,
boolean
true if the error was caused by an outbound operation.
outbound
,
Throwable
the exception that was caught
cause
,
Http2Exception.StreamException
the StreamException that is embedded in the causality chain.
streamException
)

Overrides io.netty.handler.codec.http2.Http2ConnectionHandler.onStreamError.

Exceptions for unknown streams, that is streams that have no Http2FrameStream object attached are simply logged and replied to by sending a RST_STREAM frame.
private void
pack-priv void
private void
public final void
public void
write(ChannelHandlerContext
the ChannelHandlerContext for which the write operation is made
ctx
,
Object
the message to write
msg
,
ChannelPromise
the ChannelPromise to notify once the operation completes
promise
)

Overrides io.netty.handler.codec.http2.Http2ConnectionHandler.write.

Implements io.netty.channel.ChannelOutboundHandler.write.

Processes all Http2Frames.
private void
private void
private void
writePushPromise(final ChannelHandlerContext ctx, Http2PushPromiseFrame pushPromiseFrame, final ChannelPromise promise)

Inherited from io.netty.handler.codec.http2.Http2ConnectionHandler:
bindchannelActivechannelInactivechannelReadCompletechannelReadComplete0channelWritabilityChangedclosecloseStreamcloseStreamLocalcloseStreamRemoteconnectconnectiondecodedecoderderegisterdisconnectencoderexceptionCaughtflushframeWritergoAwaygracefulShutdownTimeoutMillisgracefulShutdownTimeoutMillishandlerRemoved0handleServerHeaderDecodeSizeErroronErroronHttpClientUpgradeonHttpServerUpgradereadresetStream