@UnstableApi public class Http2ConnectionHandler extends ByteToMessageDecoder implements Http2LifecycleManager, ChannelOutboundHandler
Http2FrameListener
This class will read HTTP/2 frames and delegate the events to a Http2FrameListener
This interface enforces inbound flow control functionality through
Http2LocalFlowController
| Modifier and Type | Class and Description |
|---|---|
private class |
Http2ConnectionHandler.BaseDecoder |
private static class |
Http2ConnectionHandler.ClosingChannelFutureListener
Closes the channel when the future completes.
|
private class |
Http2ConnectionHandler.FrameDecoder |
private class |
Http2ConnectionHandler.PrefaceDecoder |
ByteToMessageDecoder.CumulatorChannelHandler.Sharable| Modifier and Type | Field and Description |
|---|---|
private Http2ConnectionHandler.BaseDecoder |
byteDecoder |
private ChannelFutureListener |
closeListener |
private Http2ConnectionDecoder |
decoder |
private Http2ConnectionEncoder |
encoder |
private long |
gracefulShutdownTimeoutMillis |
private static Http2Headers |
HEADERS_TOO_LARGE_HEADERS |
private static ByteBuf |
HTTP_1_X_BUF |
private Http2Settings |
initialSettings |
private static InternalLogger |
logger |
COMPOSITE_CUMULATOR, MERGE_CUMULATOR| Modifier | Constructor and Description |
|---|---|
protected |
Http2ConnectionHandler(Http2ConnectionDecoder decoder,
Http2ConnectionEncoder encoder,
Http2Settings initialSettings) |
| Modifier and Type | Method and Description |
|---|---|
void |
bind(ChannelHandlerContext ctx,
java.net.SocketAddress localAddress,
ChannelPromise promise)
Called once a bind operation is made.
|
void |
channelActive(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelActive() to forward
to the next ChannelInboundHandler in the ChannelPipeline. |
void |
channelInactive(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelInactive() to forward
to the next ChannelInboundHandler in the ChannelPipeline. |
void |
channelReadComplete(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelReadComplete() to forward
to the next ChannelInboundHandler in the ChannelPipeline. |
void |
channelWritabilityChanged(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelWritabilityChanged() to forward
to the next ChannelInboundHandler in the ChannelPipeline. |
private void |
checkCloseConnection(ChannelFuture future)
Closes the connection if the graceful shutdown process has completed.
|
private static ByteBuf |
clientPrefaceString(Http2Connection connection)
Returns the client preface string if this is a client connection, otherwise returns
null. |
void |
close(ChannelHandlerContext ctx,
ChannelPromise promise)
Called once a close operation is made.
|
private void |
closeConnectionOnError(ChannelHandlerContext ctx,
ChannelFuture future) |
void |
closeStream(Http2Stream stream,
ChannelFuture future)
Closes and deactivates the given
stream. |
void |
closeStreamLocal(Http2Stream stream,
ChannelFuture future)
Closes the local side of the given stream.
|
void |
closeStreamRemote(Http2Stream stream,
ChannelFuture future)
Closes the remote side of the given stream.
|
void |
connect(ChannelHandlerContext ctx,
java.net.SocketAddress remoteAddress,
java.net.SocketAddress localAddress,
ChannelPromise promise)
Called once a connect operation is made.
|
Http2Connection |
connection() |
protected void |
decode(ChannelHandlerContext ctx,
ByteBuf in,
java.util.List<java.lang.Object> out)
Decode the from one
ByteBuf to an other. |
Http2ConnectionDecoder |
decoder() |
void |
deregister(ChannelHandlerContext ctx,
ChannelPromise promise)
Called once a deregister operation is made from the current registered
EventLoop. |
void |
disconnect(ChannelHandlerContext ctx,
ChannelPromise promise)
Called once a disconnect operation is made.
|
private void |
doGracefulShutdown(ChannelHandlerContext ctx,
ChannelFuture future,
ChannelPromise promise) |
Http2ConnectionEncoder |
encoder() |
void |
exceptionCaught(ChannelHandlerContext ctx,
java.lang.Throwable cause)
Handles
Http2Exception objects that were thrown from other handlers. |
void |
flush(ChannelHandlerContext ctx)
Called once a flush operation is made.
|
protected Http2FrameWriter |
frameWriter() |
private ChannelFuture |
goAway(ChannelHandlerContext ctx,
Http2Exception cause)
Close the remote endpoint with with a
GO_AWAY frame. |
ChannelFuture |
goAway(ChannelHandlerContext ctx,
int lastStreamId,
long errorCode,
ByteBuf debugData,
ChannelPromise promise)
Prevents the peer from creating streams and close the connection if
errorCode is not
Http2Error.NO_ERROR. |
long |
gracefulShutdownTimeoutMillis()
Get the amount of time (in milliseconds) this endpoint will wait for all streams to be closed before closing
the connection during the graceful shutdown process.
|
void |
gracefulShutdownTimeoutMillis(long gracefulShutdownTimeoutMillis)
Set the amount of time (in milliseconds) this endpoint will wait for all streams to be closed before closing
the connection during the graceful shutdown process.
|
void |
handlerAdded(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.
|
protected void |
handlerRemoved0(ChannelHandlerContext ctx)
Gets called after the
ByteToMessageDecoder was removed from the actual context and it doesn't handle
events anymore. |
protected void |
handleServerHeaderDecodeSizeError(ChannelHandlerContext ctx,
Http2Stream stream)
Notifies client that this server has received headers that are larger than what it is
willing to accept.
|
protected boolean |
isGracefulShutdownComplete()
Called by the graceful shutdown logic to determine when it is safe to close the connection.
|
protected void |
onConnectionError(ChannelHandlerContext ctx,
java.lang.Throwable cause,
Http2Exception http2Ex)
Handler for a connection error.
|
void |
onError(ChannelHandlerContext ctx,
java.lang.Throwable cause)
Central handler for all exceptions caught during HTTP/2 processing.
|
void |
onHttpClientUpgrade()
Handles the client-side (cleartext) upgrade from HTTP to HTTP/2.
|
void |
onHttpServerUpgrade(Http2Settings settings)
Handles the server-side (cleartext) upgrade from HTTP to HTTP/2.
|
protected void |
onStreamError(ChannelHandlerContext ctx,
java.lang.Throwable cause,
Http2Exception.StreamException http2Ex)
Handler for a stream error.
|
private boolean |
prefaceSent() |
private static void |
processGoAwayWriteResult(ChannelHandlerContext ctx,
int lastStreamId,
long errorCode,
ByteBuf debugData,
ChannelFuture future) |
private void |
processRstStreamWriteResult(ChannelHandlerContext ctx,
Http2Stream stream,
ChannelFuture future) |
void |
read(ChannelHandlerContext ctx)
Intercepts
ChannelHandlerContext.read(). |
private ChannelFuture |
resetStream(ChannelHandlerContext ctx,
Http2Stream stream,
long errorCode,
ChannelPromise promise) |
ChannelFuture |
resetStream(ChannelHandlerContext ctx,
int streamId,
long errorCode,
ChannelPromise promise)
Ensure the stream identified by
streamId is reset. |
private ChannelFuture |
resetUnknownStream(ChannelHandlerContext ctx,
int streamId,
long errorCode,
ChannelPromise promise)
Sends a
RST_STREAM frame even if we don't know about the stream. |
void |
write(ChannelHandlerContext ctx,
java.lang.Object msg,
ChannelPromise promise)
Called once a write operation is made.
|
actualReadableBytes, callDecode, channelRead, decodeLast, discardSomeReadBytes, handlerRemoved, internalBuffer, isSingleDecode, setCumulator, setDiscardAfterReads, setSingleDecode, userEventTriggeredchannelRegistered, channelUnregisteredensureNotSharable, isSharableclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waithandlerRemovedprivate static final InternalLogger logger
private static final Http2Headers HEADERS_TOO_LARGE_HEADERS
private static final ByteBuf HTTP_1_X_BUF
private final Http2ConnectionDecoder decoder
private final Http2ConnectionEncoder encoder
private final Http2Settings initialSettings
private ChannelFutureListener closeListener
private Http2ConnectionHandler.BaseDecoder byteDecoder
private long gracefulShutdownTimeoutMillis
protected Http2ConnectionHandler(Http2ConnectionDecoder decoder, Http2ConnectionEncoder encoder, Http2Settings initialSettings)
public long gracefulShutdownTimeoutMillis()
public void gracefulShutdownTimeoutMillis(long gracefulShutdownTimeoutMillis)
gracefulShutdownTimeoutMillis - the amount of time (in milliseconds) this endpoint will wait for all
streams to be closed before closing the connection during the graceful shutdown process.public Http2Connection connection()
public Http2ConnectionDecoder decoder()
public Http2ConnectionEncoder encoder()
private boolean prefaceSent()
public void onHttpClientUpgrade()
throws Http2Exception
Http2Exceptionpublic void onHttpServerUpgrade(Http2Settings settings) throws Http2Exception
settings - the settings for the remote endpoint.Http2Exceptionpublic void flush(ChannelHandlerContext ctx) throws Http2Exception
ChannelOutboundHandlerflush in interface ChannelOutboundHandlerctx - the ChannelHandlerContext for which the flush operation is madeHttp2Exceptionpublic void handlerAdded(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelHandlerAdapterhandlerAdded in interface ChannelHandlerhandlerAdded in class ChannelHandlerAdapterjava.lang.Exceptionprotected void handlerRemoved0(ChannelHandlerContext ctx) throws java.lang.Exception
ByteToMessageDecoderByteToMessageDecoder was removed from the actual context and it doesn't handle
events anymore.handlerRemoved0 in class ByteToMessageDecoderjava.lang.Exceptionpublic void channelActive(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelInboundHandlerAdapterChannelHandlerContext.fireChannelActive() to forward
to the next ChannelInboundHandler in the ChannelPipeline.
Sub-classes may override this method to change behavior.channelActive in interface ChannelInboundHandlerchannelActive in class ChannelInboundHandlerAdapterjava.lang.Exceptionpublic void channelInactive(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelInboundHandlerAdapterChannelHandlerContext.fireChannelInactive() to forward
to the next ChannelInboundHandler in the ChannelPipeline.
Sub-classes may override this method to change behavior.channelInactive in interface ChannelInboundHandlerchannelInactive in class ByteToMessageDecoderjava.lang.Exceptionpublic void channelWritabilityChanged(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelInboundHandlerAdapterChannelHandlerContext.fireChannelWritabilityChanged() to forward
to the next ChannelInboundHandler in the ChannelPipeline.
Sub-classes may override this method to change behavior.channelWritabilityChanged in interface ChannelInboundHandlerchannelWritabilityChanged in class ChannelInboundHandlerAdapterjava.lang.Exceptionprotected void decode(ChannelHandlerContext ctx, ByteBuf in, java.util.List<java.lang.Object> out) throws java.lang.Exception
ByteToMessageDecoderByteBuf to an other. This method will be called till either the input
ByteBuf has nothing to read when return from this method or till nothing was read from the input
ByteBuf.decode in class ByteToMessageDecoderctx - the ChannelHandlerContext which this ByteToMessageDecoder belongs toin - the ByteBuf from which to read dataout - the List to which decoded messages should be addedjava.lang.Exception - is thrown if an error occurspublic void bind(ChannelHandlerContext ctx, java.net.SocketAddress localAddress, ChannelPromise promise) throws java.lang.Exception
ChannelOutboundHandlerbind in interface ChannelOutboundHandlerctx - the ChannelHandlerContext for which the bind operation is madelocalAddress - the SocketAddress to which it should boundpromise - the ChannelPromise to notify once the operation completesjava.lang.Exception - thrown if an error occurspublic void connect(ChannelHandlerContext ctx, java.net.SocketAddress remoteAddress, java.net.SocketAddress localAddress, ChannelPromise promise) throws java.lang.Exception
ChannelOutboundHandlerconnect in interface ChannelOutboundHandlerctx - the ChannelHandlerContext for which the connect operation is maderemoteAddress - the SocketAddress to which it should connectlocalAddress - the SocketAddress which is used as source on connectpromise - the ChannelPromise to notify once the operation completesjava.lang.Exception - thrown if an error occurspublic void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws java.lang.Exception
ChannelOutboundHandlerdisconnect in interface ChannelOutboundHandlerctx - the ChannelHandlerContext for which the disconnect operation is madepromise - the ChannelPromise to notify once the operation completesjava.lang.Exception - thrown if an error occurspublic void close(ChannelHandlerContext ctx, ChannelPromise promise) throws java.lang.Exception
ChannelOutboundHandlerclose in interface ChannelOutboundHandlerctx - the ChannelHandlerContext for which the close operation is madepromise - the ChannelPromise to notify once the operation completesjava.lang.Exception - thrown if an error occursprivate void doGracefulShutdown(ChannelHandlerContext ctx, ChannelFuture future, ChannelPromise promise)
public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws java.lang.Exception
ChannelOutboundHandlerEventLoop.deregister in interface ChannelOutboundHandlerctx - the ChannelHandlerContext for which the close operation is madepromise - the ChannelPromise to notify once the operation completesjava.lang.Exception - thrown if an error occurspublic void read(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelOutboundHandlerChannelHandlerContext.read().read in interface ChannelOutboundHandlerjava.lang.Exceptionpublic void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
ChannelOutboundHandlerChannelPipeline. Those are then ready to be flushed to the actual Channel once
Channel.flush() is calledwrite in interface ChannelOutboundHandlerctx - the ChannelHandlerContext for which the write operation is mademsg - the message to writepromise - the ChannelPromise to notify once the operation completesjava.lang.Exception - thrown if an error occurspublic void channelReadComplete(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelInboundHandlerAdapterChannelHandlerContext.fireChannelReadComplete() to forward
to the next ChannelInboundHandler in the ChannelPipeline.
Sub-classes may override this method to change behavior.channelReadComplete in interface ChannelInboundHandlerchannelReadComplete in class ByteToMessageDecoderjava.lang.Exceptionpublic void exceptionCaught(ChannelHandlerContext ctx, java.lang.Throwable cause) throws java.lang.Exception
Http2Exception objects that were thrown from other handlers. Ignores all other exceptions.exceptionCaught in interface ChannelHandlerexceptionCaught in interface ChannelInboundHandlerexceptionCaught in class ChannelInboundHandlerAdapterjava.lang.Exceptionpublic void closeStreamLocal(Http2Stream stream, ChannelFuture future)
closeStreamLocal in interface Http2LifecycleManagerstream - the stream to be half closed.future - If closing, the future after which to close the channel.public void closeStreamRemote(Http2Stream stream, ChannelFuture future)
closeStreamRemote in interface Http2LifecycleManagerstream - the stream to be half closed.future - If closing, the future after which to close the channel.public void closeStream(Http2Stream stream, ChannelFuture future)
Http2LifecycleManagerstream. A listener is also attached to future and upon
completion the underlying channel will be closed if Http2Connection.numActiveStreams() is 0.closeStream in interface Http2LifecycleManagerstream - the stream to be closed and deactivated.future - when completed if Http2Connection.numActiveStreams() is 0 then the underlying channel
will be closed.public void onError(ChannelHandlerContext ctx, java.lang.Throwable cause)
onError in interface Http2LifecycleManagerprotected boolean isGracefulShutdownComplete()
true
if the graceful shutdown has completed and the connection can be safely closed. This implementation just
guarantees that there are no active streams. Subclasses may override to provide additional checks.protected void onConnectionError(ChannelHandlerContext ctx, java.lang.Throwable cause, Http2Exception http2Ex)
ctx - the channel contextcause - the exception that was caughthttp2Ex - the Http2Exception that is embedded in the causality chain. This may
be null if it's an unknown exception.protected void onStreamError(ChannelHandlerContext ctx, java.lang.Throwable cause, Http2Exception.StreamException http2Ex)
RST_STREAM frame to the remote endpoint and closes the
stream.ctx - the channel contextcause - the exception that was caughthttp2Ex - the Http2Exception.StreamException that is embedded in the causality chain.protected void handleServerHeaderDecodeSizeError(ChannelHandlerContext ctx, Http2Stream stream)
ctx - the channel contextstream - the Http2Stream on which the header was receivedprotected Http2FrameWriter frameWriter()
private ChannelFuture resetUnknownStream(ChannelHandlerContext ctx, int streamId, long errorCode, ChannelPromise promise)
RST_STREAM frame even if we don't know about the stream. This error condition is most likely
triggered by the first frame of a stream being invalid. That is, there was an error reading the frame before
we could create a new stream.public ChannelFuture resetStream(ChannelHandlerContext ctx, int streamId, long errorCode, ChannelPromise promise)
Http2LifecycleManagerstreamId is reset. If our local state does not indicate the stream has
been reset yet then a RST_STREAM will be sent to the peer. If our local state indicates the stream
has already been reset then the return status will indicate success without sending anything to the peer.resetStream in interface Http2LifecycleManagerctx - The context used for communication and buffer allocation if necessary.streamId - The identifier of the stream to reset.errorCode - Justification as to why this stream is being reset. See Http2Error.promise - Used to indicate the return status of this operation.RST_STREAM frame has been sent to the peer. If the stream state has already been updated and a
RST_STREAM frame has been sent then the return status may indicate success immediately.private ChannelFuture resetStream(ChannelHandlerContext ctx, Http2Stream stream, long errorCode, ChannelPromise promise)
public ChannelFuture goAway(ChannelHandlerContext ctx, int lastStreamId, long errorCode, ByteBuf debugData, ChannelPromise promise)
Http2LifecycleManagererrorCode is not
Http2Error.NO_ERROR. After this call the peer is not allowed to create any new streams and the local
endpoint will be limited to creating streams with stream identifier <= lastStreamId. This may result in
sending a GO_AWAY frame (assuming we have not already sent one with
Last-Stream-ID <= lastStreamId), or may just return success if a GO_AWAY has previously been
sent.goAway in interface Http2LifecycleManagerctx - The context used for communication and buffer allocation if necessary.lastStreamId - The last stream that the local endpoint is claiming it will accept.errorCode - The rational as to why the connection is being closed. See Http2Error.debugData - For diagnostic purposes (carries no semantic value).promise - Used to indicate the return status of this operation.GO_AWAY frame has been sent to the peer. If the stream state has already been updated and a
GO_AWAY frame has been sent then the return status may indicate success immediately.private void checkCloseConnection(ChannelFuture future)
future - Represents the status that will be passed to the closeListener.private ChannelFuture goAway(ChannelHandlerContext ctx, Http2Exception cause)
GO_AWAY frame. Does not flush
immediately, this is the responsibility of the caller.private void processRstStreamWriteResult(ChannelHandlerContext ctx, Http2Stream stream, ChannelFuture future)
private void closeConnectionOnError(ChannelHandlerContext ctx, ChannelFuture future)
private static ByteBuf clientPrefaceString(Http2Connection connection)
null.private static void processGoAwayWriteResult(ChannelHandlerContext ctx, int lastStreamId, long errorCode, ByteBuf debugData, ChannelFuture future)