mirror of
https://github.com/apache/httpcomponents-client.git
synced 2025-02-27 21:29:12 +00:00
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:
parent
1c9e9c1e05
commit
2fd0728ca2
@ -89,8 +89,16 @@ public static void main(String[] args)throws Exception {
|
||||
|
||||
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 Socket connectSocket(
|
||||
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);
|
||||
|
@ -84,6 +84,13 @@ public PlainSocketFactory() {
|
||||
this.nameResolver = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.1
|
||||
*/
|
||||
public Socket createSocket(final HttpParams params) {
|
||||
return new Socket();
|
||||
}
|
||||
|
||||
public Socket createSocket() {
|
||||
return new Socket();
|
||||
}
|
||||
|
@ -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.
|
||||
|
@ -62,7 +62,7 @@ public Socket connectSocket(
|
||||
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();
|
||||
}
|
||||
|
||||
|
@ -34,6 +34,7 @@
|
||||
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(
|
||||
|
@ -334,6 +334,14 @@ private SSLSocketFactory() {
|
||||
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
|
||||
|
@ -133,7 +133,7 @@ public void openConnection(
|
||||
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 void openConnection(
|
||||
}
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -812,11 +812,11 @@ public Socket connectSocket(
|
||||
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 {
|
||||
|
@ -58,7 +58,7 @@ public String toString() {
|
||||
return "SocketFactoryMockup." + mockup_name;
|
||||
}
|
||||
|
||||
public Socket createSocket() {
|
||||
public Socket createSocket(final HttpParams params) {
|
||||
throw new UnsupportedOperationException("I'm a mockup!");
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user