throw new IllegalArgumentException("Cookie origin may not be null");
}
String host = origin.getHost();
String domain = cookie.getDomain();
if (domain == null) {
throw new CookieRestrictionViolationException("Cookie domain may not be null");
}
if (!domain.equals(host)) {
int dotIndex = domain.indexOf('.');
if (dotIndex == -1) {
throw new CookieRestrictionViolationException("Domain attribute \""
+ domain
+ "\" does not match the host \""
+ host + "\"");
}
// domain must start with dot
if (!domain.startsWith(".")) {
throw new CookieRestrictionViolationException("Domain attribute \""
+ domain
+ "\" violates RFC 2109: domain must start with a dot");
}
// domain must have at least one embedded dot
dotIndex = domain.indexOf('.', 1);
if (dotIndex < 0 || dotIndex == domain.length() - 1) {
throw new CookieRestrictionViolationException("Domain attribute \""
+ domain
+ "\" violates RFC 2109: domain must contain an embedded dot");
}
host = host.toLowerCase(Locale.ENGLISH);
if (!host.endsWith(domain)) {
throw new CookieRestrictionViolationException(
"Illegal domain attribute \"" + domain
+ "\". Domain of origin: \"" + host + "\"");
}
// host minus domain may not contain any dots
String hostWithoutDomain = host.substring(0, host.length() - domain.length());
if (hostWithoutDomain.indexOf('.') != -1) {
throw new CookieRestrictionViolationException("Domain attribute \""
+ domain
+ "\" violates RFC 2109: host minus domain may not contain any dots");
}
}
}