I'm new to Netty
and I looked at part of the source code using ioty.example.http.file.HttpStaticFileServer
as a starting point. I couldn't find any logic to control whether a Channel
is closed or not.
I found that in the ioty.example.http.file.HttpStaticFileServerHandler#sendAndCleanupConnection(ChannelHandlerContext ctx, FullHttpResponse response)
method the ChannelFutureListener.CLOSE
callback is added by checking if it is a keepAlive
.
/**
* If Keep-Alive is disabled, attaches "Connection: close" header to the response
* and closes the connection after the response being sent.
*/
private void sendAndCleanupConnection(ChannelHandlerContext ctx, FullHttpResponse response) {
final FullHttpRequest request = this.request;
final boolean keepAlive = HttpUtil.isKeepAlive(request);
HttpUtil.setContentLength(response, response.content().readableBytes());
if (!keepAlive) {
// We're going to close the connection as soon as the response is sent,
// so we should also make it clear for the client.
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
} else if (request.protocolVersion().equals(HTTP_1_0)) {
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
}
ChannelFuture flushPromise = ctx.writeAndFlush(response);
if (!keepAlive) {
// Close the connection as soon as the response is sent.
flushPromise.addListener(ChannelFutureListener.CLOSE);
}
}
I wonder, when writing an HTTP Server
in Netty
, do I have to implement the logic of whether the Channel
is closed or not?
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744219337a4563704.html
评论列表(0条)