Make it easier to support connections via SOCKS proxies

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@937235 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2010-04-23 10:30:24 +00:00
parent 1c9e9c1e05
commit 2fd0728ca2
9 changed files with 42 additions and 16 deletions

View File

@ -89,8 +89,16 @@ public class ClientExecuteSOCKS {
static class MySchemeSocketFactory implements SchemeSocketFactory {
public Socket createSocket() throws IOException {
return new Socket();
public Socket createSocket(final HttpParams params) throws IOException {
if (params == null) {
throw new IllegalArgumentException("HTTP parameters may not be null");
}
String proxyHost = (String) params.getParameter("socks.host");
Integer proxyPort = (Integer) params.getParameter("socks.port");
InetSocketAddress socksaddr = new InetSocketAddress(proxyHost, proxyPort);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr);
return new Socket(proxy);
}
public Socket connectSocket(
@ -105,12 +113,12 @@ public class ClientExecuteSOCKS {
if (params == null) {
throw new IllegalArgumentException("HTTP parameters may not be null");
}
String proxyHost = (String) params.getParameter("socks.host");
Integer proxyPort = (Integer) params.getParameter("socks.port");
InetSocketAddress socksaddr = new InetSocketAddress(proxyHost, proxyPort);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr);
Socket sock = new Socket(proxy);
Socket sock;
if (socket != null) {
sock = socket;
} else {
sock = createSocket(params);
}
if (localAddress != null) {
sock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
sock.bind(localAddress);

View File

@ -84,6 +84,13 @@ public final class PlainSocketFactory implements SocketFactory, SchemeSocketFact
this.nameResolver = null;
}
/**
* @since 4.1
*/
public Socket createSocket(final HttpParams params) {
return new Socket();
}
public Socket createSocket() {
return new Socket();
}

View File

@ -47,11 +47,12 @@ public interface SchemeSocketFactory {
* Creates a new, unconnected socket. The socket should subsequently be passed to
* {@link #connectSocket(Socket, InetSocketAddress, InetSocketAddress, HttpParams)}.
*
* @param params additional {@link HttpParams parameters}
* @return a new socket
*
* @throws IOException if an I/O error occurs while creating the socket
*/
Socket createSocket() throws IOException;
Socket createSocket(HttpParams params) throws IOException;
/**
* Connects a socket to the target host with the given remote address.

View File

@ -62,7 +62,7 @@ class SchemeSocketFactoryAdaptor implements SchemeSocketFactory {
return this.factory.connectSocket(sock, host, port, local, localPort, params);
}
public Socket createSocket() throws IOException {
public Socket createSocket(final HttpParams params) throws IOException {
return this.factory.createSocket();
}

View File

@ -34,6 +34,7 @@ import java.net.Socket;
import java.net.UnknownHostException;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
@Deprecated
@ -47,7 +48,8 @@ class SocketFactoryAdaptor implements SocketFactory {
}
public Socket createSocket() throws IOException {
return this.factory.createSocket();
HttpParams params = new BasicHttpParams();
return this.factory.createSocket(params);
}
public Socket connectSocket(

View File

@ -334,6 +334,14 @@ public class SSLSocketFactory implements LayeredSchemeSocketFactory, LayeredSock
this.nameResolver = null;
}
/**
* @since 4.1
*/
public Socket createSocket(final HttpParams params) throws IOException {
// the cast makes sure that the factory is working as expected
return (SSLSocket) this.socketfactory.createSocket();
}
@SuppressWarnings("cast")
public Socket createSocket() throws IOException {
// the cast makes sure that the factory is working as expected

View File

@ -133,7 +133,7 @@ public class DefaultClientConnectionOperator implements ClientConnectionOperator
InetAddress address = addresses[i];
boolean last = i == addresses.length - 1;
Socket sock = sf.createSocket();
Socket sock = sf.createSocket(params);
conn.opening(sock, target);
InetSocketAddress remoteAddress = new InetSocketAddress(address, port);
@ -164,7 +164,7 @@ public class DefaultClientConnectionOperator implements ClientConnectionOperator
}
if (this.log.isDebugEnabled()) {
this.log.debug("Connect to " + remoteAddress + " timed out. " +
"Connection will be retried using another IP address");
"Connection will be retried using another IP address");
}
}
}

View File

@ -812,11 +812,11 @@ public class TestTSCCMWithServer extends ServerTestBase {
return socket;
}
public Socket createSocket() throws IOException {
public Socket createSocket(final HttpParams params) throws IOException {
if(waitPolicy == WaitPolicy.BEFORE_CREATE)
latch();
return delegate.createSocket();
return delegate.createSocket(params);
}
public boolean isSecure(Socket sock) throws IllegalArgumentException {

View File

@ -58,7 +58,7 @@ public class SocketFactoryMockup implements SchemeSocketFactory {
return "SocketFactoryMockup." + mockup_name;
}
public Socket createSocket() {
public Socket createSocket(final HttpParams params) {
throw new UnsupportedOperationException("I'm a mockup!");
}