Package io.netty.handler.codec.http2
Class Http2ConnectionHandler
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.handler.codec.ByteToMessageDecoder
io.netty.handler.codec.http2.Http2ConnectionHandler
- All Implemented Interfaces:
ChannelHandler
,ChannelInboundHandler
,ChannelOutboundHandler
,Http2LifecycleManager
- Direct Known Subclasses:
Http2FrameCodec
,HttpToHttp2ConnectionHandler
@UnstableApi
public class Http2ConnectionHandler
extends ByteToMessageDecoder
implements Http2LifecycleManager, ChannelOutboundHandler
Provides the default implementation for processing inbound frame events and delegates to a
Http2FrameListener
This class will read HTTP/2 frames and delegate the events to a Http2FrameListener
This interface enforces inbound flow control functionality through
Http2LocalFlowController
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate class
private static final class
Closes the channel when the future completes.private final class
private final class
Nested classes/interfaces inherited from class io.netty.handler.codec.ByteToMessageDecoder
ByteToMessageDecoder.Cumulator
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ChannelFutureListener
private final Http2ConnectionDecoder
private final boolean
private final Http2ConnectionEncoder
private final boolean
private long
private static final Http2Headers
private static final ByteBuf
private final Http2Settings
private static final InternalLogger
Fields inherited from class io.netty.handler.codec.ByteToMessageDecoder
COMPOSITE_CUMULATOR, MERGE_CUMULATOR
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
Http2ConnectionHandler
(Http2ConnectionDecoder decoder, Http2ConnectionEncoder encoder, Http2Settings initialSettings) protected
Http2ConnectionHandler
(Http2ConnectionDecoder decoder, Http2ConnectionEncoder encoder, Http2Settings initialSettings, boolean decoupleCloseAndGoAway) protected
Http2ConnectionHandler
(Http2ConnectionDecoder decoder, Http2ConnectionEncoder encoder, Http2Settings initialSettings, boolean decoupleCloseAndGoAway, boolean flushPreface) -
Method Summary
Modifier and TypeMethodDescriptionvoid
bind
(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) Called once a bind operation is made.void
CallsChannelHandlerContext.fireChannelActive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.void
CallsChannelHandlerContext.fireChannelInactive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.void
CallsChannelHandlerContext.fireChannelReadComplete()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.(package private) final void
void
CallsChannelHandlerContext.fireChannelWritabilityChanged()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.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 returnsnull
.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 givenstream
.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, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) Called once a connect operation is made.protected void
decode
(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) Decode the from oneByteBuf
to an other.decoder()
void
deregister
(ChannelHandlerContext ctx, ChannelPromise promise) Called once a deregister operation is made from the current registeredEventLoop
.void
disconnect
(ChannelHandlerContext ctx, ChannelPromise promise) Called once a disconnect operation is made.private void
doCloseStream
(Http2Stream stream, ChannelFuture future) private void
doGracefulShutdown
(ChannelHandlerContext ctx, ChannelFuture future, ChannelPromise promise) encoder()
void
exceptionCaught
(ChannelHandlerContext ctx, Throwable cause) HandlesHttp2Exception
objects that were thrown from other handlers.void
Called once a flush operation is made.protected Http2FrameWriter
goAway
(ChannelHandlerContext ctx, int lastStreamId, long errorCode, ByteBuf debugData, ChannelPromise promise) Prevents the peer from creating streams and close the connection iferrorCode
is notHttp2Error.NO_ERROR
.private ChannelFuture
goAway
(ChannelHandlerContext ctx, Http2Exception cause, ChannelPromise promise) Close the remote endpoint with aGO_AWAY
frame.long
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
Do nothing by default, sub-classes may override this method.protected void
Gets called after theByteToMessageDecoder
was removed from the actual context and it doesn't handle events anymore.protected void
Notifies client that this server has received headers that are larger than what it is willing to accept.protected boolean
Called by the graceful shutdown logic to determine when it is safe to close the connection.private ChannelFutureListener
protected void
onConnectionError
(ChannelHandlerContext ctx, boolean outbound, Throwable cause, Http2Exception http2Ex) Handler for a connection error.void
onError
(ChannelHandlerContext ctx, boolean outbound, Throwable cause) Central handler for all exceptions caught during HTTP/2 processing.void
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, boolean outbound, Throwable cause, Http2Exception.StreamException http2Ex) Handler for a stream error.private boolean
private static void
processGoAwayWriteResult
(ChannelHandlerContext ctx, int lastStreamId, long errorCode, ByteBuf debugData, ChannelFuture future) private void
processRstStreamWriteResult
(ChannelHandlerContext ctx, Http2Stream stream, ChannelFuture future) void
InterceptsChannelHandlerContext.read()
.resetStream
(ChannelHandlerContext ctx, int streamId, long errorCode, ChannelPromise promise) Ensure the stream identified bystreamId
is reset.private ChannelFuture
resetStream
(ChannelHandlerContext ctx, Http2Stream stream, long errorCode, ChannelPromise promise) private ChannelFuture
resetUnknownStream
(ChannelHandlerContext ctx, int streamId, long errorCode, ChannelPromise promise) Sends aRST_STREAM
frame even if we don't know about the stream.void
write
(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) Called once a write operation is made.Methods inherited from class io.netty.handler.codec.ByteToMessageDecoder
actualReadableBytes, callDecode, channelRead, decodeLast, discardSomeReadBytes, handlerRemoved, internalBuffer, isSingleDecode, setCumulator, setDiscardAfterReads, setSingleDecode, userEventTriggered
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelRegistered, channelUnregistered
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, isSharable
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.netty.channel.ChannelHandler
handlerRemoved
-
Field Details
-
logger
-
HEADERS_TOO_LARGE_HEADERS
-
HTTP_1_X_BUF
-
decoder
-
encoder
-
initialSettings
-
decoupleCloseAndGoAway
private final boolean decoupleCloseAndGoAway -
flushPreface
private final boolean flushPreface -
closeListener
-
byteDecoder
-
gracefulShutdownTimeoutMillis
private long gracefulShutdownTimeoutMillis
-
-
Constructor Details
-
Http2ConnectionHandler
protected Http2ConnectionHandler(Http2ConnectionDecoder decoder, Http2ConnectionEncoder encoder, Http2Settings initialSettings) -
Http2ConnectionHandler
protected Http2ConnectionHandler(Http2ConnectionDecoder decoder, Http2ConnectionEncoder encoder, Http2Settings initialSettings, boolean decoupleCloseAndGoAway) -
Http2ConnectionHandler
protected Http2ConnectionHandler(Http2ConnectionDecoder decoder, Http2ConnectionEncoder encoder, Http2Settings initialSettings, boolean decoupleCloseAndGoAway, boolean flushPreface)
-
-
Method Details
-
gracefulShutdownTimeoutMillis
public 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. Returns -1 if this connection is configured to wait indefinitely for all streams to close. -
gracefulShutdownTimeoutMillis
public 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.- Parameters:
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.
-
connection
-
decoder
-
encoder
-
prefaceSent
private boolean prefaceSent() -
onHttpClientUpgrade
Handles the client-side (cleartext) upgrade from HTTP to HTTP/2. Reserves local stream 1 for the HTTP/2 response.- Throws:
Http2Exception
-
onHttpServerUpgrade
Handles the server-side (cleartext) upgrade from HTTP to HTTP/2.- Parameters:
settings
- the settings for the remote endpoint.- Throws:
Http2Exception
-
flush
Description copied from interface:ChannelOutboundHandler
Called once a flush operation is made. The flush operation will try to flush out all previous written messages that are pending.- Specified by:
flush
in interfaceChannelOutboundHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the flush operation is made
-
handlerAdded
Description copied from class:ChannelHandlerAdapter
Do nothing by default, sub-classes may override this method.- Specified by:
handlerAdded
in interfaceChannelHandler
- Overrides:
handlerAdded
in classChannelHandlerAdapter
- Throws:
Exception
-
handlerRemoved0
Description copied from class:ByteToMessageDecoder
Gets called after theByteToMessageDecoder
was removed from the actual context and it doesn't handle events anymore.- Overrides:
handlerRemoved0
in classByteToMessageDecoder
- Throws:
Exception
-
channelActive
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelActive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelActive
in interfaceChannelInboundHandler
- Overrides:
channelActive
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
channelInactive
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelInactive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelInactive
in interfaceChannelInboundHandler
- Overrides:
channelInactive
in classByteToMessageDecoder
- Throws:
Exception
-
channelWritabilityChanged
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelWritabilityChanged()
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelWritabilityChanged
in interfaceChannelInboundHandler
- Overrides:
channelWritabilityChanged
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
decode
Description copied from class:ByteToMessageDecoder
Decode the from oneByteBuf
to an other. This method will be called till either the inputByteBuf
has nothing to read when return from this method or till nothing was read from the inputByteBuf
.- Specified by:
decode
in classByteToMessageDecoder
- Parameters:
ctx
- theChannelHandlerContext
which thisByteToMessageDecoder
belongs toin
- theByteBuf
from which to read dataout
- theList
to which decoded messages should be added- Throws:
Exception
- is thrown if an error occurs
-
bind
public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception Description copied from interface:ChannelOutboundHandler
Called once a bind operation is made.- Specified by:
bind
in interfaceChannelOutboundHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the bind operation is madelocalAddress
- theSocketAddress
to which it should boundpromise
- theChannelPromise
to notify once the operation completes- Throws:
Exception
- thrown if an error occurs
-
connect
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception Description copied from interface:ChannelOutboundHandler
Called once a connect operation is made.- Specified by:
connect
in interfaceChannelOutboundHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the connect operation is maderemoteAddress
- theSocketAddress
to which it should connectlocalAddress
- theSocketAddress
which is used as source on connectpromise
- theChannelPromise
to notify once the operation completes- Throws:
Exception
- thrown if an error occurs
-
disconnect
Description copied from interface:ChannelOutboundHandler
Called once a disconnect operation is made.- Specified by:
disconnect
in interfaceChannelOutboundHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the disconnect operation is madepromise
- theChannelPromise
to notify once the operation completes- Throws:
Exception
- thrown if an error occurs
-
close
Description copied from interface:ChannelOutboundHandler
Called once a close operation is made.- Specified by:
close
in interfaceChannelOutboundHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the close operation is madepromise
- theChannelPromise
to notify once the operation completes- Throws:
Exception
- thrown if an error occurs
-
newClosingChannelFutureListener
private ChannelFutureListener newClosingChannelFutureListener(ChannelHandlerContext ctx, ChannelPromise promise) -
doGracefulShutdown
private void doGracefulShutdown(ChannelHandlerContext ctx, ChannelFuture future, ChannelPromise promise) -
deregister
Description copied from interface:ChannelOutboundHandler
Called once a deregister operation is made from the current registeredEventLoop
.- Specified by:
deregister
in interfaceChannelOutboundHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the close operation is madepromise
- theChannelPromise
to notify once the operation completes- Throws:
Exception
- thrown if an error occurs
-
read
Description copied from interface:ChannelOutboundHandler
InterceptsChannelHandlerContext.read()
.- Specified by:
read
in interfaceChannelOutboundHandler
- Throws:
Exception
-
write
Description copied from interface:ChannelOutboundHandler
Called once a write operation is made. The write operation will write the messages through theChannelPipeline
. Those are then ready to be flushed to the actualChannel
onceChannel.flush()
is called- Specified by:
write
in interfaceChannelOutboundHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the write operation is mademsg
- the message to writepromise
- theChannelPromise
to notify once the operation completes- Throws:
Exception
- thrown if an error occurs
-
channelReadComplete
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelReadComplete()
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelReadComplete
in interfaceChannelInboundHandler
- Overrides:
channelReadComplete
in classByteToMessageDecoder
- Throws:
Exception
-
channelReadComplete0
-
exceptionCaught
HandlesHttp2Exception
objects that were thrown from other handlers. Ignores all other exceptions.- Specified by:
exceptionCaught
in interfaceChannelHandler
- Specified by:
exceptionCaught
in interfaceChannelInboundHandler
- Overrides:
exceptionCaught
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
closeStreamLocal
Closes the local side of the given stream. If this causes the stream to be closed, adds a hook to close the channel after the given future completes.- Specified by:
closeStreamLocal
in interfaceHttp2LifecycleManager
- Parameters:
stream
- the stream to be half closed.future
- If closing, the future after which to close the channel.
-
closeStreamRemote
Closes the remote side of the given stream. If this causes the stream to be closed, adds a hook to close the channel after the given future completes.- Specified by:
closeStreamRemote
in interfaceHttp2LifecycleManager
- Parameters:
stream
- the stream to be half closed.future
- If closing, the future after which to close the channel.
-
closeStream
Description copied from interface:Http2LifecycleManager
Closes and deactivates the givenstream
. A listener is also attached tofuture
and upon completion the underlying channel will be closed ifHttp2Connection.numActiveStreams()
is 0.- Specified by:
closeStream
in interfaceHttp2LifecycleManager
- Parameters:
stream
- the stream to be closed and deactivated.future
- when completed ifHttp2Connection.numActiveStreams()
is 0 then the underlying channel will be closed.
-
onError
Central handler for all exceptions caught during HTTP/2 processing.- Specified by:
onError
in interfaceHttp2LifecycleManager
- Parameters:
ctx
- The context used for communication and buffer allocation if necessary.outbound
-true
if the error was caused by an outbound operation and so the correspondingChannelPromise
was failed as well.cause
- the error.
-
isGracefulShutdownComplete
protected boolean isGracefulShutdownComplete()Called by the graceful shutdown logic to determine when it is safe to close the connection. Returnstrue
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. -
onConnectionError
protected void onConnectionError(ChannelHandlerContext ctx, boolean outbound, Throwable cause, Http2Exception http2Ex) Handler for a connection error. Sends a GO_AWAY frame to the remote endpoint. Once all streams are closed, the connection is shut down.- Parameters:
ctx
- the channel contextoutbound
-true
if the error was caused by an outbound operation.cause
- the exception that was caughthttp2Ex
- theHttp2Exception
that is embedded in the causality chain. This may benull
if it's an unknown exception.
-
onStreamError
protected void onStreamError(ChannelHandlerContext ctx, boolean outbound, Throwable cause, Http2Exception.StreamException http2Ex) Handler for a stream error. Sends aRST_STREAM
frame to the remote endpoint and closes the stream.- Parameters:
ctx
- the channel contextoutbound
-true
if the error was caused by an outbound operation.cause
- the exception that was caughthttp2Ex
- theHttp2Exception.StreamException
that is embedded in the causality chain.
-
handleServerHeaderDecodeSizeError
Notifies client that this server has received headers that are larger than what it is willing to accept. Override to change behavior.- Parameters:
ctx
- the channel contextstream
- the Http2Stream on which the header was received
-
frameWriter
-
resetUnknownStream
private ChannelFuture resetUnknownStream(ChannelHandlerContext ctx, int streamId, long errorCode, ChannelPromise promise) Sends aRST_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. -
resetStream
public ChannelFuture resetStream(ChannelHandlerContext ctx, int streamId, long errorCode, ChannelPromise promise) Description copied from interface:Http2LifecycleManager
Ensure the stream identified bystreamId
is reset. If our local state does not indicate the stream has been reset yet then aRST_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.- Specified by:
resetStream
in interfaceHttp2LifecycleManager
- Parameters:
ctx
- 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. SeeHttp2Error
.promise
- Used to indicate the return status of this operation.- Returns:
- Will be considered successful when the connection and stream state has been updated, and a
RST_STREAM
frame has been sent to the peer. If the stream state has already been updated and aRST_STREAM
frame has been sent then the return status may indicate success immediately.
-
resetStream
private ChannelFuture resetStream(ChannelHandlerContext ctx, Http2Stream stream, long errorCode, ChannelPromise promise) -
goAway
public ChannelFuture goAway(ChannelHandlerContext ctx, int lastStreamId, long errorCode, ByteBuf debugData, ChannelPromise promise) Description copied from interface:Http2LifecycleManager
Prevents the peer from creating streams and close the connection iferrorCode
is notHttp2Error.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 withstream identifier <= lastStreamId
. This may result in sending aGO_AWAY
frame (assuming we have not already sent one withLast-Stream-ID <= lastStreamId
), or may just return success if aGO_AWAY
has previously been sent.- Specified by:
goAway
in interfaceHttp2LifecycleManager
- Parameters:
ctx
- 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. SeeHttp2Error
.debugData
- For diagnostic purposes (carries no semantic value).promise
- Used to indicate the return status of this operation.- Returns:
- Will be considered successful when the connection and stream state has been updated, and a
GO_AWAY
frame has been sent to the peer. If the stream state has already been updated and aGO_AWAY
frame has been sent then the return status may indicate success immediately.
-
checkCloseConnection
Closes the connection if the graceful shutdown process has completed.- Parameters:
future
- Represents the status that will be passed to thecloseListener
.
-
goAway
private ChannelFuture goAway(ChannelHandlerContext ctx, Http2Exception cause, ChannelPromise promise) Close the remote endpoint with aGO_AWAY
frame. Does not flush immediately, this is the responsibility of the caller. -
processRstStreamWriteResult
private void processRstStreamWriteResult(ChannelHandlerContext ctx, Http2Stream stream, ChannelFuture future) -
closeConnectionOnError
-
doCloseStream
-
clientPrefaceString
Returns the client preface string if this is a client connection, otherwise returnsnull
. -
processGoAwayWriteResult
private static void processGoAwayWriteResult(ChannelHandlerContext ctx, int lastStreamId, long errorCode, ByteBuf debugData, ChannelFuture future)
-