mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/cookie/CookieSpecBase.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

     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  */
    28 package ch.boye.httpclientandroidlib.impl.cookie;
    30 import java.util.ArrayList;
    31 import java.util.List;
    32 import java.util.Locale;
    34 import ch.boye.httpclientandroidlib.annotation.NotThreadSafe;
    36 import ch.boye.httpclientandroidlib.HeaderElement;
    37 import ch.boye.httpclientandroidlib.NameValuePair;
    38 import ch.boye.httpclientandroidlib.cookie.Cookie;
    39 import ch.boye.httpclientandroidlib.cookie.CookieAttributeHandler;
    40 import ch.boye.httpclientandroidlib.cookie.CookieOrigin;
    41 import ch.boye.httpclientandroidlib.cookie.MalformedCookieException;
    43 /**
    44  * Cookie management functions shared by all specification.
    45  *
    46  *
    47  * @since 4.0
    48  */
    49 @NotThreadSafe // AbstractCookieSpec is not thread-safe
    50 public abstract class CookieSpecBase extends AbstractCookieSpec {
    52     protected static String getDefaultPath(final CookieOrigin origin) {
    53         String defaultPath = origin.getPath();
    54         int lastSlashIndex = defaultPath.lastIndexOf('/');
    55         if (lastSlashIndex >= 0) {
    56             if (lastSlashIndex == 0) {
    57                 //Do not remove the very first slash
    58                 lastSlashIndex = 1;
    59             }
    60             defaultPath = defaultPath.substring(0, lastSlashIndex);
    61         }
    62         return defaultPath;
    63     }
    65     protected static String getDefaultDomain(final CookieOrigin origin) {
    66         return origin.getHost();
    67     }
    69     protected List<Cookie> parse(final HeaderElement[] elems, final CookieOrigin origin)
    70                 throws MalformedCookieException {
    71         List<Cookie> cookies = new ArrayList<Cookie>(elems.length);
    72         for (HeaderElement headerelement : elems) {
    73             String name = headerelement.getName();
    74             String value = headerelement.getValue();
    75             if (name == null || name.length() == 0) {
    76                 throw new MalformedCookieException("Cookie name may not be empty");
    77             }
    79             BasicClientCookie cookie = new BasicClientCookie(name, value);
    80             cookie.setPath(getDefaultPath(origin));
    81             cookie.setDomain(getDefaultDomain(origin));
    83             // cycle through the parameters
    84             NameValuePair[] attribs = headerelement.getParameters();
    85             for (int j = attribs.length - 1; j >= 0; j--) {
    86                 NameValuePair attrib = attribs[j];
    87                 String s = attrib.getName().toLowerCase(Locale.ENGLISH);
    89                 cookie.setAttribute(s, attrib.getValue());
    91                 CookieAttributeHandler handler = findAttribHandler(s);
    92                 if (handler != null) {
    93                     handler.parse(cookie, attrib.getValue());
    94                 }
    95             }
    96             cookies.add(cookie);
    97         }
    98         return cookies;
    99     }
   101     public void validate(final Cookie cookie, final CookieOrigin origin)
   102             throws MalformedCookieException {
   103         if (cookie == null) {
   104             throw new IllegalArgumentException("Cookie may not be null");
   105         }
   106         if (origin == null) {
   107             throw new IllegalArgumentException("Cookie origin may not be null");
   108         }
   109         for (CookieAttributeHandler handler: getAttribHandlers()) {
   110             handler.validate(cookie, origin);
   111         }
   112     }
   114     public boolean match(final Cookie cookie, final CookieOrigin origin) {
   115         if (cookie == null) {
   116             throw new IllegalArgumentException("Cookie may not be null");
   117         }
   118         if (origin == null) {
   119             throw new IllegalArgumentException("Cookie origin may not be null");
   120         }
   121         for (CookieAttributeHandler handler: getAttribHandlers()) {
   122             if (!handler.match(cookie, origin)) {
   123                 return false;
   124             }
   125         }
   126         return true;
   127     }
   129 }

mercurial