mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/cookie/NetscapeDraftSpec.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.CookieSpec;
michael@0 42 import ch.boye.httpclientandroidlib.cookie.MalformedCookieException;
michael@0 43 import ch.boye.httpclientandroidlib.cookie.SM;
michael@0 44 import ch.boye.httpclientandroidlib.message.BufferedHeader;
michael@0 45 import ch.boye.httpclientandroidlib.message.ParserCursor;
michael@0 46 import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
michael@0 47
michael@0 48 /**
michael@0 49 * This {@link CookieSpec} implementation conforms to the original draft
michael@0 50 * specification published by Netscape Communications. It should be avoided
michael@0 51 * unless absolutely necessary for compatibility with legacy code.
michael@0 52 *
michael@0 53 * @since 4.0
michael@0 54 */
michael@0 55 @NotThreadSafe // superclass is @NotThreadSafe
michael@0 56 public class NetscapeDraftSpec extends CookieSpecBase {
michael@0 57
michael@0 58 protected static final String EXPIRES_PATTERN = "EEE, dd-MMM-yy HH:mm:ss z";
michael@0 59
michael@0 60 private final String[] datepatterns;
michael@0 61
michael@0 62 /** Default constructor */
michael@0 63 public NetscapeDraftSpec(final String[] datepatterns) {
michael@0 64 super();
michael@0 65 if (datepatterns != null) {
michael@0 66 this.datepatterns = datepatterns.clone();
michael@0 67 } else {
michael@0 68 this.datepatterns = new String[] { EXPIRES_PATTERN };
michael@0 69 }
michael@0 70 registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandler());
michael@0 71 registerAttribHandler(ClientCookie.DOMAIN_ATTR, new NetscapeDomainHandler());
michael@0 72 registerAttribHandler(ClientCookie.MAX_AGE_ATTR, new BasicMaxAgeHandler());
michael@0 73 registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandler());
michael@0 74 registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandler());
michael@0 75 registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandler(
michael@0 76 this.datepatterns));
michael@0 77 }
michael@0 78
michael@0 79 /** Default constructor */
michael@0 80 public NetscapeDraftSpec() {
michael@0 81 this(null);
michael@0 82 }
michael@0 83
michael@0 84 /**
michael@0 85 * Parses the Set-Cookie value into an array of <tt>Cookie</tt>s.
michael@0 86 *
michael@0 87 * <p>Syntax of the Set-Cookie HTTP Response Header:</p>
michael@0 88 *
michael@0 89 * <p>This is the format a CGI script would use to add to
michael@0 90 * the HTTP headers a new piece of data which is to be stored by
michael@0 91 * the client for later retrieval.</p>
michael@0 92 *
michael@0 93 * <PRE>
michael@0 94 * Set-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure
michael@0 95 * </PRE>
michael@0 96 *
michael@0 97 * <p>Please note that the Netscape draft specification does not fully conform to the HTTP
michael@0 98 * header format. Comma character if present in <code>Set-Cookie</code> will not be treated
michael@0 99 * as a header element separator</p>
michael@0 100 *
michael@0 101 * @see <a href="http://web.archive.org/web/20020803110822/http://wp.netscape.com/newsref/std/cookie_spec.html">
michael@0 102 * The Cookie Spec.</a>
michael@0 103 *
michael@0 104 * @param header the <tt>Set-Cookie</tt> received from the server
michael@0 105 * @return an array of <tt>Cookie</tt>s parsed from the Set-Cookie value
michael@0 106 * @throws MalformedCookieException if an exception occurs during parsing
michael@0 107 */
michael@0 108 public List<Cookie> parse(final Header header, final CookieOrigin origin)
michael@0 109 throws MalformedCookieException {
michael@0 110 if (header == null) {
michael@0 111 throw new IllegalArgumentException("Header may not be null");
michael@0 112 }
michael@0 113 if (origin == null) {
michael@0 114 throw new IllegalArgumentException("Cookie origin may not be null");
michael@0 115 }
michael@0 116 if (!header.getName().equalsIgnoreCase(SM.SET_COOKIE)) {
michael@0 117 throw new MalformedCookieException("Unrecognized cookie header '"
michael@0 118 + header.toString() + "'");
michael@0 119 }
michael@0 120 NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT;
michael@0 121 CharArrayBuffer buffer;
michael@0 122 ParserCursor cursor;
michael@0 123 if (header instanceof FormattedHeader) {
michael@0 124 buffer = ((FormattedHeader) header).getBuffer();
michael@0 125 cursor = new ParserCursor(
michael@0 126 ((FormattedHeader) header).getValuePos(),
michael@0 127 buffer.length());
michael@0 128 } else {
michael@0 129 String s = header.getValue();
michael@0 130 if (s == null) {
michael@0 131 throw new MalformedCookieException("Header value is null");
michael@0 132 }
michael@0 133 buffer = new CharArrayBuffer(s.length());
michael@0 134 buffer.append(s);
michael@0 135 cursor = new ParserCursor(0, buffer.length());
michael@0 136 }
michael@0 137 return parse(new HeaderElement[] { parser.parseHeader(buffer, cursor) }, origin);
michael@0 138 }
michael@0 139
michael@0 140 public List<Header> formatCookies(final List<Cookie> cookies) {
michael@0 141 if (cookies == null) {
michael@0 142 throw new IllegalArgumentException("List of cookies may not be null");
michael@0 143 }
michael@0 144 if (cookies.isEmpty()) {
michael@0 145 throw new IllegalArgumentException("List of cookies may not be empty");
michael@0 146 }
michael@0 147 CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size());
michael@0 148 buffer.append(SM.COOKIE);
michael@0 149 buffer.append(": ");
michael@0 150 for (int i = 0; i < cookies.size(); i++) {
michael@0 151 Cookie cookie = cookies.get(i);
michael@0 152 if (i > 0) {
michael@0 153 buffer.append("; ");
michael@0 154 }
michael@0 155 buffer.append(cookie.getName());
michael@0 156 String s = cookie.getValue();
michael@0 157 if (s != null) {
michael@0 158 buffer.append("=");
michael@0 159 buffer.append(s);
michael@0 160 }
michael@0 161 }
michael@0 162 List<Header> headers = new ArrayList<Header>(1);
michael@0 163 headers.add(new BufferedHeader(buffer));
michael@0 164 return headers;
michael@0 165 }
michael@0 166
michael@0 167 public int getVersion() {
michael@0 168 return 0;
michael@0 169 }
michael@0 170
michael@0 171 public Header getVersionHeader() {
michael@0 172 return null;
michael@0 173 }
michael@0 174
michael@0 175 @Override
michael@0 176 public String toString() {
michael@0 177 return "netscape";
michael@0 178 }
michael@0 179
michael@0 180 }

mercurial