View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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   * @since 4.0
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              // domain must start with dot
85              if (!domain.startsWith(".")) {
86                  throw new CookieRestrictionViolationException("Domain attribute \""
87                      + domain
88                      + "\" violates RFC 2109: domain must start with a dot");
89              }
90              // domain must have at least one embedded dot
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             // host minus domain may not contain any dots
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 }