if (cookie == null) {
throw new IllegalArgumentException("Cookie may not be null");
}
if (origin == null) {
throw new IllegalArgumentException("Cookie origin may not be null");
}
// Validate the cookies domain attribute. NOTE: Domains without
// any dots are allowed to support hosts on private LANs that don't
// have DNS names. Since they have no dots, to domain-match the
// request-host and domain must be identical for the cookie to sent
// back to the origin-server.
String host = origin.getHost();
String domain = cookie.getDomain();
if (domain == null) {
throw new MalformedCookieException("Cookie domain may not be null");
}
if (host.contains(".")) {
// Not required to have at least two dots. RFC 2965.
// A Set-Cookie2 with Domain=ajax.com will be accepted.
// domain must match host
if (!host.endsWith(domain)) {
if (domain.startsWith(".")) {
domain = domain.substring(1, domain.length());
}
if (!host.equals(domain)) {
throw new MalformedCookieException(
"Illegal domain attribute \"" + domain
+ "\". Domain of origin: \"" + host + "\"");
}
}
} else {
if (!host.equals(domain)) {
throw new MalformedCookieException(
"Illegal domain attribute \"" + domain
+ "\". Domain of origin: \"" + host + "\"");
}
}