RequestAddCookies 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"); } if (context == null) { throw new IllegalArgumentException("HTTP context may not be null"); } String method = request.getRequestLine().getMethod(); if (method.equalsIgnoreCase("CONNECT")) { return; } CookieStore cookieStore = (CookieStore) context.getAttribute( ClientContext.COOKIE_STORE); if (cookieStore == null) { this.log.debug("Cookie store not specified in HTTP context"); return; } CookieSpecRegistry registry = (CookieSpecRegistry) context.getAttribute( ClientContext.COOKIESPEC_REGISTRY); if (registry == null) { this.log.debug("CookieSpec registry not specified in HTTP context"); return; } HttpHost targetHost = (HttpHost) context.getAttribute( ExecutionContext.HTTP_TARGET_HOST); if (targetHost == null) { this.log.debug("Target host not set in the context"); return; } HttpRoutedConnection conn = (HttpRoutedConnection) context.getAttribute( ExecutionContext.HTTP_CONNECTION); if (conn == null) { this.log.debug("HTTP connection not set in the context"); return; } String policy = HttpClientParams.getCookiePolicy(request.getParams()); if (this.log.isDebugEnabled()) { this.log.debug("CookieSpec selected: " + policy); } URI requestURI; if (request instanceof HttpUriRequest) { requestURI = ((HttpUriRequest) request).getURI(); } else { try { requestURI = new URI(request.getRequestLine().getUri()); } catch (URISyntaxException ex) { throw new ProtocolException("Invalid request URI: " + request.getRequestLine().getUri(), ex); } } String hostName = targetHost.getHostName(); int port = targetHost.getPort(); if (port < 0) { HttpRoute route = conn.getRoute(); if (route.getHopCount() == 1) { port = conn.getRemotePort(); } else { String scheme = targetHost.getSchemeName(); if (scheme.equalsIgnoreCase("http")) { port = 80; } else if (scheme.equalsIgnoreCase("https")) { port = 443; } else { port = 0; } } } CookieOrigin cookieOrigin = new CookieOrigin( hostName, port, requestURI.getPath(), conn.isSecure()); CookieSpec cookieSpec = registry.getCookieSpec(policy, request.getParams()); List<Cookie> cookies = new ArrayList<Cookie>(cookieStore.getCookies()); List<Cookie> matchedCookies = new ArrayList<Cookie>(); Date now = new Date(); for (Cookie cookie : cookies) { if (!cookie.isExpired(now)) { if (cookieSpec.match(cookie, cookieOrigin)) { if (this.log.isDebugEnabled()) { this.log.debug("Cookie " + cookie + " match " + cookieOrigin); } matchedCookies.add(cookie); } } else { if (this.log.isDebugEnabled()) { this.log.debug("Cookie " + cookie + " expired"); } } } if (!matchedCookies.isEmpty()) { List<Header> headers = cookieSpec.formatCookies(matchedCookies); for (Header header : headers) { request.addHeader(header); } } int ver = cookieSpec.getVersion(); if (ver > 0) { boolean needVersionHeader = false; for (Cookie cookie : matchedCookies) { if (ver != cookie.getVersion() || !(cookie instanceof SetCookie2)) { needVersionHeader = true; } } if (needVersionHeader) { Header header = cookieSpec.getVersionHeader(); if (header != null) { request.addHeader(header); } } } context.setAttribute(ClientContext.COOKIE_SPEC, cookieSpec); context.setAttribute(ClientContext.COOKIE_ORIGIN, cookieOrigin); } RequestAddCookies(); void process(final HttpRequest request, final HttpContext context);  }
@Test(expected=IllegalArgumentException.class) public void testRequestParameterCheck() throws Exception { HttpContext context = new BasicHttpContext(); HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(null, context); }
@Test(expected=IllegalArgumentException.class) public void testContextParameterCheck() throws Exception { HttpRequest request = new BasicHttpRequest("GET", "/"); HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, null); }
@Test public void testAddCookies() throws Exception { HttpRequest request = new BasicHttpRequest("GET", "/"); HttpRoute route = new HttpRoute(this.target, null, false); HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class); Mockito.when(conn.getRoute()).thenReturn(route); Mockito.when(conn.isSecure()).thenReturn(Boolean.FALSE); HttpContext context = new BasicHttpContext(); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore); context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); Header[] headers1 = request.getHeaders(SM.COOKIE); Assert.assertNotNull(headers1); Assert.assertEquals(2, headers1.length); Assert.assertEquals("$Version=1; name1=\"value1\"", headers1[0].getValue()); Assert.assertEquals("$Version=1; name2=\"value2\"", headers1[1].getValue()); Header[] headers2 = request.getHeaders(SM.COOKIE2); Assert.assertNotNull(headers2); Assert.assertEquals(0, headers2.length); CookieOrigin cookieOrigin = (CookieOrigin) context.getAttribute( ClientContext.COOKIE_ORIGIN); Assert.assertNotNull(cookieOrigin); Assert.assertEquals(this.target.getHostName(), cookieOrigin.getHost()); Assert.assertEquals(this.target.getPort(), cookieOrigin.getPort()); Assert.assertEquals("/", cookieOrigin.getPath()); Assert.assertFalse(cookieOrigin.isSecure()); }
@Test public void testCookiesForConnectRequest() throws Exception { HttpRequest request = new BasicHttpRequest("CONNECT", "www.somedomain.com"); HttpRoute route = new HttpRoute(this.target, null, false); HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class); Mockito.when(conn.getRoute()).thenReturn(route); Mockito.when(conn.isSecure()).thenReturn(Boolean.FALSE); HttpContext context = new BasicHttpContext(); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore); context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); Header[] headers1 = request.getHeaders(SM.COOKIE); Assert.assertNotNull(headers1); Assert.assertEquals(0, headers1.length); Header[] headers2 = request.getHeaders(SM.COOKIE2); Assert.assertNotNull(headers2); Assert.assertEquals(0, headers2.length); }
@Test public void testNoCookieStore() throws Exception { HttpRequest request = new BasicHttpRequest("GET", "/"); HttpRoute route = new HttpRoute(this.target, null, false); HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class); Mockito.when(conn.getRoute()).thenReturn(route); Mockito.when(conn.isSecure()).thenReturn(Boolean.FALSE); HttpContext context = new BasicHttpContext(); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ClientContext.COOKIE_STORE, null); context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); Header[] headers1 = request.getHeaders(SM.COOKIE); Assert.assertNotNull(headers1); Assert.assertEquals(0, headers1.length); Header[] headers2 = request.getHeaders(SM.COOKIE2); Assert.assertNotNull(headers2); Assert.assertEquals(0, headers2.length); }
@Test public void testNoCookieSpecRegistry() throws Exception { HttpRequest request = new BasicHttpRequest("GET", "/"); HttpRoute route = new HttpRoute(this.target, null, false); HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class); Mockito.when(conn.getRoute()).thenReturn(route); Mockito.when(conn.isSecure()).thenReturn(Boolean.FALSE); HttpContext context = new BasicHttpContext(); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore); context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, null); HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); Header[] headers1 = request.getHeaders(SM.COOKIE); Assert.assertNotNull(headers1); Assert.assertEquals(0, headers1.length); Header[] headers2 = request.getHeaders(SM.COOKIE2); Assert.assertNotNull(headers2); Assert.assertEquals(0, headers2.length); }
@Test public void testNoTargetHost() throws Exception { HttpRequest request = new BasicHttpRequest("GET", "/"); HttpRoute route = new HttpRoute(this.target, null, false); HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class); Mockito.when(conn.getRoute()).thenReturn(route); Mockito.when(conn.isSecure()).thenReturn(Boolean.FALSE); HttpContext context = new BasicHttpContext(); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, null); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore); context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); Header[] headers1 = request.getHeaders(SM.COOKIE); Assert.assertNotNull(headers1); Assert.assertEquals(0, headers1.length); Header[] headers2 = request.getHeaders(SM.COOKIE2); Assert.assertNotNull(headers2); Assert.assertEquals(0, headers2.length); }
@Test public void testNoHttpConnection() throws Exception { HttpRequest request = new BasicHttpRequest("GET", "/"); HttpContext context = new BasicHttpContext(); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_CONNECTION, null); context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore); context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); Header[] headers1 = request.getHeaders(SM.COOKIE); Assert.assertNotNull(headers1); Assert.assertEquals(0, headers1.length); Header[] headers2 = request.getHeaders(SM.COOKIE2); Assert.assertNotNull(headers2); Assert.assertEquals(0, headers2.length); }
@Test public void testAddCookiesUsingExplicitCookieSpec() throws Exception { HttpRequest request = new BasicHttpRequest("GET", "/"); request.getParams().setParameter(AllClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); HttpRoute route = new HttpRoute(this.target, null, false); HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class); Mockito.when(conn.getRoute()).thenReturn(route); Mockito.when(conn.isSecure()).thenReturn(Boolean.FALSE); HttpContext context = new BasicHttpContext(); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore); context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); CookieSpec cookieSpec = (CookieSpec) context.getAttribute( ClientContext.COOKIE_SPEC); Assert.assertTrue(cookieSpec instanceof BrowserCompatSpec); Header[] headers1 = request.getHeaders(SM.COOKIE); Assert.assertNotNull(headers1); Assert.assertEquals(1, headers1.length); Assert.assertEquals("name1=value1; name2=value2", headers1[0].getValue()); }
@Test(expected=HttpException.class) public void testAuthScopeInvalidRequestURI() throws Exception { HttpRequest request = new BasicHttpRequest("GET", "crap:"); HttpRoute route = new HttpRoute(this.target, null, false); HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class); Mockito.when(conn.getRoute()).thenReturn(route); Mockito.when(conn.isSecure()).thenReturn(Boolean.FALSE); HttpContext context = new BasicHttpContext(); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore); context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); }
@Test public void testAuthScopeRemotePortWhenDirect() throws Exception { HttpRequest request = new BasicHttpRequest("GET", "/stuff"); this.target = new HttpHost("localhost.local"); HttpRoute route = new HttpRoute(this.target, null, false); HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class); Mockito.when(conn.getRoute()).thenReturn(route); Mockito.when(conn.isSecure()).thenReturn(Boolean.FALSE); Mockito.when(conn.getRemoteAddress()).thenReturn(InetAddress.getByName("localhost")); Mockito.when(conn.getRemotePort()).thenReturn(1234); HttpContext context = new BasicHttpContext(); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore); context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); CookieOrigin cookieOrigin = (CookieOrigin) context.getAttribute( ClientContext.COOKIE_ORIGIN); Assert.assertNotNull(cookieOrigin); Assert.assertEquals(this.target.getHostName(), cookieOrigin.getHost()); Assert.assertEquals(1234, cookieOrigin.getPort()); Assert.assertEquals("/stuff", cookieOrigin.getPath()); Assert.assertFalse(cookieOrigin.isSecure()); }
@Test public void testAuthDefaultHttpPortWhenProxy() throws Exception { HttpRequest request = new BasicHttpRequest("GET", "/stuff"); this.target = new HttpHost("localhost.local"); HttpRoute route = new HttpRoute(this.target, null, new HttpHost("localhost", 8888), false); HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class); Mockito.when(conn.getRoute()).thenReturn(route); Mockito.when(conn.isSecure()).thenReturn(Boolean.FALSE); Mockito.when(conn.getRemotePort()).thenReturn(1234); HttpContext context = new BasicHttpContext(); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore); context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); CookieOrigin cookieOrigin = (CookieOrigin) context.getAttribute( ClientContext.COOKIE_ORIGIN); Assert.assertNotNull(cookieOrigin); Assert.assertEquals(this.target.getHostName(), cookieOrigin.getHost()); Assert.assertEquals(80, cookieOrigin.getPort()); Assert.assertEquals("/stuff", cookieOrigin.getPath()); Assert.assertFalse(cookieOrigin.isSecure()); }
@Test public void testAuthDefaultHttpsPortWhenProxy() throws Exception { HttpRequest request = new BasicHttpRequest("GET", "/stuff"); this.target = new HttpHost("localhost", -1, "https"); HttpRoute route = new HttpRoute(this.target, null, new HttpHost("localhost", 8888), true, TunnelType.TUNNELLED, LayerType.LAYERED); HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class); Mockito.when(conn.getRoute()).thenReturn(route); Mockito.when(conn.isSecure()).thenReturn(Boolean.TRUE); Mockito.when(conn.getRemoteAddress()).thenReturn(InetAddress.getByName("localhost")); Mockito.when(conn.getRemotePort()).thenReturn(1234); HttpContext context = new BasicHttpContext(); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore); context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); CookieOrigin cookieOrigin = (CookieOrigin) context.getAttribute( ClientContext.COOKIE_ORIGIN); Assert.assertNotNull(cookieOrigin); Assert.assertEquals(this.target.getHostName(), cookieOrigin.getHost()); Assert.assertEquals(443, cookieOrigin.getPort()); Assert.assertEquals("/stuff", cookieOrigin.getPath()); Assert.assertTrue(cookieOrigin.isSecure()); }
@Test public void testExcludeExpiredCookies() throws Exception { HttpRequest request = new BasicHttpRequest("GET", "/"); BasicClientCookie2 cookie3 = new BasicClientCookie2("name3", "value3"); cookie3.setVersion(1); cookie3.setDomain("localhost.local"); cookie3.setPath("/"); cookie3.setExpiryDate(new Date(System.currentTimeMillis() - (24 * 60 * 60 * 1000))); this.cookieStore.addCookie(cookie3); HttpRoute route = new HttpRoute(this.target, null, false); HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class); Mockito.when(conn.getRoute()).thenReturn(route); Mockito.when(conn.isSecure()).thenReturn(Boolean.FALSE); HttpContext context = new BasicHttpContext(); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore); context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); Header[] headers1 = request.getHeaders(SM.COOKIE); Assert.assertNotNull(headers1); Assert.assertEquals(2, headers1.length); Assert.assertEquals("$Version=1; name1=\"value1\"", headers1[0].getValue()); Assert.assertEquals("$Version=1; name2=\"value2\"", headers1[1].getValue()); Header[] headers2 = request.getHeaders(SM.COOKIE2); Assert.assertNotNull(headers2); Assert.assertEquals(0, headers2.length); }
@Test public void testNoMatchingCookies() throws Exception { HttpRequest request = new BasicHttpRequest("GET", "/"); this.cookieStore.clear(); BasicClientCookie cookie3 = new BasicClientCookie("name3", "value3"); cookie3.setDomain("www.somedomain.com"); cookie3.setPath("/"); this.cookieStore.addCookie(cookie3); HttpRoute route = new HttpRoute(this.target, null, false); HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class); Mockito.when(conn.getRoute()).thenReturn(route); Mockito.when(conn.isSecure()).thenReturn(Boolean.FALSE); HttpContext context = new BasicHttpContext(); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore); context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); Header[] headers1 = request.getHeaders(SM.COOKIE); Assert.assertNotNull(headers1); Assert.assertEquals(0, headers1.length); Header[] headers2 = request.getHeaders(SM.COOKIE2); Assert.assertNotNull(headers2); Assert.assertEquals(0, headers2.length); }
@Test public void testAddSpecVersionHeader() throws Exception { HttpRequest request = new BasicHttpRequest("GET", "/"); this.cookieStore.clear(); BasicClientCookie cookie1 = new BasicClientCookie("name1", "value1"); cookie1.setVersion(0); cookie1.setDomain("localhost.local"); cookie1.setPath("/"); this.cookieStore.addCookie(cookie1); BasicClientCookie cookie2 = new BasicClientCookie("name2", "value2"); cookie2.setVersion(0); cookie2.setDomain("localhost.local"); cookie2.setPath("/"); this.cookieStore.addCookie(cookie2); HttpRoute route = new HttpRoute(this.target, null, false); HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class); Mockito.when(conn.getRoute()).thenReturn(route); Mockito.when(conn.isSecure()).thenReturn(Boolean.FALSE); HttpContext context = new BasicHttpContext(); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore); context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); Header[] headers1 = request.getHeaders(SM.COOKIE); Assert.assertNotNull(headers1); Assert.assertEquals(1, headers1.length); Assert.assertEquals("name1=value1; name2=value2", headers1[0].getValue()); Header[] headers2 = request.getHeaders(SM.COOKIE2); Assert.assertNotNull(headers2); Assert.assertEquals(1, headers2.length); Assert.assertEquals("$Version=1", headers2[0].getValue()); }