src/java.net.http/share/classes/jdk/internal/net/http/AsyncSSLTunnelConnection.java
author dfuchs
Mon, 23 Apr 2018 15:45:40 +0100
branchhttp-client-branch
changeset 56474 fe2bf7b369b8
parent 56451 9585061fdb04
child 56507 2294c51eae30
permissions -rw-r--r--
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
     1
/*
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
     4
 *
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    10
 *
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    15
 * accompanied this code).
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    16
 *
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    20
 *
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    23
 * questions.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    24
 */
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    25
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    26
package jdk.internal.net.http;
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    27
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    28
import java.net.InetSocketAddress;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    29
import java.nio.channels.SocketChannel;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    30
import java.util.concurrent.CompletableFuture;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    31
import java.net.http.HttpHeaders;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    32
import jdk.internal.net.http.common.SSLTube;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    33
import jdk.internal.net.http.common.Utils;
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    34
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    35
/**
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    36
 * An SSL tunnel built on a Plain (CONNECT) TCP tunnel.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    37
 */
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    38
class AsyncSSLTunnelConnection extends AbstractAsyncSSLConnection {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    39
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    40
    final PlainTunnelingConnection plainConnection;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    41
    final PlainHttpPublisher writePublisher;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    42
    volatile SSLTube flow;
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    43
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    44
    AsyncSSLTunnelConnection(InetSocketAddress addr,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    45
                             HttpClientImpl client,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    46
                             String[] alpn,
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    47
                             InetSocketAddress proxy,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    48
                             HttpHeaders proxyHeaders)
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    49
    {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    50
        super(addr, client, Utils.getServerName(addr), addr.getPort(), alpn);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    51
        this.plainConnection = new PlainTunnelingConnection(addr, proxy, client, proxyHeaders);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    52
        this.writePublisher = new PlainHttpPublisher();
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    53
    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    54
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    55
    @Override
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    56
    public CompletableFuture<Void> connectAsync() {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    57
        if (debug.on()) debug.log("Connecting plain tunnel connection");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    58
        // This will connect the PlainHttpConnection flow, so that
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    59
        // its HttpSubscriber and HttpPublisher are subscribed to the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    60
        // SocketTube
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    61
        return plainConnection
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    62
                .connectAsync()
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    63
                .thenApply( unused -> {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    64
                    if (debug.on()) debug.log("creating SSLTube");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    65
                    // create the SSLTube wrapping the SocketTube, with the given engine
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    66
                    flow = new SSLTube(engine,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    67
                                       client().theExecutor(),
56474
fe2bf7b369b8 http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents: 56451
diff changeset
    68
                                       client().getSSLBufferSupplier()::recycle,
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    69
                                       plainConnection.getConnectionFlow());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    70
                    return null;} );
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    71
    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    72
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    73
    @Override
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    74
    boolean isTunnel() { return true; }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    75
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    76
    @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    77
    boolean connected() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    78
        return plainConnection.connected(); // && sslDelegate.connected();
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    79
    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    80
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    81
    @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    82
    HttpPublisher publisher() { return writePublisher; }
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    83
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    84
    @Override
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    85
    public String toString() {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    86
        return "AsyncSSLTunnelConnection: " + super.toString();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    87
    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    88
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    89
    @Override
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    90
    PlainTunnelingConnection plainConnection() {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    91
        return plainConnection;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    92
    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    93
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    94
    @Override
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    95
    ConnectionPool.CacheKey cacheKey() {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    96
        return ConnectionPool.cacheKey(address, plainConnection.proxyAddr);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    97
    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    98
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    99
    @Override
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   100
    public void close() {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   101
        plainConnection.close();
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   102
    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   103
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   104
    @Override
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   105
    SocketChannel channel() {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   106
        return plainConnection.channel();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   107
    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   108
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   109
    @Override
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   110
    boolean isProxied() {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   111
        return true;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   112
    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   113
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   114
    @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   115
    SSLTube getConnectionFlow() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   116
       return flow;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   117
   }
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   118
}