/** * Returns {@code true} if and only if the connection can remain open and * thus 'kept alive'. This methods respects the value of the * {@code "Connection"} header first and then the return value of * {@link HttpVersion#isKeepAliveDefault()}. */ public static boolean isKeepAlive(HttpMessage message) { String connection = message.headers().get(Names.CONNECTION); boolean close = Values.CLOSE.equalsIgnoreCase(connection); if (close) { return false; } if (message.getProtocolVersion().isKeepAliveDefault()) { return !close; } else { return Values.KEEP_ALIVE.equalsIgnoreCase(connection); } }
if (message.getProtocolVersion().isKeepAliveDefault()) { if (keepAlive) { h.remove(Names.CONNECTION);
/** Handle conditional connection close depending on keep-alive */ private void writeResponse(Channel channel, HttpRequest request, HttpResponse response) { response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes()); boolean hasKeepAliveHeader = KEEP_ALIVE.equalsIgnoreCase(request.getHeader(CONNECTION)); if (!request.getProtocolVersion().isKeepAliveDefault() && hasKeepAliveHeader) { response.setHeader(CONNECTION, KEEP_ALIVE); } ChannelFuture wf = channel.write(response); if (!HttpHeaders.isKeepAlive(request)) { wf.addListener(ChannelFutureListener.CLOSE); } }