package org.apache.http.impl.cookie;
import java.util.Locale;
import org.apache.http.annotation.Contract;
import org.apache.http.annotation.ThreadingBehavior;
import org.apache.http.conn.util.InetAddressUtils;
import org.apache.http.cookie.ClientCookie;
import org.apache.http.cookie.CommonCookieAttributeHandler;
import org.apache.http.cookie.Cookie;
import org.apache.http.cookie.CookieOrigin;
import org.apache.http.cookie.CookieRestrictionViolationException;
import org.apache.http.cookie.MalformedCookieException;
import org.apache.http.cookie.SetCookie;
import org.apache.http.util.Args;
import org.apache.http.util.TextUtils;
@Contract(threading = ThreadingBehavior.IMMUTABLE)
public class BasicDomainHandler implements CommonCookieAttributeHandler {
public BasicDomainHandler() {
super();
}
@Override
public void parse(final SetCookie cookie, final String value)
throws MalformedCookieException {
Args.notNull(cookie, "Cookie");
if (TextUtils.isBlank(value)) {
throw new MalformedCookieException("Blank or null value for domain attribute");
}
if (value.endsWith(".")) {
return;
}
String domain = value;
if (domain.startsWith(".")) {
domain = domain.substring(1);
}
domain = domain.toLowerCase(Locale.ROOT);
cookie.setDomain(domain);
}
@Override
public void validate(final Cookie cookie, final CookieOrigin origin)
throws MalformedCookieException {
Args.notNull(cookie, "Cookie");
Args.notNull(origin, "Cookie origin");
final String host = origin.getHost();
final String domain = cookie.getDomain();
if (domain == null) {
throw new CookieRestrictionViolationException("Cookie 'domain' may not be null");
}
if (!host.equals(domain) && !domainMatch(domain, host)) {
throw new CookieRestrictionViolationException(
"Illegal 'domain' attribute \"" + domain + "\". Domain of origin: \"" + host + "\"");
}
}
static boolean domainMatch(final String domain, final String host) {
if (InetAddressUtils.isIPv4Address(host) || InetAddressUtils.isIPv6Address(host)) {
return false;
}
final String normalizedDomain = domain.startsWith(".") ? domain.substring(1) : domain;
if (host.endsWith(normalizedDomain)) {
final int prefix = host.length() - normalizedDomain.length();
if (prefix == 0) {
return true;
}
if (prefix > 1 && host.charAt(prefix - 1) == '.') {
return true;
}
}
return false;
}
@Override
public boolean match(final Cookie cookie, final CookieOrigin origin) {
Args.notNull(cookie, "Cookie");
Args.notNull(origin, "Cookie origin");
final String host = origin.getHost();
String domain = cookie.getDomain();
if (domain == null) {
return false;
}
if (domain.startsWith(".")) {
domain = domain.substring(1);
}
domain = domain.toLowerCase(Locale.ROOT);
if (host.equals(domain)) {
return true;
}
if (cookie instanceof ClientCookie) {
if (((ClientCookie) cookie).containsAttribute(ClientCookie.DOMAIN_ATTR)) {
return domainMatch(domain, host);
}
}
return false;
}
@Override
public String getAttributeName() {
return ClientCookie.DOMAIN_ATTR;
}
}