1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.http.impl.cookie;
28
29 import java.util.Locale;
30
31 import org.apache.http.annotation.Contract;
32 import org.apache.http.annotation.ThreadingBehavior;
33 import org.apache.http.cookie.ClientCookie;
34 import org.apache.http.cookie.CommonCookieAttributeHandler;
35 import org.apache.http.cookie.Cookie;
36 import org.apache.http.cookie.CookieOrigin;
37 import org.apache.http.cookie.CookieRestrictionViolationException;
38 import org.apache.http.cookie.MalformedCookieException;
39 import org.apache.http.cookie.SetCookie;
40 import org.apache.http.util.Args;
41
42
43
44
45
46 @Contract(threading = ThreadingBehavior.IMMUTABLE)
47 public class RFC2109DomainHandler implements CommonCookieAttributeHandler {
48
49 public RFC2109DomainHandler() {
50 super();
51 }
52
53 @Override
54 public void parse(final SetCookie cookie, final String value)
55 throws MalformedCookieException {
56 Args.notNull(cookie, "Cookie");
57 if (value == null) {
58 throw new MalformedCookieException("Missing value for domain attribute");
59 }
60 if (value.trim().isEmpty()) {
61 throw new MalformedCookieException("Blank value for domain attribute");
62 }
63 cookie.setDomain(value);
64 }
65
66 @Override
67 public void validate(final Cookie cookie, final CookieOrigin origin)
68 throws MalformedCookieException {
69 Args.notNull(cookie, "Cookie");
70 Args.notNull(origin, "Cookie origin");
71 String host = origin.getHost();
72 final String domain = cookie.getDomain();
73 if (domain == null) {
74 throw new CookieRestrictionViolationException("Cookie domain may not be null");
75 }
76 if (!domain.equals(host)) {
77 int dotIndex = domain.indexOf('.');
78 if (dotIndex == -1) {
79 throw new CookieRestrictionViolationException("Domain attribute \""
80 + domain
81 + "\" does not match the host \""
82 + host + "\"");
83 }
84
85 if (!domain.startsWith(".")) {
86 throw new CookieRestrictionViolationException("Domain attribute \""
87 + domain
88 + "\" violates RFC 2109: domain must start with a dot");
89 }
90
91 dotIndex = domain.indexOf('.', 1);
92 if (dotIndex < 0 || dotIndex == domain.length() - 1) {
93 throw new CookieRestrictionViolationException("Domain attribute \""
94 + domain
95 + "\" violates RFC 2109: domain must contain an embedded dot");
96 }
97 host = host.toLowerCase(Locale.ROOT);
98 if (!host.endsWith(domain)) {
99 throw new CookieRestrictionViolationException(
100 "Illegal domain attribute \"" + domain
101 + "\". Domain of origin: \"" + host + "\"");
102 }
103
104 final String hostWithoutDomain = host.substring(0, host.length() - domain.length());
105 if (hostWithoutDomain.indexOf('.') != -1) {
106 throw new CookieRestrictionViolationException("Domain attribute \""
107 + domain
108 + "\" violates RFC 2109: host minus domain may not contain any dots");
109 }
110 }
111 }
112
113 @Override
114 public boolean match(final Cookie cookie, final CookieOrigin origin) {
115 Args.notNull(cookie, "Cookie");
116 Args.notNull(origin, "Cookie origin");
117 final String host = origin.getHost();
118 final String domain = cookie.getDomain();
119 if (domain == null) {
120 return false;
121 }
122 return host.equals(domain) || (domain.startsWith(".") && host.endsWith(domain));
123 }
124
125 @Override
126 public String getAttributeName() {
127 return ClientCookie.DOMAIN_ATTR;
128 }
129
130 }