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