From: <rb...@us...> - 2014-03-11 17:37:59
|
Revision: 9173 http://sourceforge.net/p/htmlunit/code/9173 Author: rbri Date: 2014-03-11 17:37:56 +0000 (Tue, 11 Mar 2014) Log Message: ----------- refactoring: cleanup our cookie handling, now the manager takes care of its own state. This solves some problems introduced by the use of the new HttpClient api (background: the httpContext is not thread save but we share the context between our threads) Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/CookieManager.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/HttpWebConnection.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/CookieManagerTest.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2014-03-11 14:34:03 UTC (rev 9172) +++ trunk/htmlunit/src/changes/changes.xml 2014-03-11 17:37:56 UTC (rev 9173) @@ -8,6 +8,11 @@ <body> <release version="2.15" date="???" description="Bugfixes"> + <action type="update" dev="rbri"> + INCOMPATIBLE CHANGE: All public methods of CookieManager are taking care of the isCookiesEnabled() + state. Subclasses have to do the same. This was done as part of the migration to the new HttpClient + api. + </action> <action type="fix" dev="mguillem" issue="1562"> HTML parsing: unknown closing tags can't close any other unknown tags. </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/CookieManager.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/CookieManager.java 2014-03-11 14:34:03 UTC (rev 9172) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/CookieManager.java 2014-03-11 17:37:56 UTC (rev 9173) @@ -36,11 +36,15 @@ /** * Manages cookies for a {@link WebClient}. This class is thread-safe. + * You can disable Cookies by calling setCookiesEnabled(false). The + * CookieManager itself takes care of this and ignores all cookie request if + * disabled. If you override this your methods have to do the same. * * @version $Revision$ * @author Daniel Gredler * @author Ahmed Ashour * @author Nicolas Belisle + * @author Ronald Brill */ public class CookieManager implements Serializable { @@ -85,19 +89,29 @@ /** * Returns the currently configured cookies, in an unmodifiable set. + * If disabled, this returns an empty set. * @return the currently configured cookies, in an unmodifiable set */ public synchronized Set<Cookie> getCookies() { + if (!isCookiesEnabled()) { + return Collections.<Cookie>emptySet(); + } + final Set<Cookie> copy = new HashSet<Cookie>(cookies_); return Collections.unmodifiableSet(copy); } /** * Returns the currently configured cookies applicable to the specified URL, in an unmodifiable set. + * If disabled, this returns an empty set. * @param url the URL on which to filter the returned cookies * @return the currently configured cookies applicable to the specified URL, in an unmodifiable set */ public synchronized Set<Cookie> getCookies(final URL url) { + if (!isCookiesEnabled()) { + return Collections.<Cookie>emptySet(); + } + final String host = url.getHost(); final String path = url.getPath(); final String protocol = url.getProtocol(); @@ -130,10 +144,15 @@ /** * Clears all cookies that have expired before supplied date. + * If disabled, this returns false. * @param date the date to use for comparison when clearing expired cookies * @return whether any cookies were found expired, and were cleared */ public synchronized boolean clearExpired(final Date date) { + if (!isCookiesEnabled()) { + return false; + } + if (date == null) { return false; } @@ -166,10 +185,15 @@ /** * Returns the currently configured cookie with the specified name, or <tt>null</tt> if one does not exist. + * If disabled, this returns null. * @param name the name of the cookie to return * @return the currently configured cookie with the specified name, or <tt>null</tt> if one does not exist */ public synchronized Cookie getCookie(final String name) { + if (!isCookiesEnabled()) { + return null; + } + for (Cookie cookie : cookies_) { if (StringUtils.equals(cookie.getName(), name)) { return cookie; @@ -180,9 +204,14 @@ /** * Adds the specified cookie. + * If disabled, this does nothing. * @param cookie the cookie to add */ public synchronized void addCookie(final Cookie cookie) { + if (!isCookiesEnabled()) { + return; + } + cookies_.remove(cookie); // don't add expired cookie @@ -193,16 +222,26 @@ /** * Removes the specified cookie. + * If disabled, this does nothing. * @param cookie the cookie to remove */ public synchronized void removeCookie(final Cookie cookie) { + if (!isCookiesEnabled()) { + return; + } + cookies_.remove(cookie); } /** * Removes all cookies. + * If disabled, this does nothing. */ public synchronized void clearCookies() { + if (!isCookiesEnabled()) { + return; + } + cookies_.clear(); } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/HttpWebConnection.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/HttpWebConnection.java 2014-03-11 14:34:03 UTC (rev 9172) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/HttpWebConnection.java 2014-03-11 17:37:56 UTC (rev 9173) @@ -391,26 +391,6 @@ httpClient.setDefaultCredentialsProvider(credentialsProvider); httpContext_.removeAttribute(HttpClientContext.CREDS_PROVIDER); - if (webClient_.getCookieManager().isCookiesEnabled()) { - // Cookies are enabled. Note that it's important that we enable single cookie headers, - // for compatibility purposes. - httpClient.setDefaultCookieStore(new HtmlUnitCookieStore(webClient_.getCookieManager())); - } - else { - // Cookies are disabled. - httpClient.setDefaultCookieStore(new CookieStore() { - public void addCookie(final Cookie cookie) { /* empty */ } - public void clear() { /* empty */ } - public boolean clearExpired(final Date date) { - return false; - } - public List<Cookie> getCookies() { - return Collections.<Cookie>emptyList(); - } - }); - } - httpContext_.removeAttribute(HttpClientContext.COOKIE_STORE); - return httpMethod; } @@ -557,6 +537,8 @@ registeryBuilder.register(HACKED_COOKIE_POLICY, htmlUnitCookieSpecProvider_); httpClientBuilder_.setDefaultCookieSpecRegistry(registeryBuilder.build()); + + httpClientBuilder_.setDefaultCookieStore(new HtmlUnitCookieStore(webClient_.getCookieManager())); } return httpClientBuilder_; Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/CookieManagerTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/CookieManagerTest.java 2014-03-11 14:34:03 UTC (rev 9172) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/CookieManagerTest.java 2014-03-11 17:37:56 UTC (rev 9173) @@ -82,22 +82,32 @@ mgr.clearCookies(); assertTrue(mgr.getCookies().isEmpty()); + // Add a cookie before disabling cookies. + mgr.addCookie(cookie); + assertEquals(1, mgr.getCookies().size()); + // Disable cookies. mgr.setCookiesEnabled(false); assertFalse(mgr.isCookiesEnabled()); + assertEquals(0, mgr.getCookies().size()); + // Add a cookie after disabling cookies. final Cookie cookie2 = new Cookie("a", "b", "c", "d", new Date(System.currentTimeMillis() + 5000), false); - - // Add a cookie after disabling cookies. - mgr.addCookie(cookie); mgr.addCookie(cookie2); - assertEquals(2, mgr.getCookies().size()); + assertEquals(0, mgr.getCookies().size()); + assertFalse(mgr.clearExpired(new Date(System.currentTimeMillis() + 10000))); // Enable cookies again. mgr.setCookiesEnabled(true); assertTrue(mgr.isCookiesEnabled()); + assertEquals(1, mgr.getCookies().size()); // Clear expired cookies + assertFalse(mgr.clearExpired(new Date(System.currentTimeMillis() + 10000))); + assertEquals(1, mgr.getCookies().size()); + + mgr.addCookie(cookie2); + assertEquals(2, mgr.getCookies().size()); assertTrue(mgr.clearExpired(new Date(System.currentTimeMillis() + 10000))); assertEquals(1, mgr.getCookies().size()); } |