src/java.base/share/classes/java/net/URLStreamHandler.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58242 94bb65cb37d3
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
55500
80abfee2afaf 8226730: Missing `@` in code tags
aefimov
parents: 55126
diff changeset
     2
 * Copyright (c) 1995, 2019, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Hashtable;
57895
82a71d82e326 8226831: Use Objects.equals() when appropriate
igerasim
parents: 55711
diff changeset
    33
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.net.util.IPAddressUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.net.www.ParseUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
    38
 * The abstract class {@code URLStreamHandler} is the common
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * superclass for all stream protocol handlers. A stream protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * handler knows how to make a connection for a particular protocol
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
    41
 * type, such as {@code http} or {@code https}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
    43
 * In most cases, an instance of a {@code URLStreamHandler}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * subclass is not created directly by an application. Rather, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * first time a protocol name is encountered when constructing a
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
    46
 * {@code URL}, the appropriate stream protocol handler is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * automatically loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author  James Gosling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @see     java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 22064
diff changeset
    51
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public abstract class URLStreamHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Opens a connection to the object referenced by the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
    56
     * {@code URL} argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * This method should be overridden by a subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * <p>If for the handler's protocol (such as HTTP or JAR), there
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * exists a public, specialized URLConnection subclass belonging
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * to one of the following packages or one of their subpackages:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * java.lang, java.io, java.util, java.net, the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * returned will be of that subclass. For example, for HTTP an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * HttpURLConnection will be returned, and for JAR a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * JarURLConnection will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param      u   the URL that this connects to.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
    68
     * @return     a {@code URLConnection} object for the {@code URL}.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57895
diff changeset
    69
     * @throws     IOException  if an I/O error occurs while opening the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *               connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 26720
diff changeset
    72
    protected abstract URLConnection openConnection(URL u) throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Same as openConnection(URL), except that the connection will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * made through the specified proxy; Protocol handlers that do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * support proxying will ignore the proxy parameter and make a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * normal connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *
55126
d2bc284803bc 8224973: URLStreamHandler.openConnection(URL,Proxy) - spec and implementation mismatch
chegar
parents: 53018
diff changeset
    80
     * <p> Calling this method preempts the system's default
22064
e3c4398c54cd 7018010: References to ProxySelector is without link
rriggs
parents: 19069
diff changeset
    81
     * {@link java.net.ProxySelector ProxySelector} settings.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
55126
d2bc284803bc 8224973: URLStreamHandler.openConnection(URL,Proxy) - spec and implementation mismatch
chegar
parents: 53018
diff changeset
    83
     * @implSpec
d2bc284803bc 8224973: URLStreamHandler.openConnection(URL,Proxy) - spec and implementation mismatch
chegar
parents: 53018
diff changeset
    84
     * The default implementation of this method first checks that the given
55500
80abfee2afaf 8226730: Missing `@` in code tags
aefimov
parents: 55126
diff changeset
    85
     * {@code URL} and {@code Proxy} are not null, then throws {@code
55126
d2bc284803bc 8224973: URLStreamHandler.openConnection(URL,Proxy) - spec and implementation mismatch
chegar
parents: 53018
diff changeset
    86
     * UnsupportedOperationException}. Subclasses should override this method
d2bc284803bc 8224973: URLStreamHandler.openConnection(URL,Proxy) - spec and implementation mismatch
chegar
parents: 53018
diff changeset
    87
     * with an appropriate implementation.
d2bc284803bc 8224973: URLStreamHandler.openConnection(URL,Proxy) - spec and implementation mismatch
chegar
parents: 53018
diff changeset
    88
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param      u   the URL that this connects to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param      p   the proxy through which the connection will be made.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *                 If direct connection is desired, Proxy.NO_PROXY
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *                 should be specified.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
    93
     * @return     a {@code URLConnection} object for the {@code URL}.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57895
diff changeset
    94
     * @throws     IOException  if an I/O error occurs while opening the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *               connection.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57895
diff changeset
    96
     * @throws     IllegalArgumentException if either u or p is null,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *               or p has the wrong type.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57895
diff changeset
    98
     * @throws     UnsupportedOperationException if the subclass that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *               implements the protocol doesn't support this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @since      1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    protected URLConnection openConnection(URL u, Proxy p) throws IOException {
55126
d2bc284803bc 8224973: URLStreamHandler.openConnection(URL,Proxy) - spec and implementation mismatch
chegar
parents: 53018
diff changeset
   103
        if (u == null || p == null)
d2bc284803bc 8224973: URLStreamHandler.openConnection(URL,Proxy) - spec and implementation mismatch
chegar
parents: 53018
diff changeset
   104
            throw new IllegalArgumentException("null " + (u == null ? "url" : "proxy"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        throw new UnsupportedOperationException("Method not implemented.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   109
     * Parses the string representation of a {@code URL} into a
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   110
     * {@code URL} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * If there is any inherited context, then it has already been
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   113
     * copied into the {@code URL} argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   115
     * The {@code parseURL} method of {@code URLStreamHandler}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * parses the string representation as if it were an
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   117
     * {@code http} specification. Most URL protocol families have a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * similar parsing. A stream protocol handler for a protocol that has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * a different syntax must override this routine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   121
     * @param   u       the {@code URL} to receive the result of parsing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *                  the spec.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   123
     * @param   spec    the {@code String} representing the URL that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *                  must be parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param   start   the character index at which to begin parsing. This is
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   126
     *                  just past the '{@code :}' (if there is one) that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *                  specifies the determination of the protocol name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param   limit   the character position to stop parsing at. This is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *                  end of the string or the position of the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   130
     *                  "{@code #}" character, if present. All information
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *                  after the sharp sign indicates an anchor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    protected void parseURL(URL u, String spec, int start, int limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // These fields may receive context content if this was relative URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        String protocol = u.getProtocol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        String authority = u.getAuthority();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        String userInfo = u.getUserInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        String host = u.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        int port = u.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        String path = u.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        String query = u.getQuery();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // This field has already been parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        String ref = u.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        boolean isRelPath = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        boolean queryOnly = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
// FIX: should not assume query if opaque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // Strip off the query part
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (start < limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            int queryStart = spec.indexOf('?');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            queryOnly = queryStart == start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if ((queryStart != -1) && (queryStart < limit)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                query = spec.substring(queryStart+1, limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                if (limit > queryStart)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    limit = queryStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                spec = spec.substring(0, queryStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        // Parse the authority part if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        boolean isUNCName = (start <= limit - 4) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                        (spec.charAt(start) == '/') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        (spec.charAt(start + 1) == '/') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        (spec.charAt(start + 2) == '/') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        (spec.charAt(start + 3) == '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (!isUNCName && (start <= limit - 2) && (spec.charAt(start) == '/') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            (spec.charAt(start + 1) == '/')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            start += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            i = spec.indexOf('/', start);
43215
f3d46da34ae9 8167223: URL handling improvements
michaelm
parents: 32649
diff changeset
   173
            if (i < 0 || i > limit) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                i = spec.indexOf('?', start);
43215
f3d46da34ae9 8167223: URL handling improvements
michaelm
parents: 32649
diff changeset
   175
                if (i < 0 || i > limit)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    i = limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            host = authority = spec.substring(start, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            int ind = authority.indexOf('@');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if (ind != -1) {
43215
f3d46da34ae9 8167223: URL handling improvements
michaelm
parents: 32649
diff changeset
   183
                if (ind != authority.lastIndexOf('@')) {
f3d46da34ae9 8167223: URL handling improvements
michaelm
parents: 32649
diff changeset
   184
                    // more than one '@' in authority. This is not server based
f3d46da34ae9 8167223: URL handling improvements
michaelm
parents: 32649
diff changeset
   185
                    userInfo = null;
f3d46da34ae9 8167223: URL handling improvements
michaelm
parents: 32649
diff changeset
   186
                    host = null;
f3d46da34ae9 8167223: URL handling improvements
michaelm
parents: 32649
diff changeset
   187
                } else {
f3d46da34ae9 8167223: URL handling improvements
michaelm
parents: 32649
diff changeset
   188
                    userInfo = authority.substring(0, ind);
f3d46da34ae9 8167223: URL handling improvements
michaelm
parents: 32649
diff changeset
   189
                    host = authority.substring(ind+1);
f3d46da34ae9 8167223: URL handling improvements
michaelm
parents: 32649
diff changeset
   190
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                userInfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if (host != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                // If the host is surrounded by [ and ] then its an IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                // literal address as specified in RFC2732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                if (host.length()>0 && (host.charAt(0) == '[')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    if ((ind = host.indexOf(']')) > 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                        String nhost = host ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                        host = nhost.substring(0,ind+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                        if (!IPAddressUtil.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                            isIPv6LiteralAddress(host.substring(1, ind))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                "Invalid host: "+ host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        port = -1 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                        if (nhost.length() > ind+1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                            if (nhost.charAt(ind+1) == ':') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                ++ind ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                // port can be null according to RFC2396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                if (nhost.length() > (ind + 1)) {
26720
6b160d97c51d 8055032: Improve numerical parsing in java.net and sun.net
redestad
parents: 25859
diff changeset
   214
                                    port = Integer.parseInt(nhost, ind + 1,
6b160d97c51d 8055032: Improve numerical parsing in java.net and sun.net
redestad
parents: 25859
diff changeset
   215
                                        nhost.length(), 10);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                    "Invalid authority field: " + authority);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                        throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                            "Invalid authority field: " + authority);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    ind = host.indexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    port = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    if (ind >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                        // port can be null according to RFC2396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                        if (host.length() > (ind + 1)) {
26720
6b160d97c51d 8055032: Improve numerical parsing in java.net and sun.net
redestad
parents: 25859
diff changeset
   232
                            port = Integer.parseInt(host, ind + 1,
6b160d97c51d 8055032: Improve numerical parsing in java.net and sun.net
redestad
parents: 25859
diff changeset
   233
                                    host.length(), 10);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                        host = host.substring(0, ind);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                host = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            if (port < -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                throw new IllegalArgumentException("Invalid port number :" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                                   port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            start = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            // If the authority is defined then the path is defined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            // spec only; See RFC 2396 Section 5.2.4.
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52902
diff changeset
   247
            if (authority != null && !authority.isEmpty())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                path = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (host == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            host = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        // Parse the file path if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (start < limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if (spec.charAt(start) == '/') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                path = spec.substring(start, limit);
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52902
diff changeset
   259
            } else if (path != null && !path.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                isRelPath = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                int ind = path.lastIndexOf('/');
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 48260
diff changeset
   262
                String separator = "";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                if (ind == -1 && authority != null)
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 48260
diff changeset
   264
                    separator = "/";
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 48260
diff changeset
   265
                path = path.substring(0, ind + 1) + separator +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                         spec.substring(start, limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            } else {
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 48260
diff changeset
   269
                String separator = (authority != null) ? "/" : "";
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 48260
diff changeset
   270
                path = separator + spec.substring(start, limit);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        } else if (queryOnly && path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            int ind = path.lastIndexOf('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (ind < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                ind = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            path = path.substring(0, ind) + "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (path == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            path = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (isRelPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            // Remove embedded /./
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            while ((i = path.indexOf("/./")) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                path = path.substring(0, i) + path.substring(i + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            // Remove embedded /../ if possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            while ((i = path.indexOf("/../", i)) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                 * A "/../" will cancel the previous segment and itself,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                 * unless that segment is a "/../" itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                 * i.e. "/a/b/../c" becomes "/a/c"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                 * but "/../../a" should stay unchanged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                if (i > 0 && (limit = path.lastIndexOf('/', i - 1)) >= 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    (path.indexOf("/../", limit) != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    path = path.substring(0, limit) + path.substring(i + 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    i = i + 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            // Remove trailing .. if possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            while (path.endsWith("/..")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                i = path.indexOf("/..");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                if ((limit = path.lastIndexOf('/', i - 1)) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    path = path.substring(0, limit+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            // Remove starting .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            if (path.startsWith("./") && path.length() > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                path = path.substring(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            // Remove trailing .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            if (path.endsWith("/."))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                path = path.substring(0, path.length() -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        setURL(u, protocol, host, port, authority, userInfo, path, query, ref);
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
     * Returns the default port for a URL parsed by this handler. This method
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 48260
diff changeset
   326
     * is meant to be overridden by handlers with default port numbers.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   327
     * @return the default port for a {@code URL} parsed by this handler.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    protected int getDefaultPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 48260
diff changeset
   335
     * Provides the default equals calculation. May be overridden by handlers
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * for other protocols that have different requirements for equals().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * This method requires that none of its arguments is null. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * guaranteed by the fact that it is only called by java.net.URL class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @param u1 a URL object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @param u2 a URL object
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   341
     * @return {@code true} if the two urls are
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 48260
diff changeset
   342
     * considered equal, i.e. they refer to the same
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * fragment in the same file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    protected boolean equals(URL u1, URL u2) {
57895
82a71d82e326 8226831: Use Objects.equals() when appropriate
igerasim
parents: 55711
diff changeset
   347
        return Objects.equals(u1.getRef(), u2.getRef()) && sameFile(u1, u2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 48260
diff changeset
   351
     * Provides the default hash calculation. May be overridden by handlers for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * other protocols that have different requirements for hashCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * calculation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @param u a URL object
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   355
     * @return an {@code int} suitable for hash table indexing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    protected int hashCode(URL u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        int h = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        // Generate the protocol part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        String protocol = u.getProtocol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (protocol != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            h += protocol.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        // Generate the host part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        InetAddress addr = getHostAddress(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (addr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            h += addr.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            String host = u.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            if (host != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                h += host.toLowerCase().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        // Generate the file part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        String file = u.getFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (file != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            h += file.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        // Generate the port part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (u.getPort() == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            h += getDefaultPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            h += u.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        // Generate the ref part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        String ref = u.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        if (ref != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            h += ref.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Compare two urls to see whether they refer to the same file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * i.e., having the same protocol, host, port, and path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * This method requires that none of its arguments is null. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * guaranteed by the fact that it is only called indirectly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * by java.net.URL class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @param u1 a URL object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @param u2 a URL object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @return true if u1 and u2 refer to the same file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    protected boolean sameFile(URL u1, URL u2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        // Compare the protocols.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (!((u1.getProtocol() == u2.getProtocol()) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
              (u1.getProtocol() != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
               u1.getProtocol().equalsIgnoreCase(u2.getProtocol()))))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        // Compare the files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        if (!(u1.getFile() == u2.getFile() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
              (u1.getFile() != null && u1.getFile().equals(u2.getFile()))))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        // Compare the ports.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        int port1, port2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        port1 = (u1.getPort() != -1) ? u1.getPort() : u1.handler.getDefaultPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        port2 = (u2.getPort() != -1) ? u2.getPort() : u2.handler.getDefaultPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if (port1 != port2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        // Compare the hosts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        if (!hostsEqual(u1, u2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * Get the IP address of our host. An empty host field or a DNS failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * will result in a null return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @param u a URL object
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   437
     * @return an {@code InetAddress} representing the host
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    protected synchronized InetAddress getHostAddress(URL u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if (u.hostAddress != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            return u.hostAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        String host = u.getHost();
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 52499
diff changeset
   446
        if (host == null || host.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                u.hostAddress = InetAddress.getByName(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            } catch (UnknownHostException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        return u.hostAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * Compares the host components of two URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @param u1 the URL of the first host to compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @param u2 the URL of the second host to compare
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   464
     * @return  {@code true} if and only if they
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   465
     * are equal, {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    protected boolean hostsEqual(URL u1, URL u2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        InetAddress a1 = getHostAddress(u1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        InetAddress a2 = getHostAddress(u2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        // if we have internet address for both, compare them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        if (a1 != null && a2 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            return a1.equals(a2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        // else, if both have host names, compare them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        } else if (u1.getHost() != null && u2.getHost() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            return u1.getHost().equalsIgnoreCase(u2.getHost());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
         else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            return u1.getHost() == null && u2.getHost() == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   482
     * Converts a {@code URL} of a specific protocol to a
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   483
     * {@code String}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @param   u   the URL.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   486
     * @return  a string representation of the {@code URL} argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    protected String toExternalForm(URL u) {
48260
8ca86cfb126f 8193034: Optimize URL.toExternalForm
martin
parents: 47216
diff changeset
   489
        String s;
8ca86cfb126f 8193034: Optimize URL.toExternalForm
martin
parents: 47216
diff changeset
   490
        return u.getProtocol()
8ca86cfb126f 8193034: Optimize URL.toExternalForm
martin
parents: 47216
diff changeset
   491
            + ':'
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52902
diff changeset
   492
            + ((s = u.getAuthority()) != null && !s.isEmpty()
48260
8ca86cfb126f 8193034: Optimize URL.toExternalForm
martin
parents: 47216
diff changeset
   493
               ? "//" + s : "")
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52902
diff changeset
   494
            + ((s = u.getPath()) != null ? s : "")
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52902
diff changeset
   495
            + ((s = u.getQuery()) != null ? '?' + s : "")
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52902
diff changeset
   496
            + ((s = u.getRef()) != null ? '#' + s : "");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   500
     * Sets the fields of the {@code URL} argument to the indicated values.
16055
311a101eb72c 4880778: URL final class has protected methods
jzavgren
parents: 14342
diff changeset
   501
     * Only classes derived from URLStreamHandler are able
311a101eb72c 4880778: URL final class has protected methods
jzavgren
parents: 14342
diff changeset
   502
     * to use this method to set the values of the URL fields.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @param   u         the URL to modify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @param   protocol  the protocol name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @param   host      the remote host value for the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @param   port      the port on the remote machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @param   authority the authority part for the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @param   userInfo the userInfo part of the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @param   path      the path component of the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @param   query     the query part for the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @param   ref       the reference.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57895
diff changeset
   513
     * @throws          SecurityException       if the protocol handler of the URL is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *                                  different from this one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     */
55711
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 55500
diff changeset
   517
    protected void setURL(URL u, String protocol, String host, int port,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                             String authority, String userInfo, String path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                             String query, String ref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        if (this != u.handler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            throw new SecurityException("handler for url different from " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                                        "this handler");
55711
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 55500
diff changeset
   523
        } else if (host != null && u.isBuiltinStreamHandler(this)) {
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 55500
diff changeset
   524
            String s = IPAddressUtil.checkHostString(host);
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 55500
diff changeset
   525
            if (s != null) throw new IllegalArgumentException(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        // ensure that no one can reset the protocol on a given URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        u.set(u.getProtocol(), host, port, authority, userInfo, path, query, ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 16055
diff changeset
   532
     * Sets the fields of the {@code URL} argument to the indicated values.
16055
311a101eb72c 4880778: URL final class has protected methods
jzavgren
parents: 14342
diff changeset
   533
     * Only classes derived from URLStreamHandler are able
311a101eb72c 4880778: URL final class has protected methods
jzavgren
parents: 14342
diff changeset
   534
     * to use this method to set the values of the URL fields.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @param   u         the URL to modify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * @param   protocol  the protocol name. This value is ignored since 1.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * @param   host      the remote host value for the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @param   port      the port on the remote machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @param   file      the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @param   ref       the reference.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57895
diff changeset
   542
     * @throws          SecurityException       if the protocol handler of the URL is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *                                  different from this one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @deprecated Use setURL(URL, String, String, int, String, String, String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *             String);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    protected void setURL(URL u, String protocol, String host, int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                          String file, String ref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
         * Only old URL handlers call this, so assume that the host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
         * field might contain "user:passwd@host". Fix as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        String authority = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        String userInfo = null;
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52902
diff changeset
   556
        if (host != null && !host.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            authority = (port == -1) ? host : host + ":" + port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            int at = host.lastIndexOf('@');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            if (at != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                userInfo = host.substring(0, at);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                host = host.substring(at+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
         * Assume file might contain query part. Fix as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        String path = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        String query = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        if (file != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            int q = file.lastIndexOf('?');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            if (q != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                query = file.substring(q+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                path = file.substring(0, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                path = file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        setURL(u, protocol, host, port, authority, userInfo, path, query, ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
}