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 2016/12/09 16:34:24 UTC

svn commit: r1773430 - in /httpcomponents/httpcore/trunk: httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/bootstrap/ httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/...

Author: olegk
Date: Fri Dec  9 16:34:24 2016
New Revision: 1773430

URL: http://svn.apache.org/viewvc?rev=1773430&view=rev
Log:
TLS strategy support for async servers and requesters

Added:
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/BasicClientTlsStrategy.java   (with props)
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/BasicServerTlsStrategy.java   (with props)
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/TlsStrategy.java   (with props)
Modified:
    httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractHttp2StreamMultiplexer.java
    httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ClientHttpProtocolNegotiator.java
    httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerHttpProtocolNegotiatorFactory.java
    httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/bootstrap/H2RequesterBootstrap.java
    httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/bootstrap/H2ServerBootstrap.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ServerHttp1IOEventHandlerFactory.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/HttpAsyncRequester.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/RequesterBootstrap.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/ServerBootstrap.java

Modified: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractHttp2StreamMultiplexer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractHttp2StreamMultiplexer.java?rev=1773430&r1=1773429&r2=1773430&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractHttp2StreamMultiplexer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractHttp2StreamMultiplexer.java Fri Dec  9 16:34:24 2016
@@ -52,6 +52,7 @@ import org.apache.hc.core5.http.Protocol
 import org.apache.hc.core5.http.ProtocolVersion;
 import org.apache.hc.core5.http.impl.BasicHttpConnectionMetrics;
 import org.apache.hc.core5.http.impl.ConnectionListener;
+import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
 import org.apache.hc.core5.http.nio.AsyncPushProducer;
 import org.apache.hc.core5.http.nio.command.ExecutionCommand;
 import org.apache.hc.core5.http.nio.command.ShutdownCommand;
@@ -567,6 +568,23 @@ abstract class AbstractHttp2StreamMultip
         }
     }
 
+    private void failPendingCommands(final Exception cause) {
+        for (;;) {
+            final Command command = ioSession.getCommand();
+            if (command != null) {
+                if (command instanceof ExecutionCommand) {
+                    final ExecutionCommand executionCommand = (ExecutionCommand) command;
+                    final AsyncClientExchangeHandler exchangeHandler = executionCommand.getExchangeHandler();
+                    exchangeHandler.failed(cause);
+                } else {
+                    command.cancel();
+                }
+            } else {
+                break;
+            }
+        }
+    }
+
     public final void onException(final Exception cause) {
         if (connectionListener != null) {
             connectionListener.onError(this, cause);
@@ -590,6 +608,7 @@ abstract class AbstractHttp2StreamMultip
                 stream.reset(cause);
             }
             streamMap.clear();
+            failPendingCommands(cause);
             connState = ConnectionHandshake.SHUTDOWN;
         } catch (IOException ignore) {
         } finally {

Modified: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ClientHttpProtocolNegotiator.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ClientHttpProtocolNegotiator.java?rev=1773430&r1=1773429&r2=1773430&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ClientHttpProtocolNegotiator.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ClientHttpProtocolNegotiator.java Fri Dec  9 16:34:24 2016
@@ -41,11 +41,14 @@ import org.apache.hc.core5.http.HttpConn
 import org.apache.hc.core5.http.ProtocolVersion;
 import org.apache.hc.core5.http.impl.ConnectionListener;
 import org.apache.hc.core5.http.impl.nio.HttpConnectionEventHandler;
+import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
 import org.apache.hc.core5.http.nio.AsyncPushConsumer;
 import org.apache.hc.core5.http.nio.HandlerFactory;
+import org.apache.hc.core5.http.nio.command.ExecutionCommand;
 import org.apache.hc.core5.http.protocol.HttpProcessor;
 import org.apache.hc.core5.http2.config.H2Config;
 import org.apache.hc.core5.http2.frame.DefaultFrameFactory;
+import org.apache.hc.core5.reactor.Command;
 import org.apache.hc.core5.reactor.IOEventHandler;
 import org.apache.hc.core5.reactor.IOSession;
 import org.apache.hc.core5.util.Args;
@@ -130,12 +133,33 @@ public class ClientHttpProtocolNegotiato
         exception(session, new SocketTimeoutException());
     }
 
+    private void failPendingCommands(final Exception cause) {
+        for (;;) {
+            final Command command = ioSession.getCommand();
+            if (command != null) {
+                if (command instanceof ExecutionCommand) {
+                    final ExecutionCommand executionCommand = (ExecutionCommand) command;
+                    final AsyncClientExchangeHandler exchangeHandler = executionCommand.getExchangeHandler();
+                    exchangeHandler.failed(cause);
+                } else {
+                    command.cancel();
+                }
+            } else {
+                break;
+            }
+        }
+    }
+
     @Override
     public void exception(final IOSession session, final Exception cause) {
-        session.close();
         if (connectionListener != null) {
             connectionListener.onError(this, new SocketTimeoutException());
         }
+        try {
+            failPendingCommands(cause);
+        } finally {
+            session.shutdown();
+        }
     }
 
     @Override

Modified: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerHttpProtocolNegotiatorFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerHttpProtocolNegotiatorFactory.java?rev=1773430&r1=1773429&r2=1773430&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerHttpProtocolNegotiatorFactory.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerHttpProtocolNegotiatorFactory.java Fri Dec  9 16:34:24 2016
@@ -35,10 +35,12 @@ import org.apache.hc.core5.annotation.Th
 import org.apache.hc.core5.http.impl.ConnectionListener;
 import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
 import org.apache.hc.core5.http.nio.HandlerFactory;
+import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
 import org.apache.hc.core5.http.protocol.HttpProcessor;
 import org.apache.hc.core5.http2.config.H2Config;
 import org.apache.hc.core5.reactor.IOEventHandlerFactory;
 import org.apache.hc.core5.reactor.IOSession;
+import org.apache.hc.core5.reactor.ssl.TlsCapable;
 import org.apache.hc.core5.util.Args;
 
 /**
@@ -51,6 +53,7 @@ public class ServerHttpProtocolNegotiato
     private final HandlerFactory<AsyncServerExchangeHandler> exchangeHandlerFactory;
     private final Charset charset;
     private final H2Config h2Config;
+    private final TlsStrategy tlsStrategy;
     private final ConnectionListener connectionListener;
     private final Http2StreamListener streamListener;
 
@@ -59,12 +62,14 @@ public class ServerHttpProtocolNegotiato
             final HandlerFactory<AsyncServerExchangeHandler> exchangeHandlerFactory,
             final Charset charset,
             final H2Config h2Config,
+            final TlsStrategy tlsStrategy,
             final ConnectionListener connectionListener,
             final Http2StreamListener streamListener) {
         this.httpProcessor = Args.notNull(httpProcessor, "HTTP processor");
         this.exchangeHandlerFactory = Args.notNull(exchangeHandlerFactory, "Exchange handler factory");
         this.charset = charset != null ? charset : StandardCharsets.US_ASCII;
         this.h2Config = h2Config != null ? h2Config : H2Config.DEFAULT;
+        this.tlsStrategy = tlsStrategy;
         this.connectionListener = connectionListener;
         this.streamListener = streamListener;
     }
@@ -72,13 +77,21 @@ public class ServerHttpProtocolNegotiato
     public ServerHttpProtocolNegotiatorFactory(
             final HttpProcessor httpProcessor,
             final HandlerFactory<AsyncServerExchangeHandler> exchangeHandlerFactory,
+            final TlsStrategy tlsStrategy,
             final ConnectionListener connectionListener,
             final Http2StreamListener streamListener) {
-        this(httpProcessor, exchangeHandlerFactory, null, null, connectionListener, streamListener);
+        this(httpProcessor, exchangeHandlerFactory, null, null, tlsStrategy, connectionListener, streamListener);
     }
 
     @Override
     public ServerHttpProtocolNegotiator createHandler(final IOSession ioSession) {
+        if (tlsStrategy != null && ioSession instanceof TlsCapable) {
+            tlsStrategy.upgrade(
+                    (TlsCapable) ioSession,
+                    null,
+                    ioSession.getLocalAddress(),
+                    ioSession.getRemoteAddress());
+        }
         return new ServerHttpProtocolNegotiator(ioSession, httpProcessor, exchangeHandlerFactory,
                 charset, h2Config, connectionListener, streamListener);
     }

Modified: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/bootstrap/H2RequesterBootstrap.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/bootstrap/H2RequesterBootstrap.java?rev=1773430&r1=1773429&r2=1773430&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/bootstrap/H2RequesterBootstrap.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/bootstrap/H2RequesterBootstrap.java Fri Dec  9 16:34:24 2016
@@ -39,6 +39,8 @@ import org.apache.hc.core5.http.impl.Con
 import org.apache.hc.core5.http.impl.nio.bootstrap.ClientEndpoint;
 import org.apache.hc.core5.http.impl.nio.bootstrap.HttpAsyncRequester;
 import org.apache.hc.core5.http.nio.AsyncPushConsumer;
+import org.apache.hc.core5.http.nio.ssl.BasicClientTlsStrategy;
+import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
 import org.apache.hc.core5.http.protocol.HttpProcessor;
 import org.apache.hc.core5.http2.config.H2Config;
 import org.apache.hc.core5.http2.impl.Http2Processors;
@@ -64,6 +66,7 @@ public class H2RequesterBootstrap {
     private int maxTotal;
     private long timeToLive;
     private TimeUnit timeUnit;
+    private TlsStrategy tlsStrategy;
     private ExceptionListener exceptionListener;
     private ConnectionListener connectionListener;
     private Http2StreamListener streamListener;
@@ -126,6 +129,14 @@ public class H2RequesterBootstrap {
     }
 
     /**
+     * Assigns {@link TlsStrategy} instance.
+     */
+    public final H2RequesterBootstrap setTlsStrategy(final TlsStrategy tlsStrategy) {
+        this.tlsStrategy = tlsStrategy;
+        return this;
+    }
+
+    /**
      * Assigns {@link ExceptionListener} instance.
      */
     public final H2RequesterBootstrap setExceptionListener(final ExceptionListener exceptionListener) {
@@ -149,6 +160,9 @@ public class H2RequesterBootstrap {
         return this;
     }
 
+    /**
+     * Assigns {@link ConnPoolListener} instance.
+     */
     public final H2RequesterBootstrap setConnPoolListener(final ConnPoolListener<HttpHost> connPoolListener) {
         this.connPoolListener = connPoolListener;
         return this;
@@ -188,9 +202,10 @@ public class H2RequesterBootstrap {
                 streamListener);
         return new HttpAsyncRequester(
                 ioReactorConfig,
-                exceptionListener,
                 ioEventHandlerFactory,
-                connPool);
+                connPool,
+                tlsStrategy != null ? tlsStrategy : new BasicClientTlsStrategy(),
+                exceptionListener);
     }
 
     private static class PushConsumerEntry {

Modified: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/bootstrap/H2ServerBootstrap.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/bootstrap/H2ServerBootstrap.java?rev=1773430&r1=1773429&r2=1773430&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/bootstrap/H2ServerBootstrap.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/bootstrap/H2ServerBootstrap.java Fri Dec  9 16:34:24 2016
@@ -37,6 +37,7 @@ import org.apache.hc.core5.http.impl.Con
 import org.apache.hc.core5.http.impl.nio.bootstrap.AsyncServerExchangeHandlerRegistry;
 import org.apache.hc.core5.http.impl.nio.bootstrap.HttpAsyncServer;
 import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
+import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
 import org.apache.hc.core5.http.nio.support.BasicServerExchangeHandler;
 import org.apache.hc.core5.http.nio.support.RequestConsumerSupplier;
 import org.apache.hc.core5.http.nio.support.ResponseHandler;
@@ -60,6 +61,7 @@ public class H2ServerBootstrap {
     private HttpProcessor httpProcessor;
     private Charset charset;
     private H2Config h2Config;
+    private TlsStrategy tlsStrategy;
     private ExceptionListener exceptionListener;
     private ConnectionListener connectionListener;
     private Http2StreamListener streamListener;
@@ -101,7 +103,7 @@ public class H2ServerBootstrap {
     /**
      * Sets HTTP/2 protocol parameters
      */
-    public final H2ServerBootstrap  setH2Config(final H2Config h2Config) {
+    public final H2ServerBootstrap setH2Config(final H2Config h2Config) {
         this.h2Config = h2Config;
         return this;
     }
@@ -109,12 +111,20 @@ public class H2ServerBootstrap {
     /**
      * Sets charset for HTTP/2 messages.
      */
-    public final H2ServerBootstrap  setCharset(final Charset charset) {
+    public final H2ServerBootstrap setCharset(final Charset charset) {
         this.charset = charset;
         return this;
     }
 
     /**
+     * Assigns {@link TlsStrategy} instance.
+     */
+    public final H2ServerBootstrap setTlsStrategy(final TlsStrategy tlsStrategy) {
+        this.tlsStrategy = tlsStrategy;
+        return this;
+    }
+
+    /**
      * Assigns {@link ExceptionListener} instance.
      */
     public final H2ServerBootstrap setExceptionListener(final ExceptionListener exceptionListener) {
@@ -195,6 +205,7 @@ public class H2ServerBootstrap {
                 exchangeHandlerFactory,
                 charset != null ? charset : StandardCharsets.US_ASCII,
                 h2Config != null ? h2Config : H2Config.DEFAULT,
+                tlsStrategy,
                 connectionListener,
                 streamListener);
         return new HttpAsyncServer(

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ServerHttp1IOEventHandlerFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ServerHttp1IOEventHandlerFactory.java?rev=1773430&r1=1773429&r2=1773430&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ServerHttp1IOEventHandlerFactory.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ServerHttp1IOEventHandlerFactory.java Fri Dec  9 16:34:24 2016
@@ -44,10 +44,12 @@ import org.apache.hc.core5.http.nio.Asyn
 import org.apache.hc.core5.http.nio.HandlerFactory;
 import org.apache.hc.core5.http.nio.NHttpMessageParserFactory;
 import org.apache.hc.core5.http.nio.NHttpMessageWriterFactory;
+import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
 import org.apache.hc.core5.http.protocol.HttpProcessor;
 import org.apache.hc.core5.reactor.IOEventHandler;
 import org.apache.hc.core5.reactor.IOEventHandlerFactory;
 import org.apache.hc.core5.reactor.IOSession;
+import org.apache.hc.core5.reactor.ssl.TlsCapable;
 import org.apache.hc.core5.util.Args;
 
 /**
@@ -64,6 +66,7 @@ public class ServerHttp1IOEventHandlerFa
     private final NHttpMessageWriterFactory<HttpResponse> responseWriterFactory;
     private final ContentLengthStrategy incomingContentStrategy;
     private final ContentLengthStrategy outgoingContentStrategy;
+    private final TlsStrategy tlsStrategy;
     private final ConnectionListener connectionListener;
     private final Http1StreamListener streamListener;
 
@@ -76,7 +79,7 @@ public class ServerHttp1IOEventHandlerFa
             final NHttpMessageWriterFactory<HttpResponse> responseWriterFactory,
             final ContentLengthStrategy incomingContentStrategy,
             final ContentLengthStrategy outgoingContentStrategy,
-            final ExceptionListener errorListener,
+            final TlsStrategy tlsStrategy,
             final ConnectionListener connectionListener,
             final Http1StreamListener streamListener) {
         this.httpProcessor = Args.notNull(httpProcessor, "HTTP processor");
@@ -92,6 +95,7 @@ public class ServerHttp1IOEventHandlerFa
                 DefaultContentLengthStrategy.INSTANCE;
         this.outgoingContentStrategy = outgoingContentStrategy != null ? outgoingContentStrategy :
                 DefaultContentLengthStrategy.INSTANCE;
+        this.tlsStrategy = tlsStrategy;
         this.connectionListener = connectionListener;
         this.streamListener = streamListener;
     }
@@ -103,31 +107,32 @@ public class ServerHttp1IOEventHandlerFa
             final ConnectionReuseStrategy connectionReuseStrategy,
             final NHttpMessageParserFactory<HttpRequest> requestParserFactory,
             final NHttpMessageWriterFactory<HttpResponse> responseWriterFactory,
-            final ExceptionListener errorListener,
+            final TlsStrategy tlsStrategy,
             final ConnectionListener connectionListener,
             final Http1StreamListener streamListener) {
         this(httpProcessor, exchangeHandlerFactory, connectionConfig,
                 connectionReuseStrategy, requestParserFactory, responseWriterFactory,
-                null, null, errorListener, connectionListener, streamListener);
+                null, null, tlsStrategy, connectionListener, streamListener);
     }
 
     public ServerHttp1IOEventHandlerFactory(
             final HttpProcessor httpProcessor,
             final HandlerFactory<AsyncServerExchangeHandler> exchangeHandlerFactory,
             final ConnectionConfig connectionConfig,
-            final ExceptionListener errorListener,
+            final TlsStrategy tlsStrategy,
             final ConnectionListener connectionListener,
             final Http1StreamListener streamListener) {
         this(httpProcessor, exchangeHandlerFactory, connectionConfig, null, null ,null,
-                errorListener, connectionListener, streamListener);
+                tlsStrategy, connectionListener, streamListener);
     }
 
     public ServerHttp1IOEventHandlerFactory(
             final HttpProcessor httpProcessor,
             final HandlerFactory<AsyncServerExchangeHandler> exchangeHandlerFactory,
             final ConnectionConfig connectionConfig,
+            final TlsStrategy tlsStrategy,
             final ExceptionListener errorListener) {
-        this(httpProcessor, exchangeHandlerFactory, connectionConfig, errorListener, null, null);
+        this(httpProcessor, exchangeHandlerFactory, connectionConfig, tlsStrategy, null, null);
     }
 
     @Override
@@ -136,6 +141,13 @@ public class ServerHttp1IOEventHandlerFa
     }
 
     protected ServerHttp1StreamDuplexer createStreamDuplexer(final IOSession ioSession) {
+        if (tlsStrategy != null && ioSession instanceof TlsCapable) {
+            tlsStrategy.upgrade(
+                    (TlsCapable) ioSession,
+                    null,
+                    ioSession.getLocalAddress(),
+                    ioSession.getRemoteAddress());
+        }
         return new ServerHttp1StreamDuplexer(ioSession, httpProcessor, exchangeHandlerFactory,
                 H1Config.DEFAULT,
                 connectionConfig,

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/HttpAsyncRequester.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/HttpAsyncRequester.java?rev=1773430&r1=1773429&r2=1773430&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/HttpAsyncRequester.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/HttpAsyncRequester.java Fri Dec  9 16:34:24 2016
@@ -42,6 +42,7 @@ import org.apache.hc.core5.http.HttpHost
 import org.apache.hc.core5.http.impl.PoolEntryHolder;
 import org.apache.hc.core5.http.nio.command.ShutdownCommand;
 import org.apache.hc.core5.http.nio.command.ShutdownType;
+import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
 import org.apache.hc.core5.pool.ControlledConnPool;
 import org.apache.hc.core5.pool.PoolEntry;
 import org.apache.hc.core5.reactor.IOEventHandlerFactory;
@@ -49,6 +50,7 @@ import org.apache.hc.core5.reactor.IORea
 import org.apache.hc.core5.reactor.IOSession;
 import org.apache.hc.core5.reactor.SessionRequest;
 import org.apache.hc.core5.reactor.SessionRequestCallback;
+import org.apache.hc.core5.reactor.ssl.TlsCapable;
 import org.apache.hc.core5.util.Args;
 
 /**
@@ -58,12 +60,14 @@ public class HttpAsyncRequester extends
 
     private final IOEventHandlerFactory handlerFactory;
     private final ControlledConnPool<HttpHost, ClientEndpoint> connPool;
+    private final TlsStrategy tlsStrategy;
 
     public HttpAsyncRequester(
             final IOReactorConfig ioReactorConfig,
-            final ExceptionListener exceptionListener,
             final IOEventHandlerFactory handlerFactory,
-            final ControlledConnPool<HttpHost, ClientEndpoint> connPool) {
+            final ControlledConnPool<HttpHost, ClientEndpoint> connPool,
+            final TlsStrategy tlsStrategy,
+            final ExceptionListener exceptionListener) {
         super(ioReactorConfig, exceptionListener, new Callback<IOSession>() {
 
             @Override
@@ -74,6 +78,7 @@ public class HttpAsyncRequester extends
         });
         this.handlerFactory = Args.notNull(handlerFactory, "Handler factory");
         this.connPool = Args.notNull(connPool, "Connection pool");
+        this.tlsStrategy = tlsStrategy;
     }
 
     public void start() throws IOException {
@@ -115,7 +120,15 @@ public class HttpAsyncRequester extends
 
                         @Override
                         public void completed(final SessionRequest request) {
-                            poolEntry.assignConnection(new ClientEndpoint(request.getSession()));
+                            final IOSession session = request.getSession();
+                            if (tlsStrategy != null && session instanceof TlsCapable) {
+                                tlsStrategy.upgrade(
+                                        (TlsCapable) session,
+                                        host.getSchemeName(),
+                                        session.getLocalAddress(),
+                                        session.getRemoteAddress());
+                            }
+                            poolEntry.assignConnection(new ClientEndpoint(session));
                             resultFuture.completed(new PooledClientEndpoint(poolEntryHolder));
                         }
 

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/RequesterBootstrap.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/RequesterBootstrap.java?rev=1773430&r1=1773429&r2=1773430&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/RequesterBootstrap.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/RequesterBootstrap.java Fri Dec  9 16:34:24 2016
@@ -40,6 +40,8 @@ import org.apache.hc.core5.http.impl.Htt
 import org.apache.hc.core5.http.impl.nio.ClientHttp1IOEventHandlerFactory;
 import org.apache.hc.core5.http.impl.nio.DefaultHttpRequestWriterFactory;
 import org.apache.hc.core5.http.impl.nio.DefaultHttpResponseParserFactory;
+import org.apache.hc.core5.http.nio.ssl.BasicClientTlsStrategy;
+import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
 import org.apache.hc.core5.http.protocol.HttpProcessor;
 import org.apache.hc.core5.pool.ConnPoolListener;
 import org.apache.hc.core5.pool.StrictConnPool;
@@ -58,6 +60,7 @@ public class RequesterBootstrap {
     private int maxTotal;
     private long timeToLive;
     private TimeUnit timeUnit;
+    private TlsStrategy tlsStrategy;
     private ExceptionListener exceptionListener;
     private ConnectionListener connectionListener;
     private Http1StreamListener streamListener;
@@ -119,6 +122,14 @@ public class RequesterBootstrap {
     }
 
     /**
+     * Assigns {@link TlsStrategy} instance.
+     */
+    public final RequesterBootstrap setTlsStrategy(final TlsStrategy tlsStrategy) {
+        this.tlsStrategy = tlsStrategy;
+        return this;
+    }
+
+    /**
      * Assigns {@link ExceptionListener} instance.
      */
     public final RequesterBootstrap setExceptionListener(final ExceptionListener exceptionListener) {
@@ -142,6 +153,9 @@ public class RequesterBootstrap {
         return this;
     }
 
+    /**
+     * Assigns {@link ConnPoolListener} instance.
+     */
     public final RequesterBootstrap setConnPoolListener(final ConnPoolListener<HttpHost> connPoolListener) {
         this.connPoolListener = connPoolListener;
         return this;
@@ -165,9 +179,10 @@ public class RequesterBootstrap {
                 streamListener);
         return new HttpAsyncRequester(
                 ioReactorConfig,
-                exceptionListener,
                 ioEventHandlerFactory,
-                connPool);
+                connPool,
+                tlsStrategy != null ? tlsStrategy : new BasicClientTlsStrategy(),
+                exceptionListener);
     }
 
 }

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/ServerBootstrap.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/ServerBootstrap.java?rev=1773430&r1=1773429&r2=1773430&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/ServerBootstrap.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/ServerBootstrap.java Fri Dec  9 16:34:24 2016
@@ -29,26 +29,28 @@ package org.apache.hc.core5.http.impl.ni
 import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.hc.core5.function.Supplier;
 import org.apache.hc.core5.http.ConnectionReuseStrategy;
 import org.apache.hc.core5.http.ExceptionListener;
-import org.apache.hc.core5.http.impl.HttpProcessors;
 import org.apache.hc.core5.http.config.ConnectionConfig;
+import org.apache.hc.core5.http.impl.ConnectionListener;
 import org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy;
 import org.apache.hc.core5.http.impl.DefaultContentLengthStrategy;
-import org.apache.hc.core5.http.impl.ConnectionListener;
+import org.apache.hc.core5.http.impl.Http1StreamListener;
+import org.apache.hc.core5.http.impl.HttpProcessors;
 import org.apache.hc.core5.http.impl.nio.DefaultHttpRequestParserFactory;
 import org.apache.hc.core5.http.impl.nio.DefaultHttpResponseWriterFactory;
-import org.apache.hc.core5.http.impl.Http1StreamListener;
 import org.apache.hc.core5.http.impl.nio.ServerHttp1IOEventHandlerFactory;
 import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
-import org.apache.hc.core5.function.Supplier;
+import org.apache.hc.core5.http.nio.ssl.BasicServerTlsStrategy;
+import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
 import org.apache.hc.core5.http.nio.support.BasicServerExchangeHandler;
 import org.apache.hc.core5.http.nio.support.RequestConsumerSupplier;
 import org.apache.hc.core5.http.nio.support.ResponseHandler;
 import org.apache.hc.core5.http.protocol.HttpProcessor;
+import org.apache.hc.core5.net.InetAddressUtils;
 import org.apache.hc.core5.reactor.IOReactorConfig;
 import org.apache.hc.core5.util.Args;
-import org.apache.hc.core5.net.InetAddressUtils;
 
 /**
  * @since 4.4
@@ -61,6 +63,7 @@ public class ServerBootstrap {
     private ConnectionConfig connectionConfig;
     private HttpProcessor httpProcessor;
     private ConnectionReuseStrategy connStrategy;
+    private TlsStrategy tlsStrategy;
     private ExceptionListener exceptionListener;
     private ConnectionListener connectionListener;
     private Http1StreamListener streamListener;
@@ -116,6 +119,14 @@ public class ServerBootstrap {
     }
 
     /**
+     * Assigns {@link TlsStrategy} instance.
+     */
+    public final ServerBootstrap setTlsStrategy(final TlsStrategy tlsStrategy) {
+        this.tlsStrategy = tlsStrategy;
+        return this;
+    }
+
+    /**
      * Assigns {@link ExceptionListener} instance.
      */
     public final ServerBootstrap setExceptionListener(final ExceptionListener exceptionListener) {
@@ -202,7 +213,7 @@ public class ServerBootstrap {
                 DefaultHttpResponseWriterFactory.INSTANCE,
                 DefaultContentLengthStrategy.INSTANCE,
                 DefaultContentLengthStrategy.INSTANCE,
-                exceptionListener,
+                tlsStrategy != null ? tlsStrategy : new BasicServerTlsStrategy(new int[] {443, 8443}),
                 connectionListener,
                 streamListener);
         return new HttpAsyncServer(

Added: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/BasicClientTlsStrategy.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/BasicClientTlsStrategy.java?rev=1773430&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/BasicClientTlsStrategy.java (added)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/BasicClientTlsStrategy.java Fri Dec  9 16:34:24 2016
@@ -0,0 +1,98 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.hc.core5.http.nio.ssl;
+
+import java.net.SocketAddress;
+
+import javax.net.ssl.SSLContext;
+
+import org.apache.hc.core5.reactor.ssl.SSLBufferManagement;
+import org.apache.hc.core5.reactor.ssl.SSLSessionInitializer;
+import org.apache.hc.core5.reactor.ssl.SSLSessionVerifier;
+import org.apache.hc.core5.reactor.ssl.TlsCapable;
+import org.apache.hc.core5.ssl.SSLContexts;
+import org.apache.hc.core5.util.Args;
+
+/**
+ * Basic client-side implementation of {@link TlsStrategy} that upgrades to TLS for all endpoints
+ * with {@code HTTPS} scheme.
+ *
+ * @since 5.0
+ */
+public class BasicClientTlsStrategy implements TlsStrategy {
+
+    private final SSLContext sslContext;
+    private final SSLBufferManagement sslBufferManagement;
+    private final SSLSessionInitializer initializer;
+    private final SSLSessionVerifier verifier;
+
+    public BasicClientTlsStrategy(
+            final SSLContext sslContext,
+            final SSLBufferManagement sslBufferManagement,
+            final SSLSessionInitializer initializer,
+            final SSLSessionVerifier verifier) {
+        this.sslContext = Args.notNull(sslContext, "SSL context");
+        this.sslBufferManagement = sslBufferManagement;
+        this.initializer = initializer;
+        this.verifier = verifier;
+    }
+
+    public BasicClientTlsStrategy(
+            final SSLContext sslContext,
+            final SSLSessionInitializer initializer,
+            final SSLSessionVerifier verifier) {
+        this(sslContext, null, initializer, verifier);
+    }
+
+    public BasicClientTlsStrategy(
+            final SSLContext sslContext,
+            final SSLSessionVerifier verifier) {
+        this(sslContext, null, null, verifier);
+    }
+
+    public BasicClientTlsStrategy(final SSLContext sslContext) {
+        this(sslContext, null, null, null);
+    }
+
+    public BasicClientTlsStrategy() {
+        this(SSLContexts.createSystemDefault());
+    }
+
+    @Override
+    public void upgrade(
+            final TlsCapable tlsCapable,
+            final String scheme,
+            final SocketAddress localAddress,
+            final SocketAddress remoteAddress,
+            final String... parameters) {
+        if ("https".equalsIgnoreCase(scheme)) {
+            tlsCapable.startTls(sslContext, sslBufferManagement, initializer, verifier);
+        }
+    }
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/BasicClientTlsStrategy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/BasicClientTlsStrategy.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/BasicClientTlsStrategy.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/BasicServerTlsStrategy.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/BasicServerTlsStrategy.java?rev=1773430&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/BasicServerTlsStrategy.java (added)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/BasicServerTlsStrategy.java Fri Dec  9 16:34:24 2016
@@ -0,0 +1,108 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.hc.core5.http.nio.ssl;
+
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+
+import javax.net.ssl.SSLContext;
+
+import org.apache.hc.core5.reactor.ssl.SSLBufferManagement;
+import org.apache.hc.core5.reactor.ssl.SSLSessionInitializer;
+import org.apache.hc.core5.reactor.ssl.SSLSessionVerifier;
+import org.apache.hc.core5.reactor.ssl.TlsCapable;
+import org.apache.hc.core5.ssl.SSLContexts;
+import org.apache.hc.core5.util.Args;
+
+/**
+ * Basic side-side implementation of {@link TlsStrategy} that upgrades to TLS for endpoints
+ * with the specified local ports.
+ *
+ * @since 5.0
+ */
+public class BasicServerTlsStrategy implements TlsStrategy {
+
+    private final int[] securePorts;
+    private final SSLContext sslContext;
+    private final SSLBufferManagement sslBufferManagement;
+    private final SSLSessionInitializer initializer;
+    private final SSLSessionVerifier verifier;
+
+    public BasicServerTlsStrategy(
+            final int[] securePorts,
+            final SSLContext sslContext,
+            final SSLBufferManagement sslBufferManagement,
+            final SSLSessionInitializer initializer,
+            final SSLSessionVerifier verifier) {
+        this.securePorts = Args.notNull(securePorts, "Array of ports");
+        this.sslContext = Args.notNull(sslContext, "SSL context");
+        this.sslBufferManagement = sslBufferManagement;
+        this.initializer = initializer;
+        this.verifier = verifier;
+    }
+
+    public BasicServerTlsStrategy(
+            final int[] securePorts,
+            final SSLContext sslContext,
+            final SSLSessionInitializer initializer,
+            final SSLSessionVerifier verifier) {
+        this(securePorts, sslContext, null, initializer, verifier);
+    }
+
+    public BasicServerTlsStrategy(
+            final int[] securePorts,
+            final SSLContext sslContext,
+            final SSLSessionVerifier verifier) {
+        this(securePorts, sslContext, null, null, verifier);
+    }
+
+    public BasicServerTlsStrategy(final int[] securePorts, final SSLContext sslContext) {
+        this(securePorts, sslContext, null, null, null);
+    }
+
+    public BasicServerTlsStrategy(final int[] securePorts) {
+        this(securePorts, SSLContexts.createSystemDefault());
+    }
+
+    @Override
+    public void upgrade(
+            final TlsCapable tlsCapable,
+            final String scheme,
+            final SocketAddress localAddress,
+            final SocketAddress remoteAddress,
+            final String... parameters) {
+        final int port = ((InetSocketAddress) localAddress).getPort();
+        for (int securePort: securePorts) {
+            if (port == securePort) {
+                tlsCapable.startTls(sslContext, sslBufferManagement, initializer, verifier);
+                break;
+            }
+        }
+    }
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/BasicServerTlsStrategy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/BasicServerTlsStrategy.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/BasicServerTlsStrategy.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/TlsStrategy.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/TlsStrategy.java?rev=1773430&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/TlsStrategy.java (added)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/TlsStrategy.java Fri Dec  9 16:34:24 2016
@@ -0,0 +1,48 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.hc.core5.http.nio.ssl;
+
+import java.net.SocketAddress;
+
+import org.apache.hc.core5.reactor.ssl.TlsCapable;
+
+/**
+ * TLS protocol upgrade strategy for non-blocking {@link TlsCapable} connections.
+ *
+ * @since 5.0
+ */
+public interface TlsStrategy {
+
+    void upgrade(
+            TlsCapable tlsCapable,
+            String scheme,
+            SocketAddress localAddress,
+            SocketAddress remoteAddress,
+            String... parameters);
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/TlsStrategy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/TlsStrategy.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/TlsStrategy.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain