1 /* 2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.incubator.http; 27 28 import java.io.IOException; 29 import java.net.InetSocketAddress; 30 import java.nio.ByteBuffer; 31 import java.nio.channels.SocketChannel; 32 import java.util.concurrent.CompletableFuture; 33 import java.util.function.Consumer; 34 import java.util.function.Supplier; 35 import javax.net.ssl.SSLEngine; 36 import javax.net.ssl.SSLParameters; 37 import jdk.incubator.http.internal.common.ByteBufferReference; 38 import jdk.incubator.http.internal.common.Utils; 39 40 /** 41 * An SSL tunnel built on a Plain (CONNECT) TCP tunnel. 42 */ 43 class AsyncSSLTunnelConnection extends AbstractAsyncSSLConnection { 44 45 final PlainTunnelingConnection plainConnection; 46 final AsyncSSLDelegate sslDelegate; 47 final String serverName; 48 49 @Override 50 public void connect() throws IOException, InterruptedException { 51 plainConnection.connect(); 52 configureMode(Mode.ASYNC); 53 startReading(); 54 sslDelegate.connect(); 55 } 56 57 @Override 58 boolean connected() { 59 return plainConnection.connected() && sslDelegate.connected(); 60 } 61 62 @Override 63 public CompletableFuture<Void> connectAsync() { 64 throw new InternalError(); 65 } 66 67 AsyncSSLTunnelConnection(InetSocketAddress addr, 68 HttpClientImpl client, 69 String[] alpn, 70 InetSocketAddress proxy) 71 { 72 super(addr, client); 73 this.serverName = Utils.getServerName(addr); 74 this.plainConnection = new PlainTunnelingConnection(addr, proxy, client); 75 this.sslDelegate = new AsyncSSLDelegate(plainConnection, client, alpn, serverName); 76 } 77 78 @Override 79 synchronized void configureMode(Mode mode) throws IOException { 80 super.configureMode(mode); 81 plainConnection.configureMode(mode); 82 } 83 84 @Override 85 SSLParameters sslParameters() { 86 return sslDelegate.getSSLParameters(); 87 } 88 89 @Override 90 public String toString() { 91 return "AsyncSSLTunnelConnection: " + super.toString(); 92 } 93 94 @Override 95 PlainTunnelingConnection plainConnection() { 96 return plainConnection; 97 } 98 99 @Override 100 AsyncSSLDelegate sslDelegate() { 101 return sslDelegate; 102 } 103 104 @Override 105 ConnectionPool.CacheKey cacheKey() { 106 return ConnectionPool.cacheKey(address, plainConnection.proxyAddr); 107 } 108 109 @Override 110 long write(ByteBuffer[] buffers, int start, int number) throws IOException { 111 //debugPrint("Send", buffers, start, number); 112 ByteBuffer[] bufs = Utils.reduce(buffers, start, number); 113 long n = Utils.remaining(bufs); 114 sslDelegate.writeAsync(ByteBufferReference.toReferences(bufs)); 115 sslDelegate.flushAsync(); 116 return n; 117 } 118 119 @Override 120 long write(ByteBuffer buffer) throws IOException { 121 //debugPrint("Send", buffer); 122 long n = buffer.remaining(); 123 sslDelegate.writeAsync(ByteBufferReference.toReferences(buffer)); 124 sslDelegate.flushAsync(); 125 return n; 126 } 127 128 @Override 129 public void writeAsync(ByteBufferReference[] buffers) throws IOException { 130 sslDelegate.writeAsync(buffers); 131 } 132 133 @Override 134 public void writeAsyncUnordered(ByteBufferReference[] buffers) throws IOException { 135 sslDelegate.writeAsyncUnordered(buffers); 136 } 137 138 @Override 139 public void flushAsync() throws IOException { 140 sslDelegate.flushAsync(); 141 } 142 143 @Override 144 public void close() { 145 Utils.close(sslDelegate, plainConnection.channel()); 146 } 147 148 @Override 149 void shutdownInput() throws IOException { 150 plainConnection.channel().shutdownInput(); 151 } 152 153 @Override 154 void shutdownOutput() throws IOException { 155 plainConnection.channel().shutdownOutput(); 156 } 157 158 @Override 159 SocketChannel channel() { 160 return plainConnection.channel(); 161 } 162 163 @Override 164 boolean isProxied() { 165 return true; 166 } 167 168 @Override 169 public void setAsyncCallbacks(Consumer<ByteBufferReference> asyncReceiver, 170 Consumer<Throwable> errorReceiver, 171 Supplier<ByteBufferReference> readBufferSupplier) { 172 sslDelegate.setAsyncCallbacks(asyncReceiver, errorReceiver, readBufferSupplier); 173 plainConnection.setAsyncCallbacks(sslDelegate::asyncReceive, errorReceiver, sslDelegate::getNetBuffer); 174 } 175 176 @Override 177 public void startReading() { 178 plainConnection.startReading(); 179 sslDelegate.startReading(); 180 } 181 182 @Override 183 public void stopAsyncReading() { 184 plainConnection.stopAsyncReading(); 185 } 186 187 @Override 188 public void enableCallback() { 189 sslDelegate.enableCallback(); 190 } 191 192 @Override 193 public void closeExceptionally(Throwable cause) throws IOException { 194 Utils.close(cause, sslDelegate, plainConnection.channel()); 195 } 196 197 @Override 198 SSLEngine getEngine() { 199 return sslDelegate.getEngine(); 200 } 201 202 @Override 203 SSLTunnelConnection downgrade() { 204 return new SSLTunnelConnection(this); 205 } 206 }