src/java.base/windows/classes/sun/net/www/protocol/file/Handler.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 55704 04cccf60fe44
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
55704
04cccf60fe44 8213431: Improve file protocol handling
prappo
parents: 53018
diff changeset
     2
 * Copyright (c) 1999, 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 sun.net.www.protocol.file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.URLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.URLStreamHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.net.www.ParseUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Open an file input stream given a URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author      James Gosling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class Handler extends URLStreamHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private String getHost(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        String host = url.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        if (host == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            host = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        return host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    protected void parseURL(URL u, String spec, int start, int limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
         * Ugly backwards compatibility. Flip any file separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
         * characters to be forward slashes. This is a nop on Unix
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
         * and "fixes" win32 file paths. According to RFC 2396,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
         * only forward slashes may be used to represent hierarchy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
         * separation in a URL but previous releases unfortunately
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
         * performed this "fixup" behavior in the file URL parsing code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
         * rather than forcing this to be fixed in the caller of the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
         * class where it belongs. Since backslash is an "unwise"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
         * character that would normally be encoded if literally intended
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
         * as a non-seperator character the damage of veering away from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
         * specification is presumably limited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        super.parseURL(u, spec.replace(File.separatorChar, '/'), start, limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public synchronized URLConnection openConnection(URL url)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return openConnection(url, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public synchronized URLConnection openConnection(URL url, Proxy p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
           throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        String path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        String file = url.getFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        String host = url.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        path = ParseUtil.decode(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        path = path.replace('/', '\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        path = path.replace('|', ':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 51759
diff changeset
    86
        if ((host == null) || host.isEmpty() ||
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                host.equalsIgnoreCase("localhost") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                host.equals("~")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
           return createFileURLConnection(url, new File(path));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
         * attempt to treat this as a UNC path. See 4180841
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        path = "\\\\" + host + path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        File f = new File(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (f.exists()) {
55704
04cccf60fe44 8213431: Improve file protocol handling
prappo
parents: 53018
diff changeset
    98
            return new UNCFileURLConnection(url, f, path);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
         * Now attempt an ftp connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        URLConnection uc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        URL newurl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            newurl = new URL("ftp", host, file +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                            (url.getRef() == null ? "":
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                            "#" + url.getRef()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                uc = newurl.openConnection(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                uc = newurl.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            uc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (uc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new IOException("Unable to connect to: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                        url.toExternalForm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return uc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
51759
ac6e9a2ebc04 8210786: Typo s/overriden/overridden/ in several places
igerasim
parents: 47216
diff changeset
   127
     * Template method to be overridden by Java Plug-in. [stanleyh]
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    protected URLConnection createFileURLConnection(URL url, File file) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return new FileURLConnection(url, file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Compares the host components of two URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param u1 the URL of the first host to compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param u2 the URL of the second host to compare
32210
958d823579c3 8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents: 25859
diff changeset
   137
     * @return  {@code true} if and only if they
958d823579c3 8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents: 25859
diff changeset
   138
     * are equal, {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    protected boolean hostsEqual(URL u1, URL u2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
         * Special case for file: URLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         * per RFC 1738 no hostname is equivalent to 'localhost'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         * i.e. file:///path is equal to file://localhost/path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        String s1 = u1.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        String s2 = u2.getHost();
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52902
diff changeset
   148
        if ("localhost".equalsIgnoreCase(s1) && (s2 == null || s2.isEmpty()))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return true;
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52902
diff changeset
   150
        if ("localhost".equalsIgnoreCase(s2) && (s1 == null || s1.isEmpty()))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return super.hostsEqual(u1, u2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
}