in httpcore5/src/main/java/org/apache/hc/core5/http/impl/DefaultConnectionReuseStrategy.java [79:161]
public boolean keepAlive(
final HttpRequest request, final HttpResponse response, final HttpContext context) {
Args.notNull(response, "HTTP response");
if (request != null) {
// Consider framing of a request message with both Content-Length and Content-Length headers faulty
if (request.containsHeader(HttpHeaders.CONTENT_LENGTH) && request.containsHeader(HttpHeaders.TRANSFER_ENCODING)) {
return false;
}
final Iterator<String> it = MessageSupport.iterateTokens(request, HttpHeaders.CONNECTION);
while (it.hasNext()) {
final String token = it.next();
if (HeaderElements.CLOSE.equalsIgnoreCase(token)) {
return false;
}
}
}
// If Transfer-Encoding is not present consider framing of a response message
// with multiple Content-Length headers faulty
final Header teh = response.getFirstHeader(HttpHeaders.TRANSFER_ENCODING);
if (teh == null
&& MessageSupport.canResponseHaveBody(request != null ? request.getMethod() : null, response)
&& response.countHeaders(HttpHeaders.CONTENT_LENGTH) != 1) {
return false;
}
final ProtocolVersion ver = response.getVersion() != null ? response.getVersion() : context.getProtocolVersion();
// Consider framing of a HTTP/1.0 response message with Transfer-Content header faulty
if (ver.lessEquals(HttpVersion.HTTP_1_0) && teh != null) {
return false;
}
// If a HTTP 204 No Content response contains a Content-length with value > 0 or Transfer-Encoding header,
// don't reuse the connection. This is to avoid getting out-of-sync if a misbehaved HTTP server
// returns content as part of a HTTP 204 response.
if (response.getCode() == HttpStatus.SC_NO_CONTENT) {
final Header clh = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
if (clh != null) {
try {
final long contentLen = Long.parseLong(clh.getValue());
if (contentLen > 0) {
return false;
}
} catch (final NumberFormatException ex) {
// fall through
}
}
if (response.containsHeader(HttpHeaders.TRANSFER_ENCODING)) {
return false;
}
}
// Check for the "Connection" header. If that is absent, check for
// the "Proxy-Connection" header. The latter is an unspecified and
// broken but unfortunately common extension of HTTP.
Iterator<Header> headerIterator = response.headerIterator(HttpHeaders.CONNECTION);
if (!headerIterator.hasNext()) {
headerIterator = response.headerIterator("Proxy-Connection");
}
if (headerIterator.hasNext()) {
if (ver.greaterEquals(HttpVersion.HTTP_1_1)) {
final Iterator<String> it = new BasicTokenIterator(headerIterator);
while (it.hasNext()) {
final String token = it.next();
if (HeaderElements.CLOSE.equalsIgnoreCase(token)) {
return false;
}
}
return true;
}
final Iterator<String> it = new BasicTokenIterator(headerIterator);
while (it.hasNext()) {
final String token = it.next();
if (HeaderElements.KEEP_ALIVE.equalsIgnoreCase(token)) {
return true;
}
}
return false;
}
return ver.greaterEquals(HttpVersion.HTTP_1_1);
}