jdk/src/share/classes/java/net/HttpCookie.java
author jccollet
Thu, 17 Apr 2008 11:05:33 +0200
changeset 480 c309ca1d3a86
parent 2 90ce3da70b43
child 715 f16baef3a20e
permissions -rw-r--r--
6644726: Cookie management issues Summary: Many changes to accomodate RFC 2965 and old Netscape specs Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.text.SimpleDateFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.TimeZone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.NullPointerException;  // for javadoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * An HttpCookie object represents an http cookie, which carries state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * information between server and user agent. Cookie is widely adopted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * to create stateful sessions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>There are 3 http cookie specifications:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *   Netscape draft<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *   RFC 2109 - <a href="http://www.ietf.org/rfc/rfc2109.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <i>http://www.ietf.org/rfc/rfc2109.txt</i></a><br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *   RFC 2965 - <a href="http://www.ietf.org/rfc/rfc2965.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <i>http://www.ietf.org/rfc/rfc2965.txt</i></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>HttpCookie class can accept all these 3 forms of syntax.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author Edward Wang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public final class HttpCookie implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /* ---------------- Fields -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // The value of the cookie itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private String name;        // NAME= ... "$Name" style is reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private String value;       // value of NAME
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // Attributes encoded in the header's cookie fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private String comment;     // Comment=VALUE ... describes cookie's use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private String commentURL;  // CommentURL="http URL" ... describes cookie's use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private boolean toDiscard;  // Discard ... discard cookie unconditionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private String domain;      // Domain=VALUE ... domain that sees cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private long maxAge = MAX_AGE_UNSPECIFIED;  // Max-Age=VALUE ... cookies auto-expire
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private String path;        // Path=VALUE ... URLs that see the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private String portlist;    // Port[="portlist"] ... the port cookie may be returned to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private boolean secure;     // Secure ... e.g. use SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private int version = 1;    // Version=1 ... RFC 2965 style
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // Hold the creation time (in seconds) of the http cookie for later
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // expiration calculation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private long whenCreated = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // Since the positive and zero max-age have their meanings,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // this value serves as a hint as 'not specify max-age'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private final static long MAX_AGE_UNSPECIFIED = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    //
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
    95
    // date formats used by Netscape's cookie draft
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
    96
    // as well as formats seen on various sites
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    //
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
    98
    private final static String[] COOKIE_DATE_FORMATS = {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
    99
        "EEE',' dd-MMM-yyyy HH:mm:ss 'GMT'",
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   100
        "EEE',' dd MMM yyyy HH:mm:ss 'GMT'",
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   101
        "EEE MMM dd yyyy HH:mm:ss 'GMT'Z"
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   102
    };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    // constant strings represent set-cookie header token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private final static String SET_COOKIE = "set-cookie:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private final static String SET_COOKIE2 = "set-cookie2:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /* ---------------- Ctors -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Constructs a cookie with a specified name and value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <p>The name must conform to RFC 2965. That means it can contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * only ASCII alphanumeric characters and cannot contain commas,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * semicolons, or white space or begin with a $ character. The cookie's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * name cannot be changed after creation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * <p>The value can be anything the server chooses to send. Its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * value is probably of interest only to the server. The cookie's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * value can be changed after creation with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * <code>setValue</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * <p>By default, cookies are created according to the RFC 2965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * cookie specification. The version can be changed with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <code>setVersion</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param name                      a <code>String</code> specifying the name of the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param value                     a <code>String</code> specifying the value of the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @throws IllegalArgumentException if the cookie name contains illegal characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *                                  or it is one of the tokens reserved for use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *                                  by the cookie protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @throws NullPointerException     if <tt>name</tt> is <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @see #setVersion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public HttpCookie(String name, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        name = name.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (name.length() == 0 || !isToken(name) || isReserved(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            throw new IllegalArgumentException("Illegal cookie name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        toDiscard = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        secure = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        whenCreated = System.currentTimeMillis();
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   156
        portlist = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Constructs cookies from set-cookie or set-cookie2 header string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * RFC 2965 section 3.2.2 set-cookie2 syntax indicates that one header line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * may contain more than one cookie definitions, so this is a static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * utility method instead of another constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param header    a <tt>String</tt> specifying the set-cookie header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *                  The header should start with "set-cookie", or "set-cookie2"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *                  token; or it should have no leading token at all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @return          a List of cookie parsed from header line string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @throws IllegalArgumentException if header string violates the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *                                  specification's syntax, or the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *                                  name contains llegal characters, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *                                  the cookie name is one of the tokens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *                                  reserved for use by the cookie protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @throws NullPointerException     if the header string is <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public static List<HttpCookie> parse(String header) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        int version = guessCookieVersion(header);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // if header start with set-cookie or set-cookie2, strip it off
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (startsWithIgnoreCase(header, SET_COOKIE2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            header = header.substring(SET_COOKIE2.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        } else if (startsWithIgnoreCase(header, SET_COOKIE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            header = header.substring(SET_COOKIE.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        List<HttpCookie> cookies = new java.util.ArrayList<HttpCookie>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // The Netscape cookie may have a comma in its expires attribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        // while the comma is the delimiter in rfc 2965/2109 cookie header string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        // so the parse logic is slightly different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (version == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            // Netscape draft cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            HttpCookie cookie = parseInternal(header);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            cookie.setVersion(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            cookies.add(cookie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            // rfc2965/2109 cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            // if header string contains more than one cookie,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            // it'll separate them with comma
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            List<String> cookieStrings = splitMultiCookies(header);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            for (String cookieStr : cookieStrings) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                HttpCookie cookie = parseInternal(cookieStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                cookie.setVersion(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                cookies.add(cookie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return cookies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /* ---------------- Public operations -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Reports whether this http cookie has expired or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @return  <tt>true</tt> to indicate this http cookie has expired;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *          otherwise, <tt>false</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public boolean hasExpired() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (maxAge == 0) return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        // if not specify max-age, this cookie should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        // discarded when user agent is to be closed, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        // it is not expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (maxAge == MAX_AGE_UNSPECIFIED) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        long deltaSecond = (System.currentTimeMillis() - whenCreated) / 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (deltaSecond > maxAge)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Specifies a comment that describes a cookie's purpose.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * The comment is useful if the browser presents the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * to the user. Comments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * are not supported by Netscape Version 0 cookies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @param purpose           a <code>String</code> specifying the comment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *                          to display to the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @see #getComment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    public void setComment(String purpose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        comment = purpose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Returns the comment describing the purpose of this cookie, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * <code>null</code> if the cookie has no comment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @return                  a <code>String</code> containing the comment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *                          or <code>null</code> if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @see #setComment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public String getComment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return comment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * Specifies a comment url that describes a cookie's purpose.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * The comment url is useful if the browser presents the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * to the user. Comment url is RFC 2965 only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @param purpose           a <code>String</code> specifying the comment url
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *                          to display to the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @see #getCommentURL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public void setCommentURL(String purpose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        commentURL = purpose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Returns the comment url describing the purpose of this cookie, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * <code>null</code> if the cookie has no comment url.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @return                  a <code>String</code> containing the comment url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *                          or <code>null</code> if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @see #setCommentURL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public String getCommentURL() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return commentURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Specify whether user agent should discard the cookie unconditionally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * This is RFC 2965 only attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @param discard   <tt>true</tt> indicates to discard cookie unconditionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @see #getDiscard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public void setDiscard(boolean discard) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        toDiscard = discard;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Return the discard attribute of the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @return  a <tt>boolean</tt> to represent this cookie's discard attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @see #setDiscard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    public boolean getDiscard() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        return toDiscard;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Specify the portlist of the cookie, which restricts the port(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * to which a cookie may be sent back in a Cookie header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @param ports     a <tt>String</tt> specify the port list, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *                  comma seperated series of digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @see #getPortlist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public void setPortlist(String ports) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        portlist = ports;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Return the port list attribute of the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @return  a <tt>String</tt> contains the port list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *          or <tt>null</tt> if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @see #setPortlist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    public String getPortlist() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        return portlist;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Specifies the domain within which this cookie should be presented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * <p>The form of the domain name is specified by RFC 2965. A domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * name begins with a dot (<code>.foo.com</code>) and means that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * the cookie is visible to servers in a specified Domain Name System
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * (DNS) zone (for example, <code>www.foo.com</code>, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * <code>a.b.foo.com</code>). By default, cookies are only returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * to the server that sent them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @param pattern           a <code>String</code> containing the domain name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *                          within which this cookie is visible;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *                          form is according to RFC 2965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @see #getDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    public void setDomain(String pattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        if (pattern != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            domain = pattern.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            domain = pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * Returns the domain name set for this cookie. The form of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * the domain name is set by RFC 2965.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @return                  a <code>String</code> containing the domain name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @see #setDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    public String getDomain() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        return domain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * Sets the maximum age of the cookie in seconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * <p>A positive value indicates that the cookie will expire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * after that many seconds have passed. Note that the value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * the <i>maximum</i> age when the cookie will expire, not the cookie's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * current age.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * <p>A negative value means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * that the cookie is not stored persistently and will be deleted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * when the Web browser exits. A zero value causes the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * to be deleted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @param expiry            an integer specifying the maximum age of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *                          cookie in seconds; if zero, the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *                          should be discarded immediately;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *                          otherwise, the cookie's max age is unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @see #getMaxAge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    public void setMaxAge(long expiry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        maxAge = expiry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * Returns the maximum age of the cookie, specified in seconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * By default, <code>-1</code> indicating the cookie will persist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * until browser shutdown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @return                  an integer specifying the maximum age of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *                          cookie in seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @see #setMaxAge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    public long getMaxAge() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        return maxAge;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * Specifies a path for the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * to which the client should return the cookie.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * <p>The cookie is visible to all the pages in the directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * you specify, and all the pages in that directory's subdirectories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * A cookie's path must include the servlet that set the cookie,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * for example, <i>/catalog</i>, which makes the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * visible to all directories on the server under <i>/catalog</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * <p>Consult RFC 2965 (available on the Internet) for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * information on setting path names for cookies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @param uri               a <code>String</code> specifying a path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @see #getPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public void setPath(String uri) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        path = uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * Returns the path on the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * to which the browser returns this cookie. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * cookie is visible to all subpaths on the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @return          a <code>String</code> specifying a path that contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *                  a servlet name, for example, <i>/catalog</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @see #setPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    public String getPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        return path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    /**
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   514
     * Indicates whether the cookie should only be sent using a secure protocol,
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   515
     * such as HTTPS or SSL.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * <p>The default value is <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   519
     * @param flag      If <code>true</code>, the cookie can only be sent over
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   520
     *                  a secure protocol like https.
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   521
     *                  If <code>false</code>, it can be sent over any protocol.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @see #getSecure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    public void setSecure(boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        secure = flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    /**
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   535
     * Returns <code>true</code> if sending this cookie should be
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   536
     * restricted to a secure protocol, or <code>false</code> if the
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   537
     * it can be sent using any protocol.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   539
     * @return          <code>false</code> if the cookie can be sent over
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   540
     *                  any standard protocol; otherwise, <code>true</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @see #setSecure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    public boolean getSecure() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        return secure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * Returns the name of the cookie. The name cannot be changed after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * creation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * @return          a <code>String</code> specifying the cookie's name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * Assigns a new value to a cookie after the cookie is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * If you use a binary value, you may want to use BASE64 encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * <p>With Version 0 cookies, values should not contain white
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * space, brackets, parentheses, equals signs, commas,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * double quotes, slashes, question marks, at signs, colons,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * and semicolons. Empty values may not behave the same way
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * on all browsers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @param newValue          a <code>String</code> specifying the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @see #getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    public void setValue(String newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        value = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * Returns the value of the cookie.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * @return                  a <code>String</code> containing the cookie's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *                          present value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    public String getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * Returns the version of the protocol this cookie complies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * with. Version 1 complies with RFC 2965/2109,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * and version 0 complies with the original
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * cookie specification drafted by Netscape. Cookies provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * by a browser use and identify the browser's cookie version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @return                  0 if the cookie complies with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     *                          original Netscape specification; 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *                          if the cookie complies with RFC 2965/2109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * @see #setVersion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    public int getVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        return version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * Sets the version of the cookie protocol this cookie complies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * with. Version 0 complies with the original Netscape cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * specification. Version 1 complies with RFC 2965/2109.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @param v                 0 if the cookie should comply with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *                          the original Netscape specification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     *                          1 if the cookie should comply with RFC 2965/2109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * @throws IllegalArgumentException if <tt>v</tt> is neither 0 nor 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * @see #getVersion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    public void setVersion(int v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        if (v != 0 && v != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            throw new IllegalArgumentException("cookie version should be 0 or 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        version = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * The utility method to check whether a host name is in a domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * <p>This concept is described in the cookie specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * To understand the concept, some terminologies need to be defined first:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * effective host name = hostname if host name contains dot<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;or = hostname.local if not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * <p>Host A's name domain-matches host B's if:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * <blockquote><ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     *   <li>their host name strings string-compare equal; or</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     *   <li>A is a HDN string and has the form NB, where N is a non-empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     *   name string, B has the form .B', and B' is a HDN string.  (So,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     *   x.y.com domain-matches .Y.com but not Y.com.)</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * </ul></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * <p>A host isn't in a domain (RFC 2965 sec. 3.3.2) if:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * <blockquote><ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *   <li>The value for the Domain attribute contains no embedded dots,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     *   and the value is not .local.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *   <li>The effective host name that derives from the request-host does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     *   not domain-match the Domain attribute.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     *   <li>The request-host is a HDN (not IP address) and has the form HD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     *   where D is the value of the Domain attribute, and H is a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     *   that contains one or more dots.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * </ul></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * <p>Examples:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * <blockquote><ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *   <li>A Set-Cookie2 from request-host y.x.foo.com for Domain=.foo.com
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     *   would be rejected, because H is y.x and contains a dot.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     *   <li>A Set-Cookie2 from request-host x.foo.com for Domain=.foo.com
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     *   would be accepted.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *   <li>A Set-Cookie2 with Domain=.com or Domain=.com., will always be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *   rejected, because there is no embedded dot.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     *   <li>A Set-Cookie2 with Domain=ajax.com will be accepted, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *   value for Domain will be taken to be .ajax.com, because a dot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     *   gets prepended to the value.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *   <li>A Set-Cookie2 from request-host example for Domain=.local will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     *   be accepted, because the effective host name for the request-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     *   host is example.local, and example.local domain-matches .local.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * </ul></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * @param domain    the domain name to check host name with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * @param host      the host name in question
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * @return          <tt>true</tt> if they domain-matches; <tt>false</tt> if not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    public static boolean domainMatches(String domain, String host) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        if (domain == null || host == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        // if there's no embedded dot in domain and domain is not .local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        boolean isLocalDomain = ".local".equalsIgnoreCase(domain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        int embeddedDotInDomain = domain.indexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        if (embeddedDotInDomain == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            embeddedDotInDomain = domain.indexOf('.', 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        if (!isLocalDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            && (embeddedDotInDomain == -1 || embeddedDotInDomain == domain.length() - 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        // if the host name contains no dot and the domain name is .local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        int firstDotInHost = host.indexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        if (firstDotInHost == -1 && isLocalDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        int domainLength = domain.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        int lengthDiff = host.length() - domainLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        if (lengthDiff == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            // if the host name and the domain name are just string-compare euqal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            return host.equalsIgnoreCase(domain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        else if (lengthDiff > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            // need to check H & D component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            String H = host.substring(0, lengthDiff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            String D = host.substring(lengthDiff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            return (H.indexOf('.') == -1 && D.equalsIgnoreCase(domain));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        else if (lengthDiff == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            // if domain is actually .host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            return (domain.charAt(0) == '.' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                        host.equalsIgnoreCase(domain.substring(1)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * Constructs a cookie header string representation of this cookie,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * which is in the format defined by corresponding cookie specification,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * but without the leading "Cookie:" token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * @return  a string form of the cookie. The string has the defined format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     */
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   757
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        if (getVersion() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            return toRFC2965HeaderString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            return toNetscapeHeaderString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * Test the equality of two http cookies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * <p> The result is <tt>true</tt> only if two cookies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * come from same domain (case-insensitive),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * have same name (case-insensitive),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * and have same path (case-sensitive).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * @return          <tt>true</tt> if 2 http cookies equal to each other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     *                  otherwise, <tt>false</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     */
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   778
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        if (!(obj instanceof HttpCookie))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        HttpCookie other = (HttpCookie)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        // One http cookie equals to another cookie (RFC 2965 sec. 3.3.3) if:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        //   1. they come from same domain (case-insensitive),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        //   2. have same name (case-insensitive),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        //   3. and have same path (case-sensitive).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        return equalsIgnoreCase(getName(), other.getName()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
               equalsIgnoreCase(getDomain(), other.getDomain()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
               equals(getPath(), other.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * Return hash code of this http cookie. The result is the sum of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * hash code value of three significant components of this cookie:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * name, domain, and path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * That is, the hash code is the value of the expression:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * getName().toLowerCase().hashCode()<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * + getDomain().toLowerCase().hashCode()<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * + getPath().hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * @return          this http cookie's hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     */
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   809
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        int h1 = name.toLowerCase().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        int h2 = (domain!=null) ? domain.toLowerCase().hashCode() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        int h3 = (path!=null) ? path.hashCode() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        return h1 + h2 + h3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * Create and return a copy of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @return          a clone of this http cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     */
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   823
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            return super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            throw new RuntimeException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    /* ---------------- Private operations -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    // Note -- disabled for now to allow full Netscape compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    // from RFC 2068, token special case characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    // private static final String tspecials = "()<>@,;:\\\"/[]?={} \t";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    private static final String tspecials = ",;";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * Tests a string and returns true if the string counts as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * @param value             the <code>String</code> to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * @return                  <code>true</code> if the <code>String</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     *                          a token; <code>false</code> if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    private static boolean isToken(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        int len = value.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            char c = value.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            if (c < 0x20 || c >= 0x7f || tspecials.indexOf(c) != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * @param name      the name to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * @return          <tt>true</tt> if the name is reserved by cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     *                  specification, <tt>false</tt> if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    private static boolean isReserved(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        if (name.equalsIgnoreCase("Comment")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            || name.equalsIgnoreCase("CommentURL")      // rfc2965 only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            || name.equalsIgnoreCase("Discard")         // rfc2965 only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            || name.equalsIgnoreCase("Domain")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            || name.equalsIgnoreCase("Expires")         // netscape draft only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            || name.equalsIgnoreCase("Max-Age")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            || name.equalsIgnoreCase("Path")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            || name.equalsIgnoreCase("Port")            // rfc2965 only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            || name.equalsIgnoreCase("Secure")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            || name.equalsIgnoreCase("Version")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            || name.charAt(0) == '$')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * Parse header string to cookie object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * @param header    header string; should contain only one NAME=VALUE pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * @return          an HttpCookie being extracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * @throws IllegalArgumentException if header string violates the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     *                                  specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    private static HttpCookie parseInternal(String header)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        HttpCookie cookie = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        String namevaluePair = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        StringTokenizer tokenizer = new StringTokenizer(header, ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        // there should always have at least on name-value pair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        // it's cookie's name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            namevaluePair = tokenizer.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            int index = namevaluePair.indexOf('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            if (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                String name = namevaluePair.substring(0, index).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                String value = namevaluePair.substring(index + 1).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                cookie = new HttpCookie(name, stripOffSurroundingQuote(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                // no "=" in name-value pair; it's an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                throw new IllegalArgumentException("Invalid cookie name-value pair");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        } catch (NoSuchElementException ignored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            throw new IllegalArgumentException("Empty cookie header string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        // remaining name-value pairs are cookie's attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        while (tokenizer.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            namevaluePair = tokenizer.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            int index = namevaluePair.indexOf('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            String name, value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            if (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                name = namevaluePair.substring(0, index).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                value = namevaluePair.substring(index + 1).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                name = namevaluePair.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            // assign attribute to cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            assignAttribute(cookie, name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        return cookie;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * assign cookie attribute value to attribute name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * use a map to simulate method dispatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    static interface CookieAttributeAssignor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            public void assign(HttpCookie cookie, String attrName, String attrValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    static java.util.Map<String, CookieAttributeAssignor> assignors = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        assignors = new java.util.HashMap<String, CookieAttributeAssignor>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        assignors.put("comment", new CookieAttributeAssignor(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                public void assign(HttpCookie cookie, String attrName, String attrValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                    if (cookie.getComment() == null) cookie.setComment(attrValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        assignors.put("commenturl", new CookieAttributeAssignor(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                public void assign(HttpCookie cookie, String attrName, String attrValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                    if (cookie.getCommentURL() == null) cookie.setCommentURL(attrValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        assignors.put("discard", new CookieAttributeAssignor(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                public void assign(HttpCookie cookie, String attrName, String attrValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                    cookie.setDiscard(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        assignors.put("domain", new CookieAttributeAssignor(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                public void assign(HttpCookie cookie, String attrName, String attrValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                    if (cookie.getDomain() == null) cookie.setDomain(attrValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        assignors.put("max-age", new CookieAttributeAssignor(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                public void assign(HttpCookie cookie, String attrName, String attrValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                        long maxage = Long.parseLong(attrValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                        if (cookie.getMaxAge() == MAX_AGE_UNSPECIFIED) cookie.setMaxAge(maxage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                    } catch (NumberFormatException ignored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                        throw new IllegalArgumentException("Illegal cookie max-age attribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        assignors.put("path", new CookieAttributeAssignor(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                public void assign(HttpCookie cookie, String attrName, String attrValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                    if (cookie.getPath() == null) cookie.setPath(attrValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        assignors.put("port", new CookieAttributeAssignor(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                public void assign(HttpCookie cookie, String attrName, String attrValue) {
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   991
                    if (cookie.getPortlist() == null) cookie.setPortlist(attrValue == null ? "" : attrValue);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        assignors.put("secure", new CookieAttributeAssignor(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                public void assign(HttpCookie cookie, String attrName, String attrValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                    cookie.setSecure(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        assignors.put("version", new CookieAttributeAssignor(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                public void assign(HttpCookie cookie, String attrName, String attrValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                        int version = Integer.parseInt(attrValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                        cookie.setVersion(version);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                    } catch (NumberFormatException ignored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                        throw new IllegalArgumentException("Illegal cookie version attribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        assignors.put("expires", new CookieAttributeAssignor(){ // Netscape only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                public void assign(HttpCookie cookie, String attrName, String attrValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                    if (cookie.getMaxAge() == MAX_AGE_UNSPECIFIED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                        cookie.setMaxAge(cookie.expiryDate2DeltaSeconds(attrValue));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    private static void assignAttribute(HttpCookie cookie,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                                       String attrName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                                       String attrValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        // strip off the surrounding "-sign if there's any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        attrValue = stripOffSurroundingQuote(attrValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        CookieAttributeAssignor assignor = assignors.get(attrName.toLowerCase());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        if (assignor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            assignor.assign(cookie, attrName, attrValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            // must be an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            throw new IllegalArgumentException("Illegal cookie attribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * Constructs a string representation of this cookie. The string format is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * as Netscape spec, but without leading "Cookie:" token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    private String toNetscapeHeaderString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        sb.append(getName() + "=" + getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * Constructs a string representation of this cookie. The string format is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * as RFC 2965/2109, but without leading "Cookie:" token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    private String toRFC2965HeaderString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        sb.append(getName()).append("=\"").append(getValue()).append('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        if (getPath() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            sb.append(";$Path=\"").append(getPath()).append('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        if (getDomain() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            sb.append(";$Domain=\"").append(getDomain()).append('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        if (getPortlist() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            sb.append(";$Port=\"").append(getPortlist()).append('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
  1063
    private static SimpleDateFormat[] cDateFormats = null;
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
  1064
    static {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
  1065
            cDateFormats = new SimpleDateFormat[COOKIE_DATE_FORMATS.length];
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
  1066
            for (int i = 0; i < COOKIE_DATE_FORMATS.length; i++) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
  1067
                cDateFormats[i] = new SimpleDateFormat(COOKIE_DATE_FORMATS[i]);
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
  1068
                cDateFormats[i].setTimeZone(TimeZone.getTimeZone("GMT"));
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
  1069
            }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
  1070
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    /*
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
  1072
     * @param dateString        a date string in one of the formats
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
  1073
     *                          defined in Netscape cookie spec
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * @return                  delta seconds between this cookie's creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     *                          time and the time specified by dateString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    private long expiryDate2DeltaSeconds(String dateString) {
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
  1079
        for (SimpleDateFormat df : cDateFormats) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
  1080
            try {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
  1081
                Date date = df.parse(dateString);
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
  1082
                return (date.getTime() - whenCreated) / 1000;
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
  1083
            } catch (Exception e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
  1085
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        }
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
  1087
        return 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * try to guess the cookie version through set-cookie header string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    private static int guessCookieVersion(String header) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        int version = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        header = header.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        if (header.indexOf("expires=") != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            // only netscape cookie using 'expires'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            version = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        } else if (header.indexOf("version=") != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            // version is mandatory for rfc 2965/2109 cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            version = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        } else if (header.indexOf("max-age") != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            // rfc 2965/2109 use 'max-age'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            version = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        } else if (startsWithIgnoreCase(header, SET_COOKIE2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            // only rfc 2965 cookie starts with 'set-cookie2'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            version = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        return version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    private static String stripOffSurroundingQuote(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        if (str != null && str.length() > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            str.charAt(0) == '"' && str.charAt(str.length() - 1) == '"') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            return str.substring(1, str.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    private static boolean equalsIgnoreCase(String s, String t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        if (s == t) return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        if ((s != null) && (t != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            return s.equalsIgnoreCase(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    private static boolean equals(String s, String t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        if (s == t) return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        if ((s != null) && (t != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            return s.equals(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    private static boolean startsWithIgnoreCase(String s, String start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        if (s == null || start == null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        if (s.length() >= start.length() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                start.equalsIgnoreCase(s.substring(0, start.length()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * Split cookie header string according to rfc 2965:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     *   1) split where it is a comma;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     *   2) but not the comma surrounding by double-quotes, which is the comma
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     *      inside port list or embeded URIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * @param header            the cookie header string to split
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * @return                  list of strings; never null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    private static List<String> splitMultiCookies(String header) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        List<String> cookies = new java.util.ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        int quoteCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        int p, q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        for (p = 0, q = 0; p < header.length(); p++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            char c = header.charAt(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            if (c == '"') quoteCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            if (c == ',' && (quoteCount % 2 == 0)) {      // it is comma and not surrounding by double-quotes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                cookies.add(header.substring(q, p));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                q = p + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        cookies.add(header.substring(q));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        return cookies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
}