Skip to content

Commit

Permalink
HTTPCLIENT-898: Improved multihome support
Browse files Browse the repository at this point in the history
Contributed by Steinar H. Gunderson <sesse at google.com>


git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x@930190 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
ok2c committed Apr 2, 2010
1 parent 293c5d8 commit 82d6013
Showing 1 changed file with 51 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HttpContext;

import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.conn.OperatedClientConnection;
import org.apache.http.conn.ClientConnectionOperator;
import org.apache.http.conn.scheme.LayeredSocketFactory;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.scheme.SocketFactory;
Expand Down Expand Up @@ -102,8 +104,6 @@ public void openConnection(OperatedClientConnection conn,
throw new IllegalArgumentException
("Target host must not be null.");
}
// local address may be null
//@@@ is context allowed to be null?
if (params == null) {
throw new IllegalArgumentException
("Parameters must not be null.");
Expand All @@ -113,21 +113,57 @@ public void openConnection(OperatedClientConnection conn,
("Connection must not be open.");
}

final Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
final SocketFactory sf = schm.getSocketFactory();

Socket sock = sf.createSocket();
conn.opening(sock, target);
SocketFactory sf = null;
LayeredSocketFactory layeredsf = null;

Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
sf = schm.getSocketFactory();
if (sf instanceof LayeredSocketFactory) {
layeredsf = (LayeredSocketFactory) sf;
sf = PlainSocketFactory.getSocketFactory();
}

try {
sock = sf.connectSocket(sock, target.getHostName(),
schm.resolvePort(target.getPort()),
local, 0, params);
} catch (ConnectException ex) {
throw new HttpHostConnectException(target, ex);
InetAddress[] addresses = InetAddress.getAllByName(target.getHostName());
for (int i = 0; i < addresses.length; i++) {
InetAddress address = addresses[i];
boolean last = i == addresses.length;
Socket sock = sf.createSocket();
conn.opening(sock, target);
try {
Socket connsock = sf.connectSocket(
sock,
address.getHostAddress(),
schm.resolvePort(target.getPort()),
local, 0, params);
if (sock != connsock) {
sock = connsock;
conn.opening(sock, target);
}
if (layeredsf != null) {
connsock = layeredsf.createSocket(
sock,
address.getHostAddress(),
schm.resolvePort(target.getPort()),
true);
if (sock != connsock) {
sock = connsock;
conn.opening(sock, target);
}
sf = layeredsf;
}
prepareSocket(sock, context, params);
conn.openCompleted(sf.isSecure(sock), params);
break;
} catch (ConnectException ex) {
if (last) {
throw new HttpHostConnectException(target, ex);
}
} catch (ConnectTimeoutException ex) {
if (last) {
throw ex;
}
}
}
prepareSocket(sock, context, params);
conn.openCompleted(sf.isSecure(sock), params);
}

public void updateSecureConnection(OperatedClientConnection conn,
Expand Down

0 comments on commit 82d6013

Please sign in to comment.