You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by mb...@apache.org on 2003/09/17 05:53:25 UTC

cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient HttpMethodDirector.java

mbecke      2003/09/16 20:53:25

  Modified:    httpclient/src/test/org/apache/commons/httpclient
                        TestHttpConnection.java
                        NoHostHttpConnectionManager.java
               httpclient/src/java/org/apache/commons/httpclient
                        HttpMethodDirector.java
  Log:
  Fixes connection release when a timeout occurs in HttpConnection.open().
  PR: 23137
  Submitted by: Michael Becke
  Reviewed by: Oleg Kalnichevski
  
  Revision  Changes    Path
  1.10      +31 -0     jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttpConnection.java
  
  Index: TestHttpConnection.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttpConnection.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- TestHttpConnection.java	11 Sep 2003 20:08:33 -0000	1.9
  +++ TestHttpConnection.java	17 Sep 2003 03:53:25 -0000	1.10
  @@ -72,6 +72,7 @@
   import junit.framework.Test;
   import junit.framework.TestSuite;
   
  +import org.apache.commons.httpclient.methods.GetMethod;
   import org.apache.commons.httpclient.protocol.Protocol;
   import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
   
  @@ -111,6 +112,36 @@
           conn.close();
           assertTrue( ! conn.isOpen() );
       }
  +
  +    public void testConnTimeoutRelease() {
  +
  +        // create a custom protocol that will delay for 500 milliseconds
  +        Protocol testProtocol = new Protocol(
  +            "timeout",
  +            new DelayedProtocolSocketFactory(
  +                500, 
  +                Protocol.getProtocol("http").getSocketFactory()
  +            ),
  +            getPort()
  +        );
  +
  +        NoHostHttpConnectionManager connectionManager = new NoHostHttpConnectionManager();
  +        connectionManager.setConnection(new HttpConnection(getHost(), getPort(), testProtocol));
  +        HttpClient client = createHttpClient(connectionManager);
  +        client.getHostConfiguration().setHost(getHost(), getPort(), testProtocol);
  +        client.setConnectionTimeout(1);
  +        
  +        try {
  +            GetMethod get = new GetMethod();
  +            client.executeMethod(get);
  +            fail("Should have timed out");
  +        } catch(IOException e) {
  +            /* should fail */
  +            assertTrue(e instanceof HttpConnection.ConnectionTimeoutException);
  +            assertTrue(connectionManager.isConnectionReleased());
  +        }
  +    }
  +
   
       public void testConnTimeout() {
   
  
  
  
  1.3       +12 -5     jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/NoHostHttpConnectionManager.java
  
  Index: NoHostHttpConnectionManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/NoHostHttpConnectionManager.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- NoHostHttpConnectionManager.java	4 Sep 2003 02:12:13 -0000	1.2
  +++ NoHostHttpConnectionManager.java	17 Sep 2003 03:53:25 -0000	1.3
  @@ -10,20 +10,25 @@
   import java.io.InputStream;
   
   /**
  - * @author mbecke
  - *
  - * To change the template for this generated type comment go to
  - * Window>Preferences>Java>Code Generation>Code and Comments
    */
   public class NoHostHttpConnectionManager implements HttpConnectionManager {
   
       private HttpConnection connection;
  +    
  +    private boolean connectionReleased = false;
   
       public NoHostHttpConnectionManager() { 
           setConnection(new SimpleHttpConnection());
       }
   
       /**
  +     * @return
  +     */
  +    public boolean isConnectionReleased() {
  +        return connectionReleased;
  +    }
  +    
  +    /**
        * @param connection
        */
       public void setConnection(HttpConnection connection) {
  @@ -54,6 +59,7 @@
               finishLastResponse(connection);
           }
           
  +        connectionReleased = false;
           return connection;
       }
   
  @@ -73,7 +79,8 @@
           if (conn != connection) {
               throw new IllegalStateException("Unexpected close on a different connection.");
           }
  -
  +        
  +        connectionReleased = true;
           finishLastResponse(connection);
       }
   
  
  
  
  1.4       +6 -6      jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodDirector.java
  
  Index: HttpMethodDirector.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodDirector.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- HttpMethodDirector.java	11 Sep 2003 20:08:33 -0000	1.3
  +++ HttpMethodDirector.java	17 Sep 2003 03:53:25 -0000	1.4
  @@ -166,10 +166,10 @@
               // responseStream == null), as subclasses, might reset the stream,
               // for example, reading the entire response into a file and then
               // setting the file as the stream.
  -            if (method.getResponseBodyAsStream() == null) {
  -                method.releaseConnection();
  -            } else if (releaseConnection && connection != null) {
  +            if (releaseConnection && connection != null) {
                   connection.releaseConnection();
  +            } else if (method.getResponseBodyAsStream() == null) {
  +                method.releaseConnection();
               }
           }