RequestClientConnControl implements HttpRequestInterceptor { public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { if (request == null) { throw new IllegalArgumentException("HTTP request may not be null"); } String method = request.getRequestLine().getMethod(); if (method.equalsIgnoreCase("CONNECT")) { request.setHeader(PROXY_CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE); return; } HttpRoutedConnection conn = (HttpRoutedConnection) context.getAttribute( ExecutionContext.HTTP_CONNECTION); if (conn == null) { this.log.debug("HTTP connection not set in the context"); return; } HttpRoute route = conn.getRoute(); if (route.getHopCount() == 1 || route.isTunnelled()) { if (!request.containsHeader(HTTP.CONN_DIRECTIVE)) { request.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE); } } if (route.getHopCount() == 2 && !route.isTunnelled()) { if (!request.containsHeader(PROXY_CONN_DIRECTIVE)) { request.addHeader(PROXY_CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE); } } } RequestClientConnControl(); void process(final HttpRequest request, final HttpContext context);  }
@Test(expected=IllegalArgumentException.class) public void testRequestParameterCheck() throws Exception { HttpContext context = new BasicHttpContext(); HttpRequestInterceptor interceptor = new RequestClientConnControl(); interceptor.process(null, context); }
@Test public void testConnectionKeepAliveForConnectRequest() throws Exception { HttpRequest request = new BasicHttpRequest("CONNECT", "www.somedomain.com"); HttpContext context = new BasicHttpContext(); HttpRequestInterceptor interceptor = new RequestClientConnControl(); interceptor.process(request, context); Header header1 = request.getFirstHeader("Proxy-Connection"); Assert.assertNotNull(header1); Assert.assertEquals(HTTP.CONN_KEEP_ALIVE, header1.getValue()); Header header2 = request.getFirstHeader(HTTP.CONN_DIRECTIVE); Assert.assertNull(header2); }
@Test public void testConnectionKeepAliveForDirectRequests() throws Exception { HttpRequest request = new BasicHttpRequest("GET", "/"); HttpContext context = new BasicHttpContext(); HttpHost target = new HttpHost("localhost", 80, "http"); HttpRoute route = new HttpRoute(target, null, false); HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class); Mockito.when(conn.getRoute()).thenReturn(route); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); HttpRequestInterceptor interceptor = new RequestClientConnControl(); interceptor.process(request, context); Header header1 = request.getFirstHeader(HTTP.CONN_DIRECTIVE); Assert.assertNotNull(header1); Assert.assertEquals(HTTP.CONN_KEEP_ALIVE, header1.getValue()); Header header2 = request.getFirstHeader("Proxy-Connection"); Assert.assertNull(header2); }
@Test public void testConnectionKeepAliveForTunneledRequests() throws Exception { HttpRequest request = new BasicHttpRequest("GET", "/"); HttpContext context = new BasicHttpContext(); HttpHost target = new HttpHost("localhost", 443, "https"); HttpHost proxy = new HttpHost("localhost", 8080); HttpRoute route = new HttpRoute(target, null, proxy, true, TunnelType.TUNNELLED, LayerType.LAYERED); HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class); Mockito.when(conn.getRoute()).thenReturn(route); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); HttpRequestInterceptor interceptor = new RequestClientConnControl(); interceptor.process(request, context); Header header1 = request.getFirstHeader(HTTP.CONN_DIRECTIVE); Assert.assertNotNull(header1); Assert.assertEquals(HTTP.CONN_KEEP_ALIVE, header1.getValue()); Header header2 = request.getFirstHeader("Proxy-Connection"); Assert.assertNull(header2); }
@Test public void testProxyConnectionKeepAliveForRequestsOverProxy() throws Exception { HttpRequest request = new BasicHttpRequest("GET", "/"); HttpContext context = new BasicHttpContext(); HttpHost target = new HttpHost("localhost", 80, "http"); HttpHost proxy = new HttpHost("localhost", 8080); HttpRoute route = new HttpRoute(target, null, proxy, false, TunnelType.PLAIN, LayerType.PLAIN); HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class); Mockito.when(conn.getRoute()).thenReturn(route); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); HttpRequestInterceptor interceptor = new RequestClientConnControl(); interceptor.process(request, context); Header header1 = request.getFirstHeader("Proxy-Connection"); Assert.assertNotNull(header1); Assert.assertEquals(HTTP.CONN_KEEP_ALIVE, header1.getValue()); Header header2 = request.getFirstHeader(HTTP.CONN_DIRECTIVE); Assert.assertNull(header2); }
@Test public void testPreserveCustomConnectionHeader() throws Exception { HttpRequest request = new BasicHttpRequest("GET", "/"); request.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE); HttpContext context = new BasicHttpContext(); HttpHost target = new HttpHost("localhost", 443, "https"); HttpHost proxy = new HttpHost("localhost", 8080); HttpRoute route = new HttpRoute(target, null, proxy, true, TunnelType.TUNNELLED, LayerType.LAYERED); HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class); Mockito.when(conn.getRoute()).thenReturn(route); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); HttpRequestInterceptor interceptor = new RequestClientConnControl(); interceptor.process(request, context); Header header1 = request.getFirstHeader(HTTP.CONN_DIRECTIVE); Assert.assertNotNull(header1); Assert.assertEquals(HTTP.CONN_CLOSE, header1.getValue()); Header header2 = request.getFirstHeader("Proxy-Connection"); Assert.assertNull(header2); }
@Test public void testPreserveCustomProxyConnectionHeader() throws Exception { HttpRequest request = new BasicHttpRequest("GET", "/"); request.addHeader("Proxy-Connection", HTTP.CONN_CLOSE); HttpContext context = new BasicHttpContext(); HttpHost target = new HttpHost("localhost", 80, "http"); HttpHost proxy = new HttpHost("localhost", 8080); HttpRoute route = new HttpRoute(target, null, proxy, false, TunnelType.PLAIN, LayerType.PLAIN); HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class); Mockito.when(conn.getRoute()).thenReturn(route); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); HttpRequestInterceptor interceptor = new RequestClientConnControl(); interceptor.process(request, context); Header header1 = request.getFirstHeader("Proxy-Connection"); Assert.assertNotNull(header1); Assert.assertEquals(HTTP.CONN_CLOSE, header1.getValue()); }