@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); }
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 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 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(); }
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); }
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);  }