1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/cookie/RFC2109Spec.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,248 @@ 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.Collections; 1.35 +import java.util.List; 1.36 + 1.37 +import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; 1.38 + 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.CookiePathComparator; 1.45 +import ch.boye.httpclientandroidlib.cookie.CookieRestrictionViolationException; 1.46 +import ch.boye.httpclientandroidlib.cookie.CookieSpec; 1.47 +import ch.boye.httpclientandroidlib.cookie.MalformedCookieException; 1.48 +import ch.boye.httpclientandroidlib.cookie.SM; 1.49 +import ch.boye.httpclientandroidlib.message.BufferedHeader; 1.50 +import ch.boye.httpclientandroidlib.util.CharArrayBuffer; 1.51 + 1.52 +/** 1.53 + * RFC 2109 compliant {@link CookieSpec} implementation. This is an older 1.54 + * version of the official HTTP state management specification superseded 1.55 + * by RFC 2965. 1.56 + * 1.57 + * @see RFC2965Spec 1.58 + * 1.59 + * @since 4.0 1.60 + */ 1.61 +@NotThreadSafe // superclass is @NotThreadSafe 1.62 +public class RFC2109Spec extends CookieSpecBase { 1.63 + 1.64 + private final static CookiePathComparator PATH_COMPARATOR = new CookiePathComparator(); 1.65 + 1.66 + private final static String[] DATE_PATTERNS = { 1.67 + DateUtils.PATTERN_RFC1123, 1.68 + DateUtils.PATTERN_RFC1036, 1.69 + DateUtils.PATTERN_ASCTIME 1.70 + }; 1.71 + 1.72 + private final String[] datepatterns; 1.73 + private final boolean oneHeader; 1.74 + 1.75 + /** Default constructor */ 1.76 + public RFC2109Spec(final String[] datepatterns, boolean oneHeader) { 1.77 + super(); 1.78 + if (datepatterns != null) { 1.79 + this.datepatterns = datepatterns.clone(); 1.80 + } else { 1.81 + this.datepatterns = DATE_PATTERNS; 1.82 + } 1.83 + this.oneHeader = oneHeader; 1.84 + registerAttribHandler(ClientCookie.VERSION_ATTR, new RFC2109VersionHandler()); 1.85 + registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandler()); 1.86 + registerAttribHandler(ClientCookie.DOMAIN_ATTR, new RFC2109DomainHandler()); 1.87 + registerAttribHandler(ClientCookie.MAX_AGE_ATTR, new BasicMaxAgeHandler()); 1.88 + registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandler()); 1.89 + registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandler()); 1.90 + registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandler( 1.91 + this.datepatterns)); 1.92 + } 1.93 + 1.94 + /** Default constructor */ 1.95 + public RFC2109Spec() { 1.96 + this(null, false); 1.97 + } 1.98 + 1.99 + public List<Cookie> parse(final Header header, final CookieOrigin origin) 1.100 + throws MalformedCookieException { 1.101 + if (header == null) { 1.102 + throw new IllegalArgumentException("Header may not be null"); 1.103 + } 1.104 + if (origin == null) { 1.105 + throw new IllegalArgumentException("Cookie origin may not be null"); 1.106 + } 1.107 + if (!header.getName().equalsIgnoreCase(SM.SET_COOKIE)) { 1.108 + throw new MalformedCookieException("Unrecognized cookie header '" 1.109 + + header.toString() + "'"); 1.110 + } 1.111 + HeaderElement[] elems = header.getElements(); 1.112 + return parse(elems, origin); 1.113 + } 1.114 + 1.115 + @Override 1.116 + public void validate(final Cookie cookie, final CookieOrigin origin) 1.117 + throws MalformedCookieException { 1.118 + if (cookie == null) { 1.119 + throw new IllegalArgumentException("Cookie may not be null"); 1.120 + } 1.121 + String name = cookie.getName(); 1.122 + if (name.indexOf(' ') != -1) { 1.123 + throw new CookieRestrictionViolationException("Cookie name may not contain blanks"); 1.124 + } 1.125 + if (name.startsWith("$")) { 1.126 + throw new CookieRestrictionViolationException("Cookie name may not start with $"); 1.127 + } 1.128 + super.validate(cookie, origin); 1.129 + } 1.130 + 1.131 + public List<Header> formatCookies(List<Cookie> cookies) { 1.132 + if (cookies == null) { 1.133 + throw new IllegalArgumentException("List of cookies may not be null"); 1.134 + } 1.135 + if (cookies.isEmpty()) { 1.136 + throw new IllegalArgumentException("List of cookies may not be empty"); 1.137 + } 1.138 + if (cookies.size() > 1) { 1.139 + // Create a mutable copy and sort the copy. 1.140 + cookies = new ArrayList<Cookie>(cookies); 1.141 + Collections.sort(cookies, PATH_COMPARATOR); 1.142 + } 1.143 + if (this.oneHeader) { 1.144 + return doFormatOneHeader(cookies); 1.145 + } else { 1.146 + return doFormatManyHeaders(cookies); 1.147 + } 1.148 + } 1.149 + 1.150 + private List<Header> doFormatOneHeader(final List<Cookie> cookies) { 1.151 + int version = Integer.MAX_VALUE; 1.152 + // Pick the lowest common denominator 1.153 + for (Cookie cookie : cookies) { 1.154 + if (cookie.getVersion() < version) { 1.155 + version = cookie.getVersion(); 1.156 + } 1.157 + } 1.158 + CharArrayBuffer buffer = new CharArrayBuffer(40 * cookies.size()); 1.159 + buffer.append(SM.COOKIE); 1.160 + buffer.append(": "); 1.161 + buffer.append("$Version="); 1.162 + buffer.append(Integer.toString(version)); 1.163 + for (Cookie cooky : cookies) { 1.164 + buffer.append("; "); 1.165 + Cookie cookie = cooky; 1.166 + formatCookieAsVer(buffer, cookie, version); 1.167 + } 1.168 + List<Header> headers = new ArrayList<Header>(1); 1.169 + headers.add(new BufferedHeader(buffer)); 1.170 + return headers; 1.171 + } 1.172 + 1.173 + private List<Header> doFormatManyHeaders(final List<Cookie> cookies) { 1.174 + List<Header> headers = new ArrayList<Header>(cookies.size()); 1.175 + for (Cookie cookie : cookies) { 1.176 + int version = cookie.getVersion(); 1.177 + CharArrayBuffer buffer = new CharArrayBuffer(40); 1.178 + buffer.append("Cookie: "); 1.179 + buffer.append("$Version="); 1.180 + buffer.append(Integer.toString(version)); 1.181 + buffer.append("; "); 1.182 + formatCookieAsVer(buffer, cookie, version); 1.183 + headers.add(new BufferedHeader(buffer)); 1.184 + } 1.185 + return headers; 1.186 + } 1.187 + 1.188 + /** 1.189 + * Return a name/value string suitable for sending in a <tt>"Cookie"</tt> 1.190 + * header as defined in RFC 2109 for backward compatibility with cookie 1.191 + * version 0 1.192 + * @param buffer The char array buffer to use for output 1.193 + * @param name The cookie name 1.194 + * @param value The cookie value 1.195 + * @param version The cookie version 1.196 + */ 1.197 + protected void formatParamAsVer(final CharArrayBuffer buffer, 1.198 + final String name, final String value, int version) { 1.199 + buffer.append(name); 1.200 + buffer.append("="); 1.201 + if (value != null) { 1.202 + if (version > 0) { 1.203 + buffer.append('\"'); 1.204 + buffer.append(value); 1.205 + buffer.append('\"'); 1.206 + } else { 1.207 + buffer.append(value); 1.208 + } 1.209 + } 1.210 + } 1.211 + 1.212 + /** 1.213 + * Return a string suitable for sending in a <tt>"Cookie"</tt> header 1.214 + * as defined in RFC 2109 for backward compatibility with cookie version 0 1.215 + * @param buffer The char array buffer to use for output 1.216 + * @param cookie The {@link Cookie} to be formatted as string 1.217 + * @param version The version to use. 1.218 + */ 1.219 + protected void formatCookieAsVer(final CharArrayBuffer buffer, 1.220 + final Cookie cookie, int version) { 1.221 + formatParamAsVer(buffer, cookie.getName(), cookie.getValue(), version); 1.222 + if (cookie.getPath() != null) { 1.223 + if (cookie instanceof ClientCookie 1.224 + && ((ClientCookie) cookie).containsAttribute(ClientCookie.PATH_ATTR)) { 1.225 + buffer.append("; "); 1.226 + formatParamAsVer(buffer, "$Path", cookie.getPath(), version); 1.227 + } 1.228 + } 1.229 + if (cookie.getDomain() != null) { 1.230 + if (cookie instanceof ClientCookie 1.231 + && ((ClientCookie) cookie).containsAttribute(ClientCookie.DOMAIN_ATTR)) { 1.232 + buffer.append("; "); 1.233 + formatParamAsVer(buffer, "$Domain", cookie.getDomain(), version); 1.234 + } 1.235 + } 1.236 + } 1.237 + 1.238 + public int getVersion() { 1.239 + return 1; 1.240 + } 1.241 + 1.242 + public Header getVersionHeader() { 1.243 + return null; 1.244 + } 1.245 + 1.246 + @Override 1.247 + public String toString() { 1.248 + return "rfc2109"; 1.249 + } 1.250 + 1.251 +}