@Test public void testRequestExplicitClose() throws Exception { final HttpRequest request = new BasicHttpRequest(Method.GET, "/"); request.addHeader("Connection", "close"); final HttpResponse response = new BasicHttpResponse(200, "OK"); response.addHeader("Transfer-Encoding", "chunked"); response.addHeader("Connection", "keep-alive"); Assert.assertFalse(reuseStrategy.keepAlive(request, response, context)); }
@Override public boolean keepAlive( final HttpRequest request, final HttpResponse response, final HttpContext context) { Args.notNull(response, "HTTP response"); if (request != null) { final Iterator<String> ti = new BasicTokenIterator(request.headerIterator(HttpHeaders.CONNECTION)); while (ti.hasNext()) { final String token = ti.next(); if (HeaderElements.CLOSE.equalsIgnoreCase(token)) { return false; } } } 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) { } } if (response.containsHeader(HttpHeaders.TRANSFER_ENCODING)) { return false; } } final Header teh = response.getFirstHeader(HttpHeaders.TRANSFER_ENCODING); if (teh != null) { if (!HeaderElements.CHUNKED_ENCODING.equalsIgnoreCase(teh.getValue())) { return false; } } else { final String method = request != null ? request.getMethod() : null; if (MessageSupport.canResponseHaveBody(method, response) && response.countHeaders(HttpHeaders.CONTENT_LENGTH) != 1) { return false; } } Iterator<Header> headerIterator = response.headerIterator(HttpHeaders.CONNECTION); if (!headerIterator.hasNext()) { headerIterator = response.headerIterator("Proxy-Connection"); } final ProtocolVersion ver = context.getProtocolVersion(); 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); }
DefaultConnectionReuseStrategy implements ConnectionReuseStrategy { @Override public boolean keepAlive( final HttpRequest request, final HttpResponse response, final HttpContext context) { Args.notNull(response, "HTTP response"); if (request != null) { final Iterator<String> ti = new BasicTokenIterator(request.headerIterator(HttpHeaders.CONNECTION)); while (ti.hasNext()) { final String token = ti.next(); if (HeaderElements.CLOSE.equalsIgnoreCase(token)) { return false; } } } 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) { } } if (response.containsHeader(HttpHeaders.TRANSFER_ENCODING)) { return false; } } final Header teh = response.getFirstHeader(HttpHeaders.TRANSFER_ENCODING); if (teh != null) { if (!HeaderElements.CHUNKED_ENCODING.equalsIgnoreCase(teh.getValue())) { return false; } } else { final String method = request != null ? request.getMethod() : null; if (MessageSupport.canResponseHaveBody(method, response) && response.countHeaders(HttpHeaders.CONTENT_LENGTH) != 1) { return false; } } Iterator<Header> headerIterator = response.headerIterator(HttpHeaders.CONNECTION); if (!headerIterator.hasNext()) { headerIterator = response.headerIterator("Proxy-Connection"); } final ProtocolVersion ver = context.getProtocolVersion(); 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); } }
DefaultConnectionReuseStrategy implements ConnectionReuseStrategy { @Override public boolean keepAlive( final HttpRequest request, final HttpResponse response, final HttpContext context) { Args.notNull(response, "HTTP response"); if (request != null) { final Iterator<String> ti = new BasicTokenIterator(request.headerIterator(HttpHeaders.CONNECTION)); while (ti.hasNext()) { final String token = ti.next(); if (HeaderElements.CLOSE.equalsIgnoreCase(token)) { return false; } } } 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) { } } if (response.containsHeader(HttpHeaders.TRANSFER_ENCODING)) { return false; } } final Header teh = response.getFirstHeader(HttpHeaders.TRANSFER_ENCODING); if (teh != null) { if (!HeaderElements.CHUNKED_ENCODING.equalsIgnoreCase(teh.getValue())) { return false; } } else { final String method = request != null ? request.getMethod() : null; if (MessageSupport.canResponseHaveBody(method, response) && response.countHeaders(HttpHeaders.CONTENT_LENGTH) != 1) { return false; } } Iterator<Header> headerIterator = response.headerIterator(HttpHeaders.CONNECTION); if (!headerIterator.hasNext()) { headerIterator = response.headerIterator("Proxy-Connection"); } final ProtocolVersion ver = context.getProtocolVersion(); 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); } DefaultConnectionReuseStrategy(); }
DefaultConnectionReuseStrategy implements ConnectionReuseStrategy { @Override public boolean keepAlive( final HttpRequest request, final HttpResponse response, final HttpContext context) { Args.notNull(response, "HTTP response"); if (request != null) { final Iterator<String> ti = new BasicTokenIterator(request.headerIterator(HttpHeaders.CONNECTION)); while (ti.hasNext()) { final String token = ti.next(); if (HeaderElements.CLOSE.equalsIgnoreCase(token)) { return false; } } } 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) { } } if (response.containsHeader(HttpHeaders.TRANSFER_ENCODING)) { return false; } } final Header teh = response.getFirstHeader(HttpHeaders.TRANSFER_ENCODING); if (teh != null) { if (!HeaderElements.CHUNKED_ENCODING.equalsIgnoreCase(teh.getValue())) { return false; } } else { final String method = request != null ? request.getMethod() : null; if (MessageSupport.canResponseHaveBody(method, response) && response.countHeaders(HttpHeaders.CONTENT_LENGTH) != 1) { return false; } } Iterator<Header> headerIterator = response.headerIterator(HttpHeaders.CONNECTION); if (!headerIterator.hasNext()) { headerIterator = response.headerIterator("Proxy-Connection"); } final ProtocolVersion ver = context.getProtocolVersion(); 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); } DefaultConnectionReuseStrategy(); @Override boolean keepAlive(
            final HttpRequest request, final HttpResponse response, final HttpContext context); }
DefaultConnectionReuseStrategy implements ConnectionReuseStrategy { @Override public boolean keepAlive( final HttpRequest request, final HttpResponse response, final HttpContext context) { Args.notNull(response, "HTTP response"); if (request != null) { final Iterator<String> ti = new BasicTokenIterator(request.headerIterator(HttpHeaders.CONNECTION)); while (ti.hasNext()) { final String token = ti.next(); if (HeaderElements.CLOSE.equalsIgnoreCase(token)) { return false; } } } 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) { } } if (response.containsHeader(HttpHeaders.TRANSFER_ENCODING)) { return false; } } final Header teh = response.getFirstHeader(HttpHeaders.TRANSFER_ENCODING); if (teh != null) { if (!HeaderElements.CHUNKED_ENCODING.equalsIgnoreCase(teh.getValue())) { return false; } } else { final String method = request != null ? request.getMethod() : null; if (MessageSupport.canResponseHaveBody(method, response) && response.countHeaders(HttpHeaders.CONTENT_LENGTH) != 1) { return false; } } Iterator<Header> headerIterator = response.headerIterator(HttpHeaders.CONNECTION); if (!headerIterator.hasNext()) { headerIterator = response.headerIterator("Proxy-Connection"); } final ProtocolVersion ver = context.getProtocolVersion(); 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); } DefaultConnectionReuseStrategy(); @Override boolean keepAlive(
            final HttpRequest request, final HttpResponse response, final HttpContext context); static final DefaultConnectionReuseStrategy INSTANCE; }