mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/cookie/RFC2965PortAttributeHandler.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/RFC2965PortAttributeHandler.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,170 @@
     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.StringTokenizer;
    1.34 +
    1.35 +import ch.boye.httpclientandroidlib.annotation.Immutable;
    1.36 +
    1.37 +import ch.boye.httpclientandroidlib.cookie.ClientCookie;
    1.38 +import ch.boye.httpclientandroidlib.cookie.Cookie;
    1.39 +import ch.boye.httpclientandroidlib.cookie.CookieAttributeHandler;
    1.40 +import ch.boye.httpclientandroidlib.cookie.CookieOrigin;
    1.41 +import ch.boye.httpclientandroidlib.cookie.CookieRestrictionViolationException;
    1.42 +import ch.boye.httpclientandroidlib.cookie.MalformedCookieException;
    1.43 +import ch.boye.httpclientandroidlib.cookie.SetCookie;
    1.44 +import ch.boye.httpclientandroidlib.cookie.SetCookie2;
    1.45 +
    1.46 +/**
    1.47 + * <tt>"Port"</tt> cookie attribute handler for RFC 2965 cookie spec.
    1.48 + *
    1.49 + * @since 4.0
    1.50 + */
    1.51 +@Immutable
    1.52 +public class RFC2965PortAttributeHandler implements CookieAttributeHandler {
    1.53 +
    1.54 +    public RFC2965PortAttributeHandler() {
    1.55 +        super();
    1.56 +    }
    1.57 +
    1.58 +    /**
    1.59 +     * Parses the given Port attribute value (e.g. "8000,8001,8002")
    1.60 +     * into an array of ports.
    1.61 +     *
    1.62 +     * @param portValue port attribute value
    1.63 +     * @return parsed array of ports
    1.64 +     * @throws MalformedCookieException if there is a problem in
    1.65 +     *          parsing due to invalid portValue.
    1.66 +     */
    1.67 +    private static int[] parsePortAttribute(final String portValue)
    1.68 +            throws MalformedCookieException {
    1.69 +        StringTokenizer st = new StringTokenizer(portValue, ",");
    1.70 +        int[] ports = new int[st.countTokens()];
    1.71 +        try {
    1.72 +            int i = 0;
    1.73 +            while(st.hasMoreTokens()) {
    1.74 +                ports[i] = Integer.parseInt(st.nextToken().trim());
    1.75 +                if (ports[i] < 0) {
    1.76 +                  throw new MalformedCookieException ("Invalid Port attribute.");
    1.77 +                }
    1.78 +                ++i;
    1.79 +            }
    1.80 +        } catch (NumberFormatException e) {
    1.81 +            throw new MalformedCookieException ("Invalid Port "
    1.82 +                                                + "attribute: " + e.getMessage());
    1.83 +        }
    1.84 +        return ports;
    1.85 +    }
    1.86 +
    1.87 +    /**
    1.88 +     * Returns <tt>true</tt> if the given port exists in the given
    1.89 +     * ports list.
    1.90 +     *
    1.91 +     * @param port port of host where cookie was received from or being sent to.
    1.92 +     * @param ports port list
    1.93 +     * @return true returns <tt>true</tt> if the given port exists in
    1.94 +     *         the given ports list; <tt>false</tt> otherwise.
    1.95 +     */
    1.96 +    private static boolean portMatch(int port, int[] ports) {
    1.97 +        boolean portInList = false;
    1.98 +        for (int i = 0, len = ports.length; i < len; i++) {
    1.99 +            if (port == ports[i]) {
   1.100 +                portInList = true;
   1.101 +                break;
   1.102 +            }
   1.103 +        }
   1.104 +        return portInList;
   1.105 +    }
   1.106 +
   1.107 +    /**
   1.108 +     * Parse cookie port attribute.
   1.109 +     */
   1.110 +    public void parse(final SetCookie cookie, final String portValue)
   1.111 +            throws MalformedCookieException {
   1.112 +        if (cookie == null) {
   1.113 +            throw new IllegalArgumentException("Cookie may not be null");
   1.114 +        }
   1.115 +        if (cookie instanceof SetCookie2) {
   1.116 +            SetCookie2 cookie2 = (SetCookie2) cookie;
   1.117 +            if (portValue != null && portValue.trim().length() > 0) {
   1.118 +                int[] ports = parsePortAttribute(portValue);
   1.119 +                cookie2.setPorts(ports);
   1.120 +            }
   1.121 +        }
   1.122 +    }
   1.123 +
   1.124 +    /**
   1.125 +     * Validate cookie port attribute. If the Port attribute was specified
   1.126 +     * in header, the request port must be in cookie's port list.
   1.127 +     */
   1.128 +    public void validate(final Cookie cookie, final CookieOrigin origin)
   1.129 +            throws MalformedCookieException {
   1.130 +        if (cookie == null) {
   1.131 +            throw new IllegalArgumentException("Cookie may not be null");
   1.132 +        }
   1.133 +        if (origin == null) {
   1.134 +            throw new IllegalArgumentException("Cookie origin may not be null");
   1.135 +        }
   1.136 +        int port = origin.getPort();
   1.137 +        if (cookie instanceof ClientCookie
   1.138 +                && ((ClientCookie) cookie).containsAttribute(ClientCookie.PORT_ATTR)) {
   1.139 +            if (!portMatch(port, cookie.getPorts())) {
   1.140 +                throw new CookieRestrictionViolationException(
   1.141 +                        "Port attribute violates RFC 2965: "
   1.142 +                        + "Request port not found in cookie's port list.");
   1.143 +            }
   1.144 +        }
   1.145 +    }
   1.146 +
   1.147 +    /**
   1.148 +     * Match cookie port attribute. If the Port attribute is not specified
   1.149 +     * in header, the cookie can be sent to any port. Otherwise, the request port
   1.150 +     * must be in the cookie's port list.
   1.151 +     */
   1.152 +    public boolean match(final Cookie cookie, final CookieOrigin origin) {
   1.153 +        if (cookie == null) {
   1.154 +            throw new IllegalArgumentException("Cookie may not be null");
   1.155 +        }
   1.156 +        if (origin == null) {
   1.157 +            throw new IllegalArgumentException("Cookie origin may not be null");
   1.158 +        }
   1.159 +        int port = origin.getPort();
   1.160 +        if (cookie instanceof ClientCookie
   1.161 +                && ((ClientCookie) cookie).containsAttribute(ClientCookie.PORT_ATTR)) {
   1.162 +            if (cookie.getPorts() == null) {
   1.163 +                // Invalid cookie state: port not specified
   1.164 +                return false;
   1.165 +            }
   1.166 +            if (!portMatch(port, cookie.getPorts())) {
   1.167 +                return false;
   1.168 +            }
   1.169 +        }
   1.170 +        return true;
   1.171 +    }
   1.172 +
   1.173 +}

mercurial