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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/cookie/BestMatchSpec.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,221 @@
     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.List;
    1.34 +
    1.35 +import ch.boye.httpclientandroidlib.annotation.NotThreadSafe;
    1.36 +
    1.37 +import ch.boye.httpclientandroidlib.FormattedHeader;
    1.38 +import ch.boye.httpclientandroidlib.Header;
    1.39 +import ch.boye.httpclientandroidlib.HeaderElement;
    1.40 +import ch.boye.httpclientandroidlib.cookie.Cookie;
    1.41 +import ch.boye.httpclientandroidlib.cookie.CookieOrigin;
    1.42 +import ch.boye.httpclientandroidlib.cookie.CookieSpec;
    1.43 +import ch.boye.httpclientandroidlib.cookie.MalformedCookieException;
    1.44 +import ch.boye.httpclientandroidlib.cookie.SM;
    1.45 +import ch.boye.httpclientandroidlib.cookie.SetCookie2;
    1.46 +import ch.boye.httpclientandroidlib.message.ParserCursor;
    1.47 +import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
    1.48 +
    1.49 +/**
    1.50 + * 'Meta' cookie specification that picks up a cookie policy based on
    1.51 + * the format of cookies sent with the HTTP response.
    1.52 + *
    1.53 + * @since 4.0
    1.54 + */
    1.55 +@NotThreadSafe // CookieSpec fields are @NotThreadSafe
    1.56 +public class BestMatchSpec implements CookieSpec {
    1.57 +
    1.58 +    private final String[] datepatterns;
    1.59 +    private final boolean oneHeader;
    1.60 +
    1.61 +    // Cached values of CookieSpec instances
    1.62 +    private RFC2965Spec strict; // @NotThreadSafe
    1.63 +    private RFC2109Spec obsoleteStrict; // @NotThreadSafe
    1.64 +    private BrowserCompatSpec compat; // @NotThreadSafe
    1.65 +
    1.66 +    public BestMatchSpec(final String[] datepatterns, boolean oneHeader) {
    1.67 +        super();
    1.68 +        this.datepatterns = datepatterns == null ? null : datepatterns.clone();
    1.69 +        this.oneHeader = oneHeader;
    1.70 +    }
    1.71 +
    1.72 +    public BestMatchSpec() {
    1.73 +        this(null, false);
    1.74 +    }
    1.75 +
    1.76 +    private RFC2965Spec getStrict() {
    1.77 +        if (this.strict == null) {
    1.78 +             this.strict = new RFC2965Spec(this.datepatterns, this.oneHeader);
    1.79 +        }
    1.80 +        return strict;
    1.81 +    }
    1.82 +
    1.83 +    private RFC2109Spec getObsoleteStrict() {
    1.84 +        if (this.obsoleteStrict == null) {
    1.85 +             this.obsoleteStrict = new RFC2109Spec(this.datepatterns, this.oneHeader);
    1.86 +        }
    1.87 +        return obsoleteStrict;
    1.88 +    }
    1.89 +
    1.90 +    private BrowserCompatSpec getCompat() {
    1.91 +        if (this.compat == null) {
    1.92 +            this.compat = new BrowserCompatSpec(this.datepatterns);
    1.93 +        }
    1.94 +        return compat;
    1.95 +    }
    1.96 +
    1.97 +    public List<Cookie> parse(
    1.98 +            final Header header,
    1.99 +            final CookieOrigin origin) throws MalformedCookieException {
   1.100 +        if (header == null) {
   1.101 +            throw new IllegalArgumentException("Header may not be null");
   1.102 +        }
   1.103 +        if (origin == null) {
   1.104 +           throw new IllegalArgumentException("Cookie origin may not be null");
   1.105 +        }
   1.106 +        HeaderElement[] helems = header.getElements();
   1.107 +        boolean versioned = false;
   1.108 +        boolean netscape = false;
   1.109 +        for (HeaderElement helem: helems) {
   1.110 +            if (helem.getParameterByName("version") != null) {
   1.111 +                versioned = true;
   1.112 +            }
   1.113 +            if (helem.getParameterByName("expires") != null) {
   1.114 +               netscape = true;
   1.115 +            }
   1.116 +        }
   1.117 +        if (netscape || !versioned) {
   1.118 +            // Need to parse the header again, because Netscape style cookies do not correctly
   1.119 +            // support multiple header elements (comma cannot be treated as an element separator)
   1.120 +            NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT;
   1.121 +            CharArrayBuffer buffer;
   1.122 +            ParserCursor cursor;
   1.123 +            if (header instanceof FormattedHeader) {
   1.124 +                buffer = ((FormattedHeader) header).getBuffer();
   1.125 +                cursor = new ParserCursor(
   1.126 +                        ((FormattedHeader) header).getValuePos(),
   1.127 +                        buffer.length());
   1.128 +            } else {
   1.129 +                String s = header.getValue();
   1.130 +                if (s == null) {
   1.131 +                    throw new MalformedCookieException("Header value is null");
   1.132 +                }
   1.133 +                buffer = new CharArrayBuffer(s.length());
   1.134 +                buffer.append(s);
   1.135 +                cursor = new ParserCursor(0, buffer.length());
   1.136 +            }
   1.137 +            helems = new HeaderElement[] { parser.parseHeader(buffer, cursor) };
   1.138 +            return getCompat().parse(helems, origin);
   1.139 +        } else {
   1.140 +            if (SM.SET_COOKIE2.equals(header.getName())) {
   1.141 +                return getStrict().parse(helems, origin);
   1.142 +            } else {
   1.143 +                return getObsoleteStrict().parse(helems, origin);
   1.144 +            }
   1.145 +        }
   1.146 +    }
   1.147 +
   1.148 +    public void validate(
   1.149 +            final Cookie cookie,
   1.150 +            final CookieOrigin origin) throws MalformedCookieException {
   1.151 +        if (cookie == null) {
   1.152 +            throw new IllegalArgumentException("Cookie may not be null");
   1.153 +        }
   1.154 +        if (origin == null) {
   1.155 +            throw new IllegalArgumentException("Cookie origin may not be null");
   1.156 +        }
   1.157 +        if (cookie.getVersion() > 0) {
   1.158 +            if (cookie instanceof SetCookie2) {
   1.159 +                getStrict().validate(cookie, origin);
   1.160 +            } else {
   1.161 +                getObsoleteStrict().validate(cookie, origin);
   1.162 +            }
   1.163 +        } else {
   1.164 +            getCompat().validate(cookie, origin);
   1.165 +        }
   1.166 +    }
   1.167 +
   1.168 +    public boolean match(final Cookie cookie, final CookieOrigin origin) {
   1.169 +        if (cookie == null) {
   1.170 +            throw new IllegalArgumentException("Cookie may not be null");
   1.171 +        }
   1.172 +        if (origin == null) {
   1.173 +            throw new IllegalArgumentException("Cookie origin may not be null");
   1.174 +        }
   1.175 +        if (cookie.getVersion() > 0) {
   1.176 +            if (cookie instanceof SetCookie2) {
   1.177 +                return getStrict().match(cookie, origin);
   1.178 +            } else {
   1.179 +                return getObsoleteStrict().match(cookie, origin);
   1.180 +            }
   1.181 +        } else {
   1.182 +            return getCompat().match(cookie, origin);
   1.183 +        }
   1.184 +    }
   1.185 +
   1.186 +    public List<Header> formatCookies(final List<Cookie> cookies) {
   1.187 +        if (cookies == null) {
   1.188 +            throw new IllegalArgumentException("List of cookies may not be null");
   1.189 +        }
   1.190 +        int version = Integer.MAX_VALUE;
   1.191 +        boolean isSetCookie2 = true;
   1.192 +        for (Cookie cookie: cookies) {
   1.193 +            if (!(cookie instanceof SetCookie2)) {
   1.194 +                isSetCookie2 = false;
   1.195 +            }
   1.196 +            if (cookie.getVersion() < version) {
   1.197 +                version = cookie.getVersion();
   1.198 +            }
   1.199 +        }
   1.200 +        if (version > 0) {
   1.201 +            if (isSetCookie2) {
   1.202 +                return getStrict().formatCookies(cookies);
   1.203 +            } else {
   1.204 +                return getObsoleteStrict().formatCookies(cookies);
   1.205 +            }
   1.206 +        } else {
   1.207 +            return getCompat().formatCookies(cookies);
   1.208 +        }
   1.209 +    }
   1.210 +
   1.211 +    public int getVersion() {
   1.212 +        return getStrict().getVersion();
   1.213 +    }
   1.214 +
   1.215 +    public Header getVersionHeader() {
   1.216 +        return getStrict().getVersionHeader();
   1.217 +    }
   1.218 +
   1.219 +    @Override
   1.220 +    public String toString() {
   1.221 +        return "best-match";
   1.222 +    }
   1.223 +
   1.224 +}

mercurial