/** * Implemented in accordance with RFC 7230 section 6.1 * https://tools.ietf.org/html/rfc7230#section-6.1 */ @Override public boolean keepAlive(HttpRequest request, HttpResponse response) { String responseConnectionHeader = connectionHeader(response); if (CLOSE.equalsIgnoreCase(responseConnectionHeader)) { return false; } else { String requestConnectionHeader = connectionHeader(request); if (request.getProtocolVersion() == HttpVersion.HTTP_1_0) { // only use keep-alive if both parties agreed upon it return KEEP_ALIVE.equalsIgnoreCase(requestConnectionHeader) && KEEP_ALIVE.equalsIgnoreCase(responseConnectionHeader); } else { // 1.1+, keep-alive is default behavior return !CLOSE.equalsIgnoreCase(requestConnectionHeader); } } }