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