michael@0: /*
michael@0: * ====================================================================
michael@0: * Licensed to the Apache Software Foundation (ASF) under one
michael@0: * or more contributor license agreements. See the NOTICE file
michael@0: * distributed with this work for additional information
michael@0: * regarding copyright ownership. The ASF licenses this file
michael@0: * to you under the Apache License, Version 2.0 (the
michael@0: * "License"); you may not use this file except in compliance
michael@0: * with the License. You may obtain a copy of the License at
michael@0: *
michael@0: * http://www.apache.org/licenses/LICENSE-2.0
michael@0: *
michael@0: * Unless required by applicable law or agreed to in writing,
michael@0: * software distributed under the License is distributed on an
michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0: * KIND, either express or implied. See the License for the
michael@0: * specific language governing permissions and limitations
michael@0: * under the License.
michael@0: * ====================================================================
michael@0: *
michael@0: * This software consists of voluntary contributions made by many
michael@0: * individuals on behalf of the Apache Software Foundation. For more
michael@0: * information on the Apache Software Foundation, please see
michael@0: * .
michael@0: *
michael@0: */
michael@0:
michael@0: package ch.boye.httpclientandroidlib.impl.cookie;
michael@0:
michael@0: import java.util.ArrayList;
michael@0: import java.util.List;
michael@0:
michael@0: import ch.boye.httpclientandroidlib.annotation.NotThreadSafe;
michael@0:
michael@0: import ch.boye.httpclientandroidlib.FormattedHeader;
michael@0: import ch.boye.httpclientandroidlib.Header;
michael@0: import ch.boye.httpclientandroidlib.HeaderElement;
michael@0: import ch.boye.httpclientandroidlib.cookie.ClientCookie;
michael@0: import ch.boye.httpclientandroidlib.cookie.Cookie;
michael@0: import ch.boye.httpclientandroidlib.cookie.CookieOrigin;
michael@0: import ch.boye.httpclientandroidlib.cookie.MalformedCookieException;
michael@0: import ch.boye.httpclientandroidlib.cookie.SM;
michael@0: import ch.boye.httpclientandroidlib.message.BufferedHeader;
michael@0: import ch.boye.httpclientandroidlib.message.ParserCursor;
michael@0: import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
michael@0:
michael@0: /**
michael@0: * Cookie specification that strives to closely mimic (mis)behavior of
michael@0: * common web browser applications such as Microsoft Internet Explorer
michael@0: * and Mozilla FireFox.
michael@0: *
michael@0: *
michael@0: * @since 4.0
michael@0: */
michael@0: @NotThreadSafe // superclass is @NotThreadSafe
michael@0: public class BrowserCompatSpec extends CookieSpecBase {
michael@0:
michael@0: @Deprecated
michael@0: protected static final String[] DATE_PATTERNS = new String[] {
michael@0: DateUtils.PATTERN_RFC1123,
michael@0: DateUtils.PATTERN_RFC1036,
michael@0: DateUtils.PATTERN_ASCTIME,
michael@0: "EEE, dd-MMM-yyyy HH:mm:ss z",
michael@0: "EEE, dd-MMM-yyyy HH-mm-ss z",
michael@0: "EEE, dd MMM yy HH:mm:ss z",
michael@0: "EEE dd-MMM-yyyy HH:mm:ss z",
michael@0: "EEE dd MMM yyyy HH:mm:ss z",
michael@0: "EEE dd-MMM-yyyy HH-mm-ss z",
michael@0: "EEE dd-MMM-yy HH:mm:ss z",
michael@0: "EEE dd MMM yy HH:mm:ss z",
michael@0: "EEE,dd-MMM-yy HH:mm:ss z",
michael@0: "EEE,dd-MMM-yyyy HH:mm:ss z",
michael@0: "EEE, dd-MM-yyyy HH:mm:ss z",
michael@0: };
michael@0:
michael@0: private static final String[] DEFAULT_DATE_PATTERNS = new String[] {
michael@0: DateUtils.PATTERN_RFC1123,
michael@0: DateUtils.PATTERN_RFC1036,
michael@0: DateUtils.PATTERN_ASCTIME,
michael@0: "EEE, dd-MMM-yyyy HH:mm:ss z",
michael@0: "EEE, dd-MMM-yyyy HH-mm-ss z",
michael@0: "EEE, dd MMM yy HH:mm:ss z",
michael@0: "EEE dd-MMM-yyyy HH:mm:ss z",
michael@0: "EEE dd MMM yyyy HH:mm:ss z",
michael@0: "EEE dd-MMM-yyyy HH-mm-ss z",
michael@0: "EEE dd-MMM-yy HH:mm:ss z",
michael@0: "EEE dd MMM yy HH:mm:ss z",
michael@0: "EEE,dd-MMM-yy HH:mm:ss z",
michael@0: "EEE,dd-MMM-yyyy HH:mm:ss z",
michael@0: "EEE, dd-MM-yyyy HH:mm:ss z",
michael@0: };
michael@0:
michael@0: private final String[] datepatterns;
michael@0:
michael@0: /** Default constructor */
michael@0: public BrowserCompatSpec(final String[] datepatterns) {
michael@0: super();
michael@0: if (datepatterns != null) {
michael@0: this.datepatterns = datepatterns.clone();
michael@0: } else {
michael@0: this.datepatterns = DEFAULT_DATE_PATTERNS;
michael@0: }
michael@0: registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandler());
michael@0: registerAttribHandler(ClientCookie.DOMAIN_ATTR, new BasicDomainHandler());
michael@0: registerAttribHandler(ClientCookie.MAX_AGE_ATTR, new BasicMaxAgeHandler());
michael@0: registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandler());
michael@0: registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandler());
michael@0: registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandler(
michael@0: this.datepatterns));
michael@0: }
michael@0:
michael@0: /** Default constructor */
michael@0: public BrowserCompatSpec() {
michael@0: this(null);
michael@0: }
michael@0:
michael@0: public List parse(final Header header, final CookieOrigin origin)
michael@0: throws MalformedCookieException {
michael@0: if (header == null) {
michael@0: throw new IllegalArgumentException("Header may not be null");
michael@0: }
michael@0: if (origin == null) {
michael@0: throw new IllegalArgumentException("Cookie origin may not be null");
michael@0: }
michael@0: String headername = header.getName();
michael@0: if (!headername.equalsIgnoreCase(SM.SET_COOKIE)) {
michael@0: throw new MalformedCookieException("Unrecognized cookie header '"
michael@0: + header.toString() + "'");
michael@0: }
michael@0: HeaderElement[] helems = header.getElements();
michael@0: boolean versioned = false;
michael@0: boolean netscape = false;
michael@0: for (HeaderElement helem: helems) {
michael@0: if (helem.getParameterByName("version") != null) {
michael@0: versioned = true;
michael@0: }
michael@0: if (helem.getParameterByName("expires") != null) {
michael@0: netscape = true;
michael@0: }
michael@0: }
michael@0: if (netscape || !versioned) {
michael@0: // Need to parse the header again, because Netscape style cookies do not correctly
michael@0: // support multiple header elements (comma cannot be treated as an element separator)
michael@0: NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT;
michael@0: CharArrayBuffer buffer;
michael@0: ParserCursor cursor;
michael@0: if (header instanceof FormattedHeader) {
michael@0: buffer = ((FormattedHeader) header).getBuffer();
michael@0: cursor = new ParserCursor(
michael@0: ((FormattedHeader) header).getValuePos(),
michael@0: buffer.length());
michael@0: } else {
michael@0: String s = header.getValue();
michael@0: if (s == null) {
michael@0: throw new MalformedCookieException("Header value is null");
michael@0: }
michael@0: buffer = new CharArrayBuffer(s.length());
michael@0: buffer.append(s);
michael@0: cursor = new ParserCursor(0, buffer.length());
michael@0: }
michael@0: helems = new HeaderElement[] { parser.parseHeader(buffer, cursor) };
michael@0: }
michael@0: return parse(helems, origin);
michael@0: }
michael@0:
michael@0: public List formatCookies(final List cookies) {
michael@0: if (cookies == null) {
michael@0: throw new IllegalArgumentException("List of cookies may not be null");
michael@0: }
michael@0: if (cookies.isEmpty()) {
michael@0: throw new IllegalArgumentException("List of cookies may not be empty");
michael@0: }
michael@0: CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size());
michael@0: buffer.append(SM.COOKIE);
michael@0: buffer.append(": ");
michael@0: for (int i = 0; i < cookies.size(); i++) {
michael@0: Cookie cookie = cookies.get(i);
michael@0: if (i > 0) {
michael@0: buffer.append("; ");
michael@0: }
michael@0: buffer.append(cookie.getName());
michael@0: buffer.append("=");
michael@0: String s = cookie.getValue();
michael@0: if (s != null) {
michael@0: buffer.append(s);
michael@0: }
michael@0: }
michael@0: List headers = new ArrayList(1);
michael@0: headers.add(new BufferedHeader(buffer));
michael@0: return headers;
michael@0: }
michael@0:
michael@0: public int getVersion() {
michael@0: return 0;
michael@0: }
michael@0:
michael@0: public Header getVersionHeader() {
michael@0: return null;
michael@0: }
michael@0:
michael@0: @Override
michael@0: public String toString() {
michael@0: return "compatibility";
michael@0: }
michael@0:
michael@0: }