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 2017/04/14 11:14:10 UTC

svn commit: r1791353 - in /httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap: HttpRequester.java HttpServer.java RequesterBootstrap.java ServerBootstrap.java

Author: olegk
Date: Fri Apr 14 11:14:10 2017
New Revision: 1791353

URL: http://svn.apache.org/viewvc?rev=1791353&view=rev
Log:
Classic HttpRequester to initialize connection socket using SocketConfig

Modified:
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpServer.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequesterBootstrap.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/ServerBootstrap.java

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java?rev=1791353&r1=1791352&r2=1791353&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java Fri Apr 14 11:14:10 2017
@@ -30,10 +30,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InterruptedIOException;
 import java.io.OutputStream;
-import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.Socket;
-import java.net.SocketAddress;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeoutException;
@@ -48,6 +46,7 @@ import org.apache.hc.core5.http.Connecti
 import org.apache.hc.core5.http.HttpEntity;
 import org.apache.hc.core5.http.HttpException;
 import org.apache.hc.core5.http.HttpHost;
+import org.apache.hc.core5.http.config.SocketConfig;
 import org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnectionFactory;
 import org.apache.hc.core5.http.impl.io.HttpRequestExecutor;
 import org.apache.hc.core5.http.io.EofSensorInputStream;
@@ -75,6 +74,7 @@ public class HttpRequester implements Gr
     private final HttpRequestExecutor requestExecutor;
     private final HttpProcessor httpProcessor;
     private final ControlledConnPool<HttpHost, HttpClientConnection> connPool;
+    private final SocketConfig socketConfig;
     private final HttpConnectionFactory<? extends HttpClientConnection> connectFactory;
     private final SSLSocketFactory sslSocketFactory;
 
@@ -82,13 +82,15 @@ public class HttpRequester implements Gr
             final HttpRequestExecutor requestExecutor,
             final HttpProcessor httpProcessor,
             final ControlledConnPool<HttpHost, HttpClientConnection> connPool,
+            final SocketConfig socketConfig,
             final HttpConnectionFactory<? extends HttpClientConnection> connectFactory,
             final SSLSocketFactory sslSocketFactory) {
         this.requestExecutor = Args.notNull(requestExecutor, "Request executor");
         this.httpProcessor = Args.notNull(httpProcessor, "HTTP processor");
         this.connPool = Args.notNull(connPool, "Connection pool");
+        this.socketConfig = socketConfig != null ? socketConfig : SocketConfig.DEFAULT;
         this.connectFactory = connectFactory != null ? connectFactory : DefaultBHttpClientConnectionFactory.INSTANCE;
-        this.sslSocketFactory = sslSocketFactory;
+        this.sslSocketFactory = sslSocketFactory != null ? sslSocketFactory : (SSLSocketFactory) SSLSocketFactory.getDefault();
     }
 
     public ClassicHttpResponse execute(
@@ -138,30 +140,44 @@ public class HttpRequester implements Gr
         }
     }
 
-    private Socket createSocket(final HttpHost host) throws IOException {
-        final String scheme = host.getSchemeName();
-        if ("https".equalsIgnoreCase(scheme)) {
-            return (sslSocketFactory != null ? sslSocketFactory : SSLSocketFactory.getDefault()).createSocket();
-        } else {
-            return new Socket();
+    private Socket createSocket(final HttpHost targetHost) throws IOException {
+        final Socket sock = new Socket();
+        sock.setSoTimeout(socketConfig.getSoTimeout().toMillisIntBound());
+        sock.setReuseAddress(socketConfig.isSoReuseAddress());
+        sock.setTcpNoDelay(socketConfig.isTcpNoDelay());
+        sock.setKeepAlive(socketConfig.isSoKeepAlive());
+        if (socketConfig.getRcvBufSize() > 0) {
+            sock.setReceiveBufferSize(socketConfig.getRcvBufSize());
+        }
+        if (socketConfig.getSndBufSize() > 0) {
+            sock.setSendBufferSize(socketConfig.getSndBufSize());
+        }
+        final int linger = socketConfig.getSoLinger().toMillisIntBound();
+        if (linger >= 0) {
+            sock.setSoLinger(true, linger);
         }
-    }
 
-    private SocketAddress toEndpoint(final HttpHost host) {
-        int port = host.getPort();
+        final String scheme = targetHost.getSchemeName();
+        int port = targetHost.getPort();
         if (port < 0) {
-            final String scheme = host.getSchemeName();
             if ("http".equalsIgnoreCase(scheme)) {
                 port = 80;
             } else if ("https".equalsIgnoreCase(scheme)) {
                 port = 443;
             }
         }
-        final InetAddress address = host.getAddress();
-        if (address != null) {
-            return new InetSocketAddress(address, port);
+        final InetSocketAddress targetAddress;
+        if (targetHost.getAddress() != null) {
+            targetAddress = new InetSocketAddress(targetHost.getAddress(), port);
+        } else {
+            targetAddress = new InetSocketAddress(targetHost.getHostName(), port);
+        }
+        sock.connect(targetAddress, socketConfig.getSoTimeout().toMillisIntBound());
+
+        if ("https".equalsIgnoreCase(scheme)) {
+            return sslSocketFactory.createSocket(sock, targetHost.getHostName(), port, true);
         } else {
-            return new InetSocketAddress(host.getHostName(), port);
+            return sock;
         }
     }
 
@@ -172,7 +188,6 @@ public class HttpRequester implements Gr
             final HttpContext context) throws HttpException, IOException {
         Args.notNull(targetHost, "HTTP host");
         Args.notNull(request, "HTTP request");
-        Args.notNull(context, "HTTP context");
         final Future<PoolEntry<HttpHost, HttpClientConnection>> leaseFuture = connPool.lease(targetHost, null, null);
         final PoolEntry<HttpHost, HttpClientConnection> poolEntry;
         final TimeValue timeout = connectTimeout != null ? connectTimeout : TimeValue.ZERO_MILLIS;
@@ -193,7 +208,6 @@ public class HttpRequester implements Gr
                 final Socket socket = createSocket(targetHost);
                 connection = connectFactory.createConnection(socket);
                 poolEntry.assignConnection(connection);
-                socket.connect(toEndpoint(targetHost), timeout.toMillisIntBound());
             }
             final ClassicHttpResponse response = execute(connection, request, context);
             final HttpEntity entity = response.getEntity();

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpServer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpServer.java?rev=1791353&r1=1791352&r2=1791353&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpServer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpServer.java Fri Apr 14 11:14:10 2017
@@ -41,6 +41,7 @@ import javax.net.ssl.SSLServerSocket;
 import org.apache.hc.core5.http.ExceptionListener;
 import org.apache.hc.core5.http.config.SocketConfig;
 import org.apache.hc.core5.http.impl.io.DefaultBHttpServerConnection;
+import org.apache.hc.core5.http.impl.io.DefaultBHttpServerConnectionFactory;
 import org.apache.hc.core5.http.impl.io.HttpService;
 import org.apache.hc.core5.http.io.HttpConnectionFactory;
 import org.apache.hc.core5.http.io.HttpServerConnection;
@@ -74,19 +75,19 @@ public class HttpServer implements Grace
 
     public HttpServer(
             final int port,
+            final HttpService httpService,
             final InetAddress ifAddress,
             final SocketConfig socketConfig,
             final ServerSocketFactory serverSocketFactory,
-            final HttpService httpService,
             final HttpConnectionFactory<? extends DefaultBHttpServerConnection> connectionFactory,
             final SSLServerSetupHandler sslSetupHandler,
             final ExceptionListener exceptionListener) {
-        this.port = port;
+        this.port = Args.notNegative(port, "Port value is negative");
+        this.httpService = Args.notNull(httpService, "HTTP service");
         this.ifAddress = ifAddress;
-        this.socketConfig = socketConfig;
-        this.serverSocketFactory = serverSocketFactory;
-        this.httpService = httpService;
-        this.connectionFactory = connectionFactory;
+        this.socketConfig = socketConfig != null ? socketConfig : SocketConfig.DEFAULT;
+        this.serverSocketFactory = serverSocketFactory != null ? serverSocketFactory : ServerSocketFactory.getDefault();
+        this.connectionFactory = connectionFactory != null ? connectionFactory : DefaultBHttpServerConnectionFactory.INSTANCE;
         this.sslSetupHandler = sslSetupHandler;
         this.exceptionListener = exceptionListener;
         this.listenerExecutorService = new ThreadPoolExecutor(

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequesterBootstrap.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequesterBootstrap.java?rev=1791353&r1=1791352&r2=1791353&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequesterBootstrap.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequesterBootstrap.java Fri Apr 14 11:14:10 2017
@@ -30,6 +30,7 @@ import javax.net.ssl.SSLSocketFactory;
 
 import org.apache.hc.core5.http.ConnectionReuseStrategy;
 import org.apache.hc.core5.http.HttpHost;
+import org.apache.hc.core5.http.config.SocketConfig;
 import org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy;
 import org.apache.hc.core5.http.impl.Http1StreamListener;
 import org.apache.hc.core5.http.impl.HttpProcessors;
@@ -50,6 +51,7 @@ public class RequesterBootstrap {
 
     private HttpProcessor httpProcessor;
     private ConnectionReuseStrategy connReuseStrategy;
+    private SocketConfig socketConfig;
     private HttpConnectionFactory<? extends HttpClientConnection> connectFactory;
     private SSLSocketFactory sslSocketFactory;
     private int defaultMaxPerRoute;
@@ -82,6 +84,14 @@ public class RequesterBootstrap {
         return this;
     }
 
+    /**
+     * Sets socket configuration.
+     */
+    public final RequesterBootstrap setSocketConfig(final SocketConfig socketConfig) {
+        this.socketConfig = socketConfig;
+        return this;
+    }
+
     public final RequesterBootstrap setConnectFactory(final HttpConnectionFactory<? extends HttpClientConnection> connectFactory) {
         this.connectFactory = connectFactory;
         return this;
@@ -137,6 +147,7 @@ public class RequesterBootstrap {
                 requestExecutor,
                 httpProcessor != null ? httpProcessor : HttpProcessors.client(),
                 connPool,
+                socketConfig != null ? socketConfig : SocketConfig.DEFAULT,
                 connectFactory != null ? connectFactory : DefaultBHttpClientConnectionFactory.INSTANCE,
                 sslSocketFactory);
     }

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/ServerBootstrap.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/ServerBootstrap.java?rev=1791353&r1=1791352&r2=1791353&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/ServerBootstrap.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/ServerBootstrap.java Fri Apr 14 11:14:10 2017
@@ -287,10 +287,10 @@ public class ServerBootstrap {
 
         return new HttpServer(
                 this.listenerPort > 0 ? this.listenerPort : 0,
+                httpService,
                 this.localAddress,
                 this.socketConfig != null ? this.socketConfig : SocketConfig.DEFAULT,
                 serverSocketFactoryCopy,
-                httpService,
                 connectionFactoryCopy,
                 this.sslSetupHandler,
                 exceptionListenerCopy);