• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/cookie/RFC2109DomainHandler.java $
3  * $Revision: 653041 $
4  * $Date: 2008-05-03 03:39:28 -0700 (Sat, 03 May 2008) $
5  *
6  * ====================================================================
7  * Licensed to the Apache Software Foundation (ASF) under one
8  * or more contributor license agreements.  See the NOTICE file
9  * distributed with this work for additional information
10  * regarding copyright ownership.  The ASF licenses this file
11  * to you under the Apache License, Version 2.0 (the
12  * "License"); you may not use this file except in compliance
13  * with the License.  You may obtain a copy of the License at
14  *
15  *   http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing,
18  * software distributed under the License is distributed on an
19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20  * KIND, either express or implied.  See the License for the
21  * specific language governing permissions and limitations
22  * under the License.
23  * ====================================================================
24  *
25  * This software consists of voluntary contributions made by many
26  * individuals on behalf of the Apache Software Foundation.  For more
27  * information on the Apache Software Foundation, please see
28  * <http://www.apache.org/>.
29  *
30  */
31 package org.apache.http.impl.cookie;
32 
33 import java.util.Locale;
34 
35 import org.apache.http.cookie.Cookie;
36 import org.apache.http.cookie.CookieAttributeHandler;
37 import org.apache.http.cookie.CookieOrigin;
38 import org.apache.http.cookie.MalformedCookieException;
39 import org.apache.http.cookie.SetCookie;
40 
41 public class RFC2109DomainHandler implements CookieAttributeHandler {
42 
RFC2109DomainHandler()43     public RFC2109DomainHandler() {
44         super();
45     }
46 
parse(final SetCookie cookie, final String value)47     public void parse(final SetCookie cookie, final String value)
48             throws MalformedCookieException {
49         if (cookie == null) {
50             throw new IllegalArgumentException("Cookie may not be null");
51         }
52         if (value == null) {
53             throw new MalformedCookieException("Missing value for domain attribute");
54         }
55         if (value.trim().length() == 0) {
56             throw new MalformedCookieException("Blank value for domain attribute");
57         }
58         cookie.setDomain(value);
59     }
60 
validate(final Cookie cookie, final CookieOrigin origin)61     public void validate(final Cookie cookie, final CookieOrigin origin)
62             throws MalformedCookieException {
63         if (cookie == null) {
64             throw new IllegalArgumentException("Cookie may not be null");
65         }
66         if (origin == null) {
67             throw new IllegalArgumentException("Cookie origin may not be null");
68         }
69         String host = origin.getHost();
70         String domain = cookie.getDomain();
71         if (domain == null) {
72             throw new MalformedCookieException("Cookie domain may not be null");
73         }
74         if (!domain.equals(host)) {
75             int dotIndex = domain.indexOf('.');
76             if (dotIndex == -1) {
77                 throw new MalformedCookieException("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 MalformedCookieException("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 MalformedCookieException("Domain attribute \""
92                     + domain
93                     + "\" violates RFC 2109: domain must contain an embedded dot");
94             }
95             host = host.toLowerCase(Locale.ENGLISH);
96             if (!host.endsWith(domain)) {
97                 throw new MalformedCookieException(
98                     "Illegal domain attribute \"" + domain
99                     + "\". Domain of origin: \"" + host + "\"");
100             }
101             // host minus domain may not contain any dots
102             String hostWithoutDomain = host.substring(0, host.length() - domain.length());
103             if (hostWithoutDomain.indexOf('.') != -1) {
104                 throw new MalformedCookieException("Domain attribute \""
105                     + domain
106                     + "\" violates RFC 2109: host minus domain may not contain any dots");
107             }
108         }
109     }
110 
match(final Cookie cookie, final CookieOrigin origin)111     public boolean match(final Cookie cookie, final CookieOrigin origin) {
112         if (cookie == null) {
113             throw new IllegalArgumentException("Cookie may not be null");
114         }
115         if (origin == null) {
116             throw new IllegalArgumentException("Cookie origin may not be null");
117         }
118         String host = origin.getHost();
119         String domain = cookie.getDomain();
120         if (domain == null) {
121             return false;
122         }
123         return host.equals(domain) || (domain.startsWith(".") && host.endsWith(domain));
124     }
125 
126 }
127