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