You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2019/01/31 08:46:21 UTC

[httpcomponents-core] branch socks-proxy created (now 5b88f39)

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a change to branch socks-proxy
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git.


      at 5b88f39  Added SOCKS proxy support to classic (blocking) HTTP requester

This branch includes the following new commits:

     new 5b88f39  Added SOCKS proxy support to classic (blocking) HTTP requester

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[httpcomponents-core] 01/01: Added SOCKS proxy support to classic (blocking) HTTP requester

Posted by ol...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch socks-proxy
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git

commit 5b88f39d2aa581d9efd484a6617fb47553abcbaa
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Thu Jan 31 09:43:53 2019 +0100

    Added SOCKS proxy support to classic (blocking) HTTP requester
---
 .../core5/http/impl/bootstrap/HttpRequester.java   | 12 +++++++---
 .../org/apache/hc/core5/http/io/SocketConfig.java  | 27 +++++++++++++++++++---
 2 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java
index 71a188b..58db00c 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java
@@ -31,6 +31,7 @@ import java.io.InputStream;
 import java.io.InterruptedIOException;
 import java.io.OutputStream;
 import java.net.InetSocketAddress;
+import java.net.Proxy;
 import java.net.Socket;
 import java.util.Set;
 import java.util.concurrent.ExecutionException;
@@ -52,7 +53,6 @@ import org.apache.hc.core5.http.HttpHost;
 import org.apache.hc.core5.http.URIScheme;
 import org.apache.hc.core5.http.config.CharCodingConfig;
 import org.apache.hc.core5.http.config.H1Config;
-import org.apache.hc.core5.http.io.SocketConfig;
 import org.apache.hc.core5.http.impl.DefaultAddressResolver;
 import org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnectionFactory;
 import org.apache.hc.core5.http.impl.io.HttpRequestExecutor;
@@ -62,13 +62,14 @@ import org.apache.hc.core5.http.io.HttpClientConnection;
 import org.apache.hc.core5.http.io.HttpClientResponseHandler;
 import org.apache.hc.core5.http.io.HttpConnectionFactory;
 import org.apache.hc.core5.http.io.HttpResponseInformationCallback;
+import org.apache.hc.core5.http.io.SocketConfig;
 import org.apache.hc.core5.http.io.entity.EntityUtils;
 import org.apache.hc.core5.http.io.entity.HttpEntityWrapper;
 import org.apache.hc.core5.http.protocol.HttpContext;
 import org.apache.hc.core5.http.protocol.HttpProcessor;
-import org.apache.hc.core5.io.ModalCloseable;
 import org.apache.hc.core5.io.CloseMode;
 import org.apache.hc.core5.io.Closer;
+import org.apache.hc.core5.io.ModalCloseable;
 import org.apache.hc.core5.net.URIAuthority;
 import org.apache.hc.core5.pool.ConnPoolControl;
 import org.apache.hc.core5.pool.ManagedConnPool;
@@ -226,7 +227,12 @@ public class HttpRequester implements ConnPoolControl<HttpHost>, ModalCloseable
     }
 
     private Socket createSocket(final HttpHost targetHost) throws IOException {
-        final Socket sock = new Socket();
+        final Socket sock;
+        if (socketConfig.getSocksProxyAddress() != null) {
+            sock = new Socket(new Proxy(Proxy.Type.SOCKS, socketConfig.getSocksProxyAddress()));
+        } else {
+            sock = new Socket();
+        }
         sock.setSoTimeout(socketConfig.getSoTimeout().toMillisIntBound());
         sock.setReuseAddress(socketConfig.isSoReuseAddress());
         sock.setTcpNoDelay(socketConfig.isTcpNoDelay());
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/io/SocketConfig.java b/httpcore5/src/main/java/org/apache/hc/core5/http/io/SocketConfig.java
index 8afa462..9bb0f3c 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/io/SocketConfig.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/io/SocketConfig.java
@@ -27,6 +27,7 @@
 
 package org.apache.hc.core5.http.io;
 
+import java.net.SocketAddress;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.hc.core5.annotation.Contract;
@@ -53,6 +54,7 @@ public class SocketConfig {
     private final int sndBufSize;
     private final int rcvBufSize;
     private final int backlogSize;
+    private final SocketAddress socksProxyAddress;
 
     SocketConfig(
             final Timeout soTimeout,
@@ -62,7 +64,8 @@ public class SocketConfig {
             final boolean tcpNoDelay,
             final int sndBufSize,
             final int rcvBufSize,
-            final int backlogSize) {
+            final int backlogSize,
+            final SocketAddress socksProxyAddress) {
         super();
         this.soTimeout = soTimeout;
         this.soReuseAddress = soReuseAddress;
@@ -72,6 +75,7 @@ public class SocketConfig {
         this.sndBufSize = sndBufSize;
         this.rcvBufSize = rcvBufSize;
         this.backlogSize = backlogSize;
+        this.socksProxyAddress = socksProxyAddress;
     }
 
     /**
@@ -186,6 +190,13 @@ public class SocketConfig {
         return backlogSize;
     }
 
+    /**
+     * The address of the SOCKS proxy to use.
+     */
+    public SocketAddress getSocksProxyAddress() {
+        return this.socksProxyAddress;
+    }
+
     @Override
     public String toString() {
         final StringBuilder builder = new StringBuilder();
@@ -197,6 +208,7 @@ public class SocketConfig {
                 .append(", sndBufSize=").append(this.sndBufSize)
                 .append(", rcvBufSize=").append(this.rcvBufSize)
                 .append(", backlogSize=").append(this.backlogSize)
+                .append(", socksProxyAddress=").append(this.socksProxyAddress)
                 .append("]");
         return builder.toString();
     }
@@ -215,7 +227,8 @@ public class SocketConfig {
             .setTcpNoDelay(config.isTcpNoDelay())
             .setSndBufSize(config.getSndBufSize())
             .setRcvBufSize(config.getRcvBufSize())
-            .setBacklogSize(config.getBacklogSize());
+            .setBacklogSize(config.getBacklogSize())
+            .setSocksProxyAddress(config.getSocksProxyAddress());
     }
 
     public static class Builder {
@@ -228,6 +241,7 @@ public class SocketConfig {
         private int sndBufSize;
         private int rcvBufSize;
         private int backlogSize;
+        private SocketAddress socksProxyAddress;
 
         Builder() {
             this.soTimeout = Timeout.ZERO_MILLISECONDS;
@@ -238,6 +252,7 @@ public class SocketConfig {
             this.sndBufSize = 0;
             this.rcvBufSize = 0;
             this.backlogSize = 0;
+            this.socksProxyAddress = null;
         }
 
         public Builder setSoTimeout(final int soTimeout, final TimeUnit timeUnit) {
@@ -299,12 +314,18 @@ public class SocketConfig {
             return this;
         }
 
+        public Builder setSocksProxyAddress(final SocketAddress socksProxyAddress) {
+            this.socksProxyAddress = socksProxyAddress;
+            return this;
+        }
+
         public SocketConfig build() {
             return new SocketConfig(
                     Timeout.defaultsToDisabled(soTimeout),
                     soReuseAddress,
                     soLinger != null ? soLinger : TimeValue.NEG_ONE_SECONDS,
-                    soKeepAlive, tcpNoDelay, sndBufSize, rcvBufSize, backlogSize);
+                    soKeepAlive, tcpNoDelay, sndBufSize, rcvBufSize, backlogSize,
+                    socksProxyAddress);
         }
 
     }