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