URLStreamHandler.java 18.1 KB
Newer Older
Tom Tromey committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/* URLStreamHandler.java -- Abstract superclass for all protocol handlers
   Copyright (C) 1998, 1999, 2002, 2003, 2004 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package java.net;

40 41
import gnu.java.lang.CPStringBuilder;

Tom Tromey committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
import java.io.File;
import java.io.IOException;


/*
 * Written using on-line Java Platform 1.2 API Specification, as well
 * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
 * Status:  Believed complete and correct.
 */

/**
 * This class is the superclass of all URL protocol handlers.  The URL
 * class loads the appropriate protocol handler to establish a connection
 * to a (possibly) remote service (eg, "http", "ftp") and to do protocol
 * specific parsing of URL's.  Refer to the URL class documentation for
 * details on how that class locates and loads protocol handlers.
 * <p>
 * A protocol handler implementation should override the openConnection()
 * method, and optionally override the parseURL() and toExternalForm()
 * methods if necessary. (The default implementations will parse/write all
 * URL's in the same form as http URL's).  A protocol  specific subclass
 * of URLConnection will most likely need to be created as well.
 * <p>
 * Note that the instance methods in this class are called as if they
 * were static methods.  That is, a URL object to act on is passed with
 * every call rather than the caller assuming the URL is stored in an
 * instance variable of the "this" object.
 * <p>
 * The methods in this class are protected and accessible only to subclasses.
 * URLStreamConnection objects are intended for use by the URL class only,
 * not by other classes (unless those classes are implementing protocols).
 *
 * @author Aaron M. Renn (arenn@urbanophile.com)
 * @author Warren Levy (warrenl@cygnus.com)
 *
 * @see URL
 */
public abstract class URLStreamHandler
{
  /**
   * Creates a URLStreamHander
   */
  public URLStreamHandler()
  {
  }

  /**
   * Returns a URLConnection for the passed in URL.  Note that this should
   * not actually create the connection to the (possibly) remote host, but
   * rather simply return a URLConnection object.  The connect() method of
   * URL connection is used to establish the actual connection, possibly
   * after the caller sets up various connection options.
   *
   * @param url The URL to get a connection object for
   *
   * @return A URLConnection object for the given URL
   *
   * @exception IOException If an error occurs
   */
  protected abstract URLConnection openConnection(URL url)
    throws IOException;

  /**
   * This method parses the string passed in as a URL and set's the
   * instance data fields in the URL object passed in to the various values
   * parsed out of the string.  The start parameter is the position to start
   * scanning the string.  This is usually the position after the ":" which
   * terminates the protocol name.  The end parameter is the position to
   * stop scanning.  This will be either the end of the String, or the
   * position of the "#" character, which separates the "file" portion of
   * the URL from the "anchor" portion.
   * <p>
   * This method assumes URL's are formatted like http protocol URL's, so
   * subclasses that implement protocols with URL's the follow a different
   * syntax should override this method.  The lone exception is that if
   * the protocol name set in the URL is "file", this method will accept
   * an empty hostname (i.e., "file:///"), which is legal for that protocol
   *
   * @param url The URL object in which to store the results
   * @param spec The String-ized URL to parse
   * @param start The position in the string to start scanning from
   * @param end The position in the string to stop scanning
   */
  protected void parseURL(URL url, String spec, int start, int end)
  {
    String host = url.getHost();
    int port = url.getPort();
    String file = url.getFile();
    String ref = url.getRef();
    String userInfo = url.getUserInfo();
    String authority = url.getAuthority();
    String query = null;
134

Tom Tromey committed
135 136 137 138
    // On Windows we need to change \ to / for file URLs
    char separator = File.separatorChar;
    if (url.getProtocol().equals("file") && separator != '/')
      {
139 140
        file = file.replace(separator, '/');
        spec = spec.replace(separator, '/');
Tom Tromey committed
141 142 143 144
      }

    if (spec.regionMatches(start, "//", 0, 2))
      {
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
        String genuineHost;
        int hostEnd;
        int colon;
        int at_host;

        start += 2;
        int slash = spec.indexOf('/', start);
        if (slash >= 0)
          hostEnd = slash;
        else
          hostEnd = end;

        authority = host = spec.substring(start, hostEnd);

        // We first need a genuine host name (with userinfo).
        // So we check for '@': if it's present check the port in the
        // section after '@' in the other case check it in the full string.
        // P.S.: We don't care having '@' at the beginning of the string.
        if ((at_host = host.indexOf('@')) >= 0)
          {
            genuineHost = host.substring(at_host);
            userInfo = host.substring(0, at_host);
          }
        else
          genuineHost = host;

        // Look for optional port number.  It is valid for the non-port
        // part of the host name to be null (e.g. a URL "http://:80").
        // TBD: JDK 1.2 in this case sets host to null rather than "";
        // this is undocumented and likely an unintended side effect in 1.2
        // so we'll be simple here and stick with "". Note that
        // "http://" or "http:///" produce a "" host in JDK 1.2.
        if ((colon = genuineHost.indexOf(':')) >= 0)
          {
            try
              {
                port = Integer.parseInt(genuineHost.substring(colon + 1));
              }
            catch (NumberFormatException e)
              {
                // Ignore invalid port values; port is already set to u's
                // port.
              }

            // Now we must cut the port number in the original string.
            if (at_host >= 0)
              host = host.substring(0, at_host + colon);
            else
              host = host.substring(0, colon);
          }
        file = null;
        start = hostEnd;
Tom Tromey committed
197 198 199 200 201 202 203
      }
    else if (host == null)
      host = "";

    if (file == null || file.length() == 0
        || (start < end && spec.charAt(start) == '/'))
      {
204 205 206 207
        // No file context available; just spec for file.
        // Or this is an absolute path name; ignore any file context.
        file = spec.substring(start, end);
        ref = null;
Tom Tromey committed
208 209 210
      }
    else if (start < end)
      {
211 212 213 214 215 216 217 218 219 220 221 222 223
        // Context is available, but only override it if there is a new file.
        int lastSlash = file.lastIndexOf('/');
        if (lastSlash < 0)
          file = spec.substring(start, end);
        else
          file = (file.substring(0, lastSlash)
                  + '/' + spec.substring(start, end));

        // For URLs constructed relative to a context, we
        // need to canonicalise the file path.
        file = canonicalizeFilename(file);

        ref = null;
Tom Tromey committed
224 225 226 227
      }

    if (ref == null)
      {
228 229 230 231 232 233 234 235
        // Normally there should be no '#' in the file part,
        // but we are nice.
        int hash = file.indexOf('#');
        if (hash != -1)
          {
            ref = file.substring(hash + 1, file.length());
            file = file.substring(0, hash);
          }
Tom Tromey committed
236 237 238 239 240
      }

    // We care about the query tag only if there is no reference at all.
    if (ref == null)
      {
241 242 243 244 245 246
          int queryTag = file.indexOf('?');
          if (queryTag != -1)
            {
              query = file.substring(queryTag + 1);
              file = file.substring(0, queryTag);
            }
Tom Tromey committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
      }

    // XXX - Classpath used to call PlatformHelper.toCanonicalForm() on
    // the file part. It seems like overhead, but supposedly there is some
    // benefit in windows based systems (it also lowercased the string).
    setURL(url, url.getProtocol(), host, port, authority, userInfo, file, query, ref);
  }

  /*
   * Canonicalize a filename.
   */
  private static String canonicalizeFilename(String file)
  {
    // XXX - GNU Classpath has an implementation that might be more appropriate
    // for Windows based systems (gnu.java.io.PlatformHelper.toCanonicalForm)
    int index;

    // Replace "/./" with "/".  This probably isn't very efficient in
    // the general case, but it's probably not bad most of the time.
    while ((index = file.indexOf("/./")) >= 0)
      file = file.substring(0, index) + file.substring(index + 2);

    // Process "/../" correctly.  This probably isn't very efficient in
    // the general case, but it's probably not bad most of the time.
    while ((index = file.indexOf("/../")) >= 0)
      {
273 274 275 276 277 278
        // Strip of the previous directory - if it exists.
        int previous = file.lastIndexOf('/', index - 1);
        if (previous >= 0)
          file = file.substring(0, previous) + file.substring(index + 3);
        else
          break;
Tom Tromey committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
      }
    return file;
  }

  /**
   * Compares two URLs, excluding the fragment component
   *
   * @param url1 The first url
   * @param url2 The second url to compare with the first
   *
   * @return True if both URLs point to the same file, false otherwise.
   *
   * @specnote Now protected
   */
  protected boolean sameFile(URL url1, URL url2)
  {
    if (url1 == url2)
      return true;

    // This comparison is very conservative.  It assumes that any
    // field can be null.
    if (url1 == null || url2 == null)
      return false;
    int p1 = url1.getPort();
    if (p1 == -1)
      p1 = url1.ph.getDefaultPort();
    int p2 = url2.getPort();
    if (p2 == -1)
      p2 = url2.ph.getDefaultPort();
    if (p1 != p2)
      return false;
    String s1;
    String s2;
    s1 = url1.getProtocol();
    s2 = url2.getProtocol();
    if (s1 != s2 && (s1 == null || ! s1.equals(s2)))
      return false;
    s1 = url1.getHost();
    s2 = url2.getHost();
    if (s1 != s2 && (s1 == null || ! s1.equals(s2)))
      return false;
    s1 = canonicalizeFilename(url1.getFile());
    s2 = canonicalizeFilename(url2.getFile());
    if (s1 != s2 && (s1 == null || ! s1.equals(s2)))
      return false;
    return true;
  }

  /**
   * This methods sets the instance variables representing the various fields
   * of the URL to the values passed in.
   *
   * @param u The URL to modify
   * @param protocol The protocol to set
   * @param host The host name to et
   * @param port The port number to set
   * @param file The filename to set
   * @param ref The reference
   *
   * @exception SecurityException If the protocol handler of the URL is
   * different from this one
   *
   * @deprecated 1.2 Please use
   * #setURL(URL,String,String,int,String,String,String,String);
   */
  protected void setURL(URL u, String protocol, String host, int port,
                        String file, String ref)
  {
    u.set(protocol, host, port, file, ref);
  }

  /**
   * Sets the fields of the URL argument to the indicated values
   *
   * @param u The URL to modify
   * @param protocol The protocol to set
   * @param host The host name to set
   * @param port The port number to set
   * @param authority The authority to set
   * @param userInfo The user information to set
   * @param path The path/filename to set
   * @param query The query part to set
   * @param ref The reference
   *
   * @exception SecurityException If the protocol handler of the URL is
   * different from this one
   */
  protected void setURL(URL u, String protocol, String host, int port,
                        String authority, String userInfo, String path,
                        String query, String ref)
  {
    u.set(protocol, host, port, authority, userInfo, path, query, ref);
  }

  /**
374 375
   * This is the default method for computing whether two URLs are
   * equivalent.  This method assumes that neither URL is null.
Tom Tromey committed
376 377
   *
   * @param url1 An URL object
378
   * @param url2 Another URL object
Tom Tromey committed
379 380 381 382 383 384 385
   *
   * @return True if both given URLs are equal, false otherwise.
   */
  protected boolean equals(URL url1, URL url2)
  {
    // This comparison is very conservative.  It assumes that any
    // field can be null.
386 387 388 389 390 391 392 393 394
    int port1 = url1.getPort();
    if (port1 == -1)
      port1 = url1.getDefaultPort();
    int port2 = url2.getPort();
    if (port2 == -1)
      port2 = url2.getDefaultPort();
    // Note that we don't bother checking the 'authority'; it is
    // redundant.
    return (port1 == port2
Tom Tromey committed
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
           && ((url1.getProtocol() == null && url2.getProtocol() == null)
           || (url1.getProtocol() != null
           && url1.getProtocol().equals(url2.getProtocol())))
           && ((url1.getUserInfo() == null && url2.getUserInfo() == null)
           || (url1.getUserInfo() != null
           && url1.getUserInfo().equals(url2.getUserInfo())))
           && ((url1.getHost() == null && url2.getHost() == null)
           || (url1.getHost() != null && url1.getHost().equals(url2.getHost())))
           && ((url1.getPath() == null && url2.getPath() == null)
           || (url1.getPath() != null && url1.getPath().equals(url2.getPath())))
           && ((url1.getQuery() == null && url2.getQuery() == null)
           || (url1.getQuery() != null
           && url1.getQuery().equals(url2.getQuery())))
           && ((url1.getRef() == null && url2.getRef() == null)
           || (url1.getRef() != null && url1.getRef().equals(url2.getRef()))));
  }

  /**
   * Compares the host components of two URLs.
   *
   * @param url1 The first URL.
   * @param url2 The second URL.
   *
   * @return True if both URLs contain the same host.
   */
  protected boolean hostsEqual(URL url1, URL url2)
  {
    InetAddress addr1 = getHostAddress(url1);
    InetAddress addr2 = getHostAddress(url2);

    if (addr1 != null && addr2 != null)
      return addr1.equals(addr2);

    String host1 = url1.getHost();
    String host2 = url2.getHost();

    if (host1 != null && host2 != null)
      return host1.equalsIgnoreCase(host2);

    return host1 == null && host2 == null;
  }

  /**
   * Get the IP address of our host. An empty host field or a DNS failure will
   * result in a null return.
   *
   * @param url The URL to return the host address for.
   *
   * @return The address of the hostname in url.
   */
  protected InetAddress getHostAddress(URL url)
  {
    String hostname = url.getHost();

    if (hostname.equals(""))
      return null;

    try
      {
454
        return InetAddress.getByName(hostname);
Tom Tromey committed
455 456 457
      }
    catch (UnknownHostException e)
      {
458
        return null;
Tom Tromey committed
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
      }
  }

  /**
   * Returns the default port for a URL parsed by this handler. This method is
   * meant to be overidden by handlers with default port numbers.
   *
   * @return The default port number.
   */
  protected int getDefaultPort()
  {
    return -1;
  }

  /**
   * Provides the default hash calculation. May be overidden by handlers for
   * other protocols that have different requirements for hashCode calculation.
   *
   * @param url The URL to calc the hashcode for.
   *
   * @return The hashcode for the given URL.
   */
  protected int hashCode(URL url)
  {
    return url.getProtocol().hashCode()
           + ((url.getHost() == null) ? 0 : url.getHost().hashCode())
           + url.getFile().hashCode() + url.getPort();
  }

  /**
   * This method converts a URL object into a String.  This method creates
   * Strings in the mold of http URL's, so protocol handlers which use URL's
   * that have a different syntax should override this method
   *
   * @param url The URL object to convert
   *
   * @return A string representation of the url
   */
  protected String toExternalForm(URL url)
  {
    String protocol;
    String file;
    String ref;
    String authority;

    protocol = url.getProtocol();
    authority = url.getAuthority();
    if (authority == null)
      authority = "";
508

Tom Tromey committed
509 510 511 512 513 514
    file = url.getFile();
    ref = url.getRef();

    // Guess a reasonable size for the string buffer so we have to resize
    // at most once.
    int size = protocol.length() + authority.length() + file.length() + 24;
515
    CPStringBuilder sb = new CPStringBuilder(size);
Tom Tromey committed
516

517
    if (protocol.length() > 0)
Tom Tromey committed
518
      {
519 520
        sb.append(protocol);
        sb.append(":");
Tom Tromey committed
521
      }
522

523 524 525 526
    // If we have superfluous leading slashes (that means, at least 2)
    // we always add the authority component ("//" + host) to
    // avoid ambiguity. Otherwise we would generate an URL like
    // proto://home/foo
527
    // where we meant:
528 529 530 531 532 533 534
    // host: <empty> - file: //home/foo
    // but URL spec says it is:
    // host: home - file: /foo
    if (authority.length() != 0 || file.startsWith("//") )
      sb.append("//").append(authority).append(file);
    else
      sb.append(file);
Tom Tromey committed
535 536 537 538 539 540 541

    if (ref != null)
      sb.append('#').append(ref);

    return sb.toString();
  }
}