1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/cookie/CookieSpecBase.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,129 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * ==================================================================== 1.23 + * 1.24 + * This software consists of voluntary contributions made by many 1.25 + * individuals on behalf of the Apache Software Foundation. For more 1.26 + * information on the Apache Software Foundation, please see 1.27 + * <http://www.apache.org/>. 1.28 + * 1.29 + */ 1.30 + 1.31 +package ch.boye.httpclientandroidlib.impl.cookie; 1.32 + 1.33 +import java.util.ArrayList; 1.34 +import java.util.List; 1.35 +import java.util.Locale; 1.36 + 1.37 +import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; 1.38 + 1.39 +import ch.boye.httpclientandroidlib.HeaderElement; 1.40 +import ch.boye.httpclientandroidlib.NameValuePair; 1.41 +import ch.boye.httpclientandroidlib.cookie.Cookie; 1.42 +import ch.boye.httpclientandroidlib.cookie.CookieAttributeHandler; 1.43 +import ch.boye.httpclientandroidlib.cookie.CookieOrigin; 1.44 +import ch.boye.httpclientandroidlib.cookie.MalformedCookieException; 1.45 + 1.46 +/** 1.47 + * Cookie management functions shared by all specification. 1.48 + * 1.49 + * 1.50 + * @since 4.0 1.51 + */ 1.52 +@NotThreadSafe // AbstractCookieSpec is not thread-safe 1.53 +public abstract class CookieSpecBase extends AbstractCookieSpec { 1.54 + 1.55 + protected static String getDefaultPath(final CookieOrigin origin) { 1.56 + String defaultPath = origin.getPath(); 1.57 + int lastSlashIndex = defaultPath.lastIndexOf('/'); 1.58 + if (lastSlashIndex >= 0) { 1.59 + if (lastSlashIndex == 0) { 1.60 + //Do not remove the very first slash 1.61 + lastSlashIndex = 1; 1.62 + } 1.63 + defaultPath = defaultPath.substring(0, lastSlashIndex); 1.64 + } 1.65 + return defaultPath; 1.66 + } 1.67 + 1.68 + protected static String getDefaultDomain(final CookieOrigin origin) { 1.69 + return origin.getHost(); 1.70 + } 1.71 + 1.72 + protected List<Cookie> parse(final HeaderElement[] elems, final CookieOrigin origin) 1.73 + throws MalformedCookieException { 1.74 + List<Cookie> cookies = new ArrayList<Cookie>(elems.length); 1.75 + for (HeaderElement headerelement : elems) { 1.76 + String name = headerelement.getName(); 1.77 + String value = headerelement.getValue(); 1.78 + if (name == null || name.length() == 0) { 1.79 + throw new MalformedCookieException("Cookie name may not be empty"); 1.80 + } 1.81 + 1.82 + BasicClientCookie cookie = new BasicClientCookie(name, value); 1.83 + cookie.setPath(getDefaultPath(origin)); 1.84 + cookie.setDomain(getDefaultDomain(origin)); 1.85 + 1.86 + // cycle through the parameters 1.87 + NameValuePair[] attribs = headerelement.getParameters(); 1.88 + for (int j = attribs.length - 1; j >= 0; j--) { 1.89 + NameValuePair attrib = attribs[j]; 1.90 + String s = attrib.getName().toLowerCase(Locale.ENGLISH); 1.91 + 1.92 + cookie.setAttribute(s, attrib.getValue()); 1.93 + 1.94 + CookieAttributeHandler handler = findAttribHandler(s); 1.95 + if (handler != null) { 1.96 + handler.parse(cookie, attrib.getValue()); 1.97 + } 1.98 + } 1.99 + cookies.add(cookie); 1.100 + } 1.101 + return cookies; 1.102 + } 1.103 + 1.104 + public void validate(final Cookie cookie, final CookieOrigin origin) 1.105 + throws MalformedCookieException { 1.106 + if (cookie == null) { 1.107 + throw new IllegalArgumentException("Cookie may not be null"); 1.108 + } 1.109 + if (origin == null) { 1.110 + throw new IllegalArgumentException("Cookie origin may not be null"); 1.111 + } 1.112 + for (CookieAttributeHandler handler: getAttribHandlers()) { 1.113 + handler.validate(cookie, origin); 1.114 + } 1.115 + } 1.116 + 1.117 + public boolean match(final Cookie cookie, final CookieOrigin origin) { 1.118 + if (cookie == null) { 1.119 + throw new IllegalArgumentException("Cookie may not be null"); 1.120 + } 1.121 + if (origin == null) { 1.122 + throw new IllegalArgumentException("Cookie origin may not be null"); 1.123 + } 1.124 + for (CookieAttributeHandler handler: getAttribHandlers()) { 1.125 + if (!handler.match(cookie, origin)) { 1.126 + return false; 1.127 + } 1.128 + } 1.129 + return true; 1.130 + } 1.131 + 1.132 +}