mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/cookie/BestMatchSpec.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.List;
michael@0 31
michael@0 32 import ch.boye.httpclientandroidlib.annotation.NotThreadSafe;
michael@0 33
michael@0 34 import ch.boye.httpclientandroidlib.FormattedHeader;
michael@0 35 import ch.boye.httpclientandroidlib.Header;
michael@0 36 import ch.boye.httpclientandroidlib.HeaderElement;
michael@0 37 import ch.boye.httpclientandroidlib.cookie.Cookie;
michael@0 38 import ch.boye.httpclientandroidlib.cookie.CookieOrigin;
michael@0 39 import ch.boye.httpclientandroidlib.cookie.CookieSpec;
michael@0 40 import ch.boye.httpclientandroidlib.cookie.MalformedCookieException;
michael@0 41 import ch.boye.httpclientandroidlib.cookie.SM;
michael@0 42 import ch.boye.httpclientandroidlib.cookie.SetCookie2;
michael@0 43 import ch.boye.httpclientandroidlib.message.ParserCursor;
michael@0 44 import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
michael@0 45
michael@0 46 /**
michael@0 47 * 'Meta' cookie specification that picks up a cookie policy based on
michael@0 48 * the format of cookies sent with the HTTP response.
michael@0 49 *
michael@0 50 * @since 4.0
michael@0 51 */
michael@0 52 @NotThreadSafe // CookieSpec fields are @NotThreadSafe
michael@0 53 public class BestMatchSpec implements CookieSpec {
michael@0 54
michael@0 55 private final String[] datepatterns;
michael@0 56 private final boolean oneHeader;
michael@0 57
michael@0 58 // Cached values of CookieSpec instances
michael@0 59 private RFC2965Spec strict; // @NotThreadSafe
michael@0 60 private RFC2109Spec obsoleteStrict; // @NotThreadSafe
michael@0 61 private BrowserCompatSpec compat; // @NotThreadSafe
michael@0 62
michael@0 63 public BestMatchSpec(final String[] datepatterns, boolean oneHeader) {
michael@0 64 super();
michael@0 65 this.datepatterns = datepatterns == null ? null : datepatterns.clone();
michael@0 66 this.oneHeader = oneHeader;
michael@0 67 }
michael@0 68
michael@0 69 public BestMatchSpec() {
michael@0 70 this(null, false);
michael@0 71 }
michael@0 72
michael@0 73 private RFC2965Spec getStrict() {
michael@0 74 if (this.strict == null) {
michael@0 75 this.strict = new RFC2965Spec(this.datepatterns, this.oneHeader);
michael@0 76 }
michael@0 77 return strict;
michael@0 78 }
michael@0 79
michael@0 80 private RFC2109Spec getObsoleteStrict() {
michael@0 81 if (this.obsoleteStrict == null) {
michael@0 82 this.obsoleteStrict = new RFC2109Spec(this.datepatterns, this.oneHeader);
michael@0 83 }
michael@0 84 return obsoleteStrict;
michael@0 85 }
michael@0 86
michael@0 87 private BrowserCompatSpec getCompat() {
michael@0 88 if (this.compat == null) {
michael@0 89 this.compat = new BrowserCompatSpec(this.datepatterns);
michael@0 90 }
michael@0 91 return compat;
michael@0 92 }
michael@0 93
michael@0 94 public List<Cookie> parse(
michael@0 95 final Header header,
michael@0 96 final CookieOrigin origin) throws MalformedCookieException {
michael@0 97 if (header == null) {
michael@0 98 throw new IllegalArgumentException("Header may not be null");
michael@0 99 }
michael@0 100 if (origin == null) {
michael@0 101 throw new IllegalArgumentException("Cookie origin may not be null");
michael@0 102 }
michael@0 103 HeaderElement[] helems = header.getElements();
michael@0 104 boolean versioned = false;
michael@0 105 boolean netscape = false;
michael@0 106 for (HeaderElement helem: helems) {
michael@0 107 if (helem.getParameterByName("version") != null) {
michael@0 108 versioned = true;
michael@0 109 }
michael@0 110 if (helem.getParameterByName("expires") != null) {
michael@0 111 netscape = true;
michael@0 112 }
michael@0 113 }
michael@0 114 if (netscape || !versioned) {
michael@0 115 // Need to parse the header again, because Netscape style cookies do not correctly
michael@0 116 // support multiple header elements (comma cannot be treated as an element separator)
michael@0 117 NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT;
michael@0 118 CharArrayBuffer buffer;
michael@0 119 ParserCursor cursor;
michael@0 120 if (header instanceof FormattedHeader) {
michael@0 121 buffer = ((FormattedHeader) header).getBuffer();
michael@0 122 cursor = new ParserCursor(
michael@0 123 ((FormattedHeader) header).getValuePos(),
michael@0 124 buffer.length());
michael@0 125 } else {
michael@0 126 String s = header.getValue();
michael@0 127 if (s == null) {
michael@0 128 throw new MalformedCookieException("Header value is null");
michael@0 129 }
michael@0 130 buffer = new CharArrayBuffer(s.length());
michael@0 131 buffer.append(s);
michael@0 132 cursor = new ParserCursor(0, buffer.length());
michael@0 133 }
michael@0 134 helems = new HeaderElement[] { parser.parseHeader(buffer, cursor) };
michael@0 135 return getCompat().parse(helems, origin);
michael@0 136 } else {
michael@0 137 if (SM.SET_COOKIE2.equals(header.getName())) {
michael@0 138 return getStrict().parse(helems, origin);
michael@0 139 } else {
michael@0 140 return getObsoleteStrict().parse(helems, origin);
michael@0 141 }
michael@0 142 }
michael@0 143 }
michael@0 144
michael@0 145 public void validate(
michael@0 146 final Cookie cookie,
michael@0 147 final CookieOrigin origin) throws MalformedCookieException {
michael@0 148 if (cookie == null) {
michael@0 149 throw new IllegalArgumentException("Cookie may not be null");
michael@0 150 }
michael@0 151 if (origin == null) {
michael@0 152 throw new IllegalArgumentException("Cookie origin may not be null");
michael@0 153 }
michael@0 154 if (cookie.getVersion() > 0) {
michael@0 155 if (cookie instanceof SetCookie2) {
michael@0 156 getStrict().validate(cookie, origin);
michael@0 157 } else {
michael@0 158 getObsoleteStrict().validate(cookie, origin);
michael@0 159 }
michael@0 160 } else {
michael@0 161 getCompat().validate(cookie, origin);
michael@0 162 }
michael@0 163 }
michael@0 164
michael@0 165 public boolean match(final Cookie cookie, final CookieOrigin origin) {
michael@0 166 if (cookie == null) {
michael@0 167 throw new IllegalArgumentException("Cookie may not be null");
michael@0 168 }
michael@0 169 if (origin == null) {
michael@0 170 throw new IllegalArgumentException("Cookie origin may not be null");
michael@0 171 }
michael@0 172 if (cookie.getVersion() > 0) {
michael@0 173 if (cookie instanceof SetCookie2) {
michael@0 174 return getStrict().match(cookie, origin);
michael@0 175 } else {
michael@0 176 return getObsoleteStrict().match(cookie, origin);
michael@0 177 }
michael@0 178 } else {
michael@0 179 return getCompat().match(cookie, origin);
michael@0 180 }
michael@0 181 }
michael@0 182
michael@0 183 public List<Header> formatCookies(final List<Cookie> cookies) {
michael@0 184 if (cookies == null) {
michael@0 185 throw new IllegalArgumentException("List of cookies may not be null");
michael@0 186 }
michael@0 187 int version = Integer.MAX_VALUE;
michael@0 188 boolean isSetCookie2 = true;
michael@0 189 for (Cookie cookie: cookies) {
michael@0 190 if (!(cookie instanceof SetCookie2)) {
michael@0 191 isSetCookie2 = false;
michael@0 192 }
michael@0 193 if (cookie.getVersion() < version) {
michael@0 194 version = cookie.getVersion();
michael@0 195 }
michael@0 196 }
michael@0 197 if (version > 0) {
michael@0 198 if (isSetCookie2) {
michael@0 199 return getStrict().formatCookies(cookies);
michael@0 200 } else {
michael@0 201 return getObsoleteStrict().formatCookies(cookies);
michael@0 202 }
michael@0 203 } else {
michael@0 204 return getCompat().formatCookies(cookies);
michael@0 205 }
michael@0 206 }
michael@0 207
michael@0 208 public int getVersion() {
michael@0 209 return getStrict().getVersion();
michael@0 210 }
michael@0 211
michael@0 212 public Header getVersionHeader() {
michael@0 213 return getStrict().getVersionHeader();
michael@0 214 }
michael@0 215
michael@0 216 @Override
michael@0 217 public String toString() {
michael@0 218 return "best-match";
michael@0 219 }
michael@0 220
michael@0 221 }

mercurial