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