mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/cookie/NetscapeDraftSpec.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/NetscapeDraftSpec.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,180 @@
     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 +
    1.36 +import ch.boye.httpclientandroidlib.annotation.NotThreadSafe;
    1.37 +
    1.38 +import ch.boye.httpclientandroidlib.FormattedHeader;
    1.39 +import ch.boye.httpclientandroidlib.Header;
    1.40 +import ch.boye.httpclientandroidlib.HeaderElement;
    1.41 +import ch.boye.httpclientandroidlib.cookie.ClientCookie;
    1.42 +import ch.boye.httpclientandroidlib.cookie.Cookie;
    1.43 +import ch.boye.httpclientandroidlib.cookie.CookieOrigin;
    1.44 +import ch.boye.httpclientandroidlib.cookie.CookieSpec;
    1.45 +import ch.boye.httpclientandroidlib.cookie.MalformedCookieException;
    1.46 +import ch.boye.httpclientandroidlib.cookie.SM;
    1.47 +import ch.boye.httpclientandroidlib.message.BufferedHeader;
    1.48 +import ch.boye.httpclientandroidlib.message.ParserCursor;
    1.49 +import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
    1.50 +
    1.51 +/**
    1.52 + * This {@link CookieSpec} implementation conforms to the original draft
    1.53 + * specification published by Netscape Communications. It should be avoided
    1.54 + * unless absolutely necessary for compatibility with legacy code.
    1.55 + *
    1.56 + * @since 4.0
    1.57 + */
    1.58 +@NotThreadSafe // superclass is @NotThreadSafe
    1.59 +public class NetscapeDraftSpec extends CookieSpecBase {
    1.60 +
    1.61 +    protected static final String EXPIRES_PATTERN = "EEE, dd-MMM-yy HH:mm:ss z";
    1.62 +
    1.63 +    private final String[] datepatterns;
    1.64 +
    1.65 +    /** Default constructor */
    1.66 +    public NetscapeDraftSpec(final String[] datepatterns) {
    1.67 +        super();
    1.68 +        if (datepatterns != null) {
    1.69 +            this.datepatterns = datepatterns.clone();
    1.70 +        } else {
    1.71 +            this.datepatterns = new String[] { EXPIRES_PATTERN };
    1.72 +        }
    1.73 +        registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandler());
    1.74 +        registerAttribHandler(ClientCookie.DOMAIN_ATTR, new NetscapeDomainHandler());
    1.75 +        registerAttribHandler(ClientCookie.MAX_AGE_ATTR, new BasicMaxAgeHandler());
    1.76 +        registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandler());
    1.77 +        registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandler());
    1.78 +        registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandler(
    1.79 +                this.datepatterns));
    1.80 +    }
    1.81 +
    1.82 +    /** Default constructor */
    1.83 +    public NetscapeDraftSpec() {
    1.84 +        this(null);
    1.85 +    }
    1.86 +
    1.87 +    /**
    1.88 +      * Parses the Set-Cookie value into an array of <tt>Cookie</tt>s.
    1.89 +      *
    1.90 +      * <p>Syntax of the Set-Cookie HTTP Response Header:</p>
    1.91 +      *
    1.92 +      * <p>This is the format a CGI script would use to add to
    1.93 +      * the HTTP headers a new piece of data which is to be stored by
    1.94 +      * the client for later retrieval.</p>
    1.95 +      *
    1.96 +      * <PRE>
    1.97 +      *  Set-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure
    1.98 +      * </PRE>
    1.99 +      *
   1.100 +      * <p>Please note that the Netscape draft specification does not fully conform to the HTTP
   1.101 +      * header format. Comma character if present in <code>Set-Cookie</code> will not be treated
   1.102 +      * as a header element separator</p>
   1.103 +      *
   1.104 +      * @see <a href="http://web.archive.org/web/20020803110822/http://wp.netscape.com/newsref/std/cookie_spec.html">
   1.105 +      *  The Cookie Spec.</a>
   1.106 +      *
   1.107 +      * @param header the <tt>Set-Cookie</tt> received from the server
   1.108 +      * @return an array of <tt>Cookie</tt>s parsed from the Set-Cookie value
   1.109 +      * @throws MalformedCookieException if an exception occurs during parsing
   1.110 +      */
   1.111 +    public List<Cookie> parse(final Header header, final CookieOrigin origin)
   1.112 +            throws MalformedCookieException {
   1.113 +        if (header == null) {
   1.114 +            throw new IllegalArgumentException("Header may not be null");
   1.115 +        }
   1.116 +        if (origin == null) {
   1.117 +            throw new IllegalArgumentException("Cookie origin may not be null");
   1.118 +        }
   1.119 +        if (!header.getName().equalsIgnoreCase(SM.SET_COOKIE)) {
   1.120 +            throw new MalformedCookieException("Unrecognized cookie header '"
   1.121 +                    + header.toString() + "'");
   1.122 +        }
   1.123 +        NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT;
   1.124 +        CharArrayBuffer buffer;
   1.125 +        ParserCursor cursor;
   1.126 +        if (header instanceof FormattedHeader) {
   1.127 +            buffer = ((FormattedHeader) header).getBuffer();
   1.128 +            cursor = new ParserCursor(
   1.129 +                    ((FormattedHeader) header).getValuePos(),
   1.130 +                    buffer.length());
   1.131 +        } else {
   1.132 +            String s = header.getValue();
   1.133 +            if (s == null) {
   1.134 +                throw new MalformedCookieException("Header value is null");
   1.135 +            }
   1.136 +            buffer = new CharArrayBuffer(s.length());
   1.137 +            buffer.append(s);
   1.138 +            cursor = new ParserCursor(0, buffer.length());
   1.139 +        }
   1.140 +        return parse(new HeaderElement[] { parser.parseHeader(buffer, cursor) }, origin);
   1.141 +    }
   1.142 +
   1.143 +    public List<Header> formatCookies(final List<Cookie> cookies) {
   1.144 +        if (cookies == null) {
   1.145 +            throw new IllegalArgumentException("List of cookies may not be null");
   1.146 +        }
   1.147 +        if (cookies.isEmpty()) {
   1.148 +            throw new IllegalArgumentException("List of cookies may not be empty");
   1.149 +        }
   1.150 +        CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size());
   1.151 +        buffer.append(SM.COOKIE);
   1.152 +        buffer.append(": ");
   1.153 +        for (int i = 0; i < cookies.size(); i++) {
   1.154 +            Cookie cookie = cookies.get(i);
   1.155 +            if (i > 0) {
   1.156 +                buffer.append("; ");
   1.157 +            }
   1.158 +            buffer.append(cookie.getName());
   1.159 +            String s = cookie.getValue();
   1.160 +            if (s != null) {
   1.161 +                buffer.append("=");
   1.162 +                buffer.append(s);
   1.163 +            }
   1.164 +        }
   1.165 +        List<Header> headers = new ArrayList<Header>(1);
   1.166 +        headers.add(new BufferedHeader(buffer));
   1.167 +        return headers;
   1.168 +    }
   1.169 +
   1.170 +    public int getVersion() {
   1.171 +        return 0;
   1.172 +    }
   1.173 +
   1.174 +    public Header getVersionHeader() {
   1.175 +        return null;
   1.176 +    }
   1.177 +
   1.178 +    @Override
   1.179 +    public String toString() {
   1.180 +        return "netscape";
   1.181 +    }
   1.182 +
   1.183 +}

mercurial