1 /* 2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/cookie/BasicDomainHandler.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 org.apache.http.cookie.Cookie; 34 import org.apache.http.cookie.CookieAttributeHandler; 35 import org.apache.http.cookie.CookieOrigin; 36 import org.apache.http.cookie.MalformedCookieException; 37 import org.apache.http.cookie.SetCookie; 38 /** 39 * @deprecated Please use {@link java.net.URL#openConnection} instead. 40 * Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a> 41 * for further details. 42 */ 43 44 @Deprecated 45 public class BasicDomainHandler implements CookieAttributeHandler { 46 47 public BasicDomainHandler() { 48 super(); 49 } 50 51 public void parse(final SetCookie cookie, final String value) 52 throws MalformedCookieException { 53 if (cookie == null) { 54 throw new IllegalArgumentException("Cookie may not be null"); 55 } 56 if (value == null) { 57 throw new MalformedCookieException("Missing value for domain attribute"); 58 } 59 if (value.trim().length() == 0) { 60 throw new MalformedCookieException("Blank value for domain attribute"); 61 } 62 cookie.setDomain(value); 63 } 64 65 public void validate(final Cookie cookie, final CookieOrigin origin) 66 throws MalformedCookieException { 67 if (cookie == null) { 68 throw new IllegalArgumentException("Cookie may not be null"); 69 } 70 if (origin == null) { 71 throw new IllegalArgumentException("Cookie origin may not be null"); 72 } 73 // Validate the cookies domain attribute. NOTE: Domains without 74 // any dots are allowed to support hosts on private LANs that don't 75 // have DNS names. Since they have no dots, to domain-match the 76 // request-host and domain must be identical for the cookie to sent 77 // back to the origin-server. 78 String host = origin.getHost(); 79 String domain = cookie.getDomain(); 80 if (domain == null) { 81 throw new MalformedCookieException("Cookie domain may not be null"); 82 } 83 if (host.contains(".")) { 84 // Not required to have at least two dots. RFC 2965. 85 // A Set-Cookie2 with Domain=ajax.com will be accepted. 86 87 // domain must match host 88 if (!host.endsWith(domain)) { 89 if (domain.startsWith(".")) { 90 domain = domain.substring(1, domain.length()); 91 } 92 if (!host.equals(domain)) { 93 throw new MalformedCookieException( 94 "Illegal domain attribute \"" + domain 95 + "\". Domain of origin: \"" + host + "\""); 96 } 97 } 98 } else { 99 if (!host.equals(domain)) { 100 throw new MalformedCookieException( 101 "Illegal domain attribute \"" + domain 102 + "\". Domain of origin: \"" + host + "\""); 103 } 104 } 105 } 106 107 public boolean match(final Cookie cookie, final CookieOrigin origin) { 108 if (cookie == null) { 109 throw new IllegalArgumentException("Cookie may not be null"); 110 } 111 if (origin == null) { 112 throw new IllegalArgumentException("Cookie origin may not be null"); 113 } 114 String host = origin.getHost(); 115 String domain = cookie.getDomain(); 116 if (domain == null) { 117 return false; 118 } 119 if (host.equals(domain)) { 120 return true; 121 } 122 if (!domain.startsWith(".")) { 123 domain = '.' + domain; 124 } 125 return host.endsWith(domain) || host.equals(domain.substring(1)); 126 } 127 128 } 129