1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/cookie/BrowserCompatSpec.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,206 @@ 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.MalformedCookieException; 1.45 +import ch.boye.httpclientandroidlib.cookie.SM; 1.46 +import ch.boye.httpclientandroidlib.message.BufferedHeader; 1.47 +import ch.boye.httpclientandroidlib.message.ParserCursor; 1.48 +import ch.boye.httpclientandroidlib.util.CharArrayBuffer; 1.49 + 1.50 +/** 1.51 + * Cookie specification that strives to closely mimic (mis)behavior of 1.52 + * common web browser applications such as Microsoft Internet Explorer 1.53 + * and Mozilla FireFox. 1.54 + * 1.55 + * 1.56 + * @since 4.0 1.57 + */ 1.58 +@NotThreadSafe // superclass is @NotThreadSafe 1.59 +public class BrowserCompatSpec extends CookieSpecBase { 1.60 + 1.61 + @Deprecated 1.62 + protected static final String[] DATE_PATTERNS = new String[] { 1.63 + DateUtils.PATTERN_RFC1123, 1.64 + DateUtils.PATTERN_RFC1036, 1.65 + DateUtils.PATTERN_ASCTIME, 1.66 + "EEE, dd-MMM-yyyy HH:mm:ss z", 1.67 + "EEE, dd-MMM-yyyy HH-mm-ss z", 1.68 + "EEE, dd MMM yy HH:mm:ss z", 1.69 + "EEE dd-MMM-yyyy HH:mm:ss z", 1.70 + "EEE dd MMM yyyy HH:mm:ss z", 1.71 + "EEE dd-MMM-yyyy HH-mm-ss z", 1.72 + "EEE dd-MMM-yy HH:mm:ss z", 1.73 + "EEE dd MMM yy HH:mm:ss z", 1.74 + "EEE,dd-MMM-yy HH:mm:ss z", 1.75 + "EEE,dd-MMM-yyyy HH:mm:ss z", 1.76 + "EEE, dd-MM-yyyy HH:mm:ss z", 1.77 + }; 1.78 + 1.79 + private static final String[] DEFAULT_DATE_PATTERNS = new String[] { 1.80 + DateUtils.PATTERN_RFC1123, 1.81 + DateUtils.PATTERN_RFC1036, 1.82 + DateUtils.PATTERN_ASCTIME, 1.83 + "EEE, dd-MMM-yyyy HH:mm:ss z", 1.84 + "EEE, dd-MMM-yyyy HH-mm-ss z", 1.85 + "EEE, dd MMM yy HH:mm:ss z", 1.86 + "EEE dd-MMM-yyyy HH:mm:ss z", 1.87 + "EEE dd MMM yyyy HH:mm:ss z", 1.88 + "EEE dd-MMM-yyyy HH-mm-ss z", 1.89 + "EEE dd-MMM-yy HH:mm:ss z", 1.90 + "EEE dd MMM yy HH:mm:ss z", 1.91 + "EEE,dd-MMM-yy HH:mm:ss z", 1.92 + "EEE,dd-MMM-yyyy HH:mm:ss z", 1.93 + "EEE, dd-MM-yyyy HH:mm:ss z", 1.94 + }; 1.95 + 1.96 + private final String[] datepatterns; 1.97 + 1.98 + /** Default constructor */ 1.99 + public BrowserCompatSpec(final String[] datepatterns) { 1.100 + super(); 1.101 + if (datepatterns != null) { 1.102 + this.datepatterns = datepatterns.clone(); 1.103 + } else { 1.104 + this.datepatterns = DEFAULT_DATE_PATTERNS; 1.105 + } 1.106 + registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandler()); 1.107 + registerAttribHandler(ClientCookie.DOMAIN_ATTR, new BasicDomainHandler()); 1.108 + registerAttribHandler(ClientCookie.MAX_AGE_ATTR, new BasicMaxAgeHandler()); 1.109 + registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandler()); 1.110 + registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandler()); 1.111 + registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandler( 1.112 + this.datepatterns)); 1.113 + } 1.114 + 1.115 + /** Default constructor */ 1.116 + public BrowserCompatSpec() { 1.117 + this(null); 1.118 + } 1.119 + 1.120 + public List<Cookie> parse(final Header header, final CookieOrigin origin) 1.121 + throws MalformedCookieException { 1.122 + if (header == null) { 1.123 + throw new IllegalArgumentException("Header may not be null"); 1.124 + } 1.125 + if (origin == null) { 1.126 + throw new IllegalArgumentException("Cookie origin may not be null"); 1.127 + } 1.128 + String headername = header.getName(); 1.129 + if (!headername.equalsIgnoreCase(SM.SET_COOKIE)) { 1.130 + throw new MalformedCookieException("Unrecognized cookie header '" 1.131 + + header.toString() + "'"); 1.132 + } 1.133 + HeaderElement[] helems = header.getElements(); 1.134 + boolean versioned = false; 1.135 + boolean netscape = false; 1.136 + for (HeaderElement helem: helems) { 1.137 + if (helem.getParameterByName("version") != null) { 1.138 + versioned = true; 1.139 + } 1.140 + if (helem.getParameterByName("expires") != null) { 1.141 + netscape = true; 1.142 + } 1.143 + } 1.144 + if (netscape || !versioned) { 1.145 + // Need to parse the header again, because Netscape style cookies do not correctly 1.146 + // support multiple header elements (comma cannot be treated as an element separator) 1.147 + NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT; 1.148 + CharArrayBuffer buffer; 1.149 + ParserCursor cursor; 1.150 + if (header instanceof FormattedHeader) { 1.151 + buffer = ((FormattedHeader) header).getBuffer(); 1.152 + cursor = new ParserCursor( 1.153 + ((FormattedHeader) header).getValuePos(), 1.154 + buffer.length()); 1.155 + } else { 1.156 + String s = header.getValue(); 1.157 + if (s == null) { 1.158 + throw new MalformedCookieException("Header value is null"); 1.159 + } 1.160 + buffer = new CharArrayBuffer(s.length()); 1.161 + buffer.append(s); 1.162 + cursor = new ParserCursor(0, buffer.length()); 1.163 + } 1.164 + helems = new HeaderElement[] { parser.parseHeader(buffer, cursor) }; 1.165 + } 1.166 + return parse(helems, origin); 1.167 + } 1.168 + 1.169 + public List<Header> formatCookies(final List<Cookie> cookies) { 1.170 + if (cookies == null) { 1.171 + throw new IllegalArgumentException("List of cookies may not be null"); 1.172 + } 1.173 + if (cookies.isEmpty()) { 1.174 + throw new IllegalArgumentException("List of cookies may not be empty"); 1.175 + } 1.176 + CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size()); 1.177 + buffer.append(SM.COOKIE); 1.178 + buffer.append(": "); 1.179 + for (int i = 0; i < cookies.size(); i++) { 1.180 + Cookie cookie = cookies.get(i); 1.181 + if (i > 0) { 1.182 + buffer.append("; "); 1.183 + } 1.184 + buffer.append(cookie.getName()); 1.185 + buffer.append("="); 1.186 + String s = cookie.getValue(); 1.187 + if (s != null) { 1.188 + buffer.append(s); 1.189 + } 1.190 + } 1.191 + List<Header> headers = new ArrayList<Header>(1); 1.192 + headers.add(new BufferedHeader(buffer)); 1.193 + return headers; 1.194 + } 1.195 + 1.196 + public int getVersion() { 1.197 + return 0; 1.198 + } 1.199 + 1.200 + public Header getVersionHeader() { 1.201 + return null; 1.202 + } 1.203 + 1.204 + @Override 1.205 + public String toString() { 1.206 + return "compatibility"; 1.207 + } 1.208 + 1.209 +}