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; }
@Test(expected=NullPointerException.class) public void testInvalidResponseArg() throws Exception { reuseStrategy.keepAlive(null, null, this.context); }
@Test public void testNoContentLengthResponseHttp1_0() throws Exception { context.setProtocolVersion(HttpVersion.HTTP_1_0); final HttpResponse response = new BasicHttpResponse(200, "OK"); Assert.assertFalse(reuseStrategy.keepAlive(null, response, context)); }
@Test public void testNoContentLengthResponseHttp1_1() throws Exception { final HttpResponse response = new BasicHttpResponse(200, "OK"); Assert.assertFalse(reuseStrategy.keepAlive(null, response, context)); }
@Test public void testChunkedContent() throws Exception { final HttpResponse response = new BasicHttpResponse(200, "OK"); response.addHeader("Transfer-Encoding", "chunked"); Assert.assertTrue(reuseStrategy.keepAlive(null, response, context)); }
@Test public void testIgnoreInvalidKeepAlive() throws Exception { context.setProtocolVersion(HttpVersion.HTTP_1_0); final HttpResponse response = new BasicHttpResponse(200, "OK"); response.addHeader("Connection", "keep-alive"); Assert.assertFalse(reuseStrategy.keepAlive(null, response, context)); }
@Test public void testExplicitClose() throws Exception { final HttpResponse response = new BasicHttpResponse(200, "OK"); response.addHeader("Transfer-Encoding", "chunked"); response.addHeader("Connection", "close"); Assert.assertFalse(reuseStrategy.keepAlive(null, response, context)); }
@Test public void testExplicitKeepAlive() throws Exception { context.setProtocolVersion(HttpVersion.HTTP_1_0); final HttpResponse response = new BasicHttpResponse(200, "OK"); response.addHeader("Content-Length", "10"); response.addHeader("Connection", "keep-alive"); Assert.assertTrue(reuseStrategy.keepAlive(null, response, context)); }
@Test public void testHTTP10Default() throws Exception { context.setProtocolVersion(HttpVersion.HTTP_1_0); final HttpResponse response = new BasicHttpResponse(200, "OK"); response.addHeader("Content-Length", "10"); Assert.assertFalse(reuseStrategy.keepAlive(null, response, context)); }
@Test public void testHTTP11Default() throws Exception { final HttpResponse response = new BasicHttpResponse(200, "OK"); response.addHeader("Content-Length", "10"); Assert.assertTrue(reuseStrategy.keepAlive(null, response, context)); }
@Test public void testBrokenConnectionDirective1() throws Exception { context.setProtocolVersion(HttpVersion.HTTP_1_0); final HttpResponse response = new BasicHttpResponse(200, "OK"); response.addHeader("Content-Length", "10"); response.addHeader("Connection", "keep--alive"); Assert.assertFalse(reuseStrategy.keepAlive(null, response, context)); }
@Test public void testBrokenConnectionDirective2() throws Exception { context.setProtocolVersion(HttpVersion.HTTP_1_0); final HttpResponse response = new BasicHttpResponse(200, "OK"); response.addHeader("Content-Length", "10"); response.addHeader("Connection", null); Assert.assertFalse(reuseStrategy.keepAlive(null, response, context)); }
@Test public void testConnectionTokens1() throws Exception { final HttpResponse response = new BasicHttpResponse(200, "OK"); response.addHeader("Transfer-Encoding", "chunked"); response.addHeader("Connection", "yadda, cLOSe, dumdy"); Assert.assertFalse(reuseStrategy.keepAlive(null, response, context)); }
@Test public void testConnectionTokens2() throws Exception { final HttpResponse response = new BasicHttpResponse(200, "OK"); response.addHeader("Transfer-Encoding", "chunked"); response.addHeader("Connection", "yadda, kEEP-alive, dumdy"); Assert.assertTrue(reuseStrategy.keepAlive(null, response, context)); }
@Test public void testConnectionTokens3() throws Exception { final HttpResponse response = new BasicHttpResponse(200, "OK"); response.addHeader("Transfer-Encoding", "chunked"); response.addHeader("Connection", "yadda, keep-alive, close, dumdy"); Assert.assertFalse(reuseStrategy.keepAlive(null, response, context)); }
@Test public void testConnectionTokens4() throws Exception { final HttpResponse response = new BasicHttpResponse(200, "OK"); response.addHeader("Transfer-Encoding", "chunked"); response.addHeader("Connection", "yadda, close, dumdy"); response.addHeader("Proxy-Connection", "keep-alive"); Assert.assertFalse(reuseStrategy.keepAlive(null, response, context)); }
@Test public void testConnectionTokens5() throws Exception { final HttpResponse response = new BasicHttpResponse(200, "OK"); response.addHeader("Transfer-Encoding", "chunked"); response.addHeader("Connection", "yadda, dumdy"); response.addHeader("Proxy-Connection", "close"); Assert.assertTrue(reuseStrategy.keepAlive(null, response, context)); }
@Test public void testConnectionTokens6() throws Exception { final HttpResponse response = new BasicHttpResponse(200, "OK"); response.addHeader("Transfer-Encoding", "chunked"); response.addHeader("Connection", ""); response.addHeader("Proxy-Connection", "close"); Assert.assertTrue(reuseStrategy.keepAlive(null, response, context)); }
@Test public void testMultipleContentLength() throws Exception { final HttpResponse response = new BasicHttpResponse(200, "OK"); response.addHeader("Content-Length", "10"); response.addHeader("Content-Length", "11"); Assert.assertFalse(reuseStrategy.keepAlive(null, response, context)); }
@Test public void testNoContentResponse() throws Exception { final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_NO_CONTENT, "No Content"); Assert.assertTrue(reuseStrategy.keepAlive(null, response, context)); }
@Test public void testNoContentResponseHttp10() throws Exception { context.setProtocolVersion(HttpVersion.HTTP_1_0); final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_NO_CONTENT, "No Content"); Assert.assertFalse(reuseStrategy.keepAlive(null, response, context)); }
@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)); }
@Test public void testRequestNoExplicitClose() throws Exception { final HttpRequest request = new BasicHttpRequest(Method.GET, "/"); request.addHeader("Connection", "blah, blah, blah"); final HttpResponse response = new BasicHttpResponse(200, "OK"); response.addHeader("Transfer-Encoding", "chunked"); response.addHeader("Connection", "keep-alive"); Assert.assertTrue(reuseStrategy.keepAlive(request, response, context)); }
@Test public void testRequestExplicitCloseMultipleTokens() throws Exception { final HttpRequest request = new BasicHttpRequest(Method.GET, "/"); request.addHeader("Connection", "blah, blah, blah"); request.addHeader("Connection", "keep-alive"); 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)); }
@Test public void testRequestClose() throws Exception { final HttpRequest request = new BasicHttpRequest(Method.GET, "/"); request.addHeader("Connection", "close"); final HttpResponse response = new BasicHttpResponse(200, "OK"); response.addHeader("Content-Length", "10"); response.addHeader("Connection", "keep-alive"); Assert.assertFalse(reuseStrategy.keepAlive(request, response, context)); }
@Test public void testHeadRequestWithout() throws Exception { final HttpRequest request = new BasicHttpRequest(Method.HEAD, "/"); final HttpResponse response = new BasicHttpResponse(200, "OK"); Assert.assertTrue(reuseStrategy.keepAlive(request, response, context)); }
@Test public void testHttp204ContentLengthGreaterThanZero() throws Exception { final HttpResponse response = new BasicHttpResponse(204, "OK"); response.addHeader("Content-Length", "10"); response.addHeader("Connection", "keep-alive"); Assert.assertFalse(reuseStrategy.keepAlive(null, response, context)); }
@Test public void testHttp204ContentLengthEqualToZero() throws Exception { final HttpResponse response = new BasicHttpResponse(204, "OK"); response.addHeader("Content-Length", "0"); response.addHeader("Connection", "keep-alive"); Assert.assertTrue(reuseStrategy.keepAlive(null, response, context)); }
@Test public void testHttp204ChunkedContent() throws Exception { final HttpResponse response = new BasicHttpResponse(204, "OK"); response.addHeader("Transfer-Encoding", "chunked"); response.addHeader("Connection", "keep-alive"); Assert.assertFalse(reuseStrategy.keepAlive(null, response, context)); }