Package io.netty.handler.codec.http
Class HttpServerKeepAliveHandler
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelInboundHandlerAdapter
-
- io.netty.channel.ChannelDuplexHandler
-
- io.netty.handler.codec.http.HttpServerKeepAliveHandler
-
- All Implemented Interfaces:
ChannelHandler
,ChannelInboundHandler
,ChannelOutboundHandler
public class HttpServerKeepAliveHandler extends ChannelDuplexHandler
HttpServerKeepAliveHandler helps close persistent connections when appropriate.The server channel is expected to set the proper 'Connection' header if it can handle persistent connections.
HttpServerKeepAliveHandler
will automatically close the channel for any LastHttpContent that corresponds to a client request for closing the connection, or if the HttpResponse associated with that LastHttpContent requested closing the connection or didn't have a self defined message length.Since
HttpServerKeepAliveHandler
expectsHttpObject
s it should be added afterHttpServerCodec
but before any other handlers that might send aHttpResponse
.ChannelPipeline
p = ...; ... p.addLast("serverCodec", newHttpServerCodec
()); p.addLast("httpKeepAlive", newHttpServerKeepAliveHandler
()); p.addLast("aggregator", newHttpObjectAggregator
(1048576)); ... p.addLast("handler", new HttpRequestHandler());
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
MULTIPART_PREFIX
private int
pendingResponses
private boolean
persistentConnection
-
Constructor Summary
Constructors Constructor Description HttpServerKeepAliveHandler()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
channelRead(ChannelHandlerContext ctx, java.lang.Object msg)
CallsChannelHandlerContext.fireChannelRead(Object)
to forward to the nextChannelInboundHandler
in theChannelPipeline
.private static boolean
isInformational(HttpResponse response)
private static boolean
isMultipart(HttpResponse response)
private static boolean
isSelfDefinedMessageLength(HttpResponse response)
Keep-alive only works if the client can detect when the message has ended without relying on the connection being closed.private boolean
shouldKeepAlive()
private void
trackResponse(HttpResponse response)
void
write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise)
CallsChannelOutboundInvoker.write(Object, ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
.-
Methods inherited from class io.netty.channel.ChannelDuplexHandler
bind, close, connect, deregister, disconnect, flush, read
-
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelInactive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, handlerRemoved, 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
handlerAdded, handlerRemoved
-
-
-
-
Field Detail
-
MULTIPART_PREFIX
private static final java.lang.String MULTIPART_PREFIX
- See Also:
- Constant Field Values
-
persistentConnection
private boolean persistentConnection
-
pendingResponses
private int pendingResponses
-
-
Method Detail
-
channelRead
public void channelRead(ChannelHandlerContext ctx, java.lang.Object msg) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelRead(Object)
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelRead
in interfaceChannelInboundHandler
- Overrides:
channelRead
in classChannelInboundHandlerAdapter
- Throws:
java.lang.Exception
-
write
public void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
Description copied from class:ChannelDuplexHandler
CallsChannelOutboundInvoker.write(Object, ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
write
in interfaceChannelOutboundHandler
- Overrides:
write
in classChannelDuplexHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the write operation is mademsg
- the message to writepromise
- theChannelPromise
to notify once the operation completes- Throws:
java.lang.Exception
- thrown if an error occurs
-
trackResponse
private void trackResponse(HttpResponse response)
-
shouldKeepAlive
private boolean shouldKeepAlive()
-
isSelfDefinedMessageLength
private static boolean isSelfDefinedMessageLength(HttpResponse response)
Keep-alive only works if the client can detect when the message has ended without relying on the connection being closed.- Parameters:
response
- The HttpResponse to check- Returns:
- true if the response has a self defined message length.
-
isInformational
private static boolean isInformational(HttpResponse response)
-
isMultipart
private static boolean isMultipart(HttpResponse response)
-
-