You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by Laura Werner <la...@lwerner.org> on 2003/05/09 20:47:24 UTC

[PATCH] take 2: add set/getLocalAddress to HostConfiguration, HttpConnection

Hi,

I addressed Michael's concerns (thanks for the review!) in this new 
patch.  In total, the new patch:

- Adds public set/getLocalAddress methods to HostConfiguration and 
HttpConnection.
- HttpConnection uses the local address when opening connections.
- Modifies HostConfiguration.equals and hostEquals to compare the local 
address too.
- SimpleHttpConnectionManager uses the local address from the provided 
config.  I also cleaned up its getConnection method a bit.
- HttpClient.executeMethod uses the local address from its default 
HostConfiguration if the method's config doesn't specify one.

One question: I wasn't sure whether to include the localAddress 
comparison as part of HostConfiguration.hostEquals or to compare it 
separately.  In this patch it's part of hostEquals because it makes life 
easier for clients and seems to make at least some sense.

Feedback is welcome, as always...

-- Laura

Index: src/java/org/apache/commons/httpclient/HostConfiguration.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HostConfiguration.java,v
retrieving revision 1.10
diff -u -r1.10 HostConfiguration.java
--- src/java/org/apache/commons/httpclient/HostConfiguration.java    19 
Apr 2003 22:29:31 -0000    1.10
+++ src/java/org/apache/commons/httpclient/HostConfiguration.java    9 
May 2003 18:43:13 -0000
@@ -65,6 +65,8 @@
 
 import org.apache.commons.httpclient.protocol.Protocol;
 
+import java.net.InetAddress;
+
 /**
  *
  * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
@@ -98,6 +100,9 @@
     /** True if a proxy server has been set */
     private boolean proxySet;
    
+    /** The local address to use when creating the socket, or null to 
use the default */
+    private InetAddress localAddress;
+   
     /**
      * Constructor for HostConfiguration.
      */
@@ -112,7 +117,7 @@
         this.proxyHost = null;
         this.proxyPort = -1;
         this.proxySet = false;
-       
+        this.localAddress = null;
     }
    
     /**
@@ -134,6 +139,7 @@
             this.proxyHost = hostConfiguration.getProxyHost();
             this.proxyPort = hostConfiguration.getProxyPort();
             this.proxySet = hostConfiguration.isProxySet();
+            this.localAddress = hostConfiguration.getLocalAddress();
         }
        
     }
@@ -177,6 +183,15 @@
             if (!this.protocol.equals(connection.getProtocol())) {
                 return false;
             }
+            if (this.localAddress != null) {
+                if 
(!this.localAddress.equals(connection.getLocalAddress())) {
+                    return false;
+                }
+            } else {
+                if (connection.getLocalAddress() != null) {
+                    return false;
+                }
+            }
             return true;
         } else {
             return false;  
@@ -389,6 +404,25 @@
     }
 
     /**
+     * Set the local address to be used when creating connections.
+     * If this is unset, the default address will be used.
+     * This is useful for specifying the interface to use on 
multi-homed or clustered systems.
+     */
+    public synchronized void setLocalAddress(InetAddress localAddress) {
+        this.localAddress = localAddress;
+    }
+
+    /**
+     * Return the local address to be used when creating connections.
+     * If this is unset, the default address should be used.
+     *
+     * @return InetAddress the local address to be used when creating 
Sockets
+     */
+    public synchronized InetAddress getLocalAddress() {
+        return this.localAddress;
+    }
+   
+    /**
      * @see java.lang.Object#equals(java.lang.Object)
      */
     public synchronized boolean equals(Object o) {
@@ -433,6 +467,15 @@
             } else if (config.getProxyHost() != null) {
                 return false;
             }           
+            if (localAddress != null) {
+                if (!localAddress.equals(config.getLocalAddress())) {
+                    return false;
+                }
+            } else {
+                if (config.getLocalAddress() != null) {
+                    return false;
+                }
+            }
 
             // everything matches
             return true;
Index: src/java/org/apache/commons/httpclient/HttpClient.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpClient.java,v
retrieving revision 1.75
diff -u -r1.75 HttpClient.java
--- src/java/org/apache/commons/httpclient/HttpClient.java    8 May 2003 
18:39:07 -0000    1.75
+++ src/java/org/apache/commons/httpclient/HttpClient.java    9 May 2003 
18:43:13 -0000
@@ -594,6 +594,11 @@
                     defaultHostConfiguration.getProxyPort()
                 );  
             }
+            if (methodConfiguration.getLocalAddress() == null
+                && defaultHostConfiguration.getLocalAddress() != null) {
+                   
+                
methodConfiguration.setLocalAddress(defaultHostConfiguration.getLocalAddress());
+            }
         }
        
         HttpConnectionManager connmanager = this.httpConnectionManager;
Index: src/java/org/apache/commons/httpclient/HttpConnection.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConnection.java,v
retrieving revision 1.65
diff -u -r1.65 HttpConnection.java
--- src/java/org/apache/commons/httpclient/HttpConnection.java    8 May 
2003 17:33:51 -0000    1.65
+++ src/java/org/apache/commons/httpclient/HttpConnection.java    9 May 
2003 18:43:13 -0000
@@ -70,6 +70,7 @@
 import java.io.OutputStream;
 import java.io.PushbackInputStream;
 import java.lang.reflect.Method;
+import java.net.InetAddress;
 import java.net.Socket;
 import java.net.SocketException;
 
@@ -217,6 +218,7 @@
              hostConfiguration.getVirtualHost(),
              hostConfiguration.getPort(),
              hostConfiguration.getProtocol());
+        this.localAddress = hostConfiguration.getLocalAddress();
     }
 
     /**
@@ -446,6 +448,25 @@
     }
 
     /**
+     * Return the local address used when creating the connection.
+     * If <tt>null</tt>, the default address is used.
+     *
+     * @return InetAddress the local address to be used when creating 
Sockets
+     */
+    public InetAddress getLocalAddress() {
+        return this.localAddress;
+    }
+   
+    /**
+     * Set the local address used when creating the connection.
+     * If unset or <tt>null</tt>, the default address is used.
+     */
+    public void setLocalAddress(InetAddress localAddress) {
+        this.localAddress = localAddress;
+    }
+   
+
+    /**
      * Return <tt>true</tt> if I am connected,
      * <tt>false</tt> otherwise.
      *
@@ -636,11 +657,19 @@
                             : protocolInUse.getSocketFactory());
 
                 if (connectTimeout == 0) {
-                    socket = socketFactory.createSocket(host, port);
+                    if (localAddress != null) {
+                        socket = socketFactory.createSocket(host, port, 
localAddress, 0);
+                    } else {
+                        socket = socketFactory.createSocket(host, port);
+                    }
                 } else {
                     SocketTask task = new SocketTask() {
                         public void doit() throws IOException {
-                            setSocket(socketFactory.createSocket(host, 
port));
+                            if (localAddress != null) {
+                                
setSocket(socketFactory.createSocket(host, port, localAddress, 0));
+                            } else {
+                                
setSocket(socketFactory.createSocket(host, port));
+                            }
                         }
                     };
                     TimeoutController.execute(task, connectTimeout);
@@ -1394,4 +1423,7 @@
    
     /** the connection manager that created this connection or null */
     private HttpConnectionManager httpConnectionManager;
+   
+    /** The local interface on which the connection is created, or null 
for the default */
+    private InetAddress localAddress;
 }
Index: 
src/java/org/apache/commons/httpclient/SimpleHttpConnectionManager.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/SimpleHttpConnectionManager.java,v
retrieving revision 1.11
diff -u -r1.11 SimpleHttpConnectionManager.java
--- 
src/java/org/apache/commons/httpclient/SimpleHttpConnectionManager.java    
9 Apr 2003 18:37:59 -0000    1.11
+++ 
src/java/org/apache/commons/httpclient/SimpleHttpConnectionManager.java    
9 May 2003 18:43:13 -0000
@@ -66,9 +66,6 @@
 import java.io.IOException;
 import java.io.InputStream;
 
-import org.apache.commons.httpclient.protocol.Protocol;
-
-
 /**
  * A connection manager that provides access to a single 
HttpConnection.  This
  * manager makes no attempt to provide exclusive access to the contained
@@ -106,30 +103,8 @@
     public HttpConnection getConnection(
         HostConfiguration hostConfiguration, long timeout) {
 
-        Protocol protocol = hostConfiguration.getProtocol();
-        String host = hostConfiguration.getHost();
-        String virtualHost = hostConfiguration.getVirtualHost();
-        int port = hostConfiguration.getPort();
-
         if (httpConnection == null) {
-
-            if (hostConfiguration.isProxySet()) {
-                httpConnection = new HttpConnection(
-                    hostConfiguration.getProxyHost(),
-                    hostConfiguration.getProxyPort(),
-                    host,
-                    virtualHost,
-                    port,
-                    protocol
-                );
-            } else {
-                httpConnection = new HttpConnection(
-                    host,
-                    virtualHost,
-                    port,
-                    protocol);
-            }
-
+            httpConnection = new HttpConnection(hostConfiguration);
         } else {
 
             // make sure the host and proxy are correct for this connection
@@ -141,10 +116,11 @@
                     httpConnection.close();
                 }
 
-                httpConnection.setHost(host);
-                httpConnection.setVirtualHost(virtualHost);
-                httpConnection.setPort(port);
-                httpConnection.setProtocol(protocol);
+                httpConnection.setHost(hostConfiguration.getHost());
+                
httpConnection.setVirtualHost(hostConfiguration.getVirtualHost());
+                httpConnection.setPort(hostConfiguration.getPort());
+                
httpConnection.setProtocol(hostConfiguration.getProtocol());
+                
httpConnection.setLocalAddress(hostConfiguration.getLocalAddress());
 
                 
httpConnection.setProxyHost(hostConfiguration.getProxyHost());
                 
httpConnection.setProxyPort(hostConfiguration.getProxyPort());



Re: [PATCH] take 2: add set/getLocalAddress to HostConfiguration, HttpConnection

Posted by Laura Werner <la...@lwerner.org>.
Michael Becke wrote:

> Could you attach this patch to a bug report.  It is being mutilated 
> by  my email client.

I think it was mangled by my client when I sent it; I couldn't use it on 
my home machine and had to come back in to work to create the bug 
report.  The patch is now attached to this bug:

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=19827

-- Laura



Re: [PATCH] take 2: add set/getLocalAddress to HostConfiguration, HttpConnection

Posted by Michael Becke <be...@u.washington.edu>.
Laura,

Could you attach this patch to a bug report.  It is being mutilated by  
my email client.

Thanks,

Mike

On Friday, May 9, 2003, at 02:47 PM, Laura Werner wrote:

> Hi,
>
> I addressed Michael's concerns (thanks for the review!) in this new  
> patch.  In total, the new patch:
>
> - Adds public set/getLocalAddress methods to HostConfiguration and  
> HttpConnection.
> - HttpConnection uses the local address when opening connections.
> - Modifies HostConfiguration.equals and hostEquals to compare the  
> local address too.
> - SimpleHttpConnectionManager uses the local address from the provided  
> config.  I also cleaned up its getConnection method a bit.
> - HttpClient.executeMethod uses the local address from its default  
> HostConfiguration if the method's config doesn't specify one.
>
> One question: I wasn't sure whether to include the localAddress  
> comparison as part of HostConfiguration.hostEquals or to compare it  
> separately.  In this patch it's part of hostEquals because it makes  
> life easier for clients and seems to make at least some sense.
>
> Feedback is welcome, as always...
>
> -- Laura
>
> Index: src/java/org/apache/commons/httpclient/HostConfiguration.java
> ===================================================================
> RCS file:  
> /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/ 
> commons/httpclient/HostConfiguration.java,v
> retrieving revision 1.10
> diff -u -r1.10 HostConfiguration.java
> --- src/java/org/apache/commons/httpclient/HostConfiguration.java     
> 19 Apr 2003 22:29:31 -0000    1.10
> +++ src/java/org/apache/commons/httpclient/HostConfiguration.java    9  
> May 2003 18:43:13 -0000
> @@ -65,6 +65,8 @@
> import org.apache.commons.httpclient.protocol.Protocol;
> +import java.net.InetAddress;
> +
> /**
>  *
>  * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
> @@ -98,6 +100,9 @@
>     /** True if a proxy server has been set */
>     private boolean proxySet;
>    +    /** The local address to use when creating the socket, or null  
> to use the default */
> +    private InetAddress localAddress;
> +       /**
>      * Constructor for HostConfiguration.
>      */
> @@ -112,7 +117,7 @@
>         this.proxyHost = null;
>         this.proxyPort = -1;
>         this.proxySet = false;
> -       +        this.localAddress = null;
>     }
>        /**
> @@ -134,6 +139,7 @@
>             this.proxyHost = hostConfiguration.getProxyHost();
>             this.proxyPort = hostConfiguration.getProxyPort();
>             this.proxySet = hostConfiguration.isProxySet();
> +            this.localAddress = hostConfiguration.getLocalAddress();
>         }
>            }
> @@ -177,6 +183,15 @@
>             if (!this.protocol.equals(connection.getProtocol())) {
>                 return false;
>             }
> +            if (this.localAddress != null) {
> +                if  
> (!this.localAddress.equals(connection.getLocalAddress())) {
> +                    return false;
> +                }
> +            } else {
> +                if (connection.getLocalAddress() != null) {
> +                    return false;
> +                }
> +            }
>             return true;
>         } else {
>             return false;  @@ -389,6 +404,25 @@
>     }
>     /**
> +     * Set the local address to be used when creating connections.
> +     * If this is unset, the default address will be used.
> +     * This is useful for specifying the interface to use on  
> multi-homed or clustered systems.
> +     */
> +    public synchronized void setLocalAddress(InetAddress  
> localAddress) {
> +        this.localAddress = localAddress;
> +    }
> +
> +    /**
> +     * Return the local address to be used when creating connections.
> +     * If this is unset, the default address should be used.
> +     *
> +     * @return InetAddress the local address to be used when creating  
> Sockets
> +     */
> +    public synchronized InetAddress getLocalAddress() {
> +        return this.localAddress;
> +    }
> +   +    /**
>      * @see java.lang.Object#equals(java.lang.Object)
>      */
>     public synchronized boolean equals(Object o) {
> @@ -433,6 +467,15 @@
>             } else if (config.getProxyHost() != null) {
>                 return false;
>             }           +            if (localAddress != null) {
> +                if (!localAddress.equals(config.getLocalAddress())) {
> +                    return false;
> +                }
> +            } else {
> +                if (config.getLocalAddress() != null) {
> +                    return false;
> +                }
> +            }
>             // everything matches
>             return true;
> Index: src/java/org/apache/commons/httpclient/HttpClient.java
> ===================================================================
> RCS file:  
> /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/ 
> commons/httpclient/HttpClient.java,v
> retrieving revision 1.75
> diff -u -r1.75 HttpClient.java
> --- src/java/org/apache/commons/httpclient/HttpClient.java    8 May  
> 2003 18:39:07 -0000    1.75
> +++ src/java/org/apache/commons/httpclient/HttpClient.java    9 May  
> 2003 18:43:13 -0000
> @@ -594,6 +594,11 @@
>                     defaultHostConfiguration.getProxyPort()
>                 );              }
> +            if (methodConfiguration.getLocalAddress() == null
> +                && defaultHostConfiguration.getLocalAddress() !=  
> null) {
> +                   +                 
> methodConfiguration.setLocalAddress(defaultHostConfiguration.getLocalAd 
> dress());
> +            }
>         }
>                HttpConnectionManager connmanager =  
> this.httpConnectionManager;
> Index: src/java/org/apache/commons/httpclient/HttpConnection.java
> ===================================================================
> RCS file:  
> /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/ 
> commons/httpclient/HttpConnection.java,v
> retrieving revision 1.65
> diff -u -r1.65 HttpConnection.java
> --- src/java/org/apache/commons/httpclient/HttpConnection.java    8  
> May 2003 17:33:51 -0000    1.65
> +++ src/java/org/apache/commons/httpclient/HttpConnection.java    9  
> May 2003 18:43:13 -0000
> @@ -70,6 +70,7 @@
> import java.io.OutputStream;
> import java.io.PushbackInputStream;
> import java.lang.reflect.Method;
> +import java.net.InetAddress;
> import java.net.Socket;
> import java.net.SocketException;
> @@ -217,6 +218,7 @@
>              hostConfiguration.getVirtualHost(),
>              hostConfiguration.getPort(),
>              hostConfiguration.getProtocol());
> +        this.localAddress = hostConfiguration.getLocalAddress();
>     }
>     /**
> @@ -446,6 +448,25 @@
>     }
>     /**
> +     * Return the local address used when creating the connection.
> +     * If <tt>null</tt>, the default address is used.
> +     *
> +     * @return InetAddress the local address to be used when creating  
> Sockets
> +     */
> +    public InetAddress getLocalAddress() {
> +        return this.localAddress;
> +    }
> +   +    /**
> +     * Set the local address used when creating the connection.
> +     * If unset or <tt>null</tt>, the default address is used.
> +     */
> +    public void setLocalAddress(InetAddress localAddress) {
> +        this.localAddress = localAddress;
> +    }
> +   +
> +    /**
>      * Return <tt>true</tt> if I am connected,
>      * <tt>false</tt> otherwise.
>      *
> @@ -636,11 +657,19 @@
>                             : protocolInUse.getSocketFactory());
>                 if (connectTimeout == 0) {
> -                    socket = socketFactory.createSocket(host, port);
> +                    if (localAddress != null) {
> +                        socket = socketFactory.createSocket(host,  
> port, localAddress, 0);
> +                    } else {
> +                        socket = socketFactory.createSocket(host,  
> port);
> +                    }
>                 } else {
>                     SocketTask task = new SocketTask() {
>                         public void doit() throws IOException {
> -                             
> setSocket(socketFactory.createSocket(host, port));
> +                            if (localAddress != null) {
> +                                 
> setSocket(socketFactory.createSocket(host, port, localAddress, 0));
> +                            } else {
> +                                 
> setSocket(socketFactory.createSocket(host, port));
> +                            }
>                         }
>                     };
>                     TimeoutController.execute(task, connectTimeout);
> @@ -1394,4 +1423,7 @@
>        /** the connection manager that created this connection or null  
> */
>     private HttpConnectionManager httpConnectionManager;
> +   +    /** The local interface on which the connection is created,  
> or null for the default */
> +    private InetAddress localAddress;
> }
> Index:  
> src/java/org/apache/commons/httpclient/> SimpleHttpConnectionManager.java
> ===================================================================
> RCS file:  
> /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/ 
> commons/httpclient/SimpleHttpConnectionManager.java,v
> retrieving revision 1.11
> diff -u -r1.11 SimpleHttpConnectionManager.java
> ---  
> src/java/org/apache/commons/httpclient/ 
> SimpleHttpConnectionManager.java    9 Apr 2003 18:37:59 -0000    1.11
> +++  
> src/java/org/apache/commons/httpclient/ 
> SimpleHttpConnectionManager.java    9 May 2003 18:43:13 -0000
> @@ -66,9 +66,6 @@
> import java.io.IOException;
> import java.io.InputStream;
> -import org.apache.commons.httpclient.protocol.Protocol;
> -
> -
> /**
>  * A connection manager that provides access to a single  
> HttpConnection.  This
>  * manager makes no attempt to provide exclusive access to the  
> contained
> @@ -106,30 +103,8 @@
>     public HttpConnection getConnection(
>         HostConfiguration hostConfiguration, long timeout) {
> -        Protocol protocol = hostConfiguration.getProtocol();
> -        String host = hostConfiguration.getHost();
> -        String virtualHost = hostConfiguration.getVirtualHost();
> -        int port = hostConfiguration.getPort();
> -
>         if (httpConnection == null) {
> -
> -            if (hostConfiguration.isProxySet()) {
> -                httpConnection = new HttpConnection(
> -                    hostConfiguration.getProxyHost(),
> -                    hostConfiguration.getProxyPort(),
> -                    host,
> -                    virtualHost,
> -                    port,
> -                    protocol
> -                );
> -            } else {
> -                httpConnection = new HttpConnection(
> -                    host,
> -                    virtualHost,
> -                    port,
> -                    protocol);
> -            }
> -
> +            httpConnection = new HttpConnection(hostConfiguration);
>         } else {
>             // make sure the host and proxy are correct for this  
> connection
> @@ -141,10 +116,11 @@
>                     httpConnection.close();
>                 }
> -                httpConnection.setHost(host);
> -                httpConnection.setVirtualHost(virtualHost);
> -                httpConnection.setPort(port);
> -                httpConnection.setProtocol(protocol);
> +                httpConnection.setHost(hostConfiguration.getHost());
> +                 
> httpConnection.setVirtualHost(hostConfiguration.getVirtualHost());
> +                httpConnection.setPort(hostConfiguration.getPort());
> +                 
> httpConnection.setProtocol(hostConfiguration.getProtocol());
> +                 
> httpConnection.setLocalAddress(hostConfiguration.getLocalAddress());
>                  
> httpConnection.setProxyHost(hostConfiguration.getProxyHost());
>                  
> httpConnection.setProxyPort(hostConfiguration.getProxyPort());
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:  
> commons-httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:  
> commons-httpclient-dev-help@jakarta.apache.org
>