src/java.net.http/share/classes/jdk/internal/net/http/Http2Connection.java
author michaelm
Mon, 28 May 2018 10:48:38 +0100
branchhttp-client-branch
changeset 56616 5d2446adafaf
parent 56598 4c502e3991bf
child 56619 57f17e890a40
permissions -rw-r--r--
http-client-branch: fix intermittent problem with stream counting by decrementing counter before completing the subscriber CF
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
/*
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
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: 48523
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
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    28
import java.io.EOFException;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    29
import java.io.IOException;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    30
import java.io.UncheckedIOException;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    31
import java.lang.System.Logger.Level;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    32
import java.net.InetSocketAddress;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    33
import java.net.URI;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    34
import java.nio.ByteBuffer;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    35
import java.nio.charset.StandardCharsets;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    36
import java.util.Iterator;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    37
import java.util.LinkedList;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    38
import java.util.List;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    39
import java.util.Locale;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    40
import java.util.Map;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    41
import java.util.Set;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    42
import java.util.concurrent.CompletableFuture;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    43
import java.util.ArrayList;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    44
import java.util.Objects;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    45
import java.util.concurrent.ConcurrentHashMap;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    46
import java.util.concurrent.ConcurrentLinkedQueue;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    47
import java.util.concurrent.Flow;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    48
import java.util.function.Function;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    49
import java.util.function.Supplier;
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
    50
import javax.net.ssl.SSLEngine;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    51
import javax.net.ssl.SSLException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    52
import java.net.http.HttpClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    53
import java.net.http.HttpHeaders;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    54
import jdk.internal.net.http.HttpConnection.HttpPublisher;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    55
import jdk.internal.net.http.common.FlowTube;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    56
import jdk.internal.net.http.common.FlowTube.TubeSubscriber;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    57
import jdk.internal.net.http.common.HttpHeadersImpl;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    58
import jdk.internal.net.http.common.Log;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    59
import jdk.internal.net.http.common.Logger;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    60
import jdk.internal.net.http.common.MinimalFuture;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    61
import jdk.internal.net.http.common.SequentialScheduler;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    62
import jdk.internal.net.http.common.Utils;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    63
import jdk.internal.net.http.frame.ContinuationFrame;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    64
import jdk.internal.net.http.frame.DataFrame;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    65
import jdk.internal.net.http.frame.ErrorFrame;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    66
import jdk.internal.net.http.frame.FramesDecoder;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    67
import jdk.internal.net.http.frame.FramesEncoder;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    68
import jdk.internal.net.http.frame.GoAwayFrame;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    69
import jdk.internal.net.http.frame.HeaderFrame;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    70
import jdk.internal.net.http.frame.HeadersFrame;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    71
import jdk.internal.net.http.frame.Http2Frame;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    72
import jdk.internal.net.http.frame.MalformedFrame;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    73
import jdk.internal.net.http.frame.OutgoingHeaders;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    74
import jdk.internal.net.http.frame.PingFrame;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    75
import jdk.internal.net.http.frame.PushPromiseFrame;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    76
import jdk.internal.net.http.frame.ResetFrame;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    77
import jdk.internal.net.http.frame.SettingsFrame;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    78
import jdk.internal.net.http.frame.WindowUpdateFrame;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    79
import jdk.internal.net.http.hpack.Encoder;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    80
import jdk.internal.net.http.hpack.Decoder;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    81
import jdk.internal.net.http.hpack.DecodingCallback;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    82
import static java.nio.charset.StandardCharsets.UTF_8;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    83
import static jdk.internal.net.http.frame.SettingsFrame.*;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    84
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    85
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    86
/**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    87
 * An Http2Connection. Encapsulates the socket(channel) and any SSLEngine used
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    88
 * over it. Contains an HttpConnection which hides the SocketChannel SSL stuff.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    89
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    90
 * Http2Connections belong to a Http2ClientImpl, (one of) which belongs
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    91
 * to a HttpClientImpl.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    92
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    93
 * Creation cases:
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    94
 * 1) upgraded HTTP/1.1 plain tcp connection
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    95
 * 2) prior knowledge directly created plain tcp connection
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    96
 * 3) directly created HTTP/2 SSL connection which uses ALPN.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    97
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    98
 * Sending is done by writing directly to underlying HttpConnection object which
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    99
 * is operating in async mode. No flow control applies on output at this level
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   100
 * and all writes are just executed as puts to an output Q belonging to HttpConnection
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   101
 * Flow control is implemented by HTTP/2 protocol itself.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   102
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   103
 * Hpack header compression
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   104
 * and outgoing stream creation is also done here, because these operations
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   105
 * must be synchronized at the socket level. Stream objects send frames simply
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   106
 * by placing them on the connection's output Queue. sendFrame() is called
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   107
 * from a higher level (Stream) thread.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   108
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   109
 * asyncReceive(ByteBuffer) is always called from the selector thread. It assembles
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   110
 * incoming Http2Frames, and directs them to the appropriate Stream.incoming()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   111
 * or handles them directly itself. This thread performs hpack decompression
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   112
 * and incoming stream creation (Server push). Incoming frames destined for a
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   113
 * stream are provided by calling Stream.incoming().
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   114
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   115
class Http2Connection  {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   116
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   117
    final Logger debug = Utils.getDebugLogger(this::dbgString, Utils.DEBUG);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   118
    final static Logger DEBUG_LOGGER =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   119
            Utils.getDebugLogger("Http2Connection"::toString, Utils.DEBUG);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   120
    private final Logger debugHpack =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   121
            Utils.getHpackLogger(this::dbgString, Utils.DEBUG_HPACK);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   122
    static final ByteBuffer EMPTY_TRIGGER = ByteBuffer.allocate(0);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   123
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   124
    static private final int MAX_CLIENT_STREAM_ID = Integer.MAX_VALUE; // 2147483647
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   125
    static private final int MAX_SERVER_STREAM_ID = Integer.MAX_VALUE - 1; // 2147483646
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   126
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   127
    /**
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   128
     * Flag set when no more streams to be opened on this connection.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   129
     * Two cases where it is used.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   130
     *
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   131
     * 1. Two connections to the same server were opened concurrently, in which
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   132
     *    case one of them will be put in the cache, and the second will expire
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   133
     *    when all its opened streams (which usually should be a single client
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   134
     *    stream + possibly some additional push-promise server streams) complete.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   135
     * 2. A cached connection reaches its maximum number of streams (~ 2^31-1)
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   136
     *    either server / or client allocated, in which case it will be taken
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   137
     *    out of the cache - allowing a new connection to replace it. It will
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   138
     *    expire when all its still open streams (which could be many) eventually
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   139
     *    complete.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   140
     */
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   141
    private boolean finalStream;
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   142
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   143
    /*
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   144
     * ByteBuffer pooling strategy for HTTP/2 protocol.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   145
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   146
     * In general there are 4 points where ByteBuffers are used:
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   147
     *  - incoming/outgoing frames from/to ByteBuffers plus incoming/outgoing
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   148
     *    encrypted data in case of SSL connection.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   149
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   150
     * 1. Outgoing frames encoded to ByteBuffers.
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   151
     *
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   152
     *  Outgoing ByteBuffers are created with required size and frequently
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   153
     *  small (except DataFrames, etc). At this place no pools at all. All
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   154
     *  outgoing buffers should eventually be collected by GC.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   155
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   156
     * 2. Incoming ByteBuffers (decoded to frames).
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   157
     *
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   158
     *  Here, total elimination of BB pool is not a good idea.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   159
     *  We don't know how many bytes we will receive through network.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   160
     *
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   161
     *  A possible future improvement ( currently not implemented ):
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   162
     *  Allocate buffers of reasonable size. The following life of the BB:
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   163
     *   - If all frames decoded from the BB are other than DataFrame and
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   164
     *     HeaderFrame (and HeaderFrame subclasses) BB is returned to pool,
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   165
     *   - If a DataFrame is decoded from the BB. In that case DataFrame refers
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   166
     *     to sub-buffer obtained by slice(). Such a BB is never returned to the
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   167
     *     pool and will eventually be GC'ed.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   168
     *   - If a HeadersFrame is decoded from the BB. Then header decoding is
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   169
     *     performed inside processFrame method and the buffer could be release
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   170
     *     back to pool.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   171
     *
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   172
     * 3. SSL encrypted buffers ( received ).
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   173
     *
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   174
     *  The current implementation recycles encrypted buffers read from the
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   175
     *  channel. The pool of buffers has a maximum size of 3, SocketTube.MAX_BUFFERS,
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   176
     *  direct buffers which are shared by all connections on a given client.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   177
     *  The pool is used by all SSL connections - whether HTTP/1.1 or HTTP/2,
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   178
     *  but only for SSL encrypted buffers that circulate between the SocketTube
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   179
     *  Publisher and the SSLFlowDelegate Reader. Limiting the pool to this
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   180
     *  particular segment allows the use of direct buffers, thus avoiding any
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   181
     *  additional copy in the NIO socket channel implementation. See
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   182
     *  HttpClientImpl.SSLDirectBufferSupplier, SocketTube.SSLDirectBufferSource,
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   183
     *  and SSLTube.recycler.
42460
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
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   187
    // A small class that allows to control frames with respect to the state of
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   188
    // the connection preface. Any data received before the connection
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   189
    // preface is sent will be buffered.
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   190
    private final class FramesController {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   191
        volatile boolean prefaceSent;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   192
        volatile List<ByteBuffer> pending;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   193
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   194
        boolean processReceivedData(FramesDecoder decoder, ByteBuffer buf)
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   195
                throws IOException
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   196
        {
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   197
            // if preface is not sent, buffers data in the pending list
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   198
            if (!prefaceSent) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   199
                if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   200
                    debug.log("Preface not sent: buffering %d", buf.remaining());
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   201
                synchronized (this) {
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   202
                    if (!prefaceSent) {
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   203
                        if (pending == null) pending = new ArrayList<>();
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   204
                        pending.add(buf);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   205
                        if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   206
                            debug.log("there are now %d bytes buffered waiting for preface to be sent"
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   207
                                    + Utils.remaining(pending)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   208
                            );
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   209
                        return false;
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   210
                    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   211
                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   212
            }
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   213
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   214
            // Preface is sent. Checks for pending data and flush it.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   215
            // We rely on this method being called from within the Http2TubeSubscriber
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   216
            // scheduler, so we know that no other thread could execute this method
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   217
            // concurrently while we're here.
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   218
            // This ensures that later incoming buffers will not
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   219
            // be processed before we have flushed the pending queue.
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   220
            // No additional synchronization is therefore necessary here.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   221
            List<ByteBuffer> pending = this.pending;
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   222
            this.pending = null;
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   223
            if (pending != null) {
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   224
                // flush pending data
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   225
                if (debug.on()) debug.log(() -> "Processing buffered data: "
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   226
                      + Utils.remaining(pending));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   227
                for (ByteBuffer b : pending) {
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   228
                    decoder.decode(b);
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   229
                }
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   230
            }
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   231
            // push the received buffer to the frames decoder.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   232
            if (buf != EMPTY_TRIGGER) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   233
                if (debug.on()) debug.log("Processing %d", buf.remaining());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   234
                decoder.decode(buf);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   235
            }
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   236
            return true;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   237
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   238
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   239
        // Mark that the connection preface is sent
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   240
        void markPrefaceSent() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   241
            assert !prefaceSent;
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   242
            synchronized (this) {
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   243
                prefaceSent = true;
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   244
            }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   245
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   246
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   247
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   248
    volatile boolean closed;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   249
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   250
    //-------------------------------------
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   251
    final HttpConnection connection;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   252
    private final Http2ClientImpl client2;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   253
    private final Map<Integer,Stream<?>> streams = new ConcurrentHashMap<>();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   254
    private int nextstreamid;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   255
    private int nextPushStream = 2;
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   256
    // actual stream ids are not allocated until the Headers frame is ready
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   257
    // to be sent. The following two fields are updated as soon as a stream
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   258
    // is created and assigned to a connection. They are checked before
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   259
    // assigning a stream to a connection.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   260
    private int lastReservedClientStreamid = 1;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   261
    private int lastReservedServerStreamid = 0;
56598
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   262
    private int numReservedClientStreams = 0; // count of current streams
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   263
    private int numReservedServerStreams = 0; // count of current streams
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   264
    private final Encoder hpackOut;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   265
    private final Decoder hpackIn;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   266
    final SettingsFrame clientSettings;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   267
    private volatile SettingsFrame serverSettings;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   268
    private final String key; // for HttpClientImpl.connections map
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   269
    private final FramesDecoder framesDecoder;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   270
    private final FramesEncoder framesEncoder = new FramesEncoder();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   271
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   272
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   273
     * Send Window controller for both connection and stream windows.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   274
     * Each of this connection's Streams MUST use this controller.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   275
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   276
    private final WindowController windowController = new WindowController();
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   277
    private final FramesController framesController = new FramesController();
56531
15ff86a732ea http-client-branch: connection shutdown should no cancel streams immediately if data is pending in the stream queue.
dfuchs
parents: 56513
diff changeset
   278
    private final Http2TubeSubscriber subscriber;
48263
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   279
    final ConnectionWindowUpdateSender windowUpdater;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   280
    private volatile Throwable cause;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   281
    private volatile Supplier<ByteBuffer> initial;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   282
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   283
    static final int DEFAULT_FRAME_SIZE = 16 * 1024;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   284
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   285
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   286
    // TODO: need list of control frames from other threads
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   287
    // that need to be sent
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   288
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   289
    private Http2Connection(HttpConnection connection,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   290
                            Http2ClientImpl client2,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   291
                            int nextstreamid,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   292
                            String key) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   293
        this.connection = connection;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   294
        this.client2 = client2;
56531
15ff86a732ea http-client-branch: connection shutdown should no cancel streams immediately if data is pending in the stream queue.
dfuchs
parents: 56513
diff changeset
   295
        this.subscriber = new Http2TubeSubscriber(client2.client());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   296
        this.nextstreamid = nextstreamid;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   297
        this.key = key;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   298
        this.clientSettings = this.client2.getClientSettings();
48263
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   299
        this.framesDecoder = new FramesDecoder(this::processFrame,
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   300
                clientSettings.getParameter(SettingsFrame.MAX_FRAME_SIZE));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   301
        // serverSettings will be updated by server
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   302
        this.serverSettings = SettingsFrame.getDefaultSettings();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   303
        this.hpackOut = new Encoder(serverSettings.getParameter(HEADER_TABLE_SIZE));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   304
        this.hpackIn = new Decoder(clientSettings.getParameter(HEADER_TABLE_SIZE));
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   305
        if (debugHpack.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   306
            debugHpack.log("For the record:" + super.toString());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   307
            debugHpack.log("Decoder created: %s", hpackIn);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   308
            debugHpack.log("Encoder created: %s", hpackOut);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   309
        }
48263
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   310
        this.windowUpdater = new ConnectionWindowUpdateSender(this,
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   311
                client2.getConnectionWindowSize(clientSettings));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   312
    }
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45530
diff changeset
   313
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45530
diff changeset
   314
    /**
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45530
diff changeset
   315
     * Case 1) Create from upgraded HTTP/1.1 connection.
56598
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   316
     * Is ready to use. Can't be SSL. exchange is the Exchange
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45530
diff changeset
   317
     * that initiated the connection, whose response will be delivered
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45530
diff changeset
   318
     * on a Stream.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45530
diff changeset
   319
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   320
    private Http2Connection(HttpConnection connection,
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   321
                    Http2ClientImpl client2,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   322
                    Exchange<?> exchange,
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   323
                    Supplier<ByteBuffer> initial)
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   324
        throws IOException, InterruptedException
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   325
    {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   326
        this(connection,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   327
                client2,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   328
                3, // stream 1 is registered during the upgrade
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   329
                keyFor(connection));
56598
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   330
        reserveStream(true);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   331
        Log.logTrace("Connection send window size {0} ", windowController.connectionWindowSize());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   332
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   333
        Stream<?> initialStream = createStream(exchange);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   334
        initialStream.registerStream(1);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   335
        windowController.registerStream(1, getInitialSendWindowSize());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   336
        initialStream.requestSent();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   337
        // Upgrading:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   338
        //    set callbacks before sending preface - makes sure anything that
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   339
        //    might be sent by the server will come our way.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   340
        this.initial = initial;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   341
        connectFlows(connection);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   342
        sendConnectionPreface();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   343
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   344
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   345
    // Used when upgrading an HTTP/1.1 connection to HTTP/2 after receiving
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   346
    // agreement from the server. Async style but completes immediately, because
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   347
    // the connection is already connected.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   348
    static CompletableFuture<Http2Connection> createAsync(HttpConnection connection,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   349
                                                          Http2ClientImpl client2,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   350
                                                          Exchange<?> exchange,
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   351
                                                          Supplier<ByteBuffer> initial)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   352
    {
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   353
        return MinimalFuture.supply(() -> new Http2Connection(connection, client2, exchange, initial));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   354
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   355
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   356
    // Requires TLS handshake. So, is really async
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   357
    static CompletableFuture<Http2Connection> createAsync(HttpRequestImpl request,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   358
                                                          Http2ClientImpl h2client) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   359
        assert request.secure();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   360
        AbstractAsyncSSLConnection connection = (AbstractAsyncSSLConnection)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   361
        HttpConnection.getConnection(request.getAddress(),
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   362
                                     h2client.client(),
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   363
                                     request,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   364
                                     HttpClient.Version.HTTP_2);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   365
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   366
        return connection.connectAsync()
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   367
                  .thenCompose(unused -> checkSSLConfig(connection))
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   368
                  .thenCompose(notused-> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   369
                      CompletableFuture<Http2Connection> cf = new MinimalFuture<>();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   370
                      try {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   371
                          Http2Connection hc = new Http2Connection(request, h2client, connection);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   372
                          cf.complete(hc);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   373
                      } catch (IOException e) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   374
                          cf.completeExceptionally(e);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   375
                      }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   376
                      return cf; } );
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   377
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   378
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   379
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   380
     * Cases 2) 3)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   381
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   382
     * request is request to be sent.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   383
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   384
    private Http2Connection(HttpRequestImpl request,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   385
                            Http2ClientImpl h2client,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   386
                            HttpConnection connection)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   387
        throws IOException
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   388
    {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   389
        this(connection,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   390
             h2client,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   391
             1,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   392
             keyFor(request.uri(), request.proxy()));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   393
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   394
        Log.logTrace("Connection send window size {0} ", windowController.connectionWindowSize());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   395
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
   396
        // safe to resume async reading now.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   397
        connectFlows(connection);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   398
        sendConnectionPreface();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   399
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   400
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   401
    private void connectFlows(HttpConnection connection) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   402
        FlowTube tube =  connection.getConnectionFlow();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   403
        // Connect the flow to our Http2TubeSubscriber:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   404
        tube.connectFlows(connection.publisher(), subscriber);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   405
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   406
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   407
    final HttpClientImpl client() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   408
        return client2.client();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   409
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   410
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   411
    // call these before assigning a request/stream to a connection
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   412
    // if false returned then a new Http2Connection is required
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   413
    // if true, the the stream may be assigned to this connection
56598
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   414
    // for server push, if false returned, then the stream should be cancelled
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   415
    synchronized boolean reserveStream(boolean clientInitiated) throws IOException {
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   416
        if (finalStream) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   417
            return false;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   418
        }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   419
        if (clientInitiated && (lastReservedClientStreamid + 2) >= MAX_CLIENT_STREAM_ID) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   420
            setFinalStream();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   421
            client2.deleteConnection(this);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   422
            return false;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   423
        } else if (!clientInitiated && (lastReservedServerStreamid + 2) >= MAX_SERVER_STREAM_ID) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   424
            setFinalStream();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   425
            client2.deleteConnection(this);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   426
            return false;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   427
        }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   428
        if (clientInitiated)
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   429
            lastReservedClientStreamid+=2;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   430
        else
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   431
            lastReservedServerStreamid+=2;
56598
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   432
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   433
        assert numReservedClientStreams >= 0;
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   434
        assert numReservedServerStreams >= 0;
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   435
        if (clientInitiated && numReservedClientStreams >= getMaxConcurrentClientStreams()) {
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   436
            throw new IOException("too many concurrent streams");
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   437
        } else if (clientInitiated) {
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   438
            numReservedClientStreams++;
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   439
        }
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   440
        if (!clientInitiated && numReservedServerStreams >= getMaxConcurrentServerStreams()) {
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   441
            return false;
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   442
        } else if (!clientInitiated) {
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   443
            numReservedServerStreams++;
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   444
        }
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   445
        return true;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   446
    }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   447
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
   448
    /**
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
   449
     * Throws an IOException if h2 was not negotiated
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
   450
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   451
    private static CompletableFuture<?> checkSSLConfig(AbstractAsyncSSLConnection aconn) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   452
        assert aconn.isSecure();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   453
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   454
        Function<String, CompletableFuture<Void>> checkAlpnCF = (alpn) -> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   455
            CompletableFuture<Void> cf = new MinimalFuture<>();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   456
            SSLEngine engine = aconn.getEngine();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   457
            assert Objects.equals(alpn, engine.getApplicationProtocol());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   458
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   459
            DEBUG_LOGGER.log("checkSSLConfig: alpn: %s", alpn );
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   460
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   461
            if (alpn == null || !alpn.equals("h2")) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   462
                String msg;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   463
                if (alpn == null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   464
                    Log.logSSL("ALPN not supported");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   465
                    msg = "ALPN not supported";
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   466
                } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   467
                    switch (alpn) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   468
                        case "":
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   469
                            Log.logSSL(msg = "No ALPN negotiated");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   470
                            break;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   471
                        case "http/1.1":
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   472
                            Log.logSSL( msg = "HTTP/1.1 ALPN returned");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   473
                            break;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   474
                        default:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   475
                            Log.logSSL(msg = "Unexpected ALPN: " + alpn);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   476
                            cf.completeExceptionally(new IOException(msg));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   477
                    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   478
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   479
                cf.completeExceptionally(new ALPNException(msg, aconn));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   480
                return cf;
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
   481
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   482
            cf.complete(null);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   483
            return cf;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   484
        };
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   485
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   486
        return aconn.getALPN()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   487
                .whenComplete((r,t) -> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   488
                    if (t != null && t instanceof SSLException) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   489
                        // something went wrong during the initial handshake
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   490
                        // close the connection
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   491
                        aconn.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   492
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   493
                })
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   494
                .thenCompose(checkAlpnCF);
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
   495
    }
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
   496
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   497
    synchronized boolean finalStream() {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   498
        return finalStream;
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   499
    }
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   500
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   501
    /**
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   502
     * Mark this connection so no more streams created on it and it will close when
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   503
     * all are complete.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   504
     */
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   505
    synchronized void setFinalStream() {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   506
        finalStream = true;
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   507
    }
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   508
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   509
    static String keyFor(HttpConnection connection) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   510
        boolean isProxy = connection.isProxied(); // tunnel or plain clear connection through proxy
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   511
        boolean isSecure = connection.isSecure();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   512
        InetSocketAddress addr = connection.address();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   513
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   514
        return keyString(isSecure, isProxy, addr.getHostString(), addr.getPort());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   515
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   516
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   517
    static String keyFor(URI uri, InetSocketAddress proxy) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   518
        boolean isSecure = uri.getScheme().equalsIgnoreCase("https");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   519
        boolean isProxy = proxy != null;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   520
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   521
        String host;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   522
        int port;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   523
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   524
        if (proxy != null && !isSecure) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   525
            // clear connection through proxy: use
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   526
            // proxy host / proxy port
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   527
            host = proxy.getHostString();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   528
            port = proxy.getPort();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   529
        } else {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   530
            // either secure tunnel connection through proxy
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   531
            // or direct connection to host, but in either
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   532
            // case only that host can be reached through
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   533
            // the connection: use target host / target port
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   534
            host = uri.getHost();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   535
            port = uri.getPort();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   536
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   537
        return keyString(isSecure, isProxy, host, port);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   538
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   539
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   540
    // {C,S}:{H:P}:host:port
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   541
    // C indicates clear text connection "http"
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   542
    // S indicates secure "https"
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   543
    // H indicates host (direct) connection
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   544
    // P indicates proxy
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   545
    // Eg: "S:H:foo.com:80"
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   546
    static String keyString(boolean secure, boolean proxy, String host, int port) {
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   547
        if (secure && port == -1)
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   548
            port = 443;
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   549
        else if (!secure && port == -1)
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   550
            port = 80;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   551
        return (secure ? "S:" : "C:") + (proxy ? "P:" : "H:") + host + ":" + port;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   552
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   553
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   554
    String key() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   555
        return this.key;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   556
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   557
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   558
    boolean offerConnection() {
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   559
        return client2.offerConnection(this);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   560
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   561
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   562
    private HttpPublisher publisher() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   563
        return connection.publisher();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   564
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   565
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   566
    private void decodeHeaders(HeaderFrame frame, DecodingCallback decoder)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   567
            throws IOException
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   568
    {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   569
        if (debugHpack.on()) debugHpack.log("decodeHeaders(%s)", decoder);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   570
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   571
        boolean endOfHeaders = frame.getFlag(HeaderFrame.END_HEADERS);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   572
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   573
        List<ByteBuffer> buffers = frame.getHeaderBlock();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   574
        int len = buffers.size();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   575
        for (int i = 0; i < len; i++) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   576
            ByteBuffer b = buffers.get(i);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   577
            hpackIn.decode(b, endOfHeaders && (i == len - 1), decoder);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   578
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   579
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   580
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   581
    final int getInitialSendWindowSize() {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   582
        return serverSettings.getParameter(INITIAL_WINDOW_SIZE);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   583
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   584
56598
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   585
    final int getMaxConcurrentClientStreams() {
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   586
        return serverSettings.getParameter(MAX_CONCURRENT_STREAMS);
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   587
    }
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   588
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   589
    final int getMaxConcurrentServerStreams() {
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   590
        return clientSettings.getParameter(MAX_CONCURRENT_STREAMS);
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   591
    }
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   592
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   593
    void close() {
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   594
        Log.logTrace("Closing HTTP/2 connection: to {0}", connection.address());
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   595
        GoAwayFrame f = new GoAwayFrame(0,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   596
                                        ErrorFrame.NO_ERROR,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   597
                                        "Requested by user".getBytes(UTF_8));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   598
        // TODO: set last stream. For now zero ok.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   599
        sendFrame(f);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   600
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   601
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   602
    long count;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   603
    final void asyncReceive(ByteBuffer buffer) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   604
        // We don't need to read anything and
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   605
        // we don't want to send anything back to the server
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   606
        // until the connection preface has been sent.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   607
        // Therefore we're going to wait if needed before reading
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   608
        // (and thus replying) to anything.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   609
        // Starting to reply to something (e.g send an ACK to a
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   610
        // SettingsFrame sent by the server) before the connection
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   611
        // preface is fully sent might result in the server
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   612
        // sending a GOAWAY frame with 'invalid_preface'.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   613
        //
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   614
        // Note: asyncReceive is only called from the Http2TubeSubscriber
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   615
        //       sequential scheduler.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   616
        try {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   617
            Supplier<ByteBuffer> bs = initial;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   618
            // ensure that we always handle the initial buffer first,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   619
            // if any.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   620
            if (bs != null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   621
                initial = null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   622
                ByteBuffer b = bs.get();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   623
                if (b.hasRemaining()) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   624
                    long c = ++count;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   625
                    if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   626
                        debug.log(() -> "H2 Receiving Initial(" + c +"): " + b.remaining());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   627
                    framesController.processReceivedData(framesDecoder, b);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   628
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   629
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   630
            ByteBuffer b = buffer;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   631
            // the Http2TubeSubscriber scheduler ensures that the order of incoming
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   632
            // buffers is preserved.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   633
            if (b == EMPTY_TRIGGER) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   634
                if (debug.on()) debug.log("H2 Received EMPTY_TRIGGER");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   635
                boolean prefaceSent = framesController.prefaceSent;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   636
                assert prefaceSent;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   637
                // call framesController.processReceivedData to potentially
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   638
                // trigger the processing of all the data buffered there.
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   639
                framesController.processReceivedData(framesDecoder, buffer);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   640
                if (debug.on()) debug.log("H2 processed buffered data");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   641
            } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   642
                long c = ++count;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   643
                if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   644
                    debug.log("H2 Receiving(%d): %d", c, b.remaining());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   645
                framesController.processReceivedData(framesDecoder, buffer);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   646
                if (debug.on()) debug.log("H2 processed(%d)", c);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   647
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   648
        } catch (Throwable e) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   649
            String msg = Utils.stackTrace(e);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   650
            Log.logTrace(msg);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   651
            shutdown(e);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   652
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   653
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   654
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   655
    Throwable getRecordedCause() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   656
        return cause;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   657
    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   658
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   659
    void shutdown(Throwable t) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   660
        if (debug.on()) debug.log(() -> "Shutting down h2c (closed="+closed+"): " + t);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   661
        if (closed == true) return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   662
        synchronized (this) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   663
            if (closed == true) return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   664
            closed = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   665
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   666
        Log.logError(t);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   667
        Throwable initialCause = this.cause;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   668
        if (initialCause == null) this.cause = t;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   669
        client2.deleteConnection(this);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   670
        List<Stream<?>> c = new LinkedList<>(streams.values());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   671
        for (Stream<?> s : c) {
56531
15ff86a732ea http-client-branch: connection shutdown should no cancel streams immediately if data is pending in the stream queue.
dfuchs
parents: 56513
diff changeset
   672
            s.connectionClosing(t);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   673
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   674
        connection.close();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   675
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   676
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   677
    /**
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   678
     * Streams initiated by a client MUST use odd-numbered stream
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   679
     * identifiers; those initiated by the server MUST use even-numbered
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   680
     * stream identifiers.
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   681
     */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   682
    private static final boolean isServerInitiatedStream(int streamid) {
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   683
        return (streamid & 0x1) == 0;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   684
    }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   685
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   686
    /**
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   687
     * Handles stream 0 (common) frames that apply to whole connection and passes
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   688
     * other stream specific frames to that Stream object.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   689
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   690
     * Invokes Stream.incoming() which is expected to process frame without
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   691
     * blocking.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   692
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   693
    void processFrame(Http2Frame frame) throws IOException {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   694
        Log.logFrames(frame, "IN");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   695
        int streamid = frame.streamid();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   696
        if (frame instanceof MalformedFrame) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   697
            Log.logError(((MalformedFrame) frame).getMessage());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   698
            if (streamid == 0) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   699
                framesDecoder.close("Malformed frame on stream 0");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   700
                protocolError(((MalformedFrame) frame).getErrorCode(),
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   701
                        ((MalformedFrame) frame).getMessage());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   702
            } else {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   703
                if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   704
                    debug.log(() -> "Reset stream: " + ((MalformedFrame) frame).getMessage());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   705
                resetStream(streamid, ((MalformedFrame) frame).getErrorCode());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   706
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   707
            return;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   708
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   709
        if (streamid == 0) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   710
            handleConnectionFrame(frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   711
        } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   712
            if (frame instanceof SettingsFrame) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   713
                // The stream identifier for a SETTINGS frame MUST be zero
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   714
                framesDecoder.close(
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   715
                        "The stream identifier for a SETTINGS frame MUST be zero");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   716
                protocolError(GoAwayFrame.PROTOCOL_ERROR);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   717
                return;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   718
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   719
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   720
            Stream<?> stream = getStream(streamid);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   721
            if (stream == null) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   722
                // Should never receive a frame with unknown stream id
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   723
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   724
                if (frame instanceof HeaderFrame) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   725
                    // always decode the headers as they may affect
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   726
                    // connection-level HPACK decoding state
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   727
                    DecodingCallback decoder = new ValidatingHeadersConsumer();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   728
                    try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   729
                        decodeHeaders((HeaderFrame) frame, decoder);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   730
                    } catch (UncheckedIOException e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   731
                        protocolError(ResetFrame.PROTOCOL_ERROR, e.getMessage());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   732
                        return;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   733
                    }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   734
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   735
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   736
                if (!(frame instanceof ResetFrame)) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   737
                    if (isServerInitiatedStream(streamid)) {
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   738
                        if (streamid < nextPushStream) {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   739
                            // trailing data on a cancelled push promise stream,
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   740
                            // reset will already have been sent, ignore
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   741
                            Log.logTrace("Ignoring cancelled push promise frame " + frame);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   742
                        } else {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   743
                            resetStream(streamid, ResetFrame.PROTOCOL_ERROR);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   744
                        }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   745
                    } else if (streamid >= nextstreamid) {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   746
                        // otherwise the stream has already been reset/closed
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   747
                        resetStream(streamid, ResetFrame.PROTOCOL_ERROR);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   748
                    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   749
                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   750
                return;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   751
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   752
            if (frame instanceof PushPromiseFrame) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   753
                PushPromiseFrame pp = (PushPromiseFrame)frame;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   754
                try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   755
                    handlePushPromise(stream, pp);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   756
                } catch (UncheckedIOException e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   757
                    protocolError(ResetFrame.PROTOCOL_ERROR, e.getMessage());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   758
                    return;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   759
                }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   760
            } else if (frame instanceof HeaderFrame) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   761
                // decode headers (or continuation)
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   762
                try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   763
                    decodeHeaders((HeaderFrame) frame, stream.rspHeadersConsumer());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   764
                } catch (UncheckedIOException e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   765
                    protocolError(ResetFrame.PROTOCOL_ERROR, e.getMessage());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   766
                    return;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   767
                }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   768
                stream.incoming(frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   769
            } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   770
                stream.incoming(frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   771
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   772
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   773
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   774
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   775
    private <T> void handlePushPromise(Stream<T> parent, PushPromiseFrame pp)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   776
        throws IOException
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   777
    {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   778
        // always decode the headers as they may affect connection-level HPACK
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   779
        // decoding state
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   780
        HeaderDecoder decoder = new HeaderDecoder();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   781
        decodeHeaders(pp, decoder);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   782
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   783
        HttpRequestImpl parentReq = parent.request;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   784
        int promisedStreamid = pp.getPromisedStream();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   785
        if (promisedStreamid != nextPushStream) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   786
            resetStream(promisedStreamid, ResetFrame.PROTOCOL_ERROR);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   787
            return;
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   788
        } else if (!reserveStream(false)) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   789
            resetStream(promisedStreamid, ResetFrame.REFUSED_STREAM);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   790
            return;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   791
        } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   792
            nextPushStream += 2;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   793
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   794
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   795
        HttpHeadersImpl headers = decoder.headers();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   796
        HttpRequestImpl pushReq = HttpRequestImpl.createPushRequest(parentReq, headers);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   797
        Exchange<T> pushExch = new Exchange<>(pushReq, parent.exchange.multi);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   798
        Stream.PushedStream<T> pushStream = createPushStream(parent, pushExch);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   799
        pushExch.exchImpl = pushStream;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   800
        pushStream.registerStream(promisedStreamid);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   801
        parent.incoming_pushPromise(pushReq, pushStream);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   802
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   803
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   804
    private void handleConnectionFrame(Http2Frame frame)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   805
        throws IOException
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   806
    {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   807
        switch (frame.type()) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   808
          case SettingsFrame.TYPE:
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   809
              handleSettings((SettingsFrame)frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   810
              break;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   811
          case PingFrame.TYPE:
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   812
              handlePing((PingFrame)frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   813
              break;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   814
          case GoAwayFrame.TYPE:
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   815
              handleGoAway((GoAwayFrame)frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   816
              break;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   817
          case WindowUpdateFrame.TYPE:
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   818
              handleWindowUpdate((WindowUpdateFrame)frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   819
              break;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   820
          default:
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   821
            protocolError(ErrorFrame.PROTOCOL_ERROR);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   822
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   823
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   824
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   825
    void resetStream(int streamid, int code) throws IOException {
56513
17cb1166de81 http-client-branch: Http2Connection should not try to send RST_STREAM if the channel is already closed
dfuchs
parents: 56507
diff changeset
   826
        try {
17cb1166de81 http-client-branch: Http2Connection should not try to send RST_STREAM if the channel is already closed
dfuchs
parents: 56507
diff changeset
   827
            if (connection.channel().isOpen()) {
17cb1166de81 http-client-branch: Http2Connection should not try to send RST_STREAM if the channel is already closed
dfuchs
parents: 56507
diff changeset
   828
                // no need to try & send a reset frame if the
17cb1166de81 http-client-branch: Http2Connection should not try to send RST_STREAM if the channel is already closed
dfuchs
parents: 56507
diff changeset
   829
                // connection channel is already closed.
17cb1166de81 http-client-branch: Http2Connection should not try to send RST_STREAM if the channel is already closed
dfuchs
parents: 56507
diff changeset
   830
                Log.logError(
17cb1166de81 http-client-branch: Http2Connection should not try to send RST_STREAM if the channel is already closed
dfuchs
parents: 56507
diff changeset
   831
                        "Resetting stream {0,number,integer} with error code {1,number,integer}",
17cb1166de81 http-client-branch: Http2Connection should not try to send RST_STREAM if the channel is already closed
dfuchs
parents: 56507
diff changeset
   832
                        streamid, code);
17cb1166de81 http-client-branch: Http2Connection should not try to send RST_STREAM if the channel is already closed
dfuchs
parents: 56507
diff changeset
   833
                ResetFrame frame = new ResetFrame(streamid, code);
17cb1166de81 http-client-branch: Http2Connection should not try to send RST_STREAM if the channel is already closed
dfuchs
parents: 56507
diff changeset
   834
                sendFrame(frame);
17cb1166de81 http-client-branch: Http2Connection should not try to send RST_STREAM if the channel is already closed
dfuchs
parents: 56507
diff changeset
   835
            } else if (debug.on()) {
17cb1166de81 http-client-branch: Http2Connection should not try to send RST_STREAM if the channel is already closed
dfuchs
parents: 56507
diff changeset
   836
                debug.log("Channel already closed, no need to reset stream %d",
17cb1166de81 http-client-branch: Http2Connection should not try to send RST_STREAM if the channel is already closed
dfuchs
parents: 56507
diff changeset
   837
                          streamid);
17cb1166de81 http-client-branch: Http2Connection should not try to send RST_STREAM if the channel is already closed
dfuchs
parents: 56507
diff changeset
   838
            }
17cb1166de81 http-client-branch: Http2Connection should not try to send RST_STREAM if the channel is already closed
dfuchs
parents: 56507
diff changeset
   839
        } finally {
56616
5d2446adafaf http-client-branch: fix intermittent problem with stream counting by decrementing counter before completing the subscriber CF
michaelm
parents: 56598
diff changeset
   840
            decrementStreamsCount(streamid);
56513
17cb1166de81 http-client-branch: Http2Connection should not try to send RST_STREAM if the channel is already closed
dfuchs
parents: 56507
diff changeset
   841
            closeStream(streamid);
17cb1166de81 http-client-branch: Http2Connection should not try to send RST_STREAM if the channel is already closed
dfuchs
parents: 56507
diff changeset
   842
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   843
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   844
56616
5d2446adafaf http-client-branch: fix intermittent problem with stream counting by decrementing counter before completing the subscriber CF
michaelm
parents: 56598
diff changeset
   845
    // reduce count of streams by 1 if stream still exists
5d2446adafaf http-client-branch: fix intermittent problem with stream counting by decrementing counter before completing the subscriber CF
michaelm
parents: 56598
diff changeset
   846
    synchronized void decrementStreamsCount(int streamid) {
5d2446adafaf http-client-branch: fix intermittent problem with stream counting by decrementing counter before completing the subscriber CF
michaelm
parents: 56598
diff changeset
   847
        Stream<?> s = streams.get(streamid);
5d2446adafaf http-client-branch: fix intermittent problem with stream counting by decrementing counter before completing the subscriber CF
michaelm
parents: 56598
diff changeset
   848
        if (s == null || !s.deRegister())
5d2446adafaf http-client-branch: fix intermittent problem with stream counting by decrementing counter before completing the subscriber CF
michaelm
parents: 56598
diff changeset
   849
            return;
5d2446adafaf http-client-branch: fix intermittent problem with stream counting by decrementing counter before completing the subscriber CF
michaelm
parents: 56598
diff changeset
   850
        if (streamid % 2 == 1) {
5d2446adafaf http-client-branch: fix intermittent problem with stream counting by decrementing counter before completing the subscriber CF
michaelm
parents: 56598
diff changeset
   851
            numReservedClientStreams--;
5d2446adafaf http-client-branch: fix intermittent problem with stream counting by decrementing counter before completing the subscriber CF
michaelm
parents: 56598
diff changeset
   852
        } else {
5d2446adafaf http-client-branch: fix intermittent problem with stream counting by decrementing counter before completing the subscriber CF
michaelm
parents: 56598
diff changeset
   853
            numReservedServerStreams--;
5d2446adafaf http-client-branch: fix intermittent problem with stream counting by decrementing counter before completing the subscriber CF
michaelm
parents: 56598
diff changeset
   854
        }
5d2446adafaf http-client-branch: fix intermittent problem with stream counting by decrementing counter before completing the subscriber CF
michaelm
parents: 56598
diff changeset
   855
    }
5d2446adafaf http-client-branch: fix intermittent problem with stream counting by decrementing counter before completing the subscriber CF
michaelm
parents: 56598
diff changeset
   856
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   857
    void closeStream(int streamid) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   858
        if (debug.on()) debug.log("Closed stream %d", streamid);
56598
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
   859
        boolean isClient = (streamid % 2) == 1;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   860
        Stream<?> s = streams.remove(streamid);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   861
        if (s != null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   862
            // decrement the reference count on the HttpClientImpl
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   863
            // to allow the SelectorManager thread to exit if no
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   864
            // other operation is pending and the facade is no
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   865
            // longer referenced.
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   866
            client().streamUnreference();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   867
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   868
        // ## Remove s != null. It is a hack for delayed cancellation,reset
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   869
        if (s != null && !(s instanceof Stream.PushedStream)) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   870
            // Since PushStreams have no request body, then they have no
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   871
            // corresponding entry in the window controller.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   872
            windowController.removeStream(streamid);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   873
        }
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   874
        if (finalStream() && streams.isEmpty()) {
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   875
            // should be only 1 stream, but there might be more if server push
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   876
            close();
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   877
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   878
    }
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   879
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   880
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   881
     * Increments this connection's send Window by the amount in the given frame.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   882
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   883
    private void handleWindowUpdate(WindowUpdateFrame f)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   884
        throws IOException
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   885
    {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   886
        int amount = f.getUpdate();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   887
        if (amount <= 0) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   888
            // ## temporarily disable to workaround a bug in Jetty where it
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   889
            // ## sends Window updates with a 0 update value.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   890
            //protocolError(ErrorFrame.PROTOCOL_ERROR);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   891
        } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   892
            boolean success = windowController.increaseConnectionWindow(amount);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   893
            if (!success) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   894
                protocolError(ErrorFrame.FLOW_CONTROL_ERROR);  // overflow
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   895
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   896
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   897
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   898
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   899
    private void protocolError(int errorCode)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   900
        throws IOException
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   901
    {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   902
        protocolError(errorCode, null);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   903
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   904
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   905
    private void protocolError(int errorCode, String msg)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   906
        throws IOException
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   907
    {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   908
        GoAwayFrame frame = new GoAwayFrame(0, errorCode);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   909
        sendFrame(frame);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   910
        shutdown(new IOException("protocol error" + (msg == null?"":(": " + msg))));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   911
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   912
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   913
    private void handleSettings(SettingsFrame frame)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   914
        throws IOException
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   915
    {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   916
        assert frame.streamid() == 0;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   917
        if (!frame.getFlag(SettingsFrame.ACK)) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   918
            int newWindowSize = frame.getParameter(INITIAL_WINDOW_SIZE);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   919
            if (newWindowSize != -1) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   920
                int oldWindowSize = serverSettings.getParameter(INITIAL_WINDOW_SIZE);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   921
                int diff = newWindowSize - oldWindowSize;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   922
                if (diff != 0) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   923
                    windowController.adjustActiveStreams(diff);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   924
                }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   925
            }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   926
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   927
            serverSettings.update(frame);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   928
            sendFrame(new SettingsFrame(SettingsFrame.ACK));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   929
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   930
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   931
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   932
    private void handlePing(PingFrame frame)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   933
        throws IOException
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   934
    {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   935
        frame.setFlag(PingFrame.ACK);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   936
        sendUnorderedFrame(frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   937
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   938
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   939
    private void handleGoAway(GoAwayFrame frame)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   940
        throws IOException
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   941
    {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   942
        shutdown(new IOException(
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   943
                        String.valueOf(connection.channel().getLocalAddress())
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   944
                        +": GOAWAY received"));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   945
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   946
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   947
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   948
     * Max frame size we are allowed to send
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   949
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   950
    public int getMaxSendFrameSize() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   951
        int param = serverSettings.getParameter(MAX_FRAME_SIZE);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   952
        if (param == -1) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   953
            param = DEFAULT_FRAME_SIZE;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   954
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   955
        return param;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   956
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   957
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   958
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   959
     * Max frame size we will receive
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   960
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   961
    public int getMaxReceiveFrameSize() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   962
        return clientSettings.getParameter(MAX_FRAME_SIZE);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   963
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   964
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   965
    private static final String CLIENT_PREFACE = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n";
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   966
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   967
    private static final byte[] PREFACE_BYTES =
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   968
        CLIENT_PREFACE.getBytes(StandardCharsets.ISO_8859_1);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   969
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   970
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   971
     * Sends Connection preface and Settings frame with current preferred
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   972
     * values
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   973
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   974
    private void sendConnectionPreface() throws IOException {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   975
        Log.logTrace("{0}: start sending connection preface to {1}",
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   976
                     connection.channel().getLocalAddress(),
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   977
                     connection.address());
48263
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   978
        SettingsFrame sf = new SettingsFrame(clientSettings);
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   979
        int initialWindowSize = sf.getParameter(INITIAL_WINDOW_SIZE);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   980
        ByteBuffer buf = framesEncoder.encodeConnectionPreface(PREFACE_BYTES, sf);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   981
        Log.logFrames(sf, "OUT");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   982
        // send preface bytes and SettingsFrame together
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   983
        HttpPublisher publisher = publisher();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   984
        publisher.enqueueUnordered(List.of(buf));
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   985
        publisher.signalEnqueued();
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   986
        // mark preface sent.
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   987
        framesController.markPrefaceSent();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   988
        Log.logTrace("PREFACE_BYTES sent");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   989
        Log.logTrace("Settings Frame sent");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   990
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   991
        // send a Window update for the receive buffer we are using
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   992
        // minus the initial 64 K specified in protocol
48263
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   993
        final int len = windowUpdater.initialWindowSize - initialWindowSize;
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   994
        if (len > 0) {
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   995
            windowUpdater.sendWindowUpdate(len);
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   996
        }
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   997
        // there will be an ACK to the windows update - which should
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   998
        // cause any pending data stored before the preface was sent to be
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   999
        // flushed (see PrefaceController).
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1000
        Log.logTrace("finished sending connection preface");
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1001
        if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1002
            debug.log("Triggering processing of buffered data"
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1003
                      + " after sending connection preface");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1004
        subscriber.onNext(List.of(EMPTY_TRIGGER));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1005
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1006
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1007
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1008
     * Returns an existing Stream with given id, or null if doesn't exist
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1009
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1010
    @SuppressWarnings("unchecked")
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1011
    <T> Stream<T> getStream(int streamid) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1012
        return (Stream<T>)streams.get(streamid);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1013
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1014
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1015
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1016
     * Creates Stream with given id.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1017
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1018
    final <T> Stream<T> createStream(Exchange<T> exchange) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1019
        Stream<T> stream = new Stream<>(this, exchange, windowController);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1020
        return stream;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1021
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1022
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1023
    <T> Stream.PushedStream<T> createPushStream(Stream<T> parent, Exchange<T> pushEx) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1024
        PushGroup<T> pg = parent.exchange.getPushGroup();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1025
        return new Stream.PushedStream<>(pg, this, pushEx);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1026
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1027
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1028
    <T> void putStream(Stream<T> stream, int streamid) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1029
        // increment the reference count on the HttpClientImpl
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1030
        // to prevent the SelectorManager thread from exiting until
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1031
        // the stream is closed.
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1032
        client().streamReference();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1033
        streams.put(streamid, stream);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1034
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1035
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1036
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1037
     * Encode the headers into a List<ByteBuffer> and then create HEADERS
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1038
     * and CONTINUATION frames from the list and return the List<Http2Frame>.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1039
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1040
    private List<HeaderFrame> encodeHeaders(OutgoingHeaders<Stream<?>> frame) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1041
        List<ByteBuffer> buffers = encodeHeadersImpl(
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1042
                getMaxSendFrameSize(),
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1043
                frame.getAttachment().getRequestPseudoHeaders(),
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1044
                frame.getUserHeaders(),
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1045
                frame.getSystemHeaders());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1046
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1047
        List<HeaderFrame> frames = new ArrayList<>(buffers.size());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1048
        Iterator<ByteBuffer> bufIterator = buffers.iterator();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1049
        HeaderFrame oframe = new HeadersFrame(frame.streamid(), frame.getFlags(), bufIterator.next());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1050
        frames.add(oframe);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1051
        while(bufIterator.hasNext()) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1052
            oframe = new ContinuationFrame(frame.streamid(), bufIterator.next());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1053
            frames.add(oframe);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1054
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1055
        oframe.setFlag(HeaderFrame.END_HEADERS);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1056
        return frames;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1057
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1058
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1059
    // Dedicated cache for headers encoding ByteBuffer.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1060
    // There can be no concurrent access to this  buffer as all access to this buffer
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1061
    // and its content happen within a single critical code block section protected
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1062
    // by the sendLock. / (see sendFrame())
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1063
    // private final ByteBufferPool headerEncodingPool = new ByteBufferPool();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1064
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1065
    private ByteBuffer getHeaderBuffer(int maxFrameSize) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1066
        ByteBuffer buf = ByteBuffer.allocate(maxFrameSize);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1067
        buf.limit(maxFrameSize);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1068
        return buf;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1069
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1070
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1071
    /*
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1072
     * Encodes all the headers from the given HttpHeaders into the given List
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1073
     * of buffers.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1074
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1075
     * From https://tools.ietf.org/html/rfc7540#section-8.1.2 :
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1076
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1077
     *     ...Just as in HTTP/1.x, header field names are strings of ASCII
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1078
     *     characters that are compared in a case-insensitive fashion.  However,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1079
     *     header field names MUST be converted to lowercase prior to their
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1080
     *     encoding in HTTP/2...
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1081
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1082
    private List<ByteBuffer> encodeHeadersImpl(int maxFrameSize, HttpHeaders... headers) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1083
        ByteBuffer buffer = getHeaderBuffer(maxFrameSize);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1084
        List<ByteBuffer> buffers = new ArrayList<>();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1085
        for(HttpHeaders header : headers) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1086
            for (Map.Entry<String, List<String>> e : header.map().entrySet()) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1087
                String lKey = e.getKey().toLowerCase(Locale.US);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1088
                List<String> values = e.getValue();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1089
                for (String value : values) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1090
                    hpackOut.header(lKey, value);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1091
                    while (!hpackOut.encode(buffer)) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1092
                        buffer.flip();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1093
                        buffers.add(buffer);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1094
                        buffer =  getHeaderBuffer(maxFrameSize);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1095
                    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1096
                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1097
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1098
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1099
        buffer.flip();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1100
        buffers.add(buffer);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1101
        return buffers;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1102
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1103
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1104
    private List<ByteBuffer> encodeHeaders(OutgoingHeaders<Stream<?>> oh, Stream<?> stream) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1105
        oh.streamid(stream.streamid);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1106
        if (Log.headers()) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1107
            StringBuilder sb = new StringBuilder("HEADERS FRAME (stream=");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1108
            sb.append(stream.streamid).append(")\n");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1109
            Log.dumpHeaders(sb, "    ", oh.getAttachment().getRequestPseudoHeaders());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1110
            Log.dumpHeaders(sb, "    ", oh.getSystemHeaders());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1111
            Log.dumpHeaders(sb, "    ", oh.getUserHeaders());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1112
            Log.logHeaders(sb.toString());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1113
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1114
        List<HeaderFrame> frames = encodeHeaders(oh);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1115
        return encodeFrames(frames);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1116
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1117
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1118
    private List<ByteBuffer> encodeFrames(List<HeaderFrame> frames) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1119
        if (Log.frames()) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1120
            frames.forEach(f -> Log.logFrames(f, "OUT"));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1121
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1122
        return framesEncoder.encodeFrames(frames);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1123
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1124
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1125
    private Stream<?> registerNewStream(OutgoingHeaders<Stream<?>> oh) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1126
        Stream<?> stream = oh.getAttachment();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1127
        int streamid = nextstreamid;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1128
        nextstreamid += 2;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1129
        stream.registerStream(streamid);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1130
        // set outgoing window here. This allows thread sending
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1131
        // body to proceed.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1132
        windowController.registerStream(streamid, getInitialSendWindowSize());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1133
        return stream;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1134
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1135
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1136
    private final Object sendlock = new Object();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1137
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1138
    void sendFrame(Http2Frame frame) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1139
        try {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1140
            HttpPublisher publisher = publisher();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1141
            synchronized (sendlock) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1142
                if (frame instanceof OutgoingHeaders) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1143
                    @SuppressWarnings("unchecked")
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1144
                    OutgoingHeaders<Stream<?>> oh = (OutgoingHeaders<Stream<?>>) frame;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1145
                    Stream<?> stream = registerNewStream(oh);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1146
                    // provide protection from inserting unordered frames between Headers and Continuation
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1147
                    publisher.enqueue(encodeHeaders(oh, stream));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1148
                } else {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1149
                    publisher.enqueue(encodeFrame(frame));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1150
                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1151
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1152
            publisher.signalEnqueued();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1153
        } catch (IOException e) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1154
            if (!closed) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1155
                Log.logError(e);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1156
                shutdown(e);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1157
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1158
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1159
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1160
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1161
    private List<ByteBuffer> encodeFrame(Http2Frame frame) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1162
        Log.logFrames(frame, "OUT");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1163
        return framesEncoder.encodeFrame(frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1164
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1165
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1166
    void sendDataFrame(DataFrame frame) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1167
        try {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1168
            HttpPublisher publisher = publisher();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1169
            publisher.enqueue(encodeFrame(frame));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1170
            publisher.signalEnqueued();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1171
        } catch (IOException e) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1172
            if (!closed) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1173
                Log.logError(e);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1174
                shutdown(e);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1175
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1176
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1177
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1178
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1179
    /*
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1180
     * Direct call of the method bypasses synchronization on "sendlock" and
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1181
     * allowed only of control frames: WindowUpdateFrame, PingFrame and etc.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1182
     * prohibited for such frames as DataFrame, HeadersFrame, ContinuationFrame.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1183
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1184
    void sendUnorderedFrame(Http2Frame frame) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1185
        try {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1186
            HttpPublisher publisher = publisher();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1187
            publisher.enqueueUnordered(encodeFrame(frame));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1188
            publisher.signalEnqueued();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1189
        } catch (IOException e) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1190
            if (!closed) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1191
                Log.logError(e);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1192
                shutdown(e);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1193
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1194
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1195
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1196
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1197
    /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1198
     * A simple tube subscriber for reading from the connection flow.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1199
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1200
    final class Http2TubeSubscriber implements TubeSubscriber {
56531
15ff86a732ea http-client-branch: connection shutdown should no cancel streams immediately if data is pending in the stream queue.
dfuchs
parents: 56513
diff changeset
  1201
        private volatile Flow.Subscription subscription;
15ff86a732ea http-client-branch: connection shutdown should no cancel streams immediately if data is pending in the stream queue.
dfuchs
parents: 56513
diff changeset
  1202
        private volatile boolean completed;
15ff86a732ea http-client-branch: connection shutdown should no cancel streams immediately if data is pending in the stream queue.
dfuchs
parents: 56513
diff changeset
  1203
        private volatile boolean dropped;
15ff86a732ea http-client-branch: connection shutdown should no cancel streams immediately if data is pending in the stream queue.
dfuchs
parents: 56513
diff changeset
  1204
        private volatile Throwable error;
15ff86a732ea http-client-branch: connection shutdown should no cancel streams immediately if data is pending in the stream queue.
dfuchs
parents: 56513
diff changeset
  1205
        private final ConcurrentLinkedQueue<ByteBuffer> queue
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1206
                = new ConcurrentLinkedQueue<>();
56531
15ff86a732ea http-client-branch: connection shutdown should no cancel streams immediately if data is pending in the stream queue.
dfuchs
parents: 56513
diff changeset
  1207
        private final SequentialScheduler scheduler =
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1208
                SequentialScheduler.synchronizedScheduler(this::processQueue);
56531
15ff86a732ea http-client-branch: connection shutdown should no cancel streams immediately if data is pending in the stream queue.
dfuchs
parents: 56513
diff changeset
  1209
        private final HttpClientImpl client;
56598
4c502e3991bf 8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents: 56531
diff changeset
  1210
56531
15ff86a732ea http-client-branch: connection shutdown should no cancel streams immediately if data is pending in the stream queue.
dfuchs
parents: 56513
diff changeset
  1211
        Http2TubeSubscriber(HttpClientImpl client) {
15ff86a732ea http-client-branch: connection shutdown should no cancel streams immediately if data is pending in the stream queue.
dfuchs
parents: 56513
diff changeset
  1212
            this.client = Objects.requireNonNull(client);
15ff86a732ea http-client-branch: connection shutdown should no cancel streams immediately if data is pending in the stream queue.
dfuchs
parents: 56513
diff changeset
  1213
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1214
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1215
        final void processQueue() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1216
            try {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1217
                while (!queue.isEmpty() && !scheduler.isStopped()) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1218
                    ByteBuffer buffer = queue.poll();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1219
                    if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1220
                        debug.log("sending %d to Http2Connection.asyncReceive",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1221
                                  buffer.remaining());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1222
                    asyncReceive(buffer);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1223
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1224
            } catch (Throwable t) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1225
                Throwable x = error;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1226
                if (x == null) error = t;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1227
            } finally {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1228
                Throwable x = error;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1229
                if (x != null) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1230
                    if (debug.on()) debug.log("Stopping scheduler", x);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1231
                    scheduler.stop();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1232
                    Http2Connection.this.shutdown(x);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1233
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1234
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1235
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1236
56531
15ff86a732ea http-client-branch: connection shutdown should no cancel streams immediately if data is pending in the stream queue.
dfuchs
parents: 56513
diff changeset
  1237
        private final void runOrSchedule() {
15ff86a732ea http-client-branch: connection shutdown should no cancel streams immediately if data is pending in the stream queue.
dfuchs
parents: 56513
diff changeset
  1238
            if (client.isSelectorThread()) {
15ff86a732ea http-client-branch: connection shutdown should no cancel streams immediately if data is pending in the stream queue.
dfuchs
parents: 56513
diff changeset
  1239
                scheduler.runOrSchedule(client.theExecutor());
15ff86a732ea http-client-branch: connection shutdown should no cancel streams immediately if data is pending in the stream queue.
dfuchs
parents: 56513
diff changeset
  1240
            } else scheduler.runOrSchedule();
15ff86a732ea http-client-branch: connection shutdown should no cancel streams immediately if data is pending in the stream queue.
dfuchs
parents: 56513
diff changeset
  1241
        }
15ff86a732ea http-client-branch: connection shutdown should no cancel streams immediately if data is pending in the stream queue.
dfuchs
parents: 56513
diff changeset
  1242
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1243
        @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1244
        public void onSubscribe(Flow.Subscription subscription) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1245
            // supports being called multiple time.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1246
            // doesn't cancel the previous subscription, since that is
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1247
            // most probably the same as the new subscription.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1248
            assert this.subscription == null || dropped == false;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1249
            this.subscription = subscription;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1250
            dropped = false;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1251
            // TODO FIXME: request(1) should be done by the delegate.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1252
            if (!completed) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1253
                if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1254
                    debug.log("onSubscribe: requesting Long.MAX_VALUE for reading");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1255
                subscription.request(Long.MAX_VALUE);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1256
            } else {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1257
                if (debug.on()) debug.log("onSubscribe: already completed");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1258
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1259
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1260
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1261
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1262
        public void onNext(List<ByteBuffer> item) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1263
            if (debug.on()) debug.log(() -> "onNext: got " + Utils.remaining(item)
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1264
                    + " bytes in " + item.size() + " buffers");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1265
            queue.addAll(item);
56531
15ff86a732ea http-client-branch: connection shutdown should no cancel streams immediately if data is pending in the stream queue.
dfuchs
parents: 56513
diff changeset
  1266
            runOrSchedule();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1267
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1268
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1269
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1270
        public void onError(Throwable throwable) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1271
            if (debug.on()) debug.log(() -> "onError: " + throwable);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1272
            error = throwable;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1273
            completed = true;
56531
15ff86a732ea http-client-branch: connection shutdown should no cancel streams immediately if data is pending in the stream queue.
dfuchs
parents: 56513
diff changeset
  1274
            runOrSchedule();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1275
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1276
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1277
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1278
        public void onComplete() {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1279
            if (debug.on()) debug.log("EOF");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1280
            error = new EOFException("EOF reached while reading");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1281
            completed = true;
56531
15ff86a732ea http-client-branch: connection shutdown should no cancel streams immediately if data is pending in the stream queue.
dfuchs
parents: 56513
diff changeset
  1282
            runOrSchedule();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1283
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1284
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1285
        @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1286
        public void dropSubscription() {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1287
            if (debug.on()) debug.log("dropSubscription");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1288
            // we could probably set subscription to null here...
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1289
            // then we might not need the 'dropped' boolean?
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1290
            dropped = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1291
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1292
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1293
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1294
    @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1295
    public final String toString() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1296
        return dbgString();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1297
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1298
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1299
    final String dbgString() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1300
        return "Http2Connection("
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1301
                    + connection.getConnectionFlow() + ")";
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1302
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1303
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1304
    static class HeaderDecoder extends ValidatingHeadersConsumer {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1305
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1306
        HttpHeadersImpl headers;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1307
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1308
        HeaderDecoder() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1309
            this.headers = new HttpHeadersImpl();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1310
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1311
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1312
        @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1313
        public void onDecoded(CharSequence name, CharSequence value) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1314
            String n = name.toString();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1315
            String v = value.toString();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1316
            super.onDecoded(n, v);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1317
            headers.addHeader(n, v);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1318
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1319
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1320
        HttpHeadersImpl headers() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1321
            return headers;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1322
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1323
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1324
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1325
    /*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1326
     * Checks RFC 7540 rules (relaxed) compliance regarding pseudo-headers.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1327
     */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1328
    static class ValidatingHeadersConsumer implements DecodingCallback {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1329
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1330
        private static final Set<String> PSEUDO_HEADERS =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1331
                Set.of(":authority", ":method", ":path", ":scheme", ":status");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1332
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1333
        /** Used to check that if there are pseudo-headers, they go first */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1334
        private boolean pseudoHeadersEnded;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1335
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1336
        /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1337
         * Called when END_HEADERS was received. This consumer may be invoked
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1338
         * again after reset() is called, but for a whole new set of headers.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1339
         */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1340
        void reset() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1341
            pseudoHeadersEnded = false;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1342
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1343
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1344
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1345
        public void onDecoded(CharSequence name, CharSequence value)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1346
                throws UncheckedIOException
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1347
        {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1348
            String n = name.toString();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1349
            if (n.startsWith(":")) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1350
                if (pseudoHeadersEnded) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1351
                    throw newException("Unexpected pseudo-header '%s'", n);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1352
                } else if (!PSEUDO_HEADERS.contains(n)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1353
                    throw newException("Unknown pseudo-header '%s'", n);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1354
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1355
            } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1356
                pseudoHeadersEnded = true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1357
                if (!Utils.isValidName(n)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1358
                    throw newException("Bad header name '%s'", n);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1359
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1360
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1361
            String v = value.toString();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1362
            if (!Utils.isValidValue(v)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1363
                throw newException("Bad header value '%s'", v);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1364
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1365
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1366
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1367
        private UncheckedIOException newException(String message, String header)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1368
        {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1369
            return new UncheckedIOException(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1370
                    new IOException(String.format(message, header)));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1371
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1372
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1373
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1374
    static final class ConnectionWindowUpdateSender extends WindowUpdateSender {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1375
48263
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
  1376
        final int initialWindowSize;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1377
        public ConnectionWindowUpdateSender(Http2Connection connection,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1378
                                            int initialWindowSize) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1379
            super(connection, initialWindowSize);
48263
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
  1380
            this.initialWindowSize = initialWindowSize;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1381
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1382
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1383
        @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1384
        int getStreamId() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1385
            return 0;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1386
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1387
    }
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1388
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1389
    /**
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1390
     * Thrown when https handshake negotiates http/1.1 alpn instead of h2
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1391
     */
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1392
    static final class ALPNException extends IOException {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1393
        private static final long serialVersionUID = 0L;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
  1394
        final transient AbstractAsyncSSLConnection connection;
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1395
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45530
diff changeset
  1396
        ALPNException(String msg, AbstractAsyncSSLConnection connection) {
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1397
            super(msg);
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1398
            this.connection = connection;
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1399
        }
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1400
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45530
diff changeset
  1401
        AbstractAsyncSSLConnection getConnection() {
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1402
            return connection;
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1403
        }
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1404
    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1405
}