tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From craig...@apache.org
Subject cvs commit: jakarta-tomcat-4.0/tester/src/bin tester.xml
Date Fri, 24 Aug 2001 23:06:08 GMT
craigmcc    01/08/24 16:06:08

  Modified:    catalina build.xml
               catalina/src/share/org/apache/catalina/connector
                        HttpResponseBase.java
               tester/src/bin tester.xml
  Added:       catalina/src/share/org/apache/catalina/util URL.java
               catalina/src/test/org/apache/catalina/util URLTestCase.java
  Log:
  Add a replacement for the java.net.URL class that does the parsing and
  relative-URL resolution performed by java.net.URL, but does not suffer
  from the limitation that it only supports protocols for which a
  URLStreamHandler is installed.  In particular, this was preventing a
  Tomcat 4 install from successfully performing a response.sendRedirect() to
  an SSL-enabled URL, unless the redirecting Tomcat itself had JSSE
  installed.
  
  Also added extensives JUnit tests to ensure that parsing and relative
  resolution works as much as possible like java.net.URL does it.  There are
  three cases where the answers are different, that are commented out.
  These are really edge cases that won't matter in real life, but they
  should eventually be corrected.
  
  NOTE:  The parsing and path normalization code is horrendously inefficient
  at the moment.  I focused first on getting the behavior right, and
  creating a bunch of unit tests so that we can make sure we don't introduce
  regressions as the string processing is optimized.
  
  PR: Bugzilla #3139
  Submitted by:	Shawn Bayern <bayern@essentially.net>
  
  Revision  Changes    Path
  1.51      +7 -0      jakarta-tomcat-4.0/catalina/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/build.xml,v
  retrieving revision 1.50
  retrieving revision 1.51
  diff -u -r1.50 -r1.51
  --- build.xml	2001/07/23 03:12:02	1.50
  +++ build.xml	2001/08/24 23:06:08	1.51
  @@ -279,6 +279,13 @@
         <classpath refid="test.classpath"/>
       </java>
   
  +    <echo message="Running URL tests"/>
  +    <java classname="${test.runner}" fork="yes"
  +        failonerror="${test.failonerror}">
  +      <arg value="org.apache.catalina.util.URLTestCase"/>
  +      <classpath refid="test.classpath"/>
  +    </java>
  +
     </target>
   
   
  
  
  
  1.37      +7 -6      jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/HttpResponseBase.java
  
  Index: HttpResponseBase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/HttpResponseBase.java,v
  retrieving revision 1.36
  retrieving revision 1.37
  diff -u -r1.36 -r1.37
  --- HttpResponseBase.java	2001/08/08 19:26:07	1.36
  +++ HttpResponseBase.java	2001/08/24 23:06:08	1.37
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/HttpResponseBase.java,v 1.36 2001/08/08 19:26:07 pier Exp $
  - * $Revision: 1.36 $
  - * $Date: 2001/08/08 19:26:07 $
  + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/HttpResponseBase.java,v 1.37 2001/08/24 23:06:08 craigmcc Exp $
  + * $Revision: 1.37 $
  + * $Date: 2001/08/24 23:06:08 $
    *
    * ====================================================================
    *
  @@ -69,7 +69,7 @@
   import java.io.OutputStreamWriter;
   import java.io.PrintWriter;
   import java.net.MalformedURLException;
  -import java.net.URL;
  +// import java.net.URL;
   import java.security.AccessController;
   import java.security.PrivilegedAction;
   import java.text.SimpleDateFormat;
  @@ -90,6 +90,7 @@
   import org.apache.catalina.Logger;
   import org.apache.catalina.util.CookieTools;
   import org.apache.catalina.util.RequestUtil;
  +import org.apache.catalina.util.URL;
   
   
   /**
  @@ -100,7 +101,7 @@
    *
    * @author Craig R. McClanahan
    * @author Remy Maucherat
  - * @version $Revision: 1.36 $ $Date: 2001/08/08 19:26:07 $
  + * @version $Revision: 1.37 $ $Date: 2001/08/24 23:06:08 $
    */
   
   public class HttpResponseBase
  @@ -690,7 +691,7 @@
                   throw new IllegalArgumentException(location);
               }
           }
  -        return (url.toString());
  +        return (url.toExternalForm());
   
       }
   
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/URL.java
  
  Index: URL.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/URL.java,v 1.1 2001/08/24 23:06:08 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2001/08/24 23:06:08 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  
  package org.apache.catalina.util;
  
  
  import java.io.Serializable;
  import java.net.MalformedURLException;
  
  
  /**
   * <p><strong>URL</strong> is designed to provide public APIs for parsing
   * and synthesizing Uniform Resource Locators as similar as possible to the
   * APIs of <code>java.net.URL</code>, but without the ability to open a
   * stream or connection.  One of the consequences of this is that you can
   * construct URLs for protocols for which a URLStreamHandler is not
   * available (such as an "https" URL when JSSE is not installed).</p>
   *
   * <p><strong>WARNING</strong> - This class assumes that the string
   * representation of a URL conforms to the <code>spec</code> argument
   * as described in RFC 2396 "Uniform Resource Identifiers: Generic Syntax":
   * <pre>
   *   &lt;scheme&gt;//&lt;authority&gt;&lt;path&gt;?&lt;query&gt;#&lt;fragment&gt;
   * </pre></p>
   *
   * <p><strong>FIXME</strong> - This class really ought to end up in a Commons
   * package someplace.</p>
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2001/08/24 23:06:08 $
   */
  
  public final class URL implements Serializable {
  
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * Create a URL object from the specified String representation.
       *
       * @param spec String representation of the URL
       *
       * @exception MalformedURLException if the string representation
       *  cannot be parsed successfully
       */
      public URL(String spec) throws MalformedURLException {
  
          this(null, spec);
  
      }
  
  
      /**
       * Create a URL object by parsing a string representation relative
       * to a specified context.  Based on logic from JDK 1.3.1's
       * <code>java.net.URL</code>.
       *
       * @param context URL against which the relative representation
       *  is resolved
       * @param spec String representation of the URL (usually relative)
       *
       * @exception MalformedURLException if the string representation
       *  cannot be parsed successfully
       */
      public URL(URL context, String spec) throws MalformedURLException {
  
          String original = spec;
          int i, limit, c;
          int start = 0;
          String newProtocol = null;
          boolean aRef = false;
  
          try {
  
              // Eliminate leading and trailing whitespace
              limit = spec.length();
              while ((limit > 0) && (spec.charAt(limit - 1) <= ' ')) {
                  limit--;
              }
              while ((start < limit) && (spec.charAt(start) <= ' ')) {
                  start++;
              }
  
              // If the string representation starts with "url:", skip it
              if (spec.regionMatches(true, start, "url:", 0, 4)) {
                  start += 4;
              }
  
              // Is this a ref relative to the context URL?
              if ((start < spec.length()) && (spec.charAt(start) == '#')) {
                  aRef = true;
              }
  
              // Parse out the new protocol
              for (i = start; !aRef && (i < limit) &&
                       ((c = spec.charAt(i)) != '/'); i++) {
                  if (c == ':') {
                      String s = spec.substring(start, i).toLowerCase();
                      // Assume all protocols are valid
                      newProtocol = s;
                      start = i + 1;
                      break;
                  }
              }
  
              // Only use our context if the protocols match
              protocol = newProtocol;
              if ((context != null) && ((newProtocol == null) ||
                   newProtocol.equalsIgnoreCase(context.getProtocol()))) {
                  // If the context is a hierarchical URL scheme and the spec
                  // contains a matching scheme then maintain backwards
                  // compatibility and treat it as if the spec didn't contain
                  // the scheme; see 5.2.3 of RFC2396
                  if ((context.getPath() != null) &&
                      (context.getPath().startsWith("/")))
                      newProtocol = null;
                  if (newProtocol == null) {
                      protocol = context.getProtocol();
                      authority = context.getAuthority();
                      userInfo = context.getUserInfo();
                      host = context.getHost();
                      port = context.getPort();
                      file = context.getFile();
                      int question = file.lastIndexOf("?");
                      if (question < 0)
                          path = file;
                      else
                          path = file.substring(0, question);
                  }
              }
  
              if (protocol == null)
                  throw new MalformedURLException("no protocol: " + original);
  
              // Parse out any ref portion of the spec
              i = spec.indexOf('#', start);
              if (i >= 0) {
                  ref = spec.substring(i + 1, limit);
                  limit = i;
              }
  
              // Parse the remainder of the spec in a protocol-specific fashion
              parse(spec, start, limit);
              if (context != null)
                  normalize();
  
  
          } catch (MalformedURLException e) {
              throw e;
          } catch (Exception e) {
              throw new MalformedURLException(e.toString());
          }
  
      }
  
  
  
  
  
      /**
       * Create a URL object from the specified components.  The default port
       * number for the specified protocol will be used.
       *
       * @param protocol Name of the protocol to use
       * @param host Name of the host addressed by this protocol
       * @param file Filename on the specified host
       *
       * @exception MalformedURLException is never thrown, but present for
       *  compatible APIs
       */
      public URL(String protocol, String host, String file)
          throws MalformedURLException {
  
          this(protocol, host, -1, file);
  
      }
  
  
      /**
       * Create a URL object from the specified components.  Specifying a port
       * number of -1 indicates that the URL should use the default port for
       * that protocol.  Based on logic from JDK 1.3.1's
       * <code>java.net.URL</code>.
       *
       * @param protocol Name of the protocol to use
       * @param host Name of the host addressed by this protocol
       * @param port Port number, or -1 for the default port for this protocol
       * @param file Filename on the specified host
       *
       * @exception MalformedURLException is never thrown, but present for
       *  compatible APIs
       */
      public URL(String protocol, String host, int port, String file)
          throws MalformedURLException {
  
          this.protocol = protocol;
          this.host = host;
          this.port = port;
  
          int hash = file.indexOf('#');
          this.file = hash < 0 ? file : file.substring(0, hash);
          this.ref = hash < 0 ? null : file.substring(hash + 1);
          int question = file.lastIndexOf('?');
          if (question >= 0) {
              query = file.substring(question + 1);
              path = file.substring(0, question);
          } else
              path = file;
  
          if ((host != null) && (host.length() > 0))
              authority = (port == -1) ? host : host + ":" + port;
  
      }
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * The authority part of the URL.
       */
      private String authority = null;
  
  
      /**
       * The filename part of the URL.
       */
      private String file = null;
  
  
      /**
       * The host name part of the URL.
       */
      private String host = null;
  
  
      /**
       * The path part of the URL.
       */
      private String path = null;
  
  
      /**
       * The port number part of the URL.
       */
      private int port = -1;
  
  
      /**
       * The protocol name part of the URL.
       */
      private String protocol = null;
  
  
      /**
       * The query part of the URL.
       */
      private String query = null;
  
  
      /**
       * The reference part of the URL.
       */
      private String ref = null;
  
  
      /**
       * The user info part of the URL.
       */
      private String userInfo = null;
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Compare two URLs for equality.  The result is <code>true</code> if and
       * only if the argument is not null, and is a <code>URL</code> object
       * that represents the same <code>URL</code> as this object.  Two
       * <code>URLs</code> are equal if they have the same protocol and
       * reference the same host, the same port number on the host,
       * and the same file and anchor on the host.
       *
       * @param obj The URL to compare against
       */
      public boolean equals(Object obj) {
  
          if (obj == null)
              return (false);
          if (!(obj instanceof URL))
              return (false);
          URL other = (URL) obj;
          if (!sameFile(other))
              return (false);
          return (compare(ref, other.getRef()));
  
      }
  
  
      /**
       * Return the authority part of the URL.
       */
      public String getAuthority() {
  
          return (this.authority);
  
      }
  
  
      /**
       * Return the filename part of the URL.  <strong>NOTE</strong> - For
       * compatibility with <code>java.net.URL</code>, this value includes
       * the query string if there was one.  For just the path portion,
       * call <code>getPath()</code> instead.
       */
      public String getFile() {
  
          if (file == null)
              return ("");
          return (this.file);
  
      }
  
  
      /**
       * Return the host name part of the URL.
       */
      public String getHost() {
  
          return (this.host);
  
      }
  
  
      /**
       * Return the path part of the URL.
       */
      public String getPath() {
  
          if (this.path == null)
              return ("");
          return (this.path);
  
      }
  
  
      /**
       * Return the port number part of the URL.
       */
      public int getPort() {
  
          return (this.port);
  
      }
  
  
      /**
       * Return the protocol name part of the URL.
       */
      public String getProtocol() {
  
          return (this.protocol);
  
      }
  
  
      /**
       * Return the query part of the URL.
       */
      public String getQuery() {
  
          return (this.query);
  
      }
  
  
      /**
       * Return the reference part of the URL.
       */
      public String getRef() {
  
          return (this.ref);
  
      }
  
  
      /**
       * Return the user info part of the URL.
       */
      public String getUserInfo() {
  
          return (this.userInfo);
  
      }
  
  
      /**
       * Normalize the <code>path</code> (and therefore <code>file</code>)
       * portions of this URL.
       * <p>
       * <strong>NOTE</strong> - This method is not part of the public API
       * of <code>java.net.URL</code>, but is provided as a value added
       * service of this implementation.
       *
       * @exception MalformedURLException if a normalization error occurs,
       *  such as trying to move about the hierarchical root
       */
      public void normalize() throws MalformedURLException {
  
          // Special case for null path
          if (path == null) {
              if (query != null)
                  file = "?" + query;
              else
                  file = "";
              return;
          }
  
          // Create a place for the normalized path
          String normalized = path;
          if (normalized.equals("/.")) {
              path = "/";
              if (query != null)
                  file = path + "?" + query;
              else
                  file = path;
              return;
          }
  
          // Normalize the slashes and add leading slash if necessary
          if (normalized.indexOf('\\') >= 0)
              normalized = normalized.replace('\\', '/');
          if (!normalized.startsWith("/"))
              normalized = "/" + normalized;
  
          // Resolve occurrences of "//" in the normalized path
          while (true) {
              int index = normalized.indexOf("//");
              if (index < 0)
                  break;
              normalized = normalized.substring(0, index) +
                  normalized.substring(index + 1);
          }
  
          // Resolve occurrences of "/./" in the normalized path
          while (true) {
              int index = normalized.indexOf("/./");
              if (index < 0)
                  break;
              normalized = normalized.substring(0, index) +
                  normalized.substring(index + 2);
          }
  
          // Resolve occurrences of "/../" in the normalized path
          while (true) {
              int index = normalized.indexOf("/../");
              if (index < 0)
                  break;
              if (index == 0)
                  throw new MalformedURLException
                      ("Invalid relative URL reference");
              int index2 = normalized.lastIndexOf('/', index - 1);
              normalized = normalized.substring(0, index2) +
                  normalized.substring(index + 3);
          }
  
          // Resolve occurrences of "/." at the end of the normalized path
          if (normalized.endsWith("/."))
              normalized = normalized.substring(0, normalized.length() - 1);
  
          // Resolve occurrences of "/.." at the end of the normalized path
          if (normalized.endsWith("/..")) {
              int index = normalized.length() - 3;
              int index2 = normalized.lastIndexOf('/', index - 1);
              if (index2 < 0)
                  throw new MalformedURLException
                      ("Invalid relative URL reference");
              normalized = normalized.substring(0, index2 + 1);
          }
  
          // Return the normalized path that we have completed
          path = normalized;
          if (query != null)
              file = path + "?" + query;
          else
              file = path;
  
      }
  
  
      /**
       * Compare two URLs, excluding the "ref" fields.  Returns <code>true</code>
       * if this <code>URL</code> and the <code>other</code> argument both refer
       * to the same resource.  The two <code>URLs</code> might not both contain
       * the same anchor.
       */
      public boolean sameFile(URL other) {
  
          if (!compare(protocol, other.getProtocol()))
              return (false);
          if (!compare(host, other.getHost()))
              return (false);
          if (port != other.getPort())
              return (false);
          if (!compare(file, other.getFile()))
              return (false);
          return (true);
  
      }
  
  
      /**
       * Return a string representation of this URL.  This follow the rules in
       * RFC 2396, Section 5.2, Step 7.
       */
      public String toExternalForm() {
  
          StringBuffer sb = new StringBuffer();
          if (protocol != null) {
              sb.append(protocol);
              sb.append(":");
          }
          if (authority != null) {
              sb.append("//");
              sb.append(authority);
          }
          if (path != null)
              sb.append(path);
          if (query != null) {
              sb.append('?');
              sb.append(query);
          }
          if (ref != null) {
              sb.append('#');
              sb.append(ref);
          }
          return (sb.toString());
  
      }
  
  
      /**
       * Return a string representation of this object.
       */
      public String toString() {
  
          StringBuffer sb = new StringBuffer("URL[");
          sb.append("authority=");
          sb.append(authority);
          sb.append(", file=");
          sb.append(file);
          sb.append(", host=");
          sb.append(host);
          sb.append(", port=");
          sb.append(port);
          sb.append(", protocol=");
          sb.append(protocol);
          sb.append(", query=");
          sb.append(query);
          sb.append(", ref=");
          sb.append(ref);
          sb.append(", userInfo=");
          sb.append(userInfo);
          sb.append("]");
          return (sb.toString());
  
          //        return (toExternalForm());
  
      }
  
  
      // -------------------------------------------------------- Private Methods
  
  
      /**
       * Compare to String values for equality, taking appropriate care if one
       * or both of the values are <code>null</code>.
       *
       * @param first First string
       * @param second Second string
       */
      private boolean compare(String first, String second) {
  
          if (first == null) {
              if (second == null)
                  return (true);
              else
                  return (false);
          } else {
              if (second == null)
                  return (false);
              else
                  return (first.equals(second));
          }
  
      }
  
  
      /**
       * Parse the specified portion of the string representation of a URL,
       * assuming that it has a format similar to that for <code>http</code>.
       *
       * <p><strong>FIXME</strong> - This algorithm can undoubtedly be optimized
       * for performance.  However, that needs to wait until after sufficient
       * unit tests are implemented to guarantee correct behavior with no
       * regressions.</p>
       *
       * @param spec String representation being parsed
       * @param start Starting offset, which will be just after the ':' (if
       *  there is one) that determined the protocol name
       * @param limit Ending position, which will be the position of the '#'
       *  (if there is one) that delimited the anchor
       *
       * @exception MalformedURLException if a parsing error occurs
       */
      private void parse(String spec, int start, int limit)
          throws MalformedURLException {
  
          // Trim the query string (if any) off the tail end
          int question = spec.lastIndexOf('?', limit - 1);
          if ((question >= 0) && (question < limit)) {
              query = spec.substring(question + 1, limit);
              limit = question;
          } else {
              query = null;
          }
  
          // Parse the authority section
          if (spec.indexOf("//", start) == start) {
              int pathStart = spec.indexOf("/", start + 2);
              if ((pathStart >= 0) && (pathStart < limit)) {
                  authority = spec.substring(start + 2, pathStart);
                  start = pathStart;
              } else {
                  authority = spec.substring(start + 2, limit);
                  start = limit;
              }
              if (authority.length() > 0) {
                  int colon = authority.indexOf(':');
                  if (colon >= 0) {
                      try {
                          port =
                              Integer.parseInt(authority.substring(colon + 1));
                      } catch (NumberFormatException e) {
                          throw new MalformedURLException(e.toString());
                      }
                      host = authority.substring(0, colon);
                  } else {
                      host = authority;
                      port = -1;
                  }
              }
          }
  
          // Parse the path section
          if (spec.indexOf("/", start) == start) {     // Absolute path
              path = spec.substring(start, limit);
              if (query != null)
                  file = path + "?" + query;
              else
                  file = path;
              return;
          }
  
          // Resolve relative path against our context's file
          if (path == null) {
              if (query != null)
                  file = "?" + query;
              else
                  file = null;
              return;
          }
          if (!path.startsWith("/"))
              throw new MalformedURLException
                  ("Base path does not start with '/'");
          if (!path.endsWith("/"))
              path += '/';
          path += "../" + spec.substring(start, limit);
          if (query != null)
              file = path + "?" + query;
          else
              file = path;
          return;
  
      }
  
  
  }
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/test/org/apache/catalina/util/URLTestCase.java
  
  Index: URLTestCase.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/test/org/apache/catalina/util/URLTestCase.java,v 1.1 2001/08/24 23:06:08 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2001/08/24 23:06:08 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */ 
  
  
  package org.apache.catalina.util;
  
  
  import java.net.MalformedURLException;
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  
  /**
   * Unit tests for the <code>org.apache.catalina.util.URL</code> class.
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2001/08/24 23:06:08 $
   */
  
  public class URLTestCase extends TestCase {
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * Construct a new instance of this test case.
       *
       * @param name Name of the test case
       */
      public URLTestCase(String name) {
  
          super(name);
  
      }
  
  
      // --------------------------------------------------- Overall Test Methods
  
  
      /**
       * Set up instance variables required by this test case.
       */
      public void setUp() {
  
          ; // No action required
  
      }
  
  
      /**
       * Return the tests included in this test suite.
       */
      public static Test suite() {
  
          return (new TestSuite(URLTestCase.class));
  
      }
  
  
      /**
       * Tear down instance variables required by this test case.
       */
      public void tearDown() {
  
          ; // No action required
  
      }
  
  
      // ------------------------------------------------ Individual Test Methods
  
  
      /**
       * Negative tests for absolute URL strings in various patterns.  Each of
       * these should throw <code>MalformedURLException</code>.
       */
      public void testNegativeAbsolute() {
  
          negative("index.html");
          negative("index.html#ref");
          negative("index.html?name=value");
          negative("index.html?name=value#ref");
  
          negative("/index.html");
          negative("/index.html#ref");
          negative("/index.html?name=value");
          negative("/index.html?name=value#ref");
  
      }
  
  
      /**
       * Negative tests for <code>normalize()</code>.  Attempts to normalize
       * these legal URLs should throw <code>MalformedURLException</code>.
       */
      public void testNegativeNormalize() {
  
          normalize("http://localhost/..");
          normalize("http://localhost/..#ref");
          normalize("http://localhost/..?name=value");
          normalize("http://localhost/..?name=value#ref");
  
          normalize("http://localhost:8080/..");
          normalize("http://localhost:8080/..#ref");
          normalize("http://localhost:8080/..?name=value");
          normalize("http://localhost:8080/..?name=value#ref");
   
          normalize("http://localhost/../");
          normalize("http://localhost/../#ref");
          normalize("http://localhost/../?name=value");
          normalize("http://localhost/../?name=value#ref");
  
          normalize("http://localhost:8080/../");
          normalize("http://localhost:8080/../#ref");
          normalize("http://localhost:8080/../?name=value");
          normalize("http://localhost:8080/../?name=value#ref");
   
          normalize("http://localhost/index.html/../../foo.html");
          normalize("http://localhost/index.html/../../foo.html#ref");
          normalize("http://localhost/index.html/../../foo.html?name=value");
          normalize("http://localhost/index.html/../../foo.html?name=value#ref");
  
          normalize("http://localhost:8080/index.html/../../foo.html");
          normalize("http://localhost:8080/index.html/../../foo.html#ref");
          normalize("http://localhost:8080/index.html/../../foo.html?name=value");
          normalize("http://localhost:8080/index.html/../../foo.html?name=value#ref");
   
      }
  
  
      /**
       * Negative tests for relative URL strings in various patterns.  Each of
       * these should throw <code>MalformedURLException</code>.
       */
      public void testNegativeRelative() {
  
          // Commented out because java.net.URL ignores extraneous "../"
          //        negative("http://a/b/c/d;p?q", "../../../g");
          //        negative("http://a/b/c/d;p?q", "/../g");
  
      }
  
  
      /**
       * Positive tests for absolute URL strings in various patterns.
       */
      public void testPositiveAbsolute() {
  
          positive("http://a/b/c/d;p?q");
  
          positive("http://localhost/index.html");
          positive("http://localhost/index.html#ref");
          positive("http://localhost/index.html?name=value");
          positive("http://localhost/index.html?name=value#ref");
  
          positive("http://localhost:8080/index.html");
          positive("http://localhost:8080/index.html#ref");
          positive("http://localhost:8080/index.html?name=value");
          positive("http://localhost:8080/index.html?name=value#ref");
  
          positive("http://localhost/index.html/.");
          positive("http://localhost/index.html/.#ref");
          positive("http://localhost/index.html/.?name=value");
          positive("http://localhost/index.html/.?name=value#ref");
  
          positive("http://localhost:8080/index.html/.");
          positive("http://localhost:8080/index.html/.#ref");
          positive("http://localhost:8080/index.html/.?name=value");
          positive("http://localhost:8080/index.html/.?name=value#ref");
  
          positive("http://localhost/index.html/foo/..");
          positive("http://localhost/index.html/foo/..#ref");
          positive("http://localhost/index.html/foo/..?name=value");
          positive("http://localhost/index.html/foo/..?name=value#ref");
  
          positive("http://localhost:8080/index.html/foo/..");
          positive("http://localhost:8080/index.html/foo/..#ref");
          positive("http://localhost:8080/index.html/foo/..?name=value");
          positive("http://localhost:8080/index.html/foo/..?name=value#ref");
  
          positive("http://localhost/index.html/../foo.html");
          positive("http://localhost/index.html/../foo.html#ref");
          positive("http://localhost/index.html/../foo.html?name=value");
          positive("http://localhost/index.html/../foo.html?name=value#ref");
  
          positive("http://localhost:8080/index.html/../foo.html");
          positive("http://localhost:8080/index.html/../foo.html#ref");
          positive("http://localhost:8080/index.html/../foo.html?name=value");
          positive("http://localhost:8080/index.html/../foo.html?name=value#ref");
  
          positive("http://localhost");
          positive("http://localhost#ref");
          positive("http://localhost?name=value");
          positive("http://localhost?name=value#ref");
  
          positive("http://localhost:8080");
          positive("http://localhost:8080#ref");
          positive("http://localhost:8080?name=value");
          positive("http://localhost:8080?name=value#ref");
  
          positive("http://localhost/");
          positive("http://localhost/#ref");
          positive("http://localhost/?name=value");
          positive("http://localhost/?name=value#ref");
  
          positive("http://localhost:8080/");
          positive("http://localhost:8080/#ref");
          positive("http://localhost:8080/?name=value");
          positive("http://localhost:8080/?name=value#ref");
  
      }
  
  
      /**
       * Positive tests for normalizing absolute URL strings in various patterns.
       */
      public void testPositiveNormalize() {
  
          normalize("http://a/b/c/d;p?q",
                    "http://a/b/c/d;p?q");
  
          normalize("http://localhost/index.html",
                    "http://localhost/index.html");
          normalize("http://localhost/index.html#ref",
                    "http://localhost/index.html#ref");
          normalize("http://localhost/index.html?name=value",
                    "http://localhost/index.html?name=value");
          normalize("http://localhost/index.html?name=value#ref",
                    "http://localhost/index.html?name=value#ref");
  
          normalize("http://localhost:8080/index.html",
                    "http://localhost:8080/index.html");
          normalize("http://localhost:8080/index.html#ref",
                    "http://localhost:8080/index.html#ref");
          normalize("http://localhost:8080/index.html?name=value",
                    "http://localhost:8080/index.html?name=value");
          normalize("http://localhost:8080/index.html?name=value#ref",
                    "http://localhost:8080/index.html?name=value#ref");
  
          normalize("http://localhost/./index.html",
                    "http://localhost/index.html");
          normalize("http://localhost/./index.html#ref",
                    "http://localhost/index.html#ref");
          normalize("http://localhost/./index.html?name=value",
                    "http://localhost/index.html?name=value");
          normalize("http://localhost/./index.html?name=value#ref",
                    "http://localhost/index.html?name=value#ref");
  
          normalize("http://localhost:8080/./index.html",
                    "http://localhost:8080/index.html");
          normalize("http://localhost:8080/./index.html#ref",
                    "http://localhost:8080/index.html#ref");
          normalize("http://localhost:8080/./index.html?name=value",
                    "http://localhost:8080/index.html?name=value");
          normalize("http://localhost:8080/./index.html?name=value#ref",
                    "http://localhost:8080/index.html?name=value#ref");
  
          normalize("http://localhost/index.html/.",
                    "http://localhost/index.html/");
          normalize("http://localhost/index.html/.#ref",
                    "http://localhost/index.html/#ref");
          normalize("http://localhost/index.html/.?name=value",
                    "http://localhost/index.html/?name=value");
          normalize("http://localhost/index.html/.?name=value#ref",
                    "http://localhost/index.html/?name=value#ref");
  
          normalize("http://localhost:8080/index.html/.",
                    "http://localhost:8080/index.html/");
          normalize("http://localhost:8080/index.html/.#ref",
                    "http://localhost:8080/index.html/#ref");
          normalize("http://localhost:8080/index.html/.?name=value",
                    "http://localhost:8080/index.html/?name=value");
          normalize("http://localhost:8080/index.html/.?name=value#ref",
                    "http://localhost:8080/index.html/?name=value#ref");
  
          normalize("http://localhost/index.html/./",
                    "http://localhost/index.html/");
          normalize("http://localhost/index.html/./#ref",
                    "http://localhost/index.html/#ref");
          normalize("http://localhost/index.html/./?name=value",
                    "http://localhost/index.html/?name=value");
          normalize("http://localhost/index.html/./?name=value#ref",
                    "http://localhost/index.html/?name=value#ref");
  
          normalize("http://localhost:8080/index.html/./",
                    "http://localhost:8080/index.html/");
          normalize("http://localhost:8080/index.html/./#ref",
                    "http://localhost:8080/index.html/#ref");
          normalize("http://localhost:8080/index.html/./?name=value",
                    "http://localhost:8080/index.html/?name=value");
          normalize("http://localhost:8080/index.html/./?name=value#ref",
                    "http://localhost:8080/index.html/?name=value#ref");
  
          normalize("http://localhost/foo.html/../index.html",
                    "http://localhost/index.html");
          normalize("http://localhost/foo.html/../index.html#ref",
                    "http://localhost/index.html#ref");
          normalize("http://localhost/foo.html/../index.html?name=value",
                    "http://localhost/index.html?name=value");
          normalize("http://localhost/foo.html/../index.html?name=value#ref",
                    "http://localhost/index.html?name=value#ref");
  
          normalize("http://localhost:8080/foo.html/../index.html",
                    "http://localhost:8080/index.html");
          normalize("http://localhost:8080/foo.html/../index.html#ref",
                    "http://localhost:8080/index.html#ref");
          normalize("http://localhost:8080/foo.html/../index.html?name=value",
                    "http://localhost:8080/index.html?name=value");
          normalize("http://localhost:8080/foo.html/../index.html?name=value#ref",
                    "http://localhost:8080/index.html?name=value#ref");
  
          normalize("http://localhost/index.html/foo.html/..",
                    "http://localhost/index.html/");
          normalize("http://localhost/index.html/foo.html/..#ref",
                    "http://localhost/index.html/#ref");
          normalize("http://localhost/index.html/foo.html/..?name=value",
                    "http://localhost/index.html/?name=value");
          normalize("http://localhost/index.html/foo.html/..?name=value#ref",
                    "http://localhost/index.html/?name=value#ref");
  
          normalize("http://localhost:8080/index.html/foo.html/..",
                    "http://localhost:8080/index.html/");
          normalize("http://localhost:8080/index.html/foo.html/..#ref",
                    "http://localhost:8080/index.html/#ref");
          normalize("http://localhost:8080/index.html/foo.html/..?name=value",
                    "http://localhost:8080/index.html/?name=value");
          normalize("http://localhost:8080/index.html/foo.html/..?name=value#ref",
                    "http://localhost:8080/index.html/?name=value#ref");
  
          normalize("http://localhost/index.html/foo.html/../",
                    "http://localhost/index.html/");
          normalize("http://localhost/index.html/foo.html/../#ref",
                    "http://localhost/index.html/#ref");
          normalize("http://localhost/index.html/foo.html/../?name=value",
                    "http://localhost/index.html/?name=value");
          normalize("http://localhost/index.html/foo.html/../?name=value#ref",
                    "http://localhost/index.html/?name=value#ref");
  
          normalize("http://localhost:8080/index.html/foo.html/../",
                    "http://localhost:8080/index.html/");
          normalize("http://localhost:8080/index.html/foo.html/../#ref",
                    "http://localhost:8080/index.html/#ref");
          normalize("http://localhost:8080/index.html/foo.html/../?name=value",
                    "http://localhost:8080/index.html/?name=value");
          normalize("http://localhost:8080/index.html/foo.html/../?name=value#ref",
                    "http://localhost:8080/index.html/?name=value#ref");
  
      }
  
  
      /**
       * Positive tests for relative URL strings in various patterns.
       */
      public void testPositiveRelative() {
  
          // Test cases based on RFC 2396, Appendix C
          positive("http://a/b/c/d;p?q", "http:h");
          positive("http://a/b/c/d;p?q", "g");
          positive("http://a/b/c/d;p?q", "./g");
          positive("http://a/b/c/d;p?q", "g/");
          positive("http://a/b/c/d;p?q", "/g");
          //        positive("http://a/b/c/d;p?q", "//g");
          positive("http://a/b/c/d;p?q", "?y");
          positive("http://a/b/c/d;p?q", "g?y");
          //        positive("http://a/b/c/d;p?q", "#s");
          positive("http://a/b/c/d;p?q", "g#s");
          positive("http://a/b/c/d;p?q", "g?y#s");
          positive("http://a/b/c/d;p?q", ";x");
          positive("http://a/b/c/d;p?q", "g;x");
          positive("http://a/b/c/d;p?q", "g;x?y#s");
          positive("http://a/b/c/d;p?q", ".");
          positive("http://a/b/c/d;p?q", "./");
          positive("http://a/b/c/d;p?q", "..");
          positive("http://a/b/c/d;p?q", "../");
          positive("http://a/b/c/d;p?q", "../g");
          positive("http://a/b/c/d;p?q", "../..");
          positive("http://a/b/c/d;p?q", "../../");
          positive("http://a/b/c/d;p?q", "../../g");
          // Commented because java.net.URL doesn't normalize out the "/./"????
          //        positive("http://a/b/c/d;p?q", "/./g");
          positive("http://a/b/c/d;p?q", "g.");
          positive("http://a/b/c/d;p?q", ".g");
          positive("http://a/b/c/d;p?q", "g..");
          positive("http://a/b/c/d;p?q", "..g");
          positive("http://a/b/c/d;p?q", "./../g");
          positive("http://a/b/c/d;p?q", "./g/.");
          positive("http://a/b/c/d;p?q", "g/./h");
          positive("http://a/b/c/d;p?q", "g/../h");
          positive("http://a/b/c/d;p?q", "g;x=1/./y");
          positive("http://a/b/c/d;p?q", "g;x=1/../y");
          positive("http://a/b/c/d;p?q", "g?y/./x");
          positive("http://a/b/c/d;p?q", "g?y/../x");
          positive("http://a/b/c/d;p?q", "g#s/./x");
          positive("http://a/b/c/d;p?q", "g#s/../x");
  
      }
  
  
      // -------------------------------------------------------- Private Methods
  
  
      /**
       * Check that both our URL class and <code>java.net.URL</code> throw
       * <code>MalformedURLException</code> on an absolute URL specification.
       *
       * @param spec Absolute URL specification to be checked
       */
      private void negative(String spec) {
  
          try {
              java.net.URL url = new java.net.URL(spec);
              fail(spec + " should have failed on java.net.URL " +
                   "but returned " + url.toExternalForm());
          } catch (MalformedURLException e) {
              ; // Expected response
          }
  
          try {
              URL url = new URL(spec);
              fail(spec + " should have failed on tested URL " +
                   "but returned " + url.toExternalForm());
          } catch (MalformedURLException e) {
              ; // Expected response
          }
  
      }
  
  
      /**
       * Check that both our URL class and <code>java.net.URL</code> throw
       * <code>MalformedURLException</code> on an absolute URL specification
       * plus the corresponding relative URL specification.
       *
       * @param abs Absolute URL specification to be checked
       * @param rel Relative URL specification to be checked
       */
      private void negative(String abs, String rel) {
  
          java.net.URL baseNet = null;
          URL baseUrl = null;
  
          try {
              baseNet = new java.net.URL(abs);
          } catch (MalformedURLException e) {
              fail(abs + " net URL threw " + e);
          }
  
          try {
              baseUrl = new URL(abs);
          } catch (MalformedURLException e) {
              fail(abs + " url URL threw " + e);
          }
  
          try {
              java.net.URL url = new java.net.URL(baseNet, rel);
              fail(rel + " should have failed on java.net.URL " +
                   "but returned " + url.toExternalForm());
          } catch (MalformedURLException e) {
              ; // Expected response
          }
  
          try {
              URL url = new URL(baseUrl, rel);
              fail(rel + " should have failed on tested URL " +
                   "but returned " + url.toExternalForm());
          } catch (MalformedURLException e) {
              ; // Expected response
          }
  
      }
  
  
      /**
       * Attempts to normalize the specified URL should throw
       * MalformedURLException.
       *
       * @param spec Unnormalized version of the URL specification
       */
      private void normalize(String spec) {
  
          URL url = null;
          try {
              url = new URL(spec);
          } catch (Throwable t) {
              fail(spec + " should not have thrown " + t);
          }
  
          try {
              url.normalize();
              fail(spec + ".normalize() should have thrown MUE");
          } catch (MalformedURLException e) {
              ; // Expected result
          }
  
      }
  
  
      /**
       * It should be possible to normalize the specified URL into the
       * specified normalized form.
       *
       * @param spec Unnormalized version of the URL specification
       * @param norm Normalized version of the URL specification
       */
      private void normalize(String spec, String norm) {
  
          try {
              URL url = new URL(spec);
              url.normalize();
              assertEquals(spec + ".normalize()", norm, url.toExternalForm());
          } catch (Throwable t) {
              fail(spec + ".normalize() threw " + t);
          }
  
      }
  
  
      /**
       * Check the details of our URL class against <code>java.net.URL</code>
       * for an absolute URL specification.
       *
       * @param spec Absolute URL specification to be checked
       */
      private void positive(String spec) {
  
          // Compare results with what java.net.URL returns
          try {
              URL url = new URL(spec);
              java.net.URL net = new java.net.URL(spec);
              assertEquals(spec + " toExternalForm()",
                           net.toExternalForm(),
                           url.toExternalForm());
              assertEquals(spec + ".getAuthority()",
                           net.getAuthority(),
                           url.getAuthority());
              assertEquals(spec + ".getFile()",
                           net.getFile(),
                           url.getFile());
              assertEquals(spec + ".getHost()",
                           net.getHost(),
                           url.getHost());
              assertEquals(spec + ".getPath()",
                           net.getPath(),
                           url.getPath());
              assertEquals(spec + ".getPort()",
                           net.getPort(),
                           url.getPort());
              assertEquals(spec + ".getProtocol()",
                           net.getProtocol(),
                           url.getProtocol());
              assertEquals(spec + ".getQuery()",
                           net.getQuery(),
                           url.getQuery());
              assertEquals(spec + ".getRef()",
                           net.getRef(),
                           url.getRef());
              assertEquals(spec + ".getUserInfo()",
                           net.getUserInfo(),
                           url.getUserInfo());
          } catch (Throwable t) {
              fail(spec + " positive test threw " + t);
          }
  
      }
  
  
      /**
       * Check the details of our URL class against <code>java.net.URL</code>
       * for a relative URL specification.
       *
       * @param abs Absolute URL specification for base reference
       * @param rel Relative URL specification to resolve
       */
      private void positive(String abs, String rel) {
  
          // Compare results with what java.net.URL returns
          try {
              URL urlBase = new URL(abs);
              java.net.URL netBase = new java.net.URL(abs);
              URL url = new URL(urlBase, rel);
              java.net.URL net = new java.net.URL(netBase, rel);
              assertEquals(rel + " toExternalForm()",
                           net.toExternalForm(),
                           url.toExternalForm());
              assertEquals(rel + ".getAuthority()",
                           net.getAuthority(),
                           url.getAuthority());
              assertEquals(rel + ".getFile()",
                           net.getFile(),
                           url.getFile());
              assertEquals(rel + ".getHost()",
                           net.getHost(),
                           url.getHost());
              assertEquals(rel + ".getPath()",
                           net.getPath(),
                           url.getPath());
              assertEquals(rel + ".getPort()",
                           net.getPort(),
                           url.getPort());
              assertEquals(rel + ".getProtocol()",
                           net.getProtocol(),
                           url.getProtocol());
              assertEquals(rel + ".getQuery()",
                           net.getQuery(),
                           url.getQuery());
              assertEquals(rel + ".getRef()",
                           net.getRef(),
                           url.getRef());
              assertEquals(rel + ".getUserInfo()",
                           net.getUserInfo(),
                           url.getUserInfo());
          } catch (Throwable t) {
              fail(rel + " positive test threw " + t);
          }
  
      }
  
  
  }
  
  
  
  1.67      +2 -2      jakarta-tomcat-4.0/tester/src/bin/tester.xml
  
  Index: tester.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/tester/src/bin/tester.xml,v
  retrieving revision 1.66
  retrieving revision 1.67
  diff -u -r1.66 -r1.67
  --- tester.xml	2001/08/22 21:25:16	1.66
  +++ tester.xml	2001/08/24 23:06:08	1.67
  @@ -33,9 +33,9 @@
             status="302"/>
   
       <!-- Should not be able to use relative path above document root -->
  -    <tester host="${host}" port="${port}" protocol="${protocol}"
  +    <tester host="${host}" port="${port}" protocol="HTTP/1.0"
            request="${examples.path}/../.." debug="${debug}"
  -          status="302"/>
  +          status="500"/>
   
       <!-- Should be able to successfully retrieve a golden file -->
       <tester host="${host}" port="${port}" protocol="${protocol}"
  
  
  

Mime
View raw message