• 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  * @deprecated Please use {@link java.net.URL#openConnection} instead.
42  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
43  *     for further details.
44 */
45 
46 @Deprecated
47 public class RFC2109DomainHandler implements CookieAttributeHandler {
48 
RFC2109DomainHandler()49     public RFC2109DomainHandler() {
50         super();
51     }
52 
parse(final SetCookie cookie, final String value)53     public void parse(final SetCookie cookie, final String value)
54             throws MalformedCookieException {
55         if (cookie == null) {
56             throw new IllegalArgumentException("Cookie may not be null");
57         }
58         if (value == null) {
59             throw new MalformedCookieException("Missing value for domain attribute");
60         }
61         if (value.trim().length() == 0) {
62             throw new MalformedCookieException("Blank value for domain attribute");
63         }
64         cookie.setDomain(value);
65     }
66 
validate(final Cookie cookie, final CookieOrigin origin)67     public void validate(final Cookie cookie, final CookieOrigin origin)
68             throws MalformedCookieException {
69         if (cookie == null) {
70             throw new IllegalArgumentException("Cookie may not be null");
71         }
72         if (origin == null) {
73             throw new IllegalArgumentException("Cookie origin may not be null");
74         }
75         String host = origin.getHost();
76         String domain = cookie.getDomain();
77         if (domain == null) {
78             throw new MalformedCookieException("Cookie domain may not be null");
79         }
80         if (!domain.equals(host)) {
81             int dotIndex = domain.indexOf('.');
82             if (dotIndex == -1) {
83                 throw new MalformedCookieException("Domain attribute \""
84                         + domain
85                         + "\" does not match the host \""
86                         + host + "\"");
87             }
88             // domain must start with dot
89             if (!domain.startsWith(".")) {
90                 throw new MalformedCookieException("Domain attribute \""
91                     + domain
92                     + "\" violates RFC 2109: domain must start with a dot");
93             }
94             // domain must have at least one embedded dot
95             dotIndex = domain.indexOf('.', 1);
96             if (dotIndex < 0 || dotIndex == domain.length() - 1) {
97                 throw new MalformedCookieException("Domain attribute \""
98                     + domain
99                     + "\" violates RFC 2109: domain must contain an embedded dot");
100             }
101             host = host.toLowerCase(Locale.ENGLISH);
102             if (!host.endsWith(domain)) {
103                 throw new MalformedCookieException(
104                     "Illegal domain attribute \"" + domain
105                     + "\". Domain of origin: \"" + host + "\"");
106             }
107             // host minus domain may not contain any dots
108             String hostWithoutDomain = host.substring(0, host.length() - domain.length());
109             if (hostWithoutDomain.indexOf('.') != -1) {
110                 throw new MalformedCookieException("Domain attribute \""
111                     + domain
112                     + "\" violates RFC 2109: host minus domain may not contain any dots");
113             }
114         }
115     }
116 
match(final Cookie cookie, final CookieOrigin origin)117     public boolean match(final Cookie cookie, final CookieOrigin origin) {
118         if (cookie == null) {
119             throw new IllegalArgumentException("Cookie may not be null");
120         }
121         if (origin == null) {
122             throw new IllegalArgumentException("Cookie origin may not be null");
123         }
124         String host = origin.getHost();
125         String domain = cookie.getDomain();
126         if (domain == null) {
127             return false;
128         }
129         return host.equals(domain) || (domain.startsWith(".") && host.endsWith(domain));
130     }
131 
132 }
133