Path: blob/master/src/java.net.http/share/classes/jdk/internal/net/http/AsyncSSLConnection.java
41171 views
/*1* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.internal.net.http;2627import java.net.InetSocketAddress;28import java.nio.channels.SocketChannel;29import java.util.concurrent.CompletableFuture;30import java.util.function.Function;31import jdk.internal.net.http.common.MinimalFuture;32import jdk.internal.net.http.common.SSLTube;33import jdk.internal.net.http.common.Utils;3435/**36* Asynchronous version of SSLConnection.37*/38class AsyncSSLConnection extends AbstractAsyncSSLConnection {3940final PlainHttpConnection plainConnection;41final PlainHttpPublisher writePublisher;42private volatile SSLTube flow;4344AsyncSSLConnection(InetSocketAddress addr,45HttpClientImpl client,46String[] alpn) {47super(addr, client, Utils.getServerName(addr), addr.getPort(), alpn);48plainConnection = new PlainHttpConnection(addr, client);49writePublisher = new PlainHttpPublisher();50}5152@Override53public CompletableFuture<Void> connectAsync(Exchange<?> exchange) {54return plainConnection55.connectAsync(exchange)56.thenApply( unused -> {57// create the SSLTube wrapping the SocketTube, with the given engine58flow = new SSLTube(engine,59client().theExecutor(),60client().getSSLBufferSupplier()::recycle,61plainConnection.getConnectionFlow());62return null; } );63}6465@Override66public CompletableFuture<Void> finishConnect() {67// The actual ALPN value, which may be the empty string, is not68// interesting at this point, only that the handshake has completed.69return getALPN()70.handle((String unused, Throwable ex) -> {71if (ex == null) {72return plainConnection.finishConnect();73} else {74plainConnection.close();75return MinimalFuture.<Void>failedFuture(ex);76} })77.thenCompose(Function.identity());78}7980@Override81boolean connected() {82return plainConnection.connected();83}8485@Override86HttpPublisher publisher() { return writePublisher; }8788@Override89boolean isProxied() {90return false;91}9293@Override94InetSocketAddress proxy() {95return null;96}9798@Override99SocketChannel channel() {100return plainConnection.channel();101}102103@Override104ConnectionPool.CacheKey cacheKey() {105return ConnectionPool.cacheKey(address, null);106}107108@Override109public void close() {110plainConnection.close();111}112113@Override114SSLTube getConnectionFlow() {115return flow;116}117}118119120