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  import java.util.StringTokenizer;
31  
32  import org.apache.http.annotation.Contract;
33  import org.apache.http.annotation.ThreadingBehavior;
34  import org.apache.http.cookie.ClientCookie;
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  import org.apache.http.util.TextUtils;
42  
43  /**
44   *
45   * @since 4.0
46   */
47  @Contract(threading = ThreadingBehavior.IMMUTABLE)
48  public class NetscapeDomainHandler extends BasicDomainHandler {
49  
50      public NetscapeDomainHandler() {
51          super();
52      }
53  
54      @Override
55      public void parse(final SetCookie cookie, final String value) throws MalformedCookieException {
56          Args.notNull(cookie, "Cookie");
57          if (TextUtils.isBlank(value)) {
58              throw new MalformedCookieException("Blank or null value for domain attribute");
59          }
60          cookie.setDomain(value);
61      }
62  
63      @Override
64      public void validate(final Cookie cookie, final CookieOrigin origin)
65              throws MalformedCookieException {
66          final String host = origin.getHost();
67          final String domain = cookie.getDomain();
68          if (!host.equals(domain) && !BasicDomainHandler.domainMatch(domain, host)) {
69              throw new CookieRestrictionViolationException(
70                      "Illegal domain attribute \"" + domain + "\". Domain of origin: \"" + host + "\"");
71          }
72          if (host.contains(".")) {
73              final int domainParts = new StringTokenizer(domain, ".").countTokens();
74  
75              if (isSpecialDomain(domain)) {
76                  if (domainParts < 2) {
77                      throw new CookieRestrictionViolationException("Domain attribute \""
78                          + domain
79                          + "\" violates the Netscape cookie specification for "
80                          + "special domains");
81                  }
82              } else {
83                  if (domainParts < 3) {
84                      throw new CookieRestrictionViolationException("Domain attribute \""
85                          + domain
86                          + "\" violates the Netscape cookie specification");
87                  }
88              }
89          }
90      }
91  
92     /**
93      * Checks if the given domain is in one of the seven special
94      * top level domains defined by the Netscape cookie specification.
95      * @param domain The domain.
96      * @return True if the specified domain is "special"
97      */
98     private static boolean isSpecialDomain(final String domain) {
99         final String ucDomain = domain.toUpperCase(Locale.ROOT);
100        return ucDomain.endsWith(".COM")
101                || ucDomain.endsWith(".EDU")
102                || ucDomain.endsWith(".NET")
103                || ucDomain.endsWith(".GOV")
104                || ucDomain.endsWith(".MIL")
105                || ucDomain.endsWith(".ORG")
106                || ucDomain.endsWith(".INT");
107    }
108 
109    @Override
110    public boolean match(final Cookie cookie, final CookieOrigin origin) {
111        Args.notNull(cookie, "Cookie");
112        Args.notNull(origin, "Cookie origin");
113        final String host = origin.getHost();
114        final String domain = cookie.getDomain();
115        if (domain == null) {
116            return false;
117        }
118        return host.endsWith(domain);
119    }
120 
121     @Override
122     public String getAttributeName() {
123         return ClientCookie.DOMAIN_ATTR;
124     }
125 
126 }