1 /*
2  * Copyright (c) 1994, 2003, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package sun.net.www.protocol.file;
27 
28 import java.net.InetAddress;
29 import java.net.URLConnection;
30 import java.net.URL;
31 import java.net.Proxy;
32 import java.net.MalformedURLException;
33 import java.net.URLStreamHandler;
34 import java.io.InputStream;
35 import java.io.IOException;
36 import sun.net.www.ParseUtil;
37 import java.io.File;
38 
39 /**
40  * Open an file input stream given a URL.
41  * @author      James Gosling
42  */
43 public class Handler extends URLStreamHandler {
44 
getHost(URL url)45     private String getHost(URL url) {
46         String host = url.getHost();
47         if (host == null)
48             host = "";
49         return host;
50     }
51 
52 
parseURL(URL u, String spec, int start, int limit)53     protected void parseURL(URL u, String spec, int start, int limit) {
54         /*
55          * Ugly backwards compatibility. Flip any file separator
56          * characters to be forward slashes. This is a nop on Unix
57          * and "fixes" win32 file paths. According to RFC 2396,
58          * only forward slashes may be used to represent hierarchy
59          * separation in a URL but previous releases unfortunately
60          * performed this "fixup" behavior in the file URL parsing code
61          * rather than forcing this to be fixed in the caller of the URL
62          * class where it belongs. Since backslash is an "unwise"
63          * character that would normally be encoded if literally intended
64          * as a non-seperator character the damage of veering away from the
65          * specification is presumably limited.
66          */
67         super.parseURL(u, spec.replace(File.separatorChar, '/'), start, limit);
68     }
69 
openConnection(URL u)70     public synchronized URLConnection openConnection(URL u)
71         throws IOException {
72         return openConnection(u, null);
73     }
74 
openConnection(URL u, Proxy p)75     public synchronized URLConnection openConnection(URL u, Proxy p)
76            throws IOException {
77         String host = u.getHost();
78         if (host == null || host.equals("") || host.equals("~") ||
79             host.equalsIgnoreCase("localhost")) {
80             File file = new File(ParseUtil.decode(u.getPath()));
81             return createFileURLConnection(u, file);
82         }
83 
84         /* If you reach here, it implies that you have a hostname
85            so attempt an ftp connection.
86          */
87         URLConnection uc;
88         URL ru;
89 
90         try {
91             ru = new URL("ftp", host, u.getFile() +
92                              (u.getRef() == null ? "": "#" + u.getRef()));
93             if (p != null) {
94                 uc = ru.openConnection(p);
95             } else {
96                 uc = ru.openConnection();
97             }
98         } catch (IOException e) {
99             uc = null;
100         }
101         if (uc == null) {
102             throw new IOException("Unable to connect to: " +
103                                                        u.toExternalForm());
104         }
105         return uc;
106     }
107 
108     // Template method to be overriden by Java Plug-in. [stanleyh]
109     //
createFileURLConnection(URL u, File file)110     protected URLConnection createFileURLConnection(URL u, File file)
111     {
112         return new FileURLConnection(u, file);
113     }
114 
115     /**
116      * Compares the host components of two URLs.
117      * @param u1 the URL of the first host to compare
118      * @param u2 the URL of the second host to compare
119      * @return  <tt>true</tt> if and only if they
120      * are equal, <tt>false</tt> otherwise.
121      */
hostsEqual(URL u1, URL u2)122     protected boolean hostsEqual(URL u1, URL u2) {
123         /*
124          * Special case for file: URLs
125          * per RFC 1738 no hostname is equivalent to 'localhost'
126          * i.e. file:///path is equal to file://localhost/path
127          */
128         String s1 = u1.getHost();
129         String s2 = u2.getHost();
130         if ("localhost".equalsIgnoreCase(s1) && ( s2 == null || "".equals(s2)))
131             return true;
132         if ("localhost".equalsIgnoreCase(s2) && ( s1 == null || "".equals(s1)))
133             return true;
134         return super.hostsEqual(u1, u2);
135     }
136 }
137