Top Description Fields Constructors Methods
io.netty5.handler.codec.http

public Class HttpServerKeepAliveHandler

extends Object
implements ChannelHandler
Class Inheritance
All Implemented Interfaces
io.netty5.channel.ChannelHandler
Imports
io.netty5.channel.ChannelFutureListeners, .ChannelHandler, .ChannelHandlerContext, .ChannelPipeline, io.netty5.util.AsciiString, io.netty5.util.concurrent.Future

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 expects HttpObjects it should be added after HttpServerCodec but before any other handlers that might send a HttpResponse.

 ChannelPipeline p = ...;
 ...
 p.addLast("serverCodec", new HttpServerCodec());
 p.addLast("httpKeepAlive", new HttpServerKeepAliveHandler());
 p.addLast("aggregator", new HttpObjectAggregator(1048576));
 ...
 p.addLast("handler", new HttpRequestHandler());
 

Field Summary

Modifier and TypeField and Description
private static final String
private int
private boolean

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
channelRead(ChannelHandlerContext ctx, Object msg)

Overrides default io.netty5.channel.ChannelHandler.channelRead.

Invoked when the current Channel has read a message from the peer.
private static boolean
private static boolean
private static boolean

Returns:

true if the response has a self defined message length.
isSelfDefinedMessageLength
(HttpResponse
The HttpResponse to check
response
)

Keep-alive only works if the client can detect when the message has ended without relying on the connection being closed.

private boolean
private void
public Future<Void>
write(ChannelHandlerContext
the ChannelHandlerContext for which the write operation is made
ctx
,
Object
the message to write
msg
)

Overrides default io.netty5.channel.ChannelHandler.write.

Called once a write operation is made.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

MULTIPART_PREFIXback to summary
private static final String MULTIPART_PREFIX
pendingResponsesback to summary
private int pendingResponses
persistentConnectionback to summary
private boolean persistentConnection

Constructor Detail

HttpServerKeepAliveHandlerback to summary
public HttpServerKeepAliveHandler()

Method Detail

channelReadback to summary
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception

Overrides default io.netty5.channel.ChannelHandler.channelRead.

Doc from io.netty5.channel.ChannelHandler.channelRead.

Invoked when the current Channel has read a message from the peer.

Annotations
@Override
isInformationalback to summary
private static boolean isInformational(HttpResponse response)
isMultipartback to summary
private static boolean isMultipart(HttpResponse response)
isSelfDefinedMessageLengthback to summary
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:HttpResponse

The HttpResponse to check

Returns:boolean

true if the response has a self defined message length.

shouldKeepAliveback to summary
private boolean shouldKeepAlive()
trackResponseback to summary
private void trackResponse(HttpResponse response)
writeback to summary
public Future<Void> write(ChannelHandlerContext ctx, Object msg)

Overrides default io.netty5.channel.ChannelHandler.write.

Doc from io.netty5.channel.ChannelHandler.write.

Called once a write operation is made. The write operation will write the messages through the ChannelPipeline. Those are then ready to be flushed to the actual Channel once Channel#flush() is called.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext for which the write operation is made

msg:Object

the message to write

Returns:Future<Void>

the Future which will be notified once the operation completes.

Annotations
@Override