public class HttpServerKeepAliveHandler extends ChannelDuplexHandler
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 expects HttpObjects it should be added after HttpServerCodec
but before any other handlers that might send a HttpResponse.
ChannelPipelinep = ...; ... p.addLast("serverCodec", newHttpServerCodec()); p.addLast("httpKeepAlive", newHttpServerKeepAliveHandler()); p.addLast("aggregator", newHttpObjectAggregator(1048576)); ... p.addLast("handler", new HttpRequestHandler());
ChannelHandler.Sharable| Modifier and Type | Field and Description |
|---|---|
private static java.lang.String |
MULTIPART_PREFIX |
private int |
pendingResponses |
private boolean |
persistentConnection |
| Constructor and Description |
|---|
HttpServerKeepAliveHandler() |
| Modifier and Type | Method and Description |
|---|---|
void |
channelRead(ChannelHandlerContext ctx,
java.lang.Object msg)
Calls
ChannelHandlerContext.fireChannelRead(Object) to forward
to the next ChannelInboundHandler in the ChannelPipeline. |
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)
Calls
ChannelOutboundInvoker.write(Object, ChannelPromise) to forward
to the next ChannelOutboundHandler in the ChannelPipeline. |
bind, close, connect, deregister, disconnect, flush, readchannelActive, channelInactive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggeredensureNotSharable, handlerAdded, handlerRemoved, isSharableclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitexceptionCaught, handlerAdded, handlerRemovedprivate static final java.lang.String MULTIPART_PREFIX
private boolean persistentConnection
private int pendingResponses
public void channelRead(ChannelHandlerContext ctx, java.lang.Object msg) throws java.lang.Exception
ChannelInboundHandlerAdapterChannelHandlerContext.fireChannelRead(Object) to forward
to the next ChannelInboundHandler in the ChannelPipeline.
Sub-classes may override this method to change behavior.channelRead in interface ChannelInboundHandlerchannelRead in class ChannelInboundHandlerAdapterjava.lang.Exceptionpublic void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
ChannelDuplexHandlerChannelOutboundInvoker.write(Object, ChannelPromise) to forward
to the next ChannelOutboundHandler in the ChannelPipeline.
Sub-classes may override this method to change behavior.write in interface ChannelOutboundHandlerwrite in class ChannelDuplexHandlerctx - 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 occursprivate void trackResponse(HttpResponse response)
private boolean shouldKeepAlive()
private static boolean isSelfDefinedMessageLength(HttpResponse response)
response - The HttpResponse to checkprivate static boolean isInformational(HttpResponse response)
private static boolean isMultipart(HttpResponse response)