jdk/src/share/classes/java/net/CookieManager.java
author xdono
Thu, 02 Oct 2008 19:58:32 -0700
changeset 1247 b4c26443dee5
parent 715 f16baef3a20e
child 1950 dd893f20667b
permissions -rw-r--r--
6754988: Update copyright year Summary: Update for files that have been modified starting July 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 480
diff changeset
     2
 * Copyright 2005-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * CookieManager provides a concrete implementation of {@link CookieHandler},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * which separates the storage of cookies from the policy surrounding accepting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * and rejecting cookies. A CookieManager is initialized with a {@link CookieStore}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * which manages storage, and a {@link CookiePolicy} object, which makes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * policy decisions on cookie acceptance/rejection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p> The HTTP cookie management in java.net package looks like:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *                  use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * CookieHandler <------- HttpURLConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *       ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *       | impl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *       |         use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * CookieManager -------> CookiePolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *             |   use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *             |--------> HttpCookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *             |              ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *             |              | use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *             |   use        |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *             |--------> CookieStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *                            ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *                            | impl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *                            |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *                  Internal in-memory implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *   <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *     CookieHandler is at the core of cookie management. User can call
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *     CookieHandler.setDefault to set a concrete CookieHanlder implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *     to be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *   </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *   <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *     CookiePolicy.shouldAccept will be called by CookieManager.put to see whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *     or not one cookie should be accepted and put into cookie store. User can use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *     any of three pre-defined CookiePolicy, namely ACCEPT_ALL, ACCEPT_NONE and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *     ACCEPT_ORIGINAL_SERVER, or user can define his own CookiePolicy implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *     and tell CookieManager to use it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *   </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *   <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *     CookieStore is the place where any accepted HTTP cookie is stored in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *     If not specified when created, a CookieManager instance will use an internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *     in-memory implementation. Or user can implements one and tell CookieManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *     to use it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *   </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *   <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *     Currently, only CookieStore.add(URI, HttpCookie) and CookieStore.get(URI)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *     are used by CookieManager. Others are for completeness and might be needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *     by a more sophisticated CookieStore implementation, e.g. a NetscapeCookieSotre.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *   </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <p>There're various ways user can hook up his own HTTP cookie management behavior, e.g.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *   <li>Use CookieHandler.setDefault to set a brand new {@link CookieHandler} implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *   <li>Let CookieManager be the default {@link CookieHandler} implementation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *       but implement user's own {@link CookieStore} and {@link CookiePolicy}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *       and tell default CookieManager to use them:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *     <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *       // this should be done at the beginning of an HTTP session
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *       CookieHandler.setDefault(new CookieManager(new MyCookieStore(), new MyCookiePolicy()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *     </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *   <li>Let CookieManager be the default {@link CookieHandler} implementation, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *       use customized {@link CookiePolicy}:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *     <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *       // this should be done at the beginning of an HTTP session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *       CookieHandler.setDefault(new CookieManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *       // this can be done at any point of an HTTP session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *       ((CookieManager)CookieHandler.getDefault()).setCookiePolicy(new MyCookiePolicy());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *     </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * <p>The implementation conforms to RFC 2965, section 3.3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * @author Edward Wang
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
public class CookieManager extends CookieHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /* ---------------- Fields -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private CookiePolicy policyCallback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private CookieStore cookieJar = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /* ---------------- Ctors -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Create a new cookie manager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <p>This constructor will create new cookie manager with default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * cookie store and accept policy. The effect is same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * <tt>CookieManager(null, null)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public CookieManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        this(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Create a new cookie manager with specified cookie store and cookie policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param store     a <tt>CookieStore</tt> to be used by cookie manager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *                  if <tt>null</tt>, cookie manager will use a default one,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *                  which is an in-memory CookieStore implmentation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param cookiePolicy      a <tt>CookiePolicy</tt> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *                          to be used by cookie manager as policy callback.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *                          if <tt>null</tt>, ACCEPT_ORIGINAL_SERVER will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *                          be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public CookieManager(CookieStore store,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                         CookiePolicy cookiePolicy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // use default cookie policy if not specify one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        policyCallback = (cookiePolicy == null) ? CookiePolicy.ACCEPT_ORIGINAL_SERVER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                                                : cookiePolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // if not specify CookieStore to use, use default one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (store == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            cookieJar = new sun.net.www.protocol.http.InMemoryCookieStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            cookieJar = store;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /* ---------------- Public operations -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * To set the cookie policy of this cookie manager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <p> A instance of <tt>CookieManager</tt> will have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * cookie policy ACCEPT_ORIGINAL_SERVER by default. Users always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * can call this method to set another cookie policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @param cookiePolicy      the cookie policy. Can be <tt>null</tt>, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *                          has no effects on current cookie policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public void setCookiePolicy(CookiePolicy cookiePolicy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (cookiePolicy != null) policyCallback = cookiePolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * To retrieve current cookie store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @return  the cookie store currently used by cookie manager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public CookieStore getCookieStore() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return cookieJar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public Map<String, List<String>>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        get(URI uri, Map<String, List<String>> requestHeaders)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        // pre-condition check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (uri == null || requestHeaders == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            throw new IllegalArgumentException("Argument is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        Map<String, List<String>> cookieMap =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                        new java.util.HashMap<String, List<String>>();
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)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            return Collections.unmodifiableMap(cookieMap);
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());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        List<HttpCookie> cookies = new java.util.ArrayList<HttpCookie>();
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())) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   220
                // Let's check the authorize port list if it exists
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   221
                String ports = cookie.getPortlist();
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   222
                if (ports != null && !ports.isEmpty()) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   223
                    int port = uri.getPort();
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   224
                    if (port == -1) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   225
                        port = "https".equals(uri.getScheme()) ? 443 : 80;
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   226
                    }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   227
                    if (isInPortList(ports, port)) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   228
                        cookies.add(cookie);
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   229
                    }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   230
                } else {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   231
                    cookies.add(cookie);
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   232
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        // apply sort rule (RFC 2965 sec. 3.3.4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        List<String> cookieHeader = sortByPath(cookies);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        cookieMap.put("Cookie", cookieHeader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        return Collections.unmodifiableMap(cookieMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        put(URI uri, Map<String, List<String>> responseHeaders)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        // pre-condition check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (uri == null || responseHeaders == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            throw new IllegalArgumentException("Argument is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        // if there's no default CookieStore, no need to remember any cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (cookieJar == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        for (String headerKey : responseHeaders.keySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            // RFC 2965 3.2.2, key must be 'Set-Cookie2'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            // we also accept 'Set-Cookie' here for backward compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            if (headerKey == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                || !(headerKey.equalsIgnoreCase("Set-Cookie2")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                     || headerKey.equalsIgnoreCase("Set-Cookie")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            for (String headerValue : responseHeaders.get(headerKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    List<HttpCookie> cookies = HttpCookie.parse(headerValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    for (HttpCookie cookie : cookies) {
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   274
                        if (cookie.getPath() == null) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   275
                            // If no path is specified, then by default
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   276
                            // the path is the directory of the page/doc
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   277
                            String path = uri.getPath();
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   278
                            if (!path.endsWith("/")) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   279
                                int i = path.lastIndexOf("/");
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   280
                                if (i > 0) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   281
                                    path = path.substring(0, i + 1);
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   282
                                } else {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   283
                                    path = "/";
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   284
                                }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   285
                            }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   286
                            cookie.setPath(path);
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   287
                        }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   288
                        String ports = cookie.getPortlist();
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   289
                        if (ports != null) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   290
                            int port = uri.getPort();
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   291
                            if (port == -1) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   292
                                port = "https".equals(uri.getScheme()) ? 443 : 80;
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   293
                            }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   294
                            if (ports.isEmpty()) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   295
                                // Empty port list means this should be restricted
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   296
                                // to the incoming URI port
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   297
                                cookie.setPortlist("" + port );
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   298
                                if (shouldAcceptInternal(uri, cookie)) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   299
                                    cookieJar.add(uri, cookie);
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   300
                                }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   301
                            } else {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   302
                                // Only store cookies with a port list
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   303
                                // IF the URI port is in that list, as per
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   304
                                // RFC 2965 section 3.3.2
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   305
                                if (isInPortList(ports, port) &&
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   306
                                        shouldAcceptInternal(uri, cookie)) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   307
                                    cookieJar.add(uri, cookie);
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   308
                                }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   309
                            }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   310
                        } else {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   311
                            if (shouldAcceptInternal(uri, cookie)) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   312
                                cookieJar.add(uri, cookie);
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   313
                            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    // invalid set-cookie header string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    // no-op
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                }
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /* ---------------- Private operations -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    // to determine whether or not accept this cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    private boolean shouldAcceptInternal(URI uri, HttpCookie cookie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            return policyCallback.shouldAccept(uri, cookie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        } catch (Exception ignored) { // pretect against malicious callback
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
480
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   337
    static private boolean isInPortList(String lst, int port) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   338
        int i = lst.indexOf(",");
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   339
        int val = -1;
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   340
        while (i > 0) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   341
            try {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   342
                val = Integer.parseInt(lst.substring(0, i));
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   343
                if (val == port) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   344
                    return true;
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   345
                }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   346
            } catch (NumberFormatException numberFormatException) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   347
            }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   348
            lst = lst.substring(i+1);
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   349
            i = lst.indexOf(",");
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   350
        }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   351
        if (!lst.isEmpty()) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   352
            try {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   353
                val = Integer.parseInt(lst);
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   354
                if (val == port) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   355
                    return true;
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   356
                }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   357
            } catch (NumberFormatException numberFormatException) {
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   358
            }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   359
        }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   360
        return false;
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   361
    }
c309ca1d3a86 6644726: Cookie management issues
jccollet
parents: 2
diff changeset
   362
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * path-matches algorithm, as defined by RFC 2965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    private boolean pathMatches(String path, String pathToMatchWith) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if (path == pathToMatchWith)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (path == null || pathToMatchWith == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if (path.startsWith(pathToMatchWith))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * sort cookies with respect to their path: those with more specific Path attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * precede those with less specific, as defined in RFC 2965 sec. 3.3.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    private List<String> sortByPath(List<HttpCookie> cookies) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        Collections.sort(cookies, new CookiePathComparator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        List<String> cookieHeader = new java.util.ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        for (HttpCookie cookie : cookies) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            // Netscape cookie spec and RFC 2965 have different format of Cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            // header; RFC 2965 requires a leading $Version="1" string while Netscape
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            // does not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            // The workaround here is to add a $Version="1" string in advance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            if (cookies.indexOf(cookie) == 0 && cookie.getVersion() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                cookieHeader.add("$Version=\"1\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            cookieHeader.add(cookie.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        return cookieHeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    static class CookiePathComparator implements Comparator<HttpCookie> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        public int compare(HttpCookie c1, HttpCookie c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            if (c1 == c2) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            if (c1 == null) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            if (c2 == null) return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            // path rule only applies to the cookies with same name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            if (!c1.getName().equals(c2.getName())) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            // those with more specific Path attributes precede those with less specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            if (c1.getPath().startsWith(c2.getPath()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            else if (c2.getPath().startsWith(c1.getPath()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
}