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