commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mbe...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient TestEquals.java TestNoHost.java
Date Tue, 27 Jan 2004 22:18:50 GMT
mbecke      2004/01/27 14:18:50

  Modified:    httpclient/src/java/org/apache/commons/httpclient/protocol
                        Protocol.java ProtocolSocketFactory.java
                        DefaultProtocolSocketFactory.java
                        SSLProtocolSocketFactory.java
               httpclient/src/contrib/org/apache/commons/httpclient/contrib/ssl
                        EasySSLProtocolSocketFactory.java
                        StrictSSLProtocolSocketFactory.java
               httpclient/src/test/org/apache/commons/httpclient
                        TestNoHost.java
  Added:       httpclient/src/test/org/apache/commons/httpclient
                        TestEquals.java
  Log:
  Fixed possible connection leak caused by lack of equals() and hashCode() on protocol
  socket factories.
  
  PR: 26139
  Submitted by: Michael Becke
  Reviewed by: Oleg Kalnichevski and Roland Weber
  
  Revision  Changes    Path
  1.8       +5 -5      jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/protocol/Protocol.java
  
  Index: Protocol.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/protocol/Protocol.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- Protocol.java	10 Nov 2003 14:33:13 -0000	1.7
  +++ Protocol.java	27 Jan 2004 22:18:47 -0000	1.8
  @@ -165,14 +165,14 @@
   
           if ("http".equals(id)) {
               final Protocol http 
  -                = new Protocol("http", new DefaultProtocolSocketFactory(), 80);
  +                = new Protocol("http", DefaultProtocolSocketFactory.getSocketFactory(),
80);
               Protocol.registerProtocol("http", http);
               return http;
           }
   
           if ("https".equals(id)) {
               final Protocol https 
  -                = new Protocol("https", new SSLProtocolSocketFactory(), 443);
  +                = new Protocol("https", SSLProtocolSocketFactory.getSocketFactory(), 443);
               Protocol.registerProtocol("https", https);
               return https;
           }
  
  
  
  1.6       +9 -3      jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/protocol/ProtocolSocketFactory.java
  
  Index: ProtocolSocketFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/protocol/ProtocolSocketFactory.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ProtocolSocketFactory.java	31 Jan 2003 00:33:36 -0000	1.5
  +++ ProtocolSocketFactory.java	27 Jan 2004 22:18:47 -0000	1.6
  @@ -71,6 +71,12 @@
   /**
    * A factory for creating Sockets.
    * 
  + * <p>Both {@link java.lang.Object#equals(java.lang.Object) Object.equals()} and

  + * {@link java.lang.Object#hashCode() Object.hashCode()} should be overridden appropriately.
 
  + * Protocol socket factories are used to uniquely identify <code>Protocol</code>s
and 
  + * <code>HostConfiguration</code>s, and <code>equals()</code> and
<code>hashCode()</code> are 
  + * required for the correct operation of some connection managers.</p>
  + * 
    * @see Protocol
    * 
    * @author Michael Becke
  
  
  
  1.6       +31 -4     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/protocol/DefaultProtocolSocketFactory.java
  
  Index: DefaultProtocolSocketFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/protocol/DefaultProtocolSocketFactory.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- DefaultProtocolSocketFactory.java	31 Jan 2003 00:33:36 -0000	1.5
  +++ DefaultProtocolSocketFactory.java	27 Jan 2004 22:18:47 -0000	1.6
  @@ -70,7 +70,7 @@
   
   /**
    * The default class for creating protocol sockets.  This class just uses the
  - * Protocol constructors.
  + * {@link java.net.Socket socket} constructors.
    * 
    * @author Michael Becke
    * 
  @@ -79,6 +79,19 @@
   public class DefaultProtocolSocketFactory implements ProtocolSocketFactory {
   
       /**
  +     * The factory singleton.
  +     */
  +    private static final DefaultProtocolSocketFactory factory = new DefaultProtocolSocketFactory();
  +    
  +    /**
  +     * Gets an singleton instance of the DefaultProtocolSocketFactory.
  +     * @return a DefaultProtocolSocketFactory
  +     */
  +    static DefaultProtocolSocketFactory getSocketFactory() {
  +        return factory;
  +    }
  +    
  +    /**
        * Constructor for DefaultProtocolSocketFactory.
        */
       public DefaultProtocolSocketFactory() {
  @@ -103,6 +116,20 @@
       public Socket createSocket(String host, int port)
           throws IOException, UnknownHostException {
           return new Socket(host, port);
  +    }
  +
  +    /**
  +     * All instances of DefaultProtocolSocketFactory are the same.
  +     */
  +    public boolean equals(Object obj) {
  +        return ((obj != null) && obj.getClass().equals(DefaultProtocolSocketFactory.class));
  +    }
  +
  +    /**
  +     * All instances of DefaultProtocolSocketFactory have the same hash code.
  +     */
  +    public int hashCode() {
  +        return DefaultProtocolSocketFactory.class.hashCode();
       }
   
   }
  
  
  
  1.6       +30 -3     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java
  
  Index: SSLProtocolSocketFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- SSLProtocolSocketFactory.java	31 Jan 2003 00:33:36 -0000	1.5
  +++ SSLProtocolSocketFactory.java	27 Jan 2004 22:18:47 -0000	1.6
  @@ -81,6 +81,19 @@
   public class SSLProtocolSocketFactory implements SecureProtocolSocketFactory {
   
       /**
  +     * The factory singleton.
  +     */
  +    private static final SSLProtocolSocketFactory factory = new SSLProtocolSocketFactory();
  +    
  +    /**
  +     * Gets an singleton instance of the SSLProtocolSocketFactory.
  +     * @return a SSLProtocolSocketFactory
  +     */
  +    static SSLProtocolSocketFactory getSocketFactory() {
  +        return factory;
  +    }    
  +    
  +    /**
        * Constructor for SSLProtocolSocketFactory.
        */
       public SSLProtocolSocketFactory() {
  @@ -132,4 +145,18 @@
           );
       }
   
  +    /**
  +     * All instances of SSLProtocolSocketFactory are the same.
  +     */
  +    public boolean equals(Object obj) {
  +        return ((obj != null) && obj.getClass().equals(SSLProtocolSocketFactory.class));
  +    }
  +
  +    /**
  +     * All instances of SSLProtocolSocketFactory have the same hash code.
  +     */
  +    public int hashCode() {
  +        return SSLProtocolSocketFactory.class.hashCode();
  +    }    
  +    
   }
  
  
  
  1.3       +9 -0      jakarta-commons/httpclient/src/contrib/org/apache/commons/httpclient/contrib/ssl/EasySSLProtocolSocketFactory.java
  
  Index: EasySSLProtocolSocketFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/contrib/org/apache/commons/httpclient/contrib/ssl/EasySSLProtocolSocketFactory.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- EasySSLProtocolSocketFactory.java	1 Apr 2003 19:04:18 -0000	1.2
  +++ EasySSLProtocolSocketFactory.java	27 Jan 2004 22:18:49 -0000	1.3
  @@ -175,4 +175,13 @@
               autoClose
           );
       }
  +
  +    public boolean equals(Object obj) {
  +        return ((obj != null) && obj.getClass().equals(EasySSLProtocolSocketFactory.class));
  +    }
  +
  +    public int hashCode() {
  +        return EasySSLProtocolSocketFactory.class.hashCode();
  +    }
  +
   }
  
  
  
  1.2       +17 -3     jakarta-commons/httpclient/src/contrib/org/apache/commons/httpclient/contrib/ssl/StrictSSLProtocolSocketFactory.java
  
  Index: StrictSSLProtocolSocketFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/contrib/org/apache/commons/httpclient/contrib/ssl/StrictSSLProtocolSocketFactory.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- StrictSSLProtocolSocketFactory.java	17 May 2003 15:40:00 -0000	1.1
  +++ StrictSSLProtocolSocketFactory.java	27 Jan 2004 22:18:49 -0000	1.2
  @@ -299,4 +299,18 @@
           }
           return dn.substring(0, i);
       }
  +    
  +    public boolean equals(Object obj) {
  +        if ((obj != null) && obj.getClass().equals(StrictSSLProtocolSocketFactory.class))
{
  +            return ((StrictSSLProtocolSocketFactory) obj).getHostnameVerification() 
  +                == this.verifyHostname;
  +        } else {
  +            return false;
  +        }
  +    }
  +
  +    public int hashCode() {
  +        return StrictSSLProtocolSocketFactory.class.hashCode();
  +    }
  +
   }
  
  
  
  1.30      +5 -4      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java
  
  Index: TestNoHost.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java,v
  retrieving revision 1.29
  retrieving revision 1.30
  diff -u -r1.29 -r1.30
  --- TestNoHost.java	6 Jan 2004 20:08:56 -0000	1.29
  +++ TestNoHost.java	27 Jan 2004 22:18:49 -0000	1.30
  @@ -108,6 +108,7 @@
           suite.addTest(TestHttpVersion.suite());
           suite.addTest(TestHttpParser.suite());
           suite.addTest(TestBadContentLength.suite());
  +        suite.addTest(TestEquals.suite());
           return suite;
       }
   
  
  
  
  1.1                  jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestEquals.java
  
  Index: TestEquals.java
  ===================================================================
  package org.apache.commons.httpclient;
  
  import org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory;
  import org.apache.commons.httpclient.protocol.Protocol;
  import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
  import org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  /**
   */
  public class TestEquals extends TestCase {
      
      public static Test suite() {
          return new TestSuite(TestEquals.class);
      }
      
      /**
       * 
       */
      public TestEquals() {
          super();
      }
  
      /**
       * @param arg0
       */
      public TestEquals(String arg0) {
          super(arg0);
      }
  
      public void testProtocol() {
          
          Protocol p1 = new Protocol("test", new DefaultProtocolSocketFactory(), 123);
          Protocol p2 = new Protocol("test", new DefaultProtocolSocketFactory(), 123);
          
          assertTrue(p1.equals(p2));
          assertTrue(p2.equals(p1));
      }
      
      public void testProtocolSocketFactory() {
          
          ProtocolSocketFactory p1 = new DefaultProtocolSocketFactory();
          ProtocolSocketFactory p2 = new DefaultProtocolSocketFactory();
  
          assertTrue(p1.equals(p2));
          assertTrue(p2.equals(p1));
  
          p1 = new SSLProtocolSocketFactory();
          p2 = new SSLProtocolSocketFactory();
  
          assertTrue(p1.equals(p2));
          assertTrue(p2.equals(p1));
          
      }
      
      public void testHostConfiguration() {
          
          HostConfiguration hc1 = new HostConfiguration();
          hc1.setHost("http", 80, "http");
  
          HostConfiguration hc2 = new HostConfiguration();
          hc2.setHost("http", 80, "http");
  
          assertTrue(hc1.equals(hc2));
          assertTrue(hc2.equals(hc1));
      }
      
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message