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