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
This commit is contained in:
Oleg Kalnichevski 2014-09-09 10:06:29 +00:00
parent d568ebdbf3
commit a4c0c6ed7c
4 changed files with 68 additions and 16 deletions

View File

@ -28,6 +28,7 @@
package org.apache.http.impl.cookie; package org.apache.http.impl.cookie;
import org.apache.http.annotation.Immutable; import org.apache.http.annotation.Immutable;
import org.apache.http.conn.util.PublicSuffixMatcher;
import org.apache.http.cookie.Cookie; import org.apache.http.cookie.Cookie;
import org.apache.http.cookie.CookieOrigin; import org.apache.http.cookie.CookieOrigin;
import org.apache.http.cookie.CookieSpec; import org.apache.http.cookie.CookieSpec;
@ -51,6 +52,7 @@ public class DefaultCookieSpecProvider implements CookieSpecProvider {
} }
private final CompatibilityLevel compatibilityLevel; private final CompatibilityLevel compatibilityLevel;
private final PublicSuffixMatcher publicSuffixMatcher;
private final String[] datepatterns; private final String[] datepatterns;
private final boolean oneHeader; private final boolean oneHeader;
@ -58,16 +60,24 @@ public class DefaultCookieSpecProvider implements CookieSpecProvider {
public DefaultCookieSpecProvider( public DefaultCookieSpecProvider(
final CompatibilityLevel compatibilityLevel, final CompatibilityLevel compatibilityLevel,
final PublicSuffixMatcher publicSuffixMatcher,
final String[] datepatterns, final String[] datepatterns,
final boolean oneHeader) { final boolean oneHeader) {
super(); super();
this.compatibilityLevel = compatibilityLevel != null ? compatibilityLevel : CompatibilityLevel.DEFAULT; this.compatibilityLevel = compatibilityLevel != null ? compatibilityLevel : CompatibilityLevel.DEFAULT;
this.publicSuffixMatcher = publicSuffixMatcher;
this.datepatterns = datepatterns; this.datepatterns = datepatterns;
this.oneHeader = oneHeader; this.oneHeader = oneHeader;
} }
public DefaultCookieSpecProvider(
final CompatibilityLevel compatibilityLevel,
final PublicSuffixMatcher publicSuffixMatcher) {
this(compatibilityLevel, publicSuffixMatcher, null, false);
}
public DefaultCookieSpecProvider() { public DefaultCookieSpecProvider() {
this(CompatibilityLevel.DEFAULT, null, false); this(CompatibilityLevel.DEFAULT, null, null, false);
} }
@Override @Override
@ -78,7 +88,8 @@ public class DefaultCookieSpecProvider implements CookieSpecProvider {
final RFC2965Spec strict = new RFC2965Spec(this.oneHeader, final RFC2965Spec strict = new RFC2965Spec(this.oneHeader,
new RFC2965VersionAttributeHandler(), new RFC2965VersionAttributeHandler(),
new BasicPathHandler(), new BasicPathHandler(),
new RFC2965DomainAttributeHandler(), PublicSuffixDomainFilter.decorate(
new RFC2965DomainAttributeHandler(), this.publicSuffixMatcher),
new RFC2965PortAttributeHandler(), new RFC2965PortAttributeHandler(),
new BasicMaxAgeHandler(), new BasicMaxAgeHandler(),
new BasicSecureHandler(), new BasicSecureHandler(),
@ -88,12 +99,14 @@ public class DefaultCookieSpecProvider implements CookieSpecProvider {
final RFC2109Spec obsoleteStrict = new RFC2109Spec(this.oneHeader, final RFC2109Spec obsoleteStrict = new RFC2109Spec(this.oneHeader,
new RFC2109VersionHandler(), new RFC2109VersionHandler(),
new BasicPathHandler(), new BasicPathHandler(),
new RFC2109DomainHandler(), PublicSuffixDomainFilter.decorate(
new RFC2109DomainHandler(), this.publicSuffixMatcher),
new BasicMaxAgeHandler(), new BasicMaxAgeHandler(),
new BasicSecureHandler(), new BasicSecureHandler(),
new BasicCommentHandler()); new BasicCommentHandler());
final NetscapeDraftSpec netscapeDraft = new NetscapeDraftSpec( final NetscapeDraftSpec netscapeDraft = new NetscapeDraftSpec(
new BasicDomainHandler(), PublicSuffixDomainFilter.decorate(
new BasicDomainHandler(), this.publicSuffixMatcher),
this.compatibilityLevel == CompatibilityLevel.IE_MEDIUM_SECURITY ? this.compatibilityLevel == CompatibilityLevel.IE_MEDIUM_SECURITY ?
new BasicPathHandler() { new BasicPathHandler() {
@Override @Override

View File

@ -51,18 +51,20 @@ import org.apache.http.util.Args;
public class PublicSuffixDomainFilter implements CommonCookieAttributeHandler { public class PublicSuffixDomainFilter implements CommonCookieAttributeHandler {
private final CommonCookieAttributeHandler handler; 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.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(handler, "Cookie handler");
Args.notNull(suffixList, "Public suffix list"); Args.notNull(suffixList, "Public suffix list");
this.handler = handler; 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 @Override
public boolean match(final Cookie cookie, final CookieOrigin origin) { public boolean match(final Cookie cookie, final CookieOrigin origin) {
final String domain = cookie.getDomain(); final String domain = cookie.getDomain();
if (!domain.equalsIgnoreCase("localhost") && matcher.matches(domain)) { if (!domain.equalsIgnoreCase("localhost") && publicSuffixMatcher.matches(domain)) {
return false; return false;
} else { } else {
return handler.match(cookie, origin); return handler.match(cookie, origin);
@ -93,4 +95,10 @@ public class PublicSuffixDomainFilter implements CommonCookieAttributeHandler {
return handler.getAttributeName(); 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;
}
} }

View File

@ -28,6 +28,7 @@
package org.apache.http.impl.cookie; package org.apache.http.impl.cookie;
import org.apache.http.annotation.Immutable; import org.apache.http.annotation.Immutable;
import org.apache.http.conn.util.PublicSuffixMatcher;
import org.apache.http.cookie.CookieSpec; import org.apache.http.cookie.CookieSpec;
import org.apache.http.cookie.CookieSpecProvider; import org.apache.http.cookie.CookieSpecProvider;
import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpContext;
@ -42,17 +43,23 @@ import org.apache.http.protocol.HttpContext;
@Immutable @Immutable
public class RFC2109SpecProvider implements CookieSpecProvider { public class RFC2109SpecProvider implements CookieSpecProvider {
private final PublicSuffixMatcher publicSuffixMatcher;
private final boolean oneHeader; private final boolean oneHeader;
private volatile CookieSpec cookieSpec; private volatile CookieSpec cookieSpec;
public RFC2109SpecProvider(final boolean oneHeader) { public RFC2109SpecProvider(final PublicSuffixMatcher publicSuffixMatcher, final boolean oneHeader) {
super(); super();
this.oneHeader = oneHeader; this.oneHeader = oneHeader;
this.publicSuffixMatcher = publicSuffixMatcher;
}
public RFC2109SpecProvider(final PublicSuffixMatcher publicSuffixMatcher) {
this(publicSuffixMatcher, false);
} }
public RFC2109SpecProvider() { public RFC2109SpecProvider() {
this(false); this(null, false);
} }
@Override @Override
@ -60,7 +67,14 @@ public class RFC2109SpecProvider implements CookieSpecProvider {
if (cookieSpec == null) { if (cookieSpec == null) {
synchronized (this) { synchronized (this) {
if (cookieSpec == null) { 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());
} }
} }
} }

View File

@ -28,6 +28,7 @@
package org.apache.http.impl.cookie; package org.apache.http.impl.cookie;
import org.apache.http.annotation.Immutable; import org.apache.http.annotation.Immutable;
import org.apache.http.conn.util.PublicSuffixMatcher;
import org.apache.http.cookie.CookieSpec; import org.apache.http.cookie.CookieSpec;
import org.apache.http.cookie.CookieSpecProvider; import org.apache.http.cookie.CookieSpecProvider;
import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpContext;
@ -42,17 +43,23 @@ import org.apache.http.protocol.HttpContext;
@Immutable @Immutable
public class RFC2965SpecProvider implements CookieSpecProvider { public class RFC2965SpecProvider implements CookieSpecProvider {
private final PublicSuffixMatcher publicSuffixMatcher;
private final boolean oneHeader; private final boolean oneHeader;
private volatile CookieSpec cookieSpec; private volatile CookieSpec cookieSpec;
public RFC2965SpecProvider(final boolean oneHeader) { public RFC2965SpecProvider(final PublicSuffixMatcher publicSuffixMatcher, final boolean oneHeader) {
super(); super();
this.oneHeader = oneHeader; this.oneHeader = oneHeader;
this.publicSuffixMatcher = publicSuffixMatcher;
}
public RFC2965SpecProvider(final PublicSuffixMatcher publicSuffixMatcher) {
this(publicSuffixMatcher, false);
} }
public RFC2965SpecProvider() { public RFC2965SpecProvider() {
this(false); this(null, false);
} }
@Override @Override
@ -60,7 +67,17 @@ public class RFC2965SpecProvider implements CookieSpecProvider {
if (cookieSpec == null) { if (cookieSpec == null) {
synchronized (this) { synchronized (this) {
if (cookieSpec == null) { 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());
} }
} }
} }