mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/cookie/BrowserCompatSpec.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 * ====================================================================
michael@0 3 * Licensed to the Apache Software Foundation (ASF) under one
michael@0 4 * or more contributor license agreements. See the NOTICE file
michael@0 5 * distributed with this work for additional information
michael@0 6 * regarding copyright ownership. The ASF licenses this file
michael@0 7 * to you under the Apache License, Version 2.0 (the
michael@0 8 * "License"); you may not use this file except in compliance
michael@0 9 * with the License. You may obtain a copy of the License at
michael@0 10 *
michael@0 11 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 12 *
michael@0 13 * Unless required by applicable law or agreed to in writing,
michael@0 14 * software distributed under the License is distributed on an
michael@0 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0 16 * KIND, either express or implied. See the License for the
michael@0 17 * specific language governing permissions and limitations
michael@0 18 * under the License.
michael@0 19 * ====================================================================
michael@0 20 *
michael@0 21 * This software consists of voluntary contributions made by many
michael@0 22 * individuals on behalf of the Apache Software Foundation. For more
michael@0 23 * information on the Apache Software Foundation, please see
michael@0 24 * <http://www.apache.org/>.
michael@0 25 *
michael@0 26 */
michael@0 27
michael@0 28 package ch.boye.httpclientandroidlib.impl.cookie;
michael@0 29
michael@0 30 import java.util.ArrayList;
michael@0 31 import java.util.List;
michael@0 32
michael@0 33 import ch.boye.httpclientandroidlib.annotation.NotThreadSafe;
michael@0 34
michael@0 35 import ch.boye.httpclientandroidlib.FormattedHeader;
michael@0 36 import ch.boye.httpclientandroidlib.Header;
michael@0 37 import ch.boye.httpclientandroidlib.HeaderElement;
michael@0 38 import ch.boye.httpclientandroidlib.cookie.ClientCookie;
michael@0 39 import ch.boye.httpclientandroidlib.cookie.Cookie;
michael@0 40 import ch.boye.httpclientandroidlib.cookie.CookieOrigin;
michael@0 41 import ch.boye.httpclientandroidlib.cookie.MalformedCookieException;
michael@0 42 import ch.boye.httpclientandroidlib.cookie.SM;
michael@0 43 import ch.boye.httpclientandroidlib.message.BufferedHeader;
michael@0 44 import ch.boye.httpclientandroidlib.message.ParserCursor;
michael@0 45 import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
michael@0 46
michael@0 47 /**
michael@0 48 * Cookie specification that strives to closely mimic (mis)behavior of
michael@0 49 * common web browser applications such as Microsoft Internet Explorer
michael@0 50 * and Mozilla FireFox.
michael@0 51 *
michael@0 52 *
michael@0 53 * @since 4.0
michael@0 54 */
michael@0 55 @NotThreadSafe // superclass is @NotThreadSafe
michael@0 56 public class BrowserCompatSpec extends CookieSpecBase {
michael@0 57
michael@0 58 @Deprecated
michael@0 59 protected static final String[] DATE_PATTERNS = new String[] {
michael@0 60 DateUtils.PATTERN_RFC1123,
michael@0 61 DateUtils.PATTERN_RFC1036,
michael@0 62 DateUtils.PATTERN_ASCTIME,
michael@0 63 "EEE, dd-MMM-yyyy HH:mm:ss z",
michael@0 64 "EEE, dd-MMM-yyyy HH-mm-ss z",
michael@0 65 "EEE, dd MMM yy HH:mm:ss z",
michael@0 66 "EEE dd-MMM-yyyy HH:mm:ss z",
michael@0 67 "EEE dd MMM yyyy HH:mm:ss z",
michael@0 68 "EEE dd-MMM-yyyy HH-mm-ss z",
michael@0 69 "EEE dd-MMM-yy HH:mm:ss z",
michael@0 70 "EEE dd MMM yy HH:mm:ss z",
michael@0 71 "EEE,dd-MMM-yy HH:mm:ss z",
michael@0 72 "EEE,dd-MMM-yyyy HH:mm:ss z",
michael@0 73 "EEE, dd-MM-yyyy HH:mm:ss z",
michael@0 74 };
michael@0 75
michael@0 76 private static final String[] DEFAULT_DATE_PATTERNS = new String[] {
michael@0 77 DateUtils.PATTERN_RFC1123,
michael@0 78 DateUtils.PATTERN_RFC1036,
michael@0 79 DateUtils.PATTERN_ASCTIME,
michael@0 80 "EEE, dd-MMM-yyyy HH:mm:ss z",
michael@0 81 "EEE, dd-MMM-yyyy HH-mm-ss z",
michael@0 82 "EEE, dd MMM yy HH:mm:ss z",
michael@0 83 "EEE dd-MMM-yyyy HH:mm:ss z",
michael@0 84 "EEE dd MMM yyyy HH:mm:ss z",
michael@0 85 "EEE dd-MMM-yyyy HH-mm-ss z",
michael@0 86 "EEE dd-MMM-yy HH:mm:ss z",
michael@0 87 "EEE dd MMM yy HH:mm:ss z",
michael@0 88 "EEE,dd-MMM-yy HH:mm:ss z",
michael@0 89 "EEE,dd-MMM-yyyy HH:mm:ss z",
michael@0 90 "EEE, dd-MM-yyyy HH:mm:ss z",
michael@0 91 };
michael@0 92
michael@0 93 private final String[] datepatterns;
michael@0 94
michael@0 95 /** Default constructor */
michael@0 96 public BrowserCompatSpec(final String[] datepatterns) {
michael@0 97 super();
michael@0 98 if (datepatterns != null) {
michael@0 99 this.datepatterns = datepatterns.clone();
michael@0 100 } else {
michael@0 101 this.datepatterns = DEFAULT_DATE_PATTERNS;
michael@0 102 }
michael@0 103 registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandler());
michael@0 104 registerAttribHandler(ClientCookie.DOMAIN_ATTR, new BasicDomainHandler());
michael@0 105 registerAttribHandler(ClientCookie.MAX_AGE_ATTR, new BasicMaxAgeHandler());
michael@0 106 registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandler());
michael@0 107 registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandler());
michael@0 108 registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandler(
michael@0 109 this.datepatterns));
michael@0 110 }
michael@0 111
michael@0 112 /** Default constructor */
michael@0 113 public BrowserCompatSpec() {
michael@0 114 this(null);
michael@0 115 }
michael@0 116
michael@0 117 public List<Cookie> parse(final Header header, final CookieOrigin origin)
michael@0 118 throws MalformedCookieException {
michael@0 119 if (header == null) {
michael@0 120 throw new IllegalArgumentException("Header may not be null");
michael@0 121 }
michael@0 122 if (origin == null) {
michael@0 123 throw new IllegalArgumentException("Cookie origin may not be null");
michael@0 124 }
michael@0 125 String headername = header.getName();
michael@0 126 if (!headername.equalsIgnoreCase(SM.SET_COOKIE)) {
michael@0 127 throw new MalformedCookieException("Unrecognized cookie header '"
michael@0 128 + header.toString() + "'");
michael@0 129 }
michael@0 130 HeaderElement[] helems = header.getElements();
michael@0 131 boolean versioned = false;
michael@0 132 boolean netscape = false;
michael@0 133 for (HeaderElement helem: helems) {
michael@0 134 if (helem.getParameterByName("version") != null) {
michael@0 135 versioned = true;
michael@0 136 }
michael@0 137 if (helem.getParameterByName("expires") != null) {
michael@0 138 netscape = true;
michael@0 139 }
michael@0 140 }
michael@0 141 if (netscape || !versioned) {
michael@0 142 // Need to parse the header again, because Netscape style cookies do not correctly
michael@0 143 // support multiple header elements (comma cannot be treated as an element separator)
michael@0 144 NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT;
michael@0 145 CharArrayBuffer buffer;
michael@0 146 ParserCursor cursor;
michael@0 147 if (header instanceof FormattedHeader) {
michael@0 148 buffer = ((FormattedHeader) header).getBuffer();
michael@0 149 cursor = new ParserCursor(
michael@0 150 ((FormattedHeader) header).getValuePos(),
michael@0 151 buffer.length());
michael@0 152 } else {
michael@0 153 String s = header.getValue();
michael@0 154 if (s == null) {
michael@0 155 throw new MalformedCookieException("Header value is null");
michael@0 156 }
michael@0 157 buffer = new CharArrayBuffer(s.length());
michael@0 158 buffer.append(s);
michael@0 159 cursor = new ParserCursor(0, buffer.length());
michael@0 160 }
michael@0 161 helems = new HeaderElement[] { parser.parseHeader(buffer, cursor) };
michael@0 162 }
michael@0 163 return parse(helems, origin);
michael@0 164 }
michael@0 165
michael@0 166 public List<Header> formatCookies(final List<Cookie> cookies) {
michael@0 167 if (cookies == null) {
michael@0 168 throw new IllegalArgumentException("List of cookies may not be null");
michael@0 169 }
michael@0 170 if (cookies.isEmpty()) {
michael@0 171 throw new IllegalArgumentException("List of cookies may not be empty");
michael@0 172 }
michael@0 173 CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size());
michael@0 174 buffer.append(SM.COOKIE);
michael@0 175 buffer.append(": ");
michael@0 176 for (int i = 0; i < cookies.size(); i++) {
michael@0 177 Cookie cookie = cookies.get(i);
michael@0 178 if (i > 0) {
michael@0 179 buffer.append("; ");
michael@0 180 }
michael@0 181 buffer.append(cookie.getName());
michael@0 182 buffer.append("=");
michael@0 183 String s = cookie.getValue();
michael@0 184 if (s != null) {
michael@0 185 buffer.append(s);
michael@0 186 }
michael@0 187 }
michael@0 188 List<Header> headers = new ArrayList<Header>(1);
michael@0 189 headers.add(new BufferedHeader(buffer));
michael@0 190 return headers;
michael@0 191 }
michael@0 192
michael@0 193 public int getVersion() {
michael@0 194 return 0;
michael@0 195 }
michael@0 196
michael@0 197 public Header getVersionHeader() {
michael@0 198 return null;
michael@0 199 }
michael@0 200
michael@0 201 @Override
michael@0 202 public String toString() {
michael@0 203 return "compatibility";
michael@0 204 }
michael@0 205
michael@0 206 }

mercurial