Domain=domain Optional. The Domain attribute specifies the domain for which the cookie is valid. An explicitly specified domain must always start with a dot.
而在RFC6265中:
If the attribute-name case-insensitively matches the string “Domain”, the user agent MUST process the cookie-av as follows.
If the attribute-value is empty, the behavior is undefined. However, the user agent SHOULD ignore the cookie-av entirely.
If the first character of the attribute-value string is %x2E (“.”):
Let cookie-domain be the attribute-value without the leading %x2E (“.”) character.
Otherwise:
Let cookie-domain be the entire attribute-value.
Convert the cookie-domain to lower case.
一个说要.一个说不要,那再来看看具体代码实现(generateHeader方法)。
LegacyCookieProcessor(省略了部分代码)
1 2 3 4 5 6 7 8 9 10 11 12
@Override public String generateHeader(Cookie cookie) { ...... Stringdomain= cookie.getDomain(); ...... // Add domain information, if present if (domain != null) { buf.append("; Domain="); maybeQuote(buf, domain, version); } ...... }
privatevoidvalidateDomain(String domain) { inti=0; intprev= -1; intcur= -1; char[] chars = domain.toCharArray(); while (i < chars.length) { prev = cur; cur = chars[i]; if (!domainValid.get(cur)) { thrownewIllegalArgumentException(sm.getString( "rfc6265CookieProcessor.invalidDomain", domain)); } // labels must start with a letter or number // RFC6265实现 if ((prev == '.' || prev == -1) && (cur == '.' || cur == '-')) { thrownewIllegalArgumentException(sm.getString( "rfc6265CookieProcessor.invalidDomain", domain)); } // labels must end with a letter or number // RFC6265实现 if (prev == '-' && cur == '.') { thrownewIllegalArgumentException(sm.getString( "rfc6265CookieProcessor.invalidDomain", domain)); } i++; } // domain must end with a label if (cur == '.' || cur == '-') { thrownewIllegalArgumentException(sm.getString( "rfc6265CookieProcessor.invalidDomain", domain)); } }