Home | History | Annotate | Download | only in cookie
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/cookie/RFC2965Spec.java $
      3  * $Revision: 653041 $
      4  * $Date: 2008-05-03 03:39:28 -0700 (Sat, 03 May 2008) $
      5  *
      6  * ====================================================================
      7  *
      8  *  Licensed to the Apache Software Foundation (ASF) under one or more
      9  *  contributor license agreements.  See the NOTICE file distributed with
     10  *  this work for additional information regarding copyright ownership.
     11  *  The ASF licenses this file to You under the Apache License, Version 2.0
     12  *  (the "License"); you may not use this file except in compliance with
     13  *  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, software
     18  *  distributed under the License is distributed on an "AS IS" BASIS,
     19  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     20  *  See the License for the specific language governing permissions and
     21  *  limitations under the License.
     22  * ====================================================================
     23  *
     24  * This software consists of voluntary contributions made by many
     25  * individuals on behalf of the Apache Software Foundation.  For more
     26  * information on the Apache Software Foundation, please see
     27  * <http://www.apache.org/>.
     28  *
     29  */
     30 
     31 package org.apache.http.impl.cookie;
     32 
     33 import java.util.ArrayList;
     34 import java.util.HashMap;
     35 import java.util.List;
     36 import java.util.Locale;
     37 import java.util.Map;
     38 
     39 import org.apache.http.Header;
     40 import org.apache.http.HeaderElement;
     41 import org.apache.http.NameValuePair;
     42 import org.apache.http.cookie.ClientCookie;
     43 import org.apache.http.cookie.Cookie;
     44 import org.apache.http.cookie.CookieAttributeHandler;
     45 import org.apache.http.cookie.CookieOrigin;
     46 import org.apache.http.cookie.MalformedCookieException;
     47 import org.apache.http.cookie.SM;
     48 import org.apache.http.message.BufferedHeader;
     49 import org.apache.http.util.CharArrayBuffer;
     50 
     51 /**
     52  * <p>RFC 2965 specific cookie management functions.</p>
     53  *
     54  * @author jain.samit (at) gmail.com (Samit Jain)
     55  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
     56  *
     57  * @since 3.1
     58  *
     59  * @deprecated Please use {@link java.net.URL#openConnection} instead.
     60  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     61  *     for further details.
     62  */
     63 @Deprecated
     64 public class RFC2965Spec extends RFC2109Spec {
     65 
     66     /**
     67      * Default constructor
     68      *
     69      */
     70     public RFC2965Spec() {
     71         this(null, false);
     72     }
     73 
     74     public RFC2965Spec(final String[] datepatterns, boolean oneHeader) {
     75         super(datepatterns, oneHeader);
     76         registerAttribHandler(ClientCookie.DOMAIN_ATTR, new RFC2965DomainAttributeHandler());
     77         registerAttribHandler(ClientCookie.PORT_ATTR, new RFC2965PortAttributeHandler());
     78         registerAttribHandler(ClientCookie.COMMENTURL_ATTR, new RFC2965CommentUrlAttributeHandler());
     79         registerAttribHandler(ClientCookie.DISCARD_ATTR, new RFC2965DiscardAttributeHandler());
     80         registerAttribHandler(ClientCookie.VERSION_ATTR, new RFC2965VersionAttributeHandler());
     81     }
     82 
     83     private BasicClientCookie createCookie(
     84             final String name, final String value, final CookieOrigin origin) {
     85         BasicClientCookie cookie = new BasicClientCookie(name, value);
     86         cookie.setPath(getDefaultPath(origin));
     87         cookie.setDomain(getDefaultDomain(origin));
     88         return cookie;
     89     }
     90 
     91     private BasicClientCookie createCookie2(
     92             final String name, final String value, final CookieOrigin origin) {
     93         BasicClientCookie2 cookie = new BasicClientCookie2(name, value);
     94         cookie.setPath(getDefaultPath(origin));
     95         cookie.setDomain(getDefaultDomain(origin));
     96         cookie.setPorts(new int [] { origin.getPort() });
     97         return cookie;
     98     }
     99 
    100     @Override
    101     public List<Cookie> parse(
    102             final Header header,
    103             CookieOrigin origin) throws MalformedCookieException {
    104         if (header == null) {
    105             throw new IllegalArgumentException("Header may not be null");
    106         }
    107         if (origin == null) {
    108             throw new IllegalArgumentException("Cookie origin may not be null");
    109         }
    110 
    111         origin = adjustEffectiveHost(origin);
    112 
    113         HeaderElement[] elems = header.getElements();
    114 
    115         List<Cookie> cookies = new ArrayList<Cookie>(elems.length);
    116         for (HeaderElement headerelement : elems) {
    117             String name = headerelement.getName();
    118             String value = headerelement.getValue();
    119             if (name == null || name.length() == 0) {
    120                 throw new MalformedCookieException("Cookie name may not be empty");
    121             }
    122 
    123             BasicClientCookie cookie;
    124             if (header.getName().equals(SM.SET_COOKIE2)) {
    125                 cookie = createCookie2(name, value, origin);
    126             } else {
    127                 cookie = createCookie(name, value, origin);
    128             }
    129 
    130             // cycle through the parameters
    131             NameValuePair[] attribs = headerelement.getParameters();
    132 
    133             // Eliminate duplicate attributes. The first occurrence takes precedence
    134             // See RFC2965: 3.2  Origin Server Role
    135             Map<String, NameValuePair> attribmap =
    136                     new HashMap<String, NameValuePair>(attribs.length);
    137             for (int j = attribs.length - 1; j >= 0; j--) {
    138                 NameValuePair param = attribs[j];
    139                 attribmap.put(param.getName().toLowerCase(Locale.ENGLISH), param);
    140             }
    141             for (Map.Entry<String, NameValuePair> entry : attribmap.entrySet()) {
    142                 NameValuePair attrib = entry.getValue();
    143                 String s = attrib.getName().toLowerCase(Locale.ENGLISH);
    144 
    145                 cookie.setAttribute(s, attrib.getValue());
    146 
    147                 CookieAttributeHandler handler = findAttribHandler(s);
    148                 if (handler != null) {
    149                     handler.parse(cookie, attrib.getValue());
    150                 }
    151             }
    152             cookies.add(cookie);
    153         }
    154         return cookies;
    155     }
    156 
    157     @Override
    158     public void validate(final Cookie cookie, CookieOrigin origin)
    159             throws MalformedCookieException {
    160         if (cookie == null) {
    161             throw new IllegalArgumentException("Cookie may not be null");
    162         }
    163         if (origin == null) {
    164             throw new IllegalArgumentException("Cookie origin may not be null");
    165         }
    166         origin = adjustEffectiveHost(origin);
    167         super.validate(cookie, origin);
    168     }
    169 
    170     @Override
    171     public boolean match(final Cookie cookie, CookieOrigin origin) {
    172         if (cookie == null) {
    173             throw new IllegalArgumentException("Cookie may not be null");
    174         }
    175         if (origin == null) {
    176             throw new IllegalArgumentException("Cookie origin may not be null");
    177         }
    178         origin = adjustEffectiveHost(origin);
    179         return super.match(cookie, origin);
    180     }
    181 
    182     /**
    183      * Adds valid Port attribute value, e.g. "8000,8001,8002"
    184      */
    185     @Override
    186     protected void formatCookieAsVer(final CharArrayBuffer buffer,
    187             final Cookie cookie, int version) {
    188         super.formatCookieAsVer(buffer, cookie, version);
    189         // format port attribute
    190         if (cookie instanceof ClientCookie) {
    191             // Test if the port attribute as set by the origin server is not blank
    192             String s = ((ClientCookie) cookie).getAttribute(ClientCookie.PORT_ATTR);
    193             if (s != null) {
    194                 buffer.append("; $Port");
    195                 buffer.append("=\"");
    196                 if (s.trim().length() > 0) {
    197                     int[] ports = cookie.getPorts();
    198                     if (ports != null) {
    199                         for (int i = 0, len = ports.length; i < len; i++) {
    200                             if (i > 0) {
    201                                 buffer.append(",");
    202                             }
    203                             buffer.append(Integer.toString(ports[i]));
    204                         }
    205                     }
    206                 }
    207                 buffer.append("\"");
    208             }
    209         }
    210     }
    211 
    212     /**
    213      * Set 'effective host name' as defined in RFC 2965.
    214      * <p>
    215      * If a host name contains no dots, the effective host name is
    216      * that name with the string .local appended to it.  Otherwise
    217      * the effective host name is the same as the host name.  Note
    218      * that all effective host names contain at least one dot.
    219      *
    220      * @param origin origin where cookie is received from or being sent to.
    221      * @return
    222      */
    223     private static CookieOrigin adjustEffectiveHost(final CookieOrigin origin) {
    224         String host = origin.getHost();
    225 
    226         // Test if the host name appears to be a fully qualified DNS name,
    227         // IPv4 address or IPv6 address
    228         boolean isLocalHost = true;
    229         for (int i = 0; i < host.length(); i++) {
    230             char ch = host.charAt(i);
    231             if (ch == '.' || ch == ':') {
    232                 isLocalHost = false;
    233                 break;
    234             }
    235         }
    236         if (isLocalHost) {
    237             host += ".local";
    238             return new CookieOrigin(
    239                     host,
    240                     origin.getPort(),
    241                     origin.getPath(),
    242                     origin.isSecure());
    243         } else {
    244             return origin;
    245         }
    246     }
    247 
    248     @Override
    249     public int getVersion() {
    250         return 1;
    251     }
    252 
    253     @Override
    254     public Header getVersionHeader() {
    255         CharArrayBuffer buffer = new CharArrayBuffer(40);
    256         buffer.append(SM.COOKIE2);
    257         buffer.append(": ");
    258         buffer.append("$Version=");
    259         buffer.append(Integer.toString(getVersion()));
    260         return new BufferedHeader(buffer);
    261     }
    262 
    263 }
    264 
    265