mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/cookie/RFC2109DomainHandler.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/RFC2109DomainHandler.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,130 @@
     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 +package ch.boye.httpclientandroidlib.impl.cookie;
    1.31 +
    1.32 +import java.util.Locale;
    1.33 +
    1.34 +import ch.boye.httpclientandroidlib.annotation.Immutable;
    1.35 +
    1.36 +import ch.boye.httpclientandroidlib.cookie.Cookie;
    1.37 +import ch.boye.httpclientandroidlib.cookie.CookieAttributeHandler;
    1.38 +import ch.boye.httpclientandroidlib.cookie.CookieOrigin;
    1.39 +import ch.boye.httpclientandroidlib.cookie.CookieRestrictionViolationException;
    1.40 +import ch.boye.httpclientandroidlib.cookie.MalformedCookieException;
    1.41 +import ch.boye.httpclientandroidlib.cookie.SetCookie;
    1.42 +
    1.43 +/**
    1.44 + *
    1.45 + * @since 4.0
    1.46 + */
    1.47 +@Immutable
    1.48 +public class RFC2109DomainHandler implements CookieAttributeHandler {
    1.49 +
    1.50 +    public RFC2109DomainHandler() {
    1.51 +        super();
    1.52 +    }
    1.53 +
    1.54 +    public void parse(final SetCookie cookie, final String value)
    1.55 +            throws MalformedCookieException {
    1.56 +        if (cookie == null) {
    1.57 +            throw new IllegalArgumentException("Cookie may not be null");
    1.58 +        }
    1.59 +        if (value == null) {
    1.60 +            throw new MalformedCookieException("Missing value for domain attribute");
    1.61 +        }
    1.62 +        if (value.trim().length() == 0) {
    1.63 +            throw new MalformedCookieException("Blank value for domain attribute");
    1.64 +        }
    1.65 +        cookie.setDomain(value);
    1.66 +    }
    1.67 +
    1.68 +    public void validate(final Cookie cookie, final CookieOrigin origin)
    1.69 +            throws MalformedCookieException {
    1.70 +        if (cookie == null) {
    1.71 +            throw new IllegalArgumentException("Cookie may not be null");
    1.72 +        }
    1.73 +        if (origin == null) {
    1.74 +            throw new IllegalArgumentException("Cookie origin may not be null");
    1.75 +        }
    1.76 +        String host = origin.getHost();
    1.77 +        String domain = cookie.getDomain();
    1.78 +        if (domain == null) {
    1.79 +            throw new CookieRestrictionViolationException("Cookie domain may not be null");
    1.80 +        }
    1.81 +        if (!domain.equals(host)) {
    1.82 +            int dotIndex = domain.indexOf('.');
    1.83 +            if (dotIndex == -1) {
    1.84 +                throw new CookieRestrictionViolationException("Domain attribute \""
    1.85 +                        + domain
    1.86 +                        + "\" does not match the host \""
    1.87 +                        + host + "\"");
    1.88 +            }
    1.89 +            // domain must start with dot
    1.90 +            if (!domain.startsWith(".")) {
    1.91 +                throw new CookieRestrictionViolationException("Domain attribute \""
    1.92 +                    + domain
    1.93 +                    + "\" violates RFC 2109: domain must start with a dot");
    1.94 +            }
    1.95 +            // domain must have at least one embedded dot
    1.96 +            dotIndex = domain.indexOf('.', 1);
    1.97 +            if (dotIndex < 0 || dotIndex == domain.length() - 1) {
    1.98 +                throw new CookieRestrictionViolationException("Domain attribute \""
    1.99 +                    + domain
   1.100 +                    + "\" violates RFC 2109: domain must contain an embedded dot");
   1.101 +            }
   1.102 +            host = host.toLowerCase(Locale.ENGLISH);
   1.103 +            if (!host.endsWith(domain)) {
   1.104 +                throw new CookieRestrictionViolationException(
   1.105 +                    "Illegal domain attribute \"" + domain
   1.106 +                    + "\". Domain of origin: \"" + host + "\"");
   1.107 +            }
   1.108 +            // host minus domain may not contain any dots
   1.109 +            String hostWithoutDomain = host.substring(0, host.length() - domain.length());
   1.110 +            if (hostWithoutDomain.indexOf('.') != -1) {
   1.111 +                throw new CookieRestrictionViolationException("Domain attribute \""
   1.112 +                    + domain
   1.113 +                    + "\" violates RFC 2109: host minus domain may not contain any dots");
   1.114 +            }
   1.115 +        }
   1.116 +    }
   1.117 +
   1.118 +    public boolean match(final Cookie cookie, final CookieOrigin origin) {
   1.119 +        if (cookie == null) {
   1.120 +            throw new IllegalArgumentException("Cookie may not be null");
   1.121 +        }
   1.122 +        if (origin == null) {
   1.123 +            throw new IllegalArgumentException("Cookie origin may not be null");
   1.124 +        }
   1.125 +        String host = origin.getHost();
   1.126 +        String domain = cookie.getDomain();
   1.127 +        if (domain == null) {
   1.128 +            return false;
   1.129 +        }
   1.130 +        return host.equals(domain) || (domain.startsWith(".") && host.endsWith(domain));
   1.131 +    }
   1.132 +
   1.133 +}

mercurial