From a4c0c6ed7cde8b41258b6e04f11752f37d1f3102 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Tue, 9 Sep 2014 10:06:29 +0000 Subject: [PATCH] Default and standard cookie specs to use public suffix list when available git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1623734 13f79535-47bb-0310-9956-ffa450edef68 --- .../cookie/DefaultCookieSpecProvider.java | 21 +++++++++++++---- .../impl/cookie/PublicSuffixDomainFilter.java | 20 +++++++++++----- .../http/impl/cookie/RFC2109SpecProvider.java | 20 +++++++++++++--- .../http/impl/cookie/RFC2965SpecProvider.java | 23 ++++++++++++++++--- 4 files changed, 68 insertions(+), 16 deletions(-) diff --git a/httpclient/src/main/java/org/apache/http/impl/cookie/DefaultCookieSpecProvider.java b/httpclient/src/main/java/org/apache/http/impl/cookie/DefaultCookieSpecProvider.java index a56fb58e5..8ed8250ad 100644 --- a/httpclient/src/main/java/org/apache/http/impl/cookie/DefaultCookieSpecProvider.java +++ b/httpclient/src/main/java/org/apache/http/impl/cookie/DefaultCookieSpecProvider.java @@ -28,6 +28,7 @@ package org.apache.http.impl.cookie; import org.apache.http.annotation.Immutable; +import org.apache.http.conn.util.PublicSuffixMatcher; import org.apache.http.cookie.Cookie; import org.apache.http.cookie.CookieOrigin; import org.apache.http.cookie.CookieSpec; @@ -51,6 +52,7 @@ public class DefaultCookieSpecProvider implements CookieSpecProvider { } private final CompatibilityLevel compatibilityLevel; + private final PublicSuffixMatcher publicSuffixMatcher; private final String[] datepatterns; private final boolean oneHeader; @@ -58,16 +60,24 @@ public class DefaultCookieSpecProvider implements CookieSpecProvider { public DefaultCookieSpecProvider( final CompatibilityLevel compatibilityLevel, + final PublicSuffixMatcher publicSuffixMatcher, final String[] datepatterns, final boolean oneHeader) { super(); this.compatibilityLevel = compatibilityLevel != null ? compatibilityLevel : CompatibilityLevel.DEFAULT; + this.publicSuffixMatcher = publicSuffixMatcher; this.datepatterns = datepatterns; this.oneHeader = oneHeader; } + public DefaultCookieSpecProvider( + final CompatibilityLevel compatibilityLevel, + final PublicSuffixMatcher publicSuffixMatcher) { + this(compatibilityLevel, publicSuffixMatcher, null, false); + } + public DefaultCookieSpecProvider() { - this(CompatibilityLevel.DEFAULT, null, false); + this(CompatibilityLevel.DEFAULT, null, null, false); } @Override @@ -78,7 +88,8 @@ public class DefaultCookieSpecProvider implements CookieSpecProvider { final RFC2965Spec strict = new RFC2965Spec(this.oneHeader, new RFC2965VersionAttributeHandler(), new BasicPathHandler(), - new RFC2965DomainAttributeHandler(), + PublicSuffixDomainFilter.decorate( + new RFC2965DomainAttributeHandler(), this.publicSuffixMatcher), new RFC2965PortAttributeHandler(), new BasicMaxAgeHandler(), new BasicSecureHandler(), @@ -88,12 +99,14 @@ public class DefaultCookieSpecProvider implements CookieSpecProvider { final RFC2109Spec obsoleteStrict = new RFC2109Spec(this.oneHeader, new RFC2109VersionHandler(), new BasicPathHandler(), - new RFC2109DomainHandler(), + PublicSuffixDomainFilter.decorate( + new RFC2109DomainHandler(), this.publicSuffixMatcher), new BasicMaxAgeHandler(), new BasicSecureHandler(), new BasicCommentHandler()); final NetscapeDraftSpec netscapeDraft = new NetscapeDraftSpec( - new BasicDomainHandler(), + PublicSuffixDomainFilter.decorate( + new BasicDomainHandler(), this.publicSuffixMatcher), this.compatibilityLevel == CompatibilityLevel.IE_MEDIUM_SECURITY ? new BasicPathHandler() { @Override diff --git a/httpclient/src/main/java/org/apache/http/impl/cookie/PublicSuffixDomainFilter.java b/httpclient/src/main/java/org/apache/http/impl/cookie/PublicSuffixDomainFilter.java index 65441b60b..3e44ae89a 100644 --- a/httpclient/src/main/java/org/apache/http/impl/cookie/PublicSuffixDomainFilter.java +++ b/httpclient/src/main/java/org/apache/http/impl/cookie/PublicSuffixDomainFilter.java @@ -51,18 +51,20 @@ import org.apache.http.util.Args; public class PublicSuffixDomainFilter implements CommonCookieAttributeHandler { private final CommonCookieAttributeHandler handler; - private final PublicSuffixMatcher matcher; + private final PublicSuffixMatcher publicSuffixMatcher; - public PublicSuffixDomainFilter(final CommonCookieAttributeHandler handler, final PublicSuffixMatcher matcher) { + public PublicSuffixDomainFilter( + final CommonCookieAttributeHandler handler, final PublicSuffixMatcher publicSuffixMatcher) { this.handler = Args.notNull(handler, "Cookie handler"); - this.matcher = Args.notNull(matcher, "Public suffix matcher"); + this.publicSuffixMatcher = Args.notNull(publicSuffixMatcher, "Public suffix matcher"); } - public PublicSuffixDomainFilter(final CommonCookieAttributeHandler handler, final PublicSuffixList suffixList) { + public PublicSuffixDomainFilter( + final CommonCookieAttributeHandler handler, final PublicSuffixList suffixList) { Args.notNull(handler, "Cookie handler"); Args.notNull(suffixList, "Public suffix list"); this.handler = handler; - this.matcher = new PublicSuffixMatcher(suffixList.getRules(), suffixList.getExceptions()); + this.publicSuffixMatcher = new PublicSuffixMatcher(suffixList.getRules(), suffixList.getExceptions()); } /** @@ -71,7 +73,7 @@ public class PublicSuffixDomainFilter implements CommonCookieAttributeHandler { @Override public boolean match(final Cookie cookie, final CookieOrigin origin) { final String domain = cookie.getDomain(); - if (!domain.equalsIgnoreCase("localhost") && matcher.matches(domain)) { + if (!domain.equalsIgnoreCase("localhost") && publicSuffixMatcher.matches(domain)) { return false; } else { return handler.match(cookie, origin); @@ -93,4 +95,10 @@ public class PublicSuffixDomainFilter implements CommonCookieAttributeHandler { return handler.getAttributeName(); } + public static CommonCookieAttributeHandler decorate( + final CommonCookieAttributeHandler handler, final PublicSuffixMatcher publicSuffixMatcher) { + Args.notNull(handler, "Cookie attribute handler"); + return publicSuffixMatcher != null ? new PublicSuffixDomainFilter(handler, publicSuffixMatcher) : handler; + } + } diff --git a/httpclient/src/main/java/org/apache/http/impl/cookie/RFC2109SpecProvider.java b/httpclient/src/main/java/org/apache/http/impl/cookie/RFC2109SpecProvider.java index 7429c430a..ba7e6c7a0 100644 --- a/httpclient/src/main/java/org/apache/http/impl/cookie/RFC2109SpecProvider.java +++ b/httpclient/src/main/java/org/apache/http/impl/cookie/RFC2109SpecProvider.java @@ -28,6 +28,7 @@ package org.apache.http.impl.cookie; import org.apache.http.annotation.Immutable; +import org.apache.http.conn.util.PublicSuffixMatcher; import org.apache.http.cookie.CookieSpec; import org.apache.http.cookie.CookieSpecProvider; import org.apache.http.protocol.HttpContext; @@ -42,17 +43,23 @@ import org.apache.http.protocol.HttpContext; @Immutable public class RFC2109SpecProvider implements CookieSpecProvider { + private final PublicSuffixMatcher publicSuffixMatcher; private final boolean oneHeader; private volatile CookieSpec cookieSpec; - public RFC2109SpecProvider(final boolean oneHeader) { + public RFC2109SpecProvider(final PublicSuffixMatcher publicSuffixMatcher, final boolean oneHeader) { super(); this.oneHeader = oneHeader; + this.publicSuffixMatcher = publicSuffixMatcher; + } + + public RFC2109SpecProvider(final PublicSuffixMatcher publicSuffixMatcher) { + this(publicSuffixMatcher, false); } public RFC2109SpecProvider() { - this(false); + this(null, false); } @Override @@ -60,7 +67,14 @@ public class RFC2109SpecProvider implements CookieSpecProvider { if (cookieSpec == null) { synchronized (this) { if (cookieSpec == null) { - this.cookieSpec = new RFC2109Spec(null, this.oneHeader); + this.cookieSpec = new RFC2109Spec(this.oneHeader, + new RFC2109VersionHandler(), + new BasicPathHandler(), + PublicSuffixDomainFilter.decorate( + new RFC2109DomainHandler(), this.publicSuffixMatcher), + new BasicMaxAgeHandler(), + new BasicSecureHandler(), + new BasicCommentHandler()); } } } diff --git a/httpclient/src/main/java/org/apache/http/impl/cookie/RFC2965SpecProvider.java b/httpclient/src/main/java/org/apache/http/impl/cookie/RFC2965SpecProvider.java index 62f595593..7e74d6d0c 100644 --- a/httpclient/src/main/java/org/apache/http/impl/cookie/RFC2965SpecProvider.java +++ b/httpclient/src/main/java/org/apache/http/impl/cookie/RFC2965SpecProvider.java @@ -28,6 +28,7 @@ package org.apache.http.impl.cookie; import org.apache.http.annotation.Immutable; +import org.apache.http.conn.util.PublicSuffixMatcher; import org.apache.http.cookie.CookieSpec; import org.apache.http.cookie.CookieSpecProvider; import org.apache.http.protocol.HttpContext; @@ -42,17 +43,23 @@ import org.apache.http.protocol.HttpContext; @Immutable public class RFC2965SpecProvider implements CookieSpecProvider { + private final PublicSuffixMatcher publicSuffixMatcher; private final boolean oneHeader; private volatile CookieSpec cookieSpec; - public RFC2965SpecProvider(final boolean oneHeader) { + public RFC2965SpecProvider(final PublicSuffixMatcher publicSuffixMatcher, final boolean oneHeader) { super(); this.oneHeader = oneHeader; + this.publicSuffixMatcher = publicSuffixMatcher; + } + + public RFC2965SpecProvider(final PublicSuffixMatcher publicSuffixMatcher) { + this(publicSuffixMatcher, false); } public RFC2965SpecProvider() { - this(false); + this(null, false); } @Override @@ -60,7 +67,17 @@ public class RFC2965SpecProvider implements CookieSpecProvider { if (cookieSpec == null) { synchronized (this) { if (cookieSpec == null) { - this.cookieSpec = new RFC2965Spec(null, this.oneHeader); + this.cookieSpec = new RFC2965Spec(this.oneHeader, + new RFC2965VersionAttributeHandler(), + new BasicPathHandler(), + PublicSuffixDomainFilter.decorate( + new RFC2965DomainAttributeHandler(), this.publicSuffixMatcher), + new RFC2965PortAttributeHandler(), + new BasicMaxAgeHandler(), + new BasicSecureHandler(), + new BasicCommentHandler(), + new RFC2965CommentUrlAttributeHandler(), + new RFC2965DiscardAttributeHandler()); } } }