- java.lang.Object
-
- io.netty5.channel.ChannelHandlerAdapter
-
- io.netty5.handler.codec.ByteToMessageDecoder
-
- io.netty5.handler.codec.http2.Http2ConnectionHandler
-
- All Implemented Interfaces:
ChannelHandler
,Http2LifecycleManager
- Direct Known Subclasses:
Http2FrameCodec
,HttpToHttp2ConnectionHandler
@UnstableApi public class Http2ConnectionHandler extends ByteToMessageDecoder implements Http2LifecycleManager
Provides the default implementation for processing inbound frame events and delegates to aHttp2FrameListener
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 classes/interfaces inherited from class io.netty5.handler.codec.ByteToMessageDecoder
ByteToMessageDecoder.Cumulator
-
-
Field Summary
-
Fields inherited from class io.netty5.handler.codec.ByteToMessageDecoder
COMPOSITE_CUMULATOR, MERGE_CUMULATOR
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
channelActive(ChannelHandlerContext ctx)
TheChannel
of theChannelHandlerContext
is now activevoid
channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause)
HandlesHttp2Exception
objects that were thrown from other handlers.void
channelInactive(ChannelHandlerContext ctx)
TheChannel
of theChannelHandlerContext
was registered is now inactive and reached its end of lifetime.void
channelReadComplete(ChannelHandlerContext ctx)
Invoked when the last message read by the current read operation has been consumed byChannelHandler.channelRead(ChannelHandlerContext, Object)
.void
channelWritabilityChanged(ChannelHandlerContext ctx)
Gets called once the writable state of aChannel
changed.Future<Void>
close(ChannelHandlerContext ctx)
Called once a close operation is made.void
closeStream(Http2Stream stream, Future<Void> future)
Closes and deactivates the givenstream
.void
closeStreamLocal(Http2Stream stream, Future<Void> future)
Closes the local side of the given stream.void
closeStreamRemote(Http2Stream stream, Future<Void> future)
Closes the remote side of the given stream.Http2Connection
connection()
protected void
decode(ChannelHandlerContext ctx, Buffer in)
Decode the from oneBuffer
to another.Http2ConnectionDecoder
decoder()
Http2ConnectionEncoder
encoder()
void
flush(ChannelHandlerContext ctx)
Called once a flush operation is made.protected Http2FrameWriter
frameWriter()
Future<Void>
goAway(ChannelHandlerContext ctx, int lastStreamId, long errorCode, Buffer debugData)
Prevents the peer from creating streams and close the connection iferrorCode
is notHttp2Error.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
handlerAdded0(ChannelHandlerContext ctx)
protected void
handlerRemoved0(ChannelHandlerContext ctx)
Gets called after theByteToMessageDecoder
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, 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
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, boolean outbound, Throwable cause, Http2Exception.StreamException http2Ex)
Handler for a stream error.Future<Void>
resetStream(ChannelHandlerContext ctx, int streamId, long errorCode)
Ensure the stream identified bystreamId
is reset.-
Methods inherited from class io.netty5.handler.codec.ByteToMessageDecoder
actualReadableBytes, channelRead, channelShutdown, decodeLast, discardSomeReadBytes, handlerAdded, handlerRemoved, internalBuffer, isSharable, isSingleDecode, setSingleDecode
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty5.channel.ChannelHandler
bind, channelInboundEvent, channelRegistered, channelUnregistered, connect, deregister, disconnect, pendingOutboundBytes, read, register, sendOutboundEvent, shutdown, write
-
-
-
-
Constructor Detail
-
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 Detail
-
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
public Http2Connection connection()
-
decoder
public Http2ConnectionDecoder decoder()
-
encoder
public Http2ConnectionEncoder encoder()
-
onHttpClientUpgrade
public void onHttpClientUpgrade() throws Http2Exception
Handles the client-side (cleartext) upgrade from HTTP to HTTP/2. Reserves local stream 1 for the HTTP/2 response.- Throws:
Http2Exception
-
onHttpServerUpgrade
public void onHttpServerUpgrade(Http2Settings settings) throws Http2Exception
Handles the server-side (cleartext) upgrade from HTTP to HTTP/2.- Parameters:
settings
- the settings for the remote endpoint.- Throws:
Http2Exception
-
flush
public void flush(ChannelHandlerContext ctx)
Description copied from interface:ChannelHandler
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 interfaceChannelHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the flush operation is made
-
handlerAdded0
public void handlerAdded0(ChannelHandlerContext ctx) throws Exception
- Overrides:
handlerAdded0
in classByteToMessageDecoder
- Throws:
Exception
-
handlerRemoved0
protected void handlerRemoved0(ChannelHandlerContext ctx) throws Exception
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
public void channelActive(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
TheChannel
of theChannelHandlerContext
is now active- Specified by:
channelActive
in interfaceChannelHandler
- Throws:
Exception
-
channelInactive
public void channelInactive(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
TheChannel
of theChannelHandlerContext
was registered is now inactive and reached its end of lifetime.- Specified by:
channelInactive
in interfaceChannelHandler
- Overrides:
channelInactive
in classByteToMessageDecoder
- Throws:
Exception
-
channelWritabilityChanged
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
Gets called once the writable state of aChannel
changed. You can check the state withChannel.writableBytes()
orChannel.isWritable()
.- Specified by:
channelWritabilityChanged
in interfaceChannelHandler
- Throws:
Exception
-
decode
protected void decode(ChannelHandlerContext ctx, Buffer in) throws Exception
Description copied from class:ByteToMessageDecoder
Decode the from oneBuffer
to another. This method will be called till either the inputBuffer
has nothing to read when return from this method or till nothing was read from the inputBuffer
.- Specified by:
decode
in classByteToMessageDecoder
- Parameters:
ctx
- theChannelHandlerContext
which thisByteToMessageDecoder
belongs toin
- theBuffer
from which to read data- Throws:
Exception
- is thrown if an error occurs
-
close
public Future<Void> close(ChannelHandlerContext ctx)
Description copied from interface:ChannelHandler
Called once a close operation is made.- Specified by:
close
in interfaceChannelHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the close operation is made- Returns:
- the
Future
which will be notified once the operation completes.
-
channelReadComplete
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
Invoked when the last message read by the current read operation has been consumed byChannelHandler.channelRead(ChannelHandlerContext, Object)
. IfChannelOption.AUTO_READ
is off, no further attempt to read an inbound data from the currentChannel
will be made untilChannelHandlerContext.read()
is called.- Specified by:
channelReadComplete
in interfaceChannelHandler
- Overrides:
channelReadComplete
in classByteToMessageDecoder
- Throws:
Exception
-
channelExceptionCaught
public void channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
HandlesHttp2Exception
objects that were thrown from other handlers. Ignores all other exceptions.- Specified by:
channelExceptionCaught
in interfaceChannelHandler
- Throws:
Exception
-
closeStreamLocal
public void closeStreamLocal(Http2Stream stream, Future<Void> future)
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
public void closeStreamRemote(Http2Stream stream, Future<Void> future)
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
public void closeStream(Http2Stream stream, Future<Void> future)
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
public void onError(ChannelHandlerContext ctx, boolean outbound, Throwable cause)
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 correspondingPromise
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
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. Override to change behavior.- Parameters:
ctx
- the channel contextstream
- the Http2Stream on which the header was received
-
frameWriter
protected Http2FrameWriter frameWriter()
-
resetStream
public Future<Void> resetStream(ChannelHandlerContext ctx, int streamId, long errorCode)
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
.- 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.
-
goAway
public Future<Void> goAway(ChannelHandlerContext ctx, int lastStreamId, long errorCode, Buffer debugData)
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).- 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.
-
-