src/java.net.http/share/classes/jdk/internal/net/http/AsyncSSLTunnelConnection.java
author egahlin
Fri, 22 Nov 2019 17:20:43 +0100
changeset 59226 a0f39cc47387
parent 51364 31d9e82b2e64
child 56868 67c7659ecda5
permissions -rw-r--r--
8233700: EventStream not closed Reviewed-by: mgronlun, mseledtsov
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;
51364
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 49944
diff changeset
    32
import java.util.function.Function;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 49944
diff changeset
    33
import jdk.internal.net.http.common.MinimalFuture;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    34
import jdk.internal.net.http.common.SSLTube;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    35
import jdk.internal.net.http.common.Utils;
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    36
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    37
/**
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    38
 * An SSL tunnel built on a Plain (CONNECT) TCP tunnel.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    39
 */
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    40
class AsyncSSLTunnelConnection extends AbstractAsyncSSLConnection {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    41
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    42
    final PlainTunnelingConnection plainConnection;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    43
    final PlainHttpPublisher writePublisher;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    44
    volatile SSLTube flow;
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    45
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    46
    AsyncSSLTunnelConnection(InetSocketAddress addr,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    47
                             HttpClientImpl client,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    48
                             String[] alpn,
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    49
                             InetSocketAddress proxy,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    50
                             HttpHeaders proxyHeaders)
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    51
    {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    52
        super(addr, client, Utils.getServerName(addr), addr.getPort(), alpn);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    53
        this.plainConnection = new PlainTunnelingConnection(addr, proxy, client, proxyHeaders);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    54
        this.writePublisher = new PlainHttpPublisher();
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    55
    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    56
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    57
    @Override
51364
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 49944
diff changeset
    58
    public CompletableFuture<Void> connectAsync(Exchange<?> exchange) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    59
        if (debug.on()) debug.log("Connecting plain tunnel connection");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    60
        // This will connect the PlainHttpConnection flow, so that
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    61
        // its HttpSubscriber and HttpPublisher are subscribed to the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    62
        // SocketTube
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    63
        return plainConnection
51364
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 49944
diff changeset
    64
                .connectAsync(exchange)
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    65
                .thenApply( unused -> {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    66
                    if (debug.on()) debug.log("creating SSLTube");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    67
                    // create the SSLTube wrapping the SocketTube, with the given engine
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    68
                    flow = new SSLTube(engine,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    69
                                       client().theExecutor(),
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
    70
                                       client().getSSLBufferSupplier()::recycle,
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    71
                                       plainConnection.getConnectionFlow());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    72
                    return null;} );
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    73
    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    74
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    75
    @Override
51364
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 49944
diff changeset
    76
    public CompletableFuture<Void> finishConnect() {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 49944
diff changeset
    77
        // The actual ALPN value, which may be the empty string, is not
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 49944
diff changeset
    78
        // interesting at this point, only that the handshake has completed.
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 49944
diff changeset
    79
        return getALPN()
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 49944
diff changeset
    80
                .handle((String unused, Throwable ex) -> {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 49944
diff changeset
    81
                    if (ex == null) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 49944
diff changeset
    82
                        return plainConnection.finishConnect();
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 49944
diff changeset
    83
                    } else {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 49944
diff changeset
    84
                        plainConnection.close();
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 49944
diff changeset
    85
                        return MinimalFuture.<Void>failedFuture(ex);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 49944
diff changeset
    86
                    } })
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 49944
diff changeset
    87
                .thenCompose(Function.identity());
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 49944
diff changeset
    88
    }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 49944
diff changeset
    89
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 49944
diff changeset
    90
    @Override
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    91
    boolean isTunnel() { return true; }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    92
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    93
    @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    94
    boolean connected() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    95
        return plainConnection.connected(); // && sslDelegate.connected();
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    96
    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    97
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    98
    @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    99
    HttpPublisher publisher() { return writePublisher; }
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   100
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   101
    @Override
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   102
    public String toString() {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   103
        return "AsyncSSLTunnelConnection: " + super.toString();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   104
    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   105
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   106
    @Override
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   107
    ConnectionPool.CacheKey cacheKey() {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   108
        return ConnectionPool.cacheKey(address, plainConnection.proxyAddr);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   109
    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   110
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   111
    @Override
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   112
    public void close() {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   113
        plainConnection.close();
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   114
    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   115
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   116
    @Override
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   117
    SocketChannel channel() {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   118
        return plainConnection.channel();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   119
    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   120
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   121
    @Override
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   122
    boolean isProxied() {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   123
        return true;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   124
    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   125
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   126
    @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   127
    SSLTube getConnectionFlow() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   128
       return flow;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   129
   }
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   130
}