src/java.base/unix/classes/sun/net/www/protocol/file/Handler.java
author redestad
Thu, 13 Dec 2018 15:31:05 +0100
changeset 53018 8bf9268df0e2
parent 52902 e3398b2e1ab0
permissions -rw-r--r--
8215281: Use String.isEmpty() when applicable in java.base Reviewed-by: dfuchs, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51759
ac6e9a2ebc04 8210786: Typo s/overriden/overridden/ in several places
igerasim
parents: 47216
diff changeset
     2
 * Copyright (c) 1994, 2018, 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 u)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return openConnection(u, 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 u, Proxy p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
           throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        String host = u.getHost();
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 51759
diff changeset
    78
        if (host == null || host.isEmpty() || host.equals("~") ||
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            host.equalsIgnoreCase("localhost")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            File file = new File(ParseUtil.decode(u.getPath()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            return createFileURLConnection(u, file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        /* If you reach here, it implies that you have a hostname
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
           so attempt an ftp connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        URLConnection uc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        URL ru;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            ru = new URL("ftp", host, u.getFile() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                             (u.getRef() == null ? "": "#" + u.getRef()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                uc = ru.openConnection(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                uc = ru.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            uc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (uc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new IOException("Unable to connect to: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                                       u.toExternalForm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return uc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
51759
ac6e9a2ebc04 8210786: Typo s/overriden/overridden/ in several places
igerasim
parents: 47216
diff changeset
   108
    // Template method to be overridden by Java Plug-in. [stanleyh]
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    protected URLConnection createFileURLConnection(URL u, File file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return new FileURLConnection(u, file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Compares the host components of two URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param u1 the URL of the first host to compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @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
   119
     * @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
   120
     * are equal, {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    protected boolean hostsEqual(URL u1, URL u2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
         * Special case for file: URLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
         * per RFC 1738 no hostname is equivalent to 'localhost'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
         * i.e. file:///path is equal to file://localhost/path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        String s1 = u1.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        String s2 = u2.getHost();
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52902
diff changeset
   130
        if ("localhost".equalsIgnoreCase(s1) && (s2 == null || s2.isEmpty()))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            return true;
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52902
diff changeset
   132
        if ("localhost".equalsIgnoreCase(s2) && (s1 == null || s1.isEmpty()))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return super.hostsEqual(u1, u2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
}