src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/SSLDelegate.java
branchhttp-client-branch
changeset 55792 0936888d5a4a
parent 55763 634d8e14c172
child 55800 c4307c12419d
equal deleted inserted replaced
55791:4849d9e433be 55792:0936888d5a4a
    26 package jdk.incubator.http;
    26 package jdk.incubator.http;
    27 
    27 
    28 import java.io.IOException;
    28 import java.io.IOException;
    29 import java.nio.ByteBuffer;
    29 import java.nio.ByteBuffer;
    30 import java.nio.channels.SocketChannel;
    30 import java.nio.channels.SocketChannel;
    31 import java.util.Arrays;
       
    32 import java.util.List;
       
    33 import java.util.concurrent.locks.Lock;
    31 import java.util.concurrent.locks.Lock;
    34 import java.util.concurrent.locks.ReentrantLock;
    32 import java.util.concurrent.locks.ReentrantLock;
    35 import javax.net.ssl.SSLEngineResult.HandshakeStatus;
    33 import javax.net.ssl.SSLEngineResult.HandshakeStatus;
    36 import javax.net.ssl.SSLEngineResult.Status;
    34 import javax.net.ssl.SSLEngineResult.Status;
    37 import javax.net.ssl.*;
    35 import javax.net.ssl.*;
    65         this.sslParameters = engine.getSSLParameters();
    63         this.sslParameters = engine.getSSLParameters();
    66         this.serverName = sn;
    64         this.serverName = sn;
    67     }
    65     }
    68 
    66 
    69     // alpn[] may be null
    67     // alpn[] may be null
    70     SSLDelegate(SocketChannel chan, HttpClientImpl client, String[] alpn, String sn)
    68 //    SSLDelegate(SocketChannel chan, HttpClientImpl client, String[] alpn, String sn)
    71         throws IOException
    69 //        throws IOException
    72     {
    70 //    {
    73         serverName = sn;
    71 //        serverName = sn;
    74         SSLContext context = client.sslContext();
    72 //        SSLContext context = client.sslContext();
    75         engine = context.createSSLEngine();
    73 //        engine = context.createSSLEngine();
    76         engine.setUseClientMode(true);
    74 //        engine.setUseClientMode(true);
    77         SSLParameters sslp = client.sslParameters();
    75 //        SSLParameters sslp = client.sslParameters();
    78         sslParameters = Utils.copySSLParameters(sslp);
    76 //        sslParameters = Utils.copySSLParameters(sslp);
    79         if (sn != null) {
    77 //        if (sn != null) {
    80             SNIHostName sni = new SNIHostName(sn);
    78 //            SNIHostName sni = new SNIHostName(sn);
    81             sslParameters.setServerNames(List.of(sni));
    79 //            sslParameters.setServerNames(List.of(sni));
    82         }
    80 //        }
    83         if (alpn != null) {
    81 //        if (alpn != null) {
    84             sslParameters.setApplicationProtocols(alpn);
    82 //            sslParameters.setApplicationProtocols(alpn);
    85             Log.logSSL("SSLDelegate: Setting application protocols: {0}" + Arrays.toString(alpn));
    83 //            Log.logSSL("SSLDelegate: Setting application protocols: {0}" + Arrays.toString(alpn));
    86         } else {
    84 //        } else {
    87             Log.logSSL("SSLDelegate: No application protocols proposed");
    85 //            Log.logSSL("SSLDelegate: No application protocols proposed");
    88         }
    86 //        }
    89         engine.setSSLParameters(sslParameters);
    87 //        engine.setSSLParameters(sslParameters);
    90         wrapper = new EngineWrapper(chan, engine);
    88 //        wrapper = new EngineWrapper(chan, engine);
    91         this.chan = chan;
    89 //        this.chan = chan;
    92         this.client = client;
    90 //        this.client = client;
    93     }
    91 //    }
    94 
    92 
    95     SSLParameters getSSLParameters() {
    93 //    SSLParameters getSSLParameters() {
    96         return sslParameters;
    94 //        return sslParameters;
    97     }
    95 //    }
    98 
    96 
    99     static long countBytes(ByteBuffer[] buffers, int start, int number) {
    97     static long countBytes(ByteBuffer[] buffers, int start, int number) {
   100         long c = 0;
    98         long c = 0;
   101         for (int i=0; i<number; i++) {
    99         for (int i=0; i<number; i++) {
   102             c+= buffers[start+i].remaining();
   100             c+= buffers[start+i].remaining();
   206             unwrapLock = new Object();
   204             unwrapLock = new Object();
   207             unwrap_src = allocate(BufType.PACKET);
   205             unwrap_src = allocate(BufType.PACKET);
   208             wrap_dst = allocate(BufType.PACKET);
   206             wrap_dst = allocate(BufType.PACKET);
   209         }
   207         }
   210 
   208 
   211         void close () throws IOException {
   209 //        void close () throws IOException {
   212         }
   210 //        }
   213 
   211 
   214         WrapperResult wrapAndSend(ByteBuffer src, boolean ignoreClose)
   212         WrapperResult wrapAndSend(ByteBuffer src, boolean ignoreClose)
   215             throws IOException
   213             throws IOException
   216         {
   214         {
   217             ByteBuffer[] buffers = new ByteBuffer[1];
   215             ByteBuffer[] buffers = new ByteBuffer[1];
   321             u_remaining = unwrap_src.remaining();
   319             u_remaining = unwrap_src.remaining();
   322             return r;
   320             return r;
   323         }
   321         }
   324     }
   322     }
   325 
   323 
   326     WrapperResult sendData (ByteBuffer src) throws IOException {
   324 //    WrapperResult sendData (ByteBuffer src) throws IOException {
   327         ByteBuffer[] buffers = new ByteBuffer[1];
   325 //        ByteBuffer[] buffers = new ByteBuffer[1];
   328         buffers[0] = src;
   326 //        buffers[0] = src;
   329         return sendData(buffers, 0, 1);
   327 //        return sendData(buffers, 0, 1);
   330     }
   328 //    }
   331 
   329 
   332     /**
   330     /**
   333      * send the data in the given ByteBuffer. If a handshake is needed
   331      * send the data in the given ByteBuffer. If a handshake is needed
   334      * then this is handled within this method. When this call returns,
   332      * then this is handled within this method. When this call returns,
   335      * all of the given user data has been sent and any handshake has been
   333      * all of the given user data has been sent and any handshake has been
   454         } finally {
   452         } finally {
   455             handshaking.unlock();
   453             handshaking.unlock();
   456         }
   454         }
   457     }
   455     }
   458 
   456 
   459     static void printParams(SSLParameters p) {
   457 //    static void printParams(SSLParameters p) {
   460         System.out.println("SSLParameters:");
   458 //        System.out.println("SSLParameters:");
   461         if (p == null) {
   459 //        if (p == null) {
   462             System.out.println("Null params");
   460 //            System.out.println("Null params");
   463             return;
   461 //            return;
   464         }
   462 //        }
   465         for (String cipher : p.getCipherSuites()) {
   463 //        for (String cipher : p.getCipherSuites()) {
   466                 System.out.printf("cipher: %s\n", cipher);
   464 //                System.out.printf("cipher: %s\n", cipher);
   467         }
   465 //        }
   468         // JDK 8 EXCL START
   466 //        // JDK 8 EXCL START
   469         for (String approto : p.getApplicationProtocols()) {
   467 //        for (String approto : p.getApplicationProtocols()) {
   470                 System.out.printf("application protocol: %s\n", approto);
   468 //                System.out.printf("application protocol: %s\n", approto);
   471         }
   469 //        }
   472         // JDK 8 EXCL END
   470 //        // JDK 8 EXCL END
   473         for (String protocol : p.getProtocols()) {
   471 //        for (String protocol : p.getProtocols()) {
   474                 System.out.printf("protocol: %s\n", protocol);
   472 //                System.out.printf("protocol: %s\n", protocol);
   475         }
   473 //        }
   476         if (p.getServerNames() != null) {
   474 //        if (p.getServerNames() != null) {
   477             for (SNIServerName sname : p.getServerNames()) {
   475 //            for (SNIServerName sname : p.getServerNames()) {
   478                 System.out.printf("server name: %s\n", sname.toString());
   476 //                System.out.printf("server name: %s\n", sname.toString());
   479             }
   477 //            }
   480         }
   478 //        }
   481     }
   479 //    }
   482 
   480 
   483     String getSessionInfo() {
   481     String getSessionInfo() {
   484         StringBuilder sb = new StringBuilder();
   482         StringBuilder sb = new StringBuilder();
   485         String application = engine.getApplicationProtocol();
   483         String application = engine.getApplicationProtocol();
   486         SSLSession sess = engine.getSession();
   484         SSLSession sess = engine.getSession();