src/java.net.http/share/classes/jdk/internal/net/http/Http2ClientImpl.java
author chegar
Tue, 17 Apr 2018 08:54:17 -0700
changeset 49765 ee6f7a61f3a5
parent 48376 src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/Http2ClientImpl.java@41ae5c69b09c
child 49944 4690a2871b44
child 56451 9585061fdb04
permissions -rw-r--r--
8197564: HTTP Client implementation Reviewed-by: chegar, dfuchs, michaelm, prappo Contributed-by: Chris Hegarty <chris.hegarty@oracle.com>, Daniel Fuchs <daniel.fuchs@oracle.com>, Michael McMahon <michael.x.mcmahon@oracle.com>, Pavel Rappo <pavel.rappo@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     1
/*
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     4
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    10
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    15
 * accompanied this code).
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    16
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    20
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    23
 * questions.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    24
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    25
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    26
package jdk.internal.net.http;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    27
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    28
import java.net.InetSocketAddress;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    29
import java.net.URI;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    30
import java.util.Base64;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    31
import java.util.Collections;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    32
import java.util.HashSet;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    33
import java.util.Map;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    34
import java.util.Set;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    35
import java.util.concurrent.ConcurrentHashMap;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    36
import java.util.concurrent.CompletableFuture;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    37
import jdk.internal.net.http.common.Log;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    38
import jdk.internal.net.http.common.Logger;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    39
import jdk.internal.net.http.common.MinimalFuture;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    40
import jdk.internal.net.http.common.Utils;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    41
import jdk.internal.net.http.frame.SettingsFrame;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    42
import static jdk.internal.net.http.frame.SettingsFrame.INITIAL_WINDOW_SIZE;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    43
import static jdk.internal.net.http.frame.SettingsFrame.ENABLE_PUSH;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    44
import static jdk.internal.net.http.frame.SettingsFrame.HEADER_TABLE_SIZE;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    45
import static jdk.internal.net.http.frame.SettingsFrame.MAX_CONCURRENT_STREAMS;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    46
import static jdk.internal.net.http.frame.SettingsFrame.MAX_FRAME_SIZE;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    47
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    48
/**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    49
 *  Http2 specific aspects of HttpClientImpl
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    50
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    51
class Http2ClientImpl {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    52
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    53
    final static Logger debug =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    54
            Utils.getDebugLogger("Http2ClientImpl"::toString, Utils.DEBUG);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    55
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    56
    private final HttpClientImpl client;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    57
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    58
    Http2ClientImpl(HttpClientImpl client) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    59
        this.client = client;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    60
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    61
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    62
    /* Map key is "scheme:host:port" */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    63
    private final Map<String,Http2Connection> connections = new ConcurrentHashMap<>();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    64
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    65
    private final Set<String> failures = Collections.synchronizedSet(new HashSet<>());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    66
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    67
    /**
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    68
     * When HTTP/2 requested only. The following describes the aggregate behavior including the
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    69
     * calling code. In all cases, the HTTP2 connection cache
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    70
     * is checked first for a suitable connection and that is returned if available.
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    71
     * If not, a new connection is opened, except in https case when a previous negotiate failed.
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    72
     * In that case, we want to continue using http/1.1. When a connection is to be opened and
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    73
     * if multiple requests are sent in parallel then each will open a new connection.
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    74
     *
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    75
     * If negotiation/upgrade succeeds then
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    76
     * one connection will be put in the cache and the others will be closed
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    77
     * after the initial request completes (not strictly necessary for h2, only for h2c)
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    78
     *
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    79
     * If negotiate/upgrade fails, then any opened connections remain open (as http/1.1)
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    80
     * and will be used and cached in the http/1 cache. Note, this method handles the
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    81
     * https failure case only (by completing the CF with an ALPN exception, handled externally)
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    82
     * The h2c upgrade is handled externally also.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    83
     *
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    84
     * Specific CF behavior of this method.
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    85
     * 1. completes with ALPN exception: h2 negotiate failed for first time. failure recorded.
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    86
     * 2. completes with other exception: failure not recorded. Caller must handle
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    87
     * 3. completes normally with null: no connection in cache for h2c or h2 failed previously
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    88
     * 4. completes normally with connection: h2 or h2c connection in cache. Use it.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    89
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    90
    CompletableFuture<Http2Connection> getConnectionFor(HttpRequestImpl req) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    91
        URI uri = req.uri();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    92
        InetSocketAddress proxy = req.proxy();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    93
        String key = Http2Connection.keyFor(uri, proxy);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    94
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
    95
        synchronized (this) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    96
            Http2Connection connection = connections.get(key);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    97
            if (connection != null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    98
                if (connection.closed) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    99
                    if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   100
                        debug.log("removing found closed connection: %s", connection);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   101
                    connections.remove(key);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   102
                } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   103
                    // fast path if connection already exists
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   104
                    if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   105
                        debug.log("found connection in the pool: %s", connection);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   106
                    return MinimalFuture.completedFuture(connection);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   107
                }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   108
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   109
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   110
            if (!req.secure() || failures.contains(key)) {
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   111
                // secure: negotiate failed before. Use http/1.1
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   112
                // !secure: no connection available in cache. Attempt upgrade
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   113
                if (debug.on()) debug.log("not found in connection pool");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   114
                return MinimalFuture.completedFuture(null);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   115
            }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   116
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   117
        return Http2Connection
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   118
                .createAsync(req, this)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   119
                .whenComplete((conn, t) -> {
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   120
                    synchronized (Http2ClientImpl.this) {
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   121
                        if (conn != null) {
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   122
                            offerConnection(conn);
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   123
                        } else {
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   124
                            Throwable cause = Utils.getCompletionCause(t);
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   125
                            if (cause instanceof Http2Connection.ALPNException)
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   126
                                failures.add(key);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   127
                        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   128
                    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   129
                });
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   130
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   131
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   132
    /*
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   133
     * Cache the given connection, if no connection to the same
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   134
     * destination exists. If one exists, then we let the initial stream
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   135
     * complete but allow it to close itself upon completion.
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   136
     * This situation should not arise with https because the request
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   137
     * has not been sent as part of the initial alpn negotiation
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   138
     */
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   139
    boolean offerConnection(Http2Connection c) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   140
        if (debug.on()) debug.log("offering to the connection pool: %s", c);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   141
        if (c.closed) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   142
            if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   143
                debug.log("skipping offered closed connection: %s", c);
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   144
            return false;
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   145
        }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   146
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   147
        String key = c.key();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   148
        synchronized(this) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   149
            Http2Connection c1 = connections.putIfAbsent(key, c);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   150
            if (c1 != null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   151
                c.setSingleStream(true);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   152
                if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   153
                    debug.log("existing entry in connection pool for %s", key);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   154
                return false;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   155
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   156
            if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   157
                debug.log("put in the connection pool: %s", c);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   158
            return true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   159
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   160
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   161
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   162
    void deleteConnection(Http2Connection c) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   163
        if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   164
            debug.log("removing from the connection pool: %s", c);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   165
        synchronized (this) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   166
            connections.remove(c.key());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   167
            if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   168
                debug.log("removed from the connection pool: %s", c);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   169
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   170
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   171
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   172
    void stop() {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   173
        if (debug.on()) debug.log("stopping");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   174
        connections.values().forEach(this::close);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   175
        connections.clear();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   176
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   177
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   178
    private void close(Http2Connection h2c) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   179
        try { h2c.close(); } catch (Throwable t) {}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   180
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   181
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   182
    HttpClientImpl client() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   183
        return client;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   184
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   185
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   186
    /** Returns the client settings as a base64 (url) encoded string */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   187
    String getSettingsString() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   188
        SettingsFrame sf = getClientSettings();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   189
        byte[] settings = sf.toByteArray(); // without the header
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   190
        Base64.Encoder encoder = Base64.getUrlEncoder()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   191
                                       .withoutPadding();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   192
        return encoder.encodeToString(settings);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   193
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   194
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   195
    private static final int K = 1024;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   196
48263
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   197
    private static int getParameter(String property, int min, int max, int defaultValue) {
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   198
        int value =  Utils.getIntegerNetProperty(property, defaultValue);
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   199
        // use default value if misconfigured
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   200
        if (value < min || value > max) {
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   201
            Log.logError("Property value for {0}={1} not in [{2}..{3}]: " +
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   202
                    "using default={4}", property, value, min, max, defaultValue);
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   203
            value = defaultValue;
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   204
        }
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   205
        return value;
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   206
    }
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   207
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   208
    // used for the connection window, to have a connection window size
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   209
    // bigger than the initial stream window size.
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   210
    int getConnectionWindowSize(SettingsFrame clientSettings) {
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   211
        // Maximum size is 2^31-1. Don't allow window size to be less
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   212
        // than the stream window size. HTTP/2 specify a default of 64 * K -1,
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   213
        // but we use 2^26 by default for better performance.
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   214
        int streamWindow = clientSettings.getParameter(INITIAL_WINDOW_SIZE);
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   215
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   216
        // The default is the max between the stream window size
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   217
        // and the connection window size.
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   218
        int defaultValue = Math.min(Integer.MAX_VALUE,
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   219
                Math.max(streamWindow, K*K*32));
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   220
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   221
        return getParameter(
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   222
                "jdk.httpclient.connectionWindowSize",
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   223
                streamWindow, Integer.MAX_VALUE, defaultValue);
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   224
    }
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   225
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   226
    SettingsFrame getClientSettings() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   227
        SettingsFrame frame = new SettingsFrame();
48263
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   228
        // default defined for HTTP/2 is 4 K, we use 16 K.
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   229
        frame.setParameter(HEADER_TABLE_SIZE, getParameter(
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   230
                "jdk.httpclient.hpack.maxheadertablesize",
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   231
                0, Integer.MAX_VALUE, 16 * K));
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   232
        // O: does not accept push streams. 1: accepts push streams.
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   233
        frame.setParameter(ENABLE_PUSH, getParameter(
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   234
                "jdk.httpclient.enablepush",
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   235
                0, 1, 1));
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   236
        // HTTP/2 recommends to set the number of concurrent streams
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   237
        // no lower than 100. We use 100. 0 means no stream would be
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   238
        // accepted. That would render the client to be non functional,
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   239
        // so we won't let 0 be configured for our Http2ClientImpl.
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   240
        frame.setParameter(MAX_CONCURRENT_STREAMS, getParameter(
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   241
                "jdk.httpclient.maxstreams",
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   242
                1, Integer.MAX_VALUE, 100));
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   243
        // Maximum size is 2^31-1. Don't allow window size to be less
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   244
        // than the minimum frame size as this is likely to be a
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   245
        // configuration error. HTTP/2 specify a default of 64 * K -1,
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   246
        // but we use 16 M  for better performance.
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   247
        frame.setParameter(INITIAL_WINDOW_SIZE, getParameter(
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   248
                "jdk.httpclient.windowsize",
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   249
                16 * K, Integer.MAX_VALUE, 16*K*K));
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   250
        // HTTP/2 specify a minimum size of 16 K, a maximum size of 2^24-1,
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   251
        // and a default of 16 K. We use 16 K as default.
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   252
        frame.setParameter(MAX_FRAME_SIZE, getParameter(
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   253
                "jdk.httpclient.maxframesize",
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   254
                16 * K, 16 * K * K -1, 16 * K));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   255
        return frame;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   256
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   257
}