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.Immutable;
32 import org.apache.http.cookie.Cookie;
33 import org.apache.http.cookie.CookieAttributeHandler;
34 import org.apache.http.cookie.CookieOrigin;
35 import org.apache.http.cookie.CookieRestrictionViolationException;
36 import org.apache.http.cookie.MalformedCookieException;
37 import org.apache.http.cookie.SetCookie;
38 import org.apache.http.util.Args;
39
40
41
42
43
44 @Immutable
45 public class RFC2109DomainHandler implements CookieAttributeHandler {
46
47 public RFC2109DomainHandler() {
48 super();
49 }
50
51 public void parse(final SetCookie cookie, final String value)
52 throws MalformedCookieException {
53 Args.notNull(cookie, "Cookie");
54 if (value == null) {
55 throw new MalformedCookieException("Missing value for domain attribute");
56 }
57 if (value.trim().length() == 0) {
58 throw new MalformedCookieException("Blank value for domain attribute");
59 }
60 cookie.setDomain(value);
61 }
62
63 public void validate(final Cookie cookie, final CookieOrigin origin)
64 throws MalformedCookieException {
65 Args.notNull(cookie, "Cookie");
66 Args.notNull(origin, "Cookie origin");
67 String host = origin.getHost();
68 final String domain = cookie.getDomain();
69 if (domain == null) {
70 throw new CookieRestrictionViolationException("Cookie domain may not be null");
71 }
72 if (!domain.equals(host)) {
73 int dotIndex = domain.indexOf('.');
74 if (dotIndex == -1) {
75 throw new CookieRestrictionViolationException("Domain attribute \""
76 + domain
77 + "\" does not match the host \""
78 + host + "\"");
79 }
80
81 if (!domain.startsWith(".")) {
82 throw new CookieRestrictionViolationException("Domain attribute \""
83 + domain
84 + "\" violates RFC 2109: domain must start with a dot");
85 }
86
87 dotIndex = domain.indexOf('.', 1);
88 if (dotIndex < 0 || dotIndex == domain.length() - 1) {
89 throw new CookieRestrictionViolationException("Domain attribute \""
90 + domain
91 + "\" violates RFC 2109: domain must contain an embedded dot");
92 }
93 host = host.toLowerCase(Locale.ENGLISH);
94 if (!host.endsWith(domain)) {
95 throw new CookieRestrictionViolationException(
96 "Illegal domain attribute \"" + domain
97 + "\". Domain of origin: \"" + host + "\"");
98 }
99
100 final String hostWithoutDomain = host.substring(0, host.length() - domain.length());
101 if (hostWithoutDomain.indexOf('.') != -1) {
102 throw new CookieRestrictionViolationException("Domain attribute \""
103 + domain
104 + "\" violates RFC 2109: host minus domain may not contain any dots");
105 }
106 }
107 }
108
109 public boolean match(final Cookie cookie, final CookieOrigin origin) {
110 Args.notNull(cookie, "Cookie");
111 Args.notNull(origin, "Cookie origin");
112 final String host = origin.getHost();
113 final String domain = cookie.getDomain();
114 if (domain == null) {
115 return false;
116 }
117 return host.equals(domain) || (domain.startsWith(".") && host.endsWith(domain));
118 }
119
120 }