src/java.base/share/classes/java/net/CookieManager.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 52499 768b1c612100
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18178
diff changeset
     2
 * Copyright (c) 2005, 2013, 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.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
4324
5fd48b8b450b 6901170: HttpCookie parsing of version and max-age mis-handled
jccollet
parents: 4154
diff changeset
    33
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * CookieManager provides a concrete implementation of {@link CookieHandler},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * which separates the storage of cookies from the policy surrounding accepting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * and rejecting cookies. A CookieManager is initialized with a {@link CookieStore}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * which manages storage, and a {@link CookiePolicy} object, which makes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * policy decisions on cookie acceptance/rejection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p> The HTTP cookie management in java.net package looks like:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <blockquote>
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 14342
diff changeset
    44
 * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *                  use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * CookieHandler <------- HttpURLConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *       ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *       | impl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *       |         use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * CookieManager -------> CookiePolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *             |   use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *             |--------> HttpCookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *             |              ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *             |              | use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *             |   use        |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *             |--------> CookieStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *                            ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *                            | impl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *                            |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *                  Internal in-memory implementation
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 14342
diff changeset
    61
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *   <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *     CookieHandler is at the core of cookie management. User can call
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 51724
diff changeset
    65
 *     CookieHandler.setDefault to set a concrete CookieHandler implementation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *     to be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *   </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *   <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *     CookiePolicy.shouldAccept will be called by CookieManager.put to see whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *     or not one cookie should be accepted and put into cookie store. User can use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *     any of three pre-defined CookiePolicy, namely ACCEPT_ALL, ACCEPT_NONE and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *     ACCEPT_ORIGINAL_SERVER, or user can define his own CookiePolicy implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *     and tell CookieManager to use it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *   </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *   <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *     CookieStore is the place where any accepted HTTP cookie is stored in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *     If not specified when created, a CookieManager instance will use an internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *     in-memory implementation. Or user can implements one and tell CookieManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *     to use it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *   </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *   <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *     Currently, only CookieStore.add(URI, HttpCookie) and CookieStore.get(URI)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *     are used by CookieManager. Others are for completeness and might be needed
41482
05e75e5f3afb 8134373: use collections convenience factories in the JDK
smarks
parents: 32649
diff changeset
    84
 *     by a more sophisticated CookieStore implementation, e.g. a NetscapeCookieStore.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *   </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <p>There're various ways user can hook up his own HTTP cookie management behavior, e.g.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *   <li>Use CookieHandler.setDefault to set a brand new {@link CookieHandler} implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *   <li>Let CookieManager be the default {@link CookieHandler} implementation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *       but implement user's own {@link CookieStore} and {@link CookiePolicy}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *       and tell default CookieManager to use them:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *     <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *       // this should be done at the beginning of an HTTP session
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *       CookieHandler.setDefault(new CookieManager(new MyCookieStore(), new MyCookiePolicy()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *     </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *   <li>Let CookieManager be the default {@link CookieHandler} implementation, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *       use customized {@link CookiePolicy}:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *     <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *       // this should be done at the beginning of an HTTP session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *       CookieHandler.setDefault(new CookieManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *       // this can be done at any point of an HTTP session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *       ((CookieManager)CookieHandler.getDefault()).setCookiePolicy(new MyCookiePolicy());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *     </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
1950
dd893f20667b 6585546: Please update API doc for java.net.CookieManager
jccollet
parents: 715
diff changeset
   111
 * <p>The implementation conforms to <a href="http://www.ietf.org/rfc/rfc2965.txt">RFC 2965</a>, section 3.3.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
1950
dd893f20667b 6585546: Please update API doc for java.net.CookieManager
jccollet
parents: 715
diff changeset
   113
 * @see CookiePolicy
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * @author Edward Wang
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
public class CookieManager extends CookieHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /* ---------------- Fields -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private CookiePolicy policyCallback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private CookieStore cookieJar = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /* ---------------- Ctors -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Create a new cookie manager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * <p>This constructor will create new cookie manager with default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * cookie store and accept policy. The effect is same as
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18178
diff changeset
   134
     * {@code CookieManager(null, null)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public CookieManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        this(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Create a new cookie manager with specified cookie store and cookie policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18178
diff changeset
   144
     * @param store     a {@code CookieStore} to be used by cookie manager.
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18178
diff changeset
   145
     *                  if {@code null}, cookie manager will use a default one,
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19069
diff changeset
   146
     *                  which is an in-memory CookieStore implementation.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18178
diff changeset
   147
     * @param cookiePolicy      a {@code CookiePolicy} instance
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *                          to be used by cookie manager as policy callback.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18178
diff changeset
   149
     *                          if {@code null}, ACCEPT_ORIGINAL_SERVER will
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *                          be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public CookieManager(CookieStore store,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                         CookiePolicy cookiePolicy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // use default cookie policy if not specify one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        policyCallback = (cookiePolicy == null) ? CookiePolicy.ACCEPT_ORIGINAL_SERVER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                                : cookiePolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        // if not specify CookieStore to use, use default one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (store == null) {
3858
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents: 2924
diff changeset
   161
            cookieJar = new InMemoryCookieStore();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            cookieJar = store;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /* ---------------- Public operations -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * To set the cookie policy of this cookie manager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18178
diff changeset
   173
     * <p> A instance of {@code CookieManager} will have
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * cookie policy ACCEPT_ORIGINAL_SERVER by default. Users always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * can call this method to set another cookie policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18178
diff changeset
   177
     * @param cookiePolicy      the cookie policy. Can be {@code null}, which
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *                          has no effects on current cookie policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public void setCookiePolicy(CookiePolicy cookiePolicy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (cookiePolicy != null) policyCallback = cookiePolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * To retrieve current cookie store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @return  the cookie store currently used by cookie manager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public CookieStore getCookieStore() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return cookieJar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public Map<String, List<String>>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        get(URI uri, Map<String, List<String>> requestHeaders)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        // pre-condition check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (uri == null || requestHeaders == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            throw new IllegalArgumentException("Argument is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        // if there's no default CookieStore, no way for us to get any cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (cookieJar == null)
41482
05e75e5f3afb 8134373: use collections convenience factories in the JDK
smarks
parents: 32649
diff changeset
   206
            return Map.of();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   208
        boolean secureLink = "https".equalsIgnoreCase(uri.getScheme());
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 26720
diff changeset
   209
        List<HttpCookie> cookies = new java.util.ArrayList<>();
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   210
        String path = uri.getPath();
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   211
        if (path == null || path.isEmpty()) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   212
            path = "/";
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   213
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        for (HttpCookie cookie : cookieJar.get(uri)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            // apply path-matches rule (RFC 2965 sec. 3.3.4)
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   216
            // and check for the possible "secure" tag (i.e. don't send
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   217
            // 'secure' cookies over unsecure links)
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   218
            if (pathMatches(path, cookie.getPath()) &&
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   219
                    (secureLink || !cookie.getSecure())) {
4154
afd948aaf965 6873543: CookieManager doesn't enforce httpOnly
jccollet
parents: 3858
diff changeset
   220
                // Enforce httponly attribute
afd948aaf965 6873543: CookieManager doesn't enforce httpOnly
jccollet
parents: 3858
diff changeset
   221
                if (cookie.isHttpOnly()) {
afd948aaf965 6873543: CookieManager doesn't enforce httpOnly
jccollet
parents: 3858
diff changeset
   222
                    String s = uri.getScheme();
afd948aaf965 6873543: CookieManager doesn't enforce httpOnly
jccollet
parents: 3858
diff changeset
   223
                    if (!"http".equalsIgnoreCase(s) && !"https".equalsIgnoreCase(s)) {
afd948aaf965 6873543: CookieManager doesn't enforce httpOnly
jccollet
parents: 3858
diff changeset
   224
                        continue;
afd948aaf965 6873543: CookieManager doesn't enforce httpOnly
jccollet
parents: 3858
diff changeset
   225
                    }
afd948aaf965 6873543: CookieManager doesn't enforce httpOnly
jccollet
parents: 3858
diff changeset
   226
                }
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   227
                // Let's check the authorize port list if it exists
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   228
                String ports = cookie.getPortlist();
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   229
                if (ports != null && !ports.isEmpty()) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   230
                    int port = uri.getPort();
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   231
                    if (port == -1) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   232
                        port = "https".equals(uri.getScheme()) ? 443 : 80;
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   233
                    }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   234
                    if (isInPortList(ports, port)) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   235
                        cookies.add(cookie);
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   236
                    }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   237
                } else {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   238
                    cookies.add(cookie);
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   239
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        // apply sort rule (RFC 2965 sec. 3.3.4)
51724
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   244
        List<String> cookieHeader = sortByPathAndAge(cookies);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
41482
05e75e5f3afb 8134373: use collections convenience factories in the JDK
smarks
parents: 32649
diff changeset
   246
        return Map.of("Cookie", cookieHeader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        put(URI uri, Map<String, List<String>> responseHeaders)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        // pre-condition check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (uri == null || responseHeaders == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            throw new IllegalArgumentException("Argument is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        // if there's no default CookieStore, no need to remember any cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (cookieJar == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
4324
5fd48b8b450b 6901170: HttpCookie parsing of version and max-age mis-handled
jccollet
parents: 4154
diff changeset
   263
    PlatformLogger logger = PlatformLogger.getLogger("java.net.CookieManager");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        for (String headerKey : responseHeaders.keySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            // RFC 2965 3.2.2, key must be 'Set-Cookie2'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            // we also accept 'Set-Cookie' here for backward compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            if (headerKey == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                || !(headerKey.equalsIgnoreCase("Set-Cookie2")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                     || headerKey.equalsIgnoreCase("Set-Cookie")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            for (String headerValue : responseHeaders.get(headerKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                try {
4324
5fd48b8b450b 6901170: HttpCookie parsing of version and max-age mis-handled
jccollet
parents: 4154
diff changeset
   278
                    List<HttpCookie> cookies;
5fd48b8b450b 6901170: HttpCookie parsing of version and max-age mis-handled
jccollet
parents: 4154
diff changeset
   279
                    try {
5fd48b8b450b 6901170: HttpCookie parsing of version and max-age mis-handled
jccollet
parents: 4154
diff changeset
   280
                        cookies = HttpCookie.parse(headerValue);
5fd48b8b450b 6901170: HttpCookie parsing of version and max-age mis-handled
jccollet
parents: 4154
diff changeset
   281
                    } catch (IllegalArgumentException e) {
5fd48b8b450b 6901170: HttpCookie parsing of version and max-age mis-handled
jccollet
parents: 4154
diff changeset
   282
                        // Bogus header, make an empty list and log the error
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   283
                        cookies = java.util.Collections.emptyList();
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 18174
diff changeset
   284
                        if (logger.isLoggable(PlatformLogger.Level.SEVERE)) {
4324
5fd48b8b450b 6901170: HttpCookie parsing of version and max-age mis-handled
jccollet
parents: 4154
diff changeset
   285
                            logger.severe("Invalid cookie for " + uri + ": " + headerValue);
5fd48b8b450b 6901170: HttpCookie parsing of version and max-age mis-handled
jccollet
parents: 4154
diff changeset
   286
                        }
5fd48b8b450b 6901170: HttpCookie parsing of version and max-age mis-handled
jccollet
parents: 4154
diff changeset
   287
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    for (HttpCookie cookie : cookies) {
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   289
                        if (cookie.getPath() == null) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   290
                            // If no path is specified, then by default
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   291
                            // the path is the directory of the page/doc
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   292
                            String path = uri.getPath();
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   293
                            if (!path.endsWith("/")) {
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 21278
diff changeset
   294
                                int i = path.lastIndexOf('/');
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   295
                                if (i > 0) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   296
                                    path = path.substring(0, i + 1);
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   297
                                } else {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   298
                                    path = "/";
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   299
                                }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   300
                            }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   301
                            cookie.setPath(path);
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   302
                        }
2924
a67f12d92bdd 6349566: java.net.CookieManager doesn't set default domain
jccollet
parents: 1950
diff changeset
   303
a67f12d92bdd 6349566: java.net.CookieManager doesn't set default domain
jccollet
parents: 1950
diff changeset
   304
                        // As per RFC 2965, section 3.3.1:
a67f12d92bdd 6349566: java.net.CookieManager doesn't set default domain
jccollet
parents: 1950
diff changeset
   305
                        // Domain  Defaults to the effective request-host.  (Note that because
a67f12d92bdd 6349566: java.net.CookieManager doesn't set default domain
jccollet
parents: 1950
diff changeset
   306
                        // there is no dot at the beginning of effective request-host,
a67f12d92bdd 6349566: java.net.CookieManager doesn't set default domain
jccollet
parents: 1950
diff changeset
   307
                        // the default Domain can only domain-match itself.)
a67f12d92bdd 6349566: java.net.CookieManager doesn't set default domain
jccollet
parents: 1950
diff changeset
   308
                        if (cookie.getDomain() == null) {
18174
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents: 18156
diff changeset
   309
                            String host = uri.getHost();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents: 18156
diff changeset
   310
                            if (host != null && !host.contains("."))
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents: 18156
diff changeset
   311
                                host += ".local";
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents: 18156
diff changeset
   312
                            cookie.setDomain(host);
2924
a67f12d92bdd 6349566: java.net.CookieManager doesn't set default domain
jccollet
parents: 1950
diff changeset
   313
                        }
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   314
                        String ports = cookie.getPortlist();
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   315
                        if (ports != null) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   316
                            int port = uri.getPort();
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   317
                            if (port == -1) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   318
                                port = "https".equals(uri.getScheme()) ? 443 : 80;
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   319
                            }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   320
                            if (ports.isEmpty()) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   321
                                // Empty port list means this should be restricted
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   322
                                // to the incoming URI port
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   323
                                cookie.setPortlist("" + port );
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   324
                                if (shouldAcceptInternal(uri, cookie)) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   325
                                    cookieJar.add(uri, cookie);
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   326
                                }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   327
                            } else {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   328
                                // Only store cookies with a port list
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   329
                                // IF the URI port is in that list, as per
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   330
                                // RFC 2965 section 3.3.2
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   331
                                if (isInPortList(ports, port) &&
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   332
                                        shouldAcceptInternal(uri, cookie)) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   333
                                    cookieJar.add(uri, cookie);
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   334
                                }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   335
                            }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   336
                        } else {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   337
                            if (shouldAcceptInternal(uri, cookie)) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   338
                                cookieJar.add(uri, cookie);
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   339
                            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    // invalid set-cookie header string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    // no-op
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
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /* ---------------- Private operations -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    // to determine whether or not accept this cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    private boolean shouldAcceptInternal(URI uri, HttpCookie cookie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            return policyCallback.shouldAccept(uri, cookie);
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 51724
diff changeset
   357
        } catch (Exception ignored) { // protect against malicious callback
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29986
diff changeset
   363
    private static boolean isInPortList(String lst, int port) {
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 21278
diff changeset
   364
        int i = lst.indexOf(',');
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   365
        int val = -1;
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   366
        while (i > 0) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   367
            try {
26720
6b160d97c51d 8055032: Improve numerical parsing in java.net and sun.net
redestad
parents: 25859
diff changeset
   368
                val = Integer.parseInt(lst, 0, i, 10);
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   369
                if (val == port) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   370
                    return true;
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   371
                }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   372
            } catch (NumberFormatException numberFormatException) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   373
            }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   374
            lst = lst.substring(i+1);
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 21278
diff changeset
   375
            i = lst.indexOf(',');
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   376
        }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   377
        if (!lst.isEmpty()) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   378
            try {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   379
                val = Integer.parseInt(lst);
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   380
                if (val == port) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   381
                    return true;
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   382
                }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   383
            } catch (NumberFormatException numberFormatException) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   384
            }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   385
        }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   386
        return false;
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   387
    }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   388
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * path-matches algorithm, as defined by RFC 2965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    private boolean pathMatches(String path, String pathToMatchWith) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (path == pathToMatchWith)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        if (path == null || pathToMatchWith == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (path.startsWith(pathToMatchWith))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    /*
51724
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   405
     * sort cookies with respect to their path and age: those with more longer Path attributes
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   406
     * precede those with shorter, as defined in RFC 6265. Cookies with the same length
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   407
     * path are distinguished by creation time (older first). Method made PP to enable testing.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     */
51724
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   409
    static List<String> sortByPathAndAge(List<HttpCookie> cookies) {
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   410
        Collections.sort(cookies, new CookieComparator());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 26720
diff changeset
   412
        List<String> cookieHeader = new java.util.ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        for (HttpCookie cookie : cookies) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            // Netscape cookie spec and RFC 2965 have different format of Cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            // header; RFC 2965 requires a leading $Version="1" string while Netscape
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            // does not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            // The workaround here is to add a $Version="1" string in advance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            if (cookies.indexOf(cookie) == 0 && cookie.getVersion() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                cookieHeader.add("$Version=\"1\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            cookieHeader.add(cookie.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return cookieHeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
51724
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   428
    // Comparator compares the length of the path. Longer paths should precede shorter ones.
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   429
    // As per rfc6265 cookies with equal path lengths sort on creation time.
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   430
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   431
    static class CookieComparator implements Comparator<HttpCookie> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        public int compare(HttpCookie c1, HttpCookie c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            if (c1 == c2) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            if (c1 == null) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            if (c2 == null) return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
51724
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   437
            String p1 = c1.getPath();
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   438
            String p2 = c2.getPath();
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   439
            p1 = (p1 == null) ? "" : p1;
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   440
            p2 = (p2 == null) ? "" : p2;
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   441
            int len1 = p1.length();
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   442
            int len2 = p2.length();
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   443
            if (len1 > len2)
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   444
                return -1;
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   445
            if (len2 > len1)
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   446
                return 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
51724
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   448
            // Check creation time. Sort older first
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   449
            long creation1 = c1.getCreationTime();
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   450
            long creation2 = c2.getCreationTime();
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   451
            if (creation1 < creation2) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                return -1;
51724
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   453
            }
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   454
            if (creation1 > creation2) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                return 1;
51724
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   456
            }
8abb0fa2c334 8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents: 47216
diff changeset
   457
            return 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
}