Path: blob/master/src/java.net.http/share/classes/jdk/internal/net/http/AsyncSSLTunnelConnection.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.net.http.HttpHeaders;31import java.util.function.Function;32import jdk.internal.net.http.common.MinimalFuture;33import jdk.internal.net.http.common.SSLTube;34import jdk.internal.net.http.common.Utils;35import static jdk.internal.net.http.common.Utils.ProxyHeaders;3637/**38* An SSL tunnel built on a Plain (CONNECT) TCP tunnel.39*/40class AsyncSSLTunnelConnection extends AbstractAsyncSSLConnection {4142final PlainTunnelingConnection plainConnection;43final PlainHttpPublisher writePublisher;44volatile SSLTube flow;4546AsyncSSLTunnelConnection(InetSocketAddress addr,47HttpClientImpl client,48String[] alpn,49InetSocketAddress proxy,50ProxyHeaders proxyHeaders)51{52super(addr, client, Utils.getServerName(addr), addr.getPort(), alpn);53this.plainConnection = new PlainTunnelingConnection(addr, proxy, client, proxyHeaders);54this.writePublisher = new PlainHttpPublisher();55}5657@Override58public CompletableFuture<Void> connectAsync(Exchange<?> exchange) {59if (debug.on()) debug.log("Connecting plain tunnel connection");60// This will connect the PlainHttpConnection flow, so that61// its HttpSubscriber and HttpPublisher are subscribed to the62// SocketTube63return plainConnection64.connectAsync(exchange)65.thenApply( unused -> {66if (debug.on()) debug.log("creating SSLTube");67// create the SSLTube wrapping the SocketTube, with the given engine68flow = new SSLTube(engine,69client().theExecutor(),70client().getSSLBufferSupplier()::recycle,71plainConnection.getConnectionFlow());72return null;} );73}7475@Override76public CompletableFuture<Void> finishConnect() {77// The actual ALPN value, which may be the empty string, is not78// interesting at this point, only that the handshake has completed.79return getALPN()80.handle((String unused, Throwable ex) -> {81if (ex == null) {82return plainConnection.finishConnect();83} else {84plainConnection.close();85return MinimalFuture.<Void>failedFuture(ex);86} })87.thenCompose(Function.identity());88}8990@Override91boolean isTunnel() { return true; }9293@Override94boolean connected() {95return plainConnection.connected(); // && sslDelegate.connected();96}9798@Override99HttpPublisher publisher() { return writePublisher; }100101@Override102public String toString() {103return "AsyncSSLTunnelConnection: " + super.toString();104}105106@Override107ConnectionPool.CacheKey cacheKey() {108return ConnectionPool.cacheKey(address, plainConnection.proxyAddr);109}110111@Override112public void close() {113plainConnection.close();114}115116@Override117SocketChannel channel() {118return plainConnection.channel();119}120121@Override122boolean isProxied() {123return true;124}125126@Override127InetSocketAddress proxy() {128return plainConnection.proxyAddr;129}130131@Override132SSLTube getConnectionFlow() {133return flow;134}135}136137138