1 /* 2 * Copyright (c) 2015, 2016, 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 java.net.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 javax.net.ssl.SSLEngineResult.Status; 34 import javax.net.ssl.SSLParameters; 35 import java.net.http.SSLDelegate.BufType; 36 import java.net.http.SSLDelegate.WrapperResult; 37 38 /** 39 * An SSL connection built on a Plain TCP connection. 40 */ 41 class SSLConnection extends HttpConnection { 42 43 PlainHttpConnection delegate; 44 SSLDelegate sslDelegate; 45 final String[] alpn; 46 47 @Override 48 public CompletableFuture<Void> connectAsync() { 49 return delegate.connectAsync() 50 .thenCompose((Void v) -> { 51 CompletableFuture<Void> cf = new CompletableFuture<>(); 52 try { 53 this.sslDelegate = new SSLDelegate(delegate.channel(), 54 client, 55 alpn); 56 cf.complete(null); 57 } catch (IOException e) { 58 cf.completeExceptionally(e); 59 } 60 return cf; 61 }); 62 } 63 64 @Override 65 public void connect() throws IOException { 66 delegate.connect(); 67 this.sslDelegate = new SSLDelegate(delegate.channel(), client, alpn); 68 } 69 70 SSLConnection(InetSocketAddress addr, HttpClientImpl client, String[] ap) { 71 super(addr, client); 72 this.alpn = ap; 73 delegate = new PlainHttpConnection(addr, client); 74 } 75 76 @Override 77 SSLParameters sslParameters() { 78 return sslDelegate.getSSLParameters(); 79 } 80 81 @Override 82 public String toString() { 83 return "SSLConnection: " + super.toString(); 84 } 85 86 private static long countBytes(ByteBuffer[] buffers, int start, int length) { 87 long c = 0; 88 for (int i=0; i<length; i++) { 89 c+= buffers[start+i].remaining(); 90 } 91 return c; 92 } 93 94 @Override 95 ConnectionPool.CacheKey cacheKey() { 96 return ConnectionPool.cacheKey(address, null); 97 } 98 99 @Override 100 long write(ByteBuffer[] buffers, int start, int number) throws IOException { 101 //debugPrint("Send", buffers, start, number); 102 long l = countBytes(buffers, start, number); 103 WrapperResult r = sslDelegate.sendData(buffers, start, number); 104 if (r.result.getStatus() == Status.CLOSED) { 105 if (l > 0) { 106 throw new IOException("SSLHttpConnection closed"); 107 } 108 } 109 return l; 110 } 111 112 @Override 113 long write(ByteBuffer buffer) throws IOException { 114 //debugPrint("Send", buffer); 115 long l = buffer.remaining(); 116 WrapperResult r = sslDelegate.sendData(buffer); 117 if (r.result.getStatus() == Status.CLOSED) { 118 if (l > 0) { 119 throw new IOException("SSLHttpConnection closed"); 120 } 121 } 122 return l; 123 } 124 125 @Override 126 public void close() { 127 Utils.close(delegate.channel()); 128 } 129 130 @Override 131 protected ByteBuffer readImpl(int length) throws IOException { 132 ByteBuffer buf = sslDelegate.allocate(BufType.PACKET, length); 133 WrapperResult r = sslDelegate.recvData(buf); 134 // TODO: check for closure 135 String s = "Receive) "; 136 //debugPrint(s, r.buf); 137 return r.buf; 138 } 139 140 @Override 141 protected int readImpl(ByteBuffer buf) throws IOException { 142 // TODO: need to ensure that buf is big enough for application data 143 WrapperResult r = sslDelegate.recvData(buf); 144 // TODO: check for closure 145 String s = "Receive) "; 146 //debugPrint(s, r.buf); 147 if (r.result.bytesProduced() > 0) { 148 assert buf == r.buf; 149 } 150 return r.result.bytesProduced(); 151 } 152 153 @Override 154 boolean connected() { 155 return delegate.connected(); 156 } 157 158 @Override 159 SocketChannel channel() { 160 return delegate.channel(); 161 } 162 163 @Override 164 CompletableFuture<Void> whenReceivingResponse() { 165 return delegate.whenReceivingResponse(); 166 } 167 168 @Override 169 boolean isSecure() { 170 return true; 171 } 172 173 @Override 174 boolean isProxied() { 175 return false; 176 } 177 178 }