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:
io.netty.channel.ChannelHandler,io.netty.channel.ChannelInboundHandler,io.netty.channel.ChannelOutboundHandler,Http2LifecycleManager
- Direct Known Subclasses:
Http2FrameCodec,HttpToHttp2ConnectionHandler
public class Http2ConnectionHandler extends io.netty.handler.codec.ByteToMessageDecoder implements Http2LifecycleManager, io.netty.channel.ChannelOutboundHandler
Provides the default implementation for processing inbound frame events and delegates to aHttp2FrameListenerThis class will read HTTP/2 frames and delegate the events to a
Http2FrameListenerThis interface enforces inbound flow control functionality through
Http2LocalFlowController
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedHttp2ConnectionHandler(Http2ConnectionDecoder decoder, Http2ConnectionEncoder encoder, Http2Settings initialSettings)protectedHttp2ConnectionHandler(Http2ConnectionDecoder decoder, Http2ConnectionEncoder encoder, Http2Settings initialSettings, boolean decoupleCloseAndGoAway)protectedHttp2ConnectionHandler(Http2ConnectionDecoder decoder, Http2ConnectionEncoder encoder, Http2Settings initialSettings, boolean decoupleCloseAndGoAway, boolean flushPreface)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbind(io.netty.channel.ChannelHandlerContext ctx, SocketAddress localAddress, io.netty.channel.ChannelPromise promise)voidchannelActive(io.netty.channel.ChannelHandlerContext ctx)voidchannelInactive(io.netty.channel.ChannelHandlerContext ctx)voidchannelReadComplete(io.netty.channel.ChannelHandlerContext ctx)voidchannelWritabilityChanged(io.netty.channel.ChannelHandlerContext ctx)voidclose(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise)voidcloseStream(Http2Stream stream, io.netty.channel.ChannelFuture future)Closes and deactivates the givenstream.voidcloseStreamLocal(Http2Stream stream, io.netty.channel.ChannelFuture future)Closes the local side of the given stream.voidcloseStreamRemote(Http2Stream stream, io.netty.channel.ChannelFuture future)Closes the remote side of the given stream.voidconnect(io.netty.channel.ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, io.netty.channel.ChannelPromise promise)Http2Connectionconnection()protected voiddecode(io.netty.channel.ChannelHandlerContext ctx, io.netty.buffer.ByteBuf in, List<Object> out)Http2ConnectionDecoderdecoder()voidderegister(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise)voiddisconnect(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise)Http2ConnectionEncoderencoder()voidexceptionCaught(io.netty.channel.ChannelHandlerContext ctx, Throwable cause)HandlesHttp2Exceptionobjects that were thrown from other handlers.voidflush(io.netty.channel.ChannelHandlerContext ctx)protected Http2FrameWriterframeWriter()io.netty.channel.ChannelFuturegoAway(io.netty.channel.ChannelHandlerContext ctx, int lastStreamId, long errorCode, io.netty.buffer.ByteBuf debugData, io.netty.channel.ChannelPromise promise)Prevents the peer from creating streams and close the connection iferrorCodeis notHttp2Error.NO_ERROR.longgracefulShutdownTimeoutMillis()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.voidgracefulShutdownTimeoutMillis(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.voidhandlerAdded(io.netty.channel.ChannelHandlerContext ctx)protected voidhandlerRemoved0(io.netty.channel.ChannelHandlerContext ctx)protected voidhandleServerHeaderDecodeSizeError(io.netty.channel.ChannelHandlerContext ctx, Http2Stream stream)Notifies client that this server has received headers that are larger than what it is willing to accept.protected booleanisGracefulShutdownComplete()Called by the graceful shutdown logic to determine when it is safe to close the connection.protected voidonConnectionError(io.netty.channel.ChannelHandlerContext ctx, boolean outbound, Throwable cause, Http2Exception http2Ex)Handler for a connection error.voidonError(io.netty.channel.ChannelHandlerContext ctx, boolean outbound, Throwable cause)Central handler for all exceptions caught during HTTP/2 processing.voidonHttpClientUpgrade()Handles the client-side (cleartext) upgrade from HTTP to HTTP/2.voidonHttpServerUpgrade(Http2Settings settings)Handles the server-side (cleartext) upgrade from HTTP to HTTP/2.protected voidonStreamError(io.netty.channel.ChannelHandlerContext ctx, boolean outbound, Throwable cause, Http2Exception.StreamException http2Ex)Handler for a stream error.voidread(io.netty.channel.ChannelHandlerContext ctx)io.netty.channel.ChannelFutureresetStream(io.netty.channel.ChannelHandlerContext ctx, int streamId, long errorCode, io.netty.channel.ChannelPromise promise)Ensure the stream identified bystreamIdis reset.voidwrite(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise)-
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
-
-
-
-
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 Http2ExceptionHandles 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(io.netty.channel.ChannelHandlerContext ctx)
- Specified by:
flushin interfaceio.netty.channel.ChannelOutboundHandler
-
handlerAdded
public void handlerAdded(io.netty.channel.ChannelHandlerContext ctx) throws Exception- Specified by:
handlerAddedin interfaceio.netty.channel.ChannelHandler- Overrides:
handlerAddedin classio.netty.channel.ChannelHandlerAdapter- Throws:
Exception
-
handlerRemoved0
protected void handlerRemoved0(io.netty.channel.ChannelHandlerContext ctx) throws Exception- Overrides:
handlerRemoved0in classio.netty.handler.codec.ByteToMessageDecoder- Throws:
Exception
-
channelActive
public void channelActive(io.netty.channel.ChannelHandlerContext ctx) throws Exception- Specified by:
channelActivein interfaceio.netty.channel.ChannelInboundHandler- Overrides:
channelActivein classio.netty.channel.ChannelInboundHandlerAdapter- Throws:
Exception
-
channelInactive
public void channelInactive(io.netty.channel.ChannelHandlerContext ctx) throws Exception- Specified by:
channelInactivein interfaceio.netty.channel.ChannelInboundHandler- Overrides:
channelInactivein classio.netty.handler.codec.ByteToMessageDecoder- Throws:
Exception
-
channelWritabilityChanged
public void channelWritabilityChanged(io.netty.channel.ChannelHandlerContext ctx) throws Exception- Specified by:
channelWritabilityChangedin interfaceio.netty.channel.ChannelInboundHandler- Overrides:
channelWritabilityChangedin classio.netty.channel.ChannelInboundHandlerAdapter- Throws:
Exception
-
decode
protected void decode(io.netty.channel.ChannelHandlerContext ctx, io.netty.buffer.ByteBuf in, List<Object> out) throws Exception- Specified by:
decodein classio.netty.handler.codec.ByteToMessageDecoder- Throws:
Exception
-
bind
public void bind(io.netty.channel.ChannelHandlerContext ctx, SocketAddress localAddress, io.netty.channel.ChannelPromise promise) throws Exception- Specified by:
bindin interfaceio.netty.channel.ChannelOutboundHandler- Throws:
Exception
-
connect
public void connect(io.netty.channel.ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, io.netty.channel.ChannelPromise promise) throws Exception- Specified by:
connectin interfaceio.netty.channel.ChannelOutboundHandler- Throws:
Exception
-
disconnect
public void disconnect(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise) throws Exception- Specified by:
disconnectin interfaceio.netty.channel.ChannelOutboundHandler- Throws:
Exception
-
close
public void close(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise) throws Exception- Specified by:
closein interfaceio.netty.channel.ChannelOutboundHandler- Throws:
Exception
-
deregister
public void deregister(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise) throws Exception- Specified by:
deregisterin interfaceio.netty.channel.ChannelOutboundHandler- Throws:
Exception
-
read
public void read(io.netty.channel.ChannelHandlerContext ctx) throws Exception- Specified by:
readin interfaceio.netty.channel.ChannelOutboundHandler- Throws:
Exception
-
write
public void write(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise) throws Exception- Specified by:
writein interfaceio.netty.channel.ChannelOutboundHandler- Throws:
Exception
-
channelReadComplete
public void channelReadComplete(io.netty.channel.ChannelHandlerContext ctx) throws Exception- Specified by:
channelReadCompletein interfaceio.netty.channel.ChannelInboundHandler- Overrides:
channelReadCompletein classio.netty.handler.codec.ByteToMessageDecoder- Throws:
Exception
-
exceptionCaught
public void exceptionCaught(io.netty.channel.ChannelHandlerContext ctx, Throwable cause) throws ExceptionHandlesHttp2Exceptionobjects that were thrown from other handlers. Ignores all other exceptions.- Specified by:
exceptionCaughtin interfaceio.netty.channel.ChannelHandler- Specified by:
exceptionCaughtin interfaceio.netty.channel.ChannelInboundHandler- Overrides:
exceptionCaughtin classio.netty.channel.ChannelInboundHandlerAdapter- Throws:
Exception
-
closeStreamLocal
public void closeStreamLocal(Http2Stream stream, io.netty.channel.ChannelFuture 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:
closeStreamLocalin 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, io.netty.channel.ChannelFuture 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:
closeStreamRemotein 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, io.netty.channel.ChannelFuture future)
Description copied from interface:Http2LifecycleManagerCloses and deactivates the givenstream. A listener is also attached tofutureand upon completion the underlying channel will be closed ifHttp2Connection.numActiveStreams()is 0.- Specified by:
closeStreamin 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(io.netty.channel.ChannelHandlerContext ctx, boolean outbound, Throwable cause)Central handler for all exceptions caught during HTTP/2 processing.- Specified by:
onErrorin interfaceHttp2LifecycleManager- Parameters:
ctx- The context used for communication and buffer allocation if necessary.outbound-trueif the error was caused by an outbound operation and so the correspondingChannelPromisewas 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. Returnstrueif 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(io.netty.channel.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-trueif the error was caused by an outbound operation.cause- the exception that was caughthttp2Ex- theHttp2Exceptionthat is embedded in the causality chain. This may benullif it's an unknown exception.
-
onStreamError
protected void onStreamError(io.netty.channel.ChannelHandlerContext ctx, boolean outbound, Throwable cause, Http2Exception.StreamException http2Ex)Handler for a stream error. Sends aRST_STREAMframe to the remote endpoint and closes the stream.- Parameters:
ctx- the channel contextoutbound-trueif the error was caused by an outbound operation.cause- the exception that was caughthttp2Ex- theHttp2Exception.StreamExceptionthat is embedded in the causality chain.
-
handleServerHeaderDecodeSizeError
protected void handleServerHeaderDecodeSizeError(io.netty.channel.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 io.netty.channel.ChannelFuture resetStream(io.netty.channel.ChannelHandlerContext ctx, int streamId, long errorCode, io.netty.channel.ChannelPromise promise)Description copied from interface:Http2LifecycleManagerEnsure the stream identified bystreamIdis reset. If our local state does not indicate the stream has been reset yet then aRST_STREAMwill 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:
resetStreamin 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_STREAMframe has been sent to the peer. If the stream state has already been updated and aRST_STREAMframe has been sent then the return status may indicate success immediately.
-
goAway
public io.netty.channel.ChannelFuture goAway(io.netty.channel.ChannelHandlerContext ctx, int lastStreamId, long errorCode, io.netty.buffer.ByteBuf debugData, io.netty.channel.ChannelPromise promise)Description copied from interface:Http2LifecycleManagerPrevents the peer from creating streams and close the connection iferrorCodeis 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_AWAYframe (assuming we have not already sent one withLast-Stream-ID <= lastStreamId), or may just return success if aGO_AWAYhas previously been sent.- Specified by:
goAwayin 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_AWAYframe has been sent to the peer. If the stream state has already been updated and aGO_AWAYframe has been sent then the return status may indicate success immediately.
-
-