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