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