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