0N/A/*
2362N/A * Copyright (c) 1995, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.net;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.io.InputStream;
0N/Aimport java.io.File;
0N/Aimport java.io.OutputStream;
0N/Aimport java.util.Hashtable;
0N/Aimport sun.net.util.IPAddressUtil;
0N/Aimport sun.net.www.ParseUtil;
0N/A
0N/A/**
0N/A * The abstract class <code>URLStreamHandler</code> is the common
0N/A * superclass for all stream protocol handlers. A stream protocol
0N/A * handler knows how to make a connection for a particular protocol
0N/A * type, such as <code>http</code>, <code>ftp</code>, or
0N/A * <code>gopher</code>.
0N/A * <p>
0N/A * In most cases, an instance of a <code>URLStreamHandler</code>
0N/A * subclass is not created directly by an application. Rather, the
0N/A * first time a protocol name is encountered when constructing a
0N/A * <code>URL</code>, the appropriate stream protocol handler is
0N/A * automatically loaded.
0N/A *
0N/A * @author James Gosling
0N/A * @see java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
0N/A * @since JDK1.0
0N/A */
0N/Apublic abstract class URLStreamHandler {
0N/A /**
0N/A * Opens a connection to the object referenced by the
0N/A * <code>URL</code> argument.
0N/A * This method should be overridden by a subclass.
0N/A *
0N/A * <p>If for the handler's protocol (such as HTTP or JAR), there
0N/A * exists a public, specialized URLConnection subclass belonging
0N/A * to one of the following packages or one of their subpackages:
0N/A * java.lang, java.io, java.util, java.net, the connection
0N/A * returned will be of that subclass. For example, for HTTP an
0N/A * HttpURLConnection will be returned, and for JAR a
0N/A * JarURLConnection will be returned.
0N/A *
0N/A * @param u the URL that this connects to.
0N/A * @return a <code>URLConnection</code> object for the <code>URL</code>.
0N/A * @exception IOException if an I/O error occurs while opening the
0N/A * connection.
0N/A */
0N/A abstract protected URLConnection openConnection(URL u) throws IOException;
0N/A
0N/A /**
0N/A * Same as openConnection(URL), except that the connection will be
0N/A * made through the specified proxy; Protocol handlers that do not
0N/A * support proxying will ignore the proxy parameter and make a
0N/A * normal connection.
0N/A *
0N/A * Calling this method preempts the system's default ProxySelector
0N/A * settings.
0N/A *
0N/A * @param u the URL that this connects to.
0N/A * @param p the proxy through which the connection will be made.
0N/A * If direct connection is desired, Proxy.NO_PROXY
0N/A * should be specified.
0N/A * @return a <code>URLConnection</code> object for the <code>URL</code>.
0N/A * @exception IOException if an I/O error occurs while opening the
0N/A * connection.
0N/A * @exception IllegalArgumentException if either u or p is null,
0N/A * or p has the wrong type.
0N/A * @exception UnsupportedOperationException if the subclass that
0N/A * implements the protocol doesn't support this method.
0N/A * @since 1.5
0N/A */
0N/A protected URLConnection openConnection(URL u, Proxy p) throws IOException {
0N/A throw new UnsupportedOperationException("Method not implemented.");
0N/A }
0N/A
0N/A /**
0N/A * Parses the string representation of a <code>URL</code> into a
0N/A * <code>URL</code> object.
0N/A * <p>
0N/A * If there is any inherited context, then it has already been
0N/A * copied into the <code>URL</code> argument.
0N/A * <p>
0N/A * The <code>parseURL</code> method of <code>URLStreamHandler</code>
0N/A * parses the string representation as if it were an
0N/A * <code>http</code> specification. Most URL protocol families have a
0N/A * similar parsing. A stream protocol handler for a protocol that has
0N/A * a different syntax must override this routine.
0N/A *
0N/A * @param u the <code>URL</code> to receive the result of parsing
0N/A * the spec.
0N/A * @param spec the <code>String</code> representing the URL that
0N/A * must be parsed.
0N/A * @param start the character index at which to begin parsing. This is
0N/A * just past the '<code>:</code>' (if there is one) that
0N/A * specifies the determination of the protocol name.
0N/A * @param limit the character position to stop parsing at. This is the
0N/A * end of the string or the position of the
0N/A * "<code>#</code>" character, if present. All information
0N/A * after the sharp sign indicates an anchor.
0N/A */
0N/A protected void parseURL(URL u, String spec, int start, int limit) {
0N/A // These fields may receive context content if this was relative URL
0N/A String protocol = u.getProtocol();
0N/A String authority = u.getAuthority();
0N/A String userInfo = u.getUserInfo();
0N/A String host = u.getHost();
0N/A int port = u.getPort();
0N/A String path = u.getPath();
0N/A String query = u.getQuery();
0N/A
0N/A // This field has already been parsed
0N/A String ref = u.getRef();
0N/A
0N/A boolean isRelPath = false;
0N/A boolean queryOnly = false;
0N/A
0N/A// FIX: should not assume query if opaque
0N/A // Strip off the query part
0N/A if (start < limit) {
0N/A int queryStart = spec.indexOf('?');
0N/A queryOnly = queryStart == start;
0N/A if ((queryStart != -1) && (queryStart < limit)) {
0N/A query = spec.substring(queryStart+1, limit);
0N/A if (limit > queryStart)
0N/A limit = queryStart;
0N/A spec = spec.substring(0, queryStart);
0N/A }
0N/A }
0N/A
0N/A int i = 0;
0N/A // Parse the authority part if any
0N/A boolean isUNCName = (start <= limit - 4) &&
0N/A (spec.charAt(start) == '/') &&
0N/A (spec.charAt(start + 1) == '/') &&
0N/A (spec.charAt(start + 2) == '/') &&
0N/A (spec.charAt(start + 3) == '/');
0N/A if (!isUNCName && (start <= limit - 2) && (spec.charAt(start) == '/') &&
0N/A (spec.charAt(start + 1) == '/')) {
0N/A start += 2;
0N/A i = spec.indexOf('/', start);
0N/A if (i < 0) {
0N/A i = spec.indexOf('?', start);
0N/A if (i < 0)
0N/A i = limit;
0N/A }
0N/A
0N/A host = authority = spec.substring(start, i);
0N/A
0N/A int ind = authority.indexOf('@');
0N/A if (ind != -1) {
0N/A userInfo = authority.substring(0, ind);
0N/A host = authority.substring(ind+1);
0N/A } else {
0N/A userInfo = null;
0N/A }
0N/A if (host != null) {
0N/A // If the host is surrounded by [ and ] then its an IPv6
0N/A // literal address as specified in RFC2732
0N/A if (host.length()>0 && (host.charAt(0) == '[')) {
0N/A if ((ind = host.indexOf(']')) > 2) {
0N/A
0N/A String nhost = host ;
0N/A host = nhost.substring(0,ind+1);
0N/A if (!IPAddressUtil.
0N/A isIPv6LiteralAddress(host.substring(1, ind))) {
0N/A throw new IllegalArgumentException(
0N/A "Invalid host: "+ host);
0N/A }
0N/A
0N/A port = -1 ;
0N/A if (nhost.length() > ind+1) {
0N/A if (nhost.charAt(ind+1) == ':') {
0N/A ++ind ;
0N/A // port can be null according to RFC2396
0N/A if (nhost.length() > (ind + 1)) {
0N/A port = Integer.parseInt(nhost.substring(ind+1));
0N/A }
0N/A } else {
0N/A throw new IllegalArgumentException(
0N/A "Invalid authority field: " + authority);
0N/A }
0N/A }
0N/A } else {
0N/A throw new IllegalArgumentException(
0N/A "Invalid authority field: " + authority);
0N/A }
0N/A } else {
0N/A ind = host.indexOf(':');
0N/A port = -1;
0N/A if (ind >= 0) {
0N/A // port can be null according to RFC2396
0N/A if (host.length() > (ind + 1)) {
0N/A port = Integer.parseInt(host.substring(ind + 1));
0N/A }
0N/A host = host.substring(0, ind);
0N/A }
0N/A }
0N/A } else {
0N/A host = "";
0N/A }
0N/A if (port < -1)
0N/A throw new IllegalArgumentException("Invalid port number :" +
0N/A port);
0N/A start = i;
0N/A // If the authority is defined then the path is defined by the
0N/A // spec only; See RFC 2396 Section 5.2.4.
0N/A if (authority != null && authority.length() > 0)
0N/A path = "";
0N/A }
0N/A
0N/A if (host == null) {
0N/A host = "";
0N/A }
0N/A
0N/A // Parse the file path if any
0N/A if (start < limit) {
0N/A if (spec.charAt(start) == '/') {
0N/A path = spec.substring(start, limit);
0N/A } else if (path != null && path.length() > 0) {
0N/A isRelPath = true;
0N/A int ind = path.lastIndexOf('/');
0N/A String seperator = "";
0N/A if (ind == -1 && authority != null)
0N/A seperator = "/";
0N/A path = path.substring(0, ind + 1) + seperator +
0N/A spec.substring(start, limit);
0N/A
0N/A } else {
0N/A String seperator = (authority != null) ? "/" : "";
0N/A path = seperator + spec.substring(start, limit);
0N/A }
0N/A } else if (queryOnly && path != null) {
0N/A int ind = path.lastIndexOf('/');
0N/A if (ind < 0)
0N/A ind = 0;
0N/A path = path.substring(0, ind) + "/";
0N/A }
0N/A if (path == null)
0N/A path = "";
0N/A
0N/A if (isRelPath) {
0N/A // Remove embedded /./
0N/A while ((i = path.indexOf("/./")) >= 0) {
0N/A path = path.substring(0, i) + path.substring(i + 2);
0N/A }
0N/A // Remove embedded /../ if possible
0N/A i = 0;
0N/A while ((i = path.indexOf("/../", i)) >= 0) {
0N/A /*
0N/A * A "/../" will cancel the previous segment and itself,
0N/A * unless that segment is a "/../" itself
0N/A * i.e. "/a/b/../c" becomes "/a/c"
0N/A * but "/../../a" should stay unchanged
0N/A */
0N/A if (i > 0 && (limit = path.lastIndexOf('/', i - 1)) >= 0 &&
0N/A (path.indexOf("/../", limit) != 0)) {
0N/A path = path.substring(0, limit) + path.substring(i + 3);
0N/A i = 0;
0N/A } else {
0N/A i = i + 3;
0N/A }
0N/A }
0N/A // Remove trailing .. if possible
0N/A while (path.endsWith("/..")) {
0N/A i = path.indexOf("/..");
0N/A if ((limit = path.lastIndexOf('/', i - 1)) >= 0) {
0N/A path = path.substring(0, limit+1);
0N/A } else {
0N/A break;
0N/A }
0N/A }
0N/A // Remove starting .
0N/A if (path.startsWith("./") && path.length() > 2)
0N/A path = path.substring(2);
0N/A
0N/A // Remove trailing .
0N/A if (path.endsWith("/."))
0N/A path = path.substring(0, path.length() -1);
0N/A }
0N/A
0N/A setURL(u, protocol, host, port, authority, userInfo, path, query, ref);
0N/A }
0N/A
0N/A /**
0N/A * Returns the default port for a URL parsed by this handler. This method
0N/A * is meant to be overidden by handlers with default port numbers.
0N/A * @return the default port for a <code>URL</code> parsed by this handler.
0N/A * @since 1.3
0N/A */
0N/A protected int getDefaultPort() {
0N/A return -1;
0N/A }
0N/A
0N/A /**
0N/A * Provides the default equals calculation. May be overidden by handlers
0N/A * for other protocols that have different requirements for equals().
0N/A * This method requires that none of its arguments is null. This is
0N/A * guaranteed by the fact that it is only called by java.net.URL class.
0N/A * @param u1 a URL object
0N/A * @param u2 a URL object
0N/A * @return <tt>true</tt> if the two urls are
0N/A * considered equal, ie. they refer to the same
0N/A * fragment in the same file.
0N/A * @since 1.3
0N/A */
0N/A protected boolean equals(URL u1, URL u2) {
0N/A String ref1 = u1.getRef();
0N/A String ref2 = u2.getRef();
0N/A return (ref1 == ref2 || (ref1 != null && ref1.equals(ref2))) &&
0N/A sameFile(u1, u2);
0N/A }
0N/A
0N/A /**
0N/A * Provides the default hash calculation. May be overidden by handlers for
0N/A * other protocols that have different requirements for hashCode
0N/A * calculation.
0N/A * @param u a URL object
0N/A * @return an <tt>int</tt> suitable for hash table indexing
0N/A * @since 1.3
0N/A */
0N/A protected int hashCode(URL u) {
0N/A int h = 0;
0N/A
0N/A // Generate the protocol part.
0N/A String protocol = u.getProtocol();
0N/A if (protocol != null)
0N/A h += protocol.hashCode();
0N/A
0N/A // Generate the host part.
0N/A InetAddress addr = getHostAddress(u);
0N/A if (addr != null) {
0N/A h += addr.hashCode();
0N/A } else {
0N/A String host = u.getHost();
0N/A if (host != null)
0N/A h += host.toLowerCase().hashCode();
0N/A }
0N/A
0N/A // Generate the file part.
0N/A String file = u.getFile();
0N/A if (file != null)
0N/A h += file.hashCode();
0N/A
0N/A // Generate the port part.
0N/A if (u.getPort() == -1)
0N/A h += getDefaultPort();
0N/A else
0N/A h += u.getPort();
0N/A
0N/A // Generate the ref part.
0N/A String ref = u.getRef();
0N/A if (ref != null)
0N/A h += ref.hashCode();
0N/A
0N/A return h;
0N/A }
0N/A
0N/A /**
0N/A * Compare two urls to see whether they refer to the same file,
0N/A * i.e., having the same protocol, host, port, and path.
0N/A * This method requires that none of its arguments is null. This is
0N/A * guaranteed by the fact that it is only called indirectly
0N/A * by java.net.URL class.
0N/A * @param u1 a URL object
0N/A * @param u2 a URL object
0N/A * @return true if u1 and u2 refer to the same file
0N/A * @since 1.3
0N/A */
0N/A protected boolean sameFile(URL u1, URL u2) {
0N/A // Compare the protocols.
0N/A if (!((u1.getProtocol() == u2.getProtocol()) ||
0N/A (u1.getProtocol() != null &&
0N/A u1.getProtocol().equalsIgnoreCase(u2.getProtocol()))))
0N/A return false;
0N/A
0N/A // Compare the files.
0N/A if (!(u1.getFile() == u2.getFile() ||
0N/A (u1.getFile() != null && u1.getFile().equals(u2.getFile()))))
0N/A return false;
0N/A
0N/A // Compare the ports.
0N/A int port1, port2;
0N/A port1 = (u1.getPort() != -1) ? u1.getPort() : u1.handler.getDefaultPort();
0N/A port2 = (u2.getPort() != -1) ? u2.getPort() : u2.handler.getDefaultPort();
0N/A if (port1 != port2)
0N/A return false;
0N/A
0N/A // Compare the hosts.
0N/A if (!hostsEqual(u1, u2))
0N/A return false;
0N/A
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Get the IP address of our host. An empty host field or a DNS failure
0N/A * will result in a null return.
0N/A *
0N/A * @param u a URL object
0N/A * @return an <code>InetAddress</code> representing the host
0N/A * IP address.
0N/A * @since 1.3
0N/A */
0N/A protected synchronized InetAddress getHostAddress(URL u) {
0N/A if (u.hostAddress != null)
0N/A return u.hostAddress;
0N/A
0N/A String host = u.getHost();
0N/A if (host == null || host.equals("")) {
0N/A return null;
0N/A } else {
0N/A try {
0N/A u.hostAddress = InetAddress.getByName(host);
0N/A } catch (UnknownHostException ex) {
0N/A return null;
0N/A } catch (SecurityException se) {
0N/A return null;
0N/A }
0N/A }
0N/A return u.hostAddress;
0N/A }
0N/A
0N/A /**
0N/A * Compares the host components of two URLs.
0N/A * @param u1 the URL of the first host to compare
0N/A * @param u2 the URL of the second host to compare
0N/A * @return <tt>true</tt> if and only if they
0N/A * are equal, <tt>false</tt> otherwise.
0N/A * @since 1.3
0N/A */
0N/A protected boolean hostsEqual(URL u1, URL u2) {
0N/A InetAddress a1 = getHostAddress(u1);
0N/A InetAddress a2 = getHostAddress(u2);
0N/A // if we have internet address for both, compare them
0N/A if (a1 != null && a2 != null) {
0N/A return a1.equals(a2);
0N/A // else, if both have host names, compare them
0N/A } else if (u1.getHost() != null && u2.getHost() != null)
0N/A return u1.getHost().equalsIgnoreCase(u2.getHost());
0N/A else
0N/A return u1.getHost() == null && u2.getHost() == null;
0N/A }
0N/A
0N/A /**
0N/A * Converts a <code>URL</code> of a specific protocol to a
0N/A * <code>String</code>.
0N/A *
0N/A * @param u the URL.
0N/A * @return a string representation of the <code>URL</code> argument.
0N/A */
0N/A protected String toExternalForm(URL u) {
0N/A
0N/A // pre-compute length of StringBuffer
0N/A int len = u.getProtocol().length() + 1;
0N/A if (u.getAuthority() != null && u.getAuthority().length() > 0)
0N/A len += 2 + u.getAuthority().length();
0N/A if (u.getPath() != null) {
0N/A len += u.getPath().length();
0N/A }
0N/A if (u.getQuery() != null) {
0N/A len += 1 + u.getQuery().length();
0N/A }
0N/A if (u.getRef() != null)
0N/A len += 1 + u.getRef().length();
0N/A
0N/A StringBuffer result = new StringBuffer(len);
0N/A result.append(u.getProtocol());
0N/A result.append(":");
0N/A if (u.getAuthority() != null && u.getAuthority().length() > 0) {
0N/A result.append("//");
0N/A result.append(u.getAuthority());
0N/A }
0N/A if (u.getPath() != null) {
0N/A result.append(u.getPath());
0N/A }
0N/A if (u.getQuery() != null) {
0N/A result.append('?');
0N/A result.append(u.getQuery());
0N/A }
0N/A if (u.getRef() != null) {
0N/A result.append("#");
0N/A result.append(u.getRef());
0N/A }
0N/A return result.toString();
0N/A }
0N/A
0N/A /**
0N/A * Sets the fields of the <code>URL</code> argument to the indicated values.
0N/A * Only classes derived from URLStreamHandler are supposed to be able
0N/A * to call the set method on a URL.
0N/A *
0N/A * @param u the URL to modify.
0N/A * @param protocol the protocol name.
0N/A * @param host the remote host value for the URL.
0N/A * @param port the port on the remote machine.
0N/A * @param authority the authority part for the URL.
0N/A * @param userInfo the userInfo part of the URL.
0N/A * @param path the path component of the URL.
0N/A * @param query the query part for the URL.
0N/A * @param ref the reference.
0N/A * @exception SecurityException if the protocol handler of the URL is
0N/A * different from this one
0N/A * @see java.net.URL#set(java.lang.String, java.lang.String, int, java.lang.String, java.lang.String)
0N/A * @since 1.3
0N/A */
0N/A protected void setURL(URL u, String protocol, String host, int port,
0N/A String authority, String userInfo, String path,
0N/A String query, String ref) {
0N/A if (this != u.handler) {
0N/A throw new SecurityException("handler for url different from " +
0N/A "this handler");
0N/A }
0N/A // ensure that no one can reset the protocol on a given URL.
0N/A u.set(u.getProtocol(), host, port, authority, userInfo, path, query, ref);
0N/A }
0N/A
0N/A /**
0N/A * Sets the fields of the <code>URL</code> argument to the indicated values.
0N/A * Only classes derived from URLStreamHandler are supposed to be able
0N/A * to call the set method on a URL.
0N/A *
0N/A * @param u the URL to modify.
0N/A * @param protocol the protocol name. This value is ignored since 1.2.
0N/A * @param host the remote host value for the URL.
0N/A * @param port the port on the remote machine.
0N/A * @param file the file.
0N/A * @param ref the reference.
0N/A * @exception SecurityException if the protocol handler of the URL is
0N/A * different from this one
0N/A * @deprecated Use setURL(URL, String, String, int, String, String, String,
0N/A * String);
0N/A */
0N/A @Deprecated
0N/A protected void setURL(URL u, String protocol, String host, int port,
0N/A String file, String ref) {
0N/A /*
0N/A * Only old URL handlers call this, so assume that the host
0N/A * field might contain "user:passwd@host". Fix as necessary.
0N/A */
0N/A String authority = null;
0N/A String userInfo = null;
0N/A if (host != null && host.length() != 0) {
0N/A authority = (port == -1) ? host : host + ":" + port;
0N/A int at = host.lastIndexOf('@');
0N/A if (at != -1) {
0N/A userInfo = host.substring(0, at);
0N/A host = host.substring(at+1);
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Assume file might contain query part. Fix as necessary.
0N/A */
0N/A String path = null;
0N/A String query = null;
0N/A if (file != null) {
0N/A int q = file.lastIndexOf('?');
0N/A if (q != -1) {
0N/A query = file.substring(q+1);
0N/A path = file.substring(0, q);
0N/A } else
0N/A path = file;
0N/A }
0N/A setURL(u, protocol, host, port, authority, userInfo, path, query, ref);
0N/A }
0N/A}