author | michaelm |
Wed, 23 May 2018 16:44:13 +0100 | |
branch | http-client-branch |
changeset 56598 | 4c502e3991bf |
parent 56451 | 9585061fdb04 |
child 56619 | 57f17e890a40 |
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:
48083
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. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
8 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
9 |
* 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
|
10 |
* 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
|
11 |
* 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
|
12 |
* 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
|
13 |
* accompanied this code). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
14 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
15 |
* 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
|
16 |
* 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
|
17 |
* 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
|
18 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
19 |
* 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
|
20 |
* 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
|
21 |
* questions. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
22 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
23 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
24 |
import java.io.BufferedInputStream; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
25 |
import java.io.BufferedOutputStream; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
26 |
import java.io.Closeable; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
import java.io.IOException; |
45713
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
28 |
import java.io.UncheckedIOException; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
29 |
import java.io.InputStream; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
30 |
import java.io.OutputStream; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
31 |
import java.net.Socket; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
32 |
import java.net.URI; |
45713
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
33 |
import java.net.InetAddress; |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
34 |
import javax.net.ssl.*; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
35 |
import java.net.URISyntaxException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
36 |
import java.nio.ByteBuffer; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
37 |
import java.nio.charset.StandardCharsets; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
38 |
import java.util.*; |
48083 | 39 |
import java.util.concurrent.CompletableFuture; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
40 |
import java.util.concurrent.ExecutorService; |
48083 | 41 |
import java.util.concurrent.ConcurrentLinkedQueue; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
42 |
import java.util.function.Consumer; |
49765 | 43 |
import jdk.internal.net.http.common.HttpHeadersImpl; |
44 |
import jdk.internal.net.http.frame.*; |
|
45 |
import jdk.internal.net.http.hpack.Decoder; |
|
46 |
import jdk.internal.net.http.hpack.DecodingCallback; |
|
47 |
import jdk.internal.net.http.hpack.Encoder; |
|
48083 | 48 |
import sun.net.www.http.ChunkedInputStream; |
49 |
import sun.net.www.http.HttpClient; |
|
49765 | 50 |
import static jdk.internal.net.http.frame.SettingsFrame.HEADER_TABLE_SIZE; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
51 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
* Represents one HTTP2 connection, either plaintext upgraded from HTTP/1.1 |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
54 |
* or HTTPS opened using "h2" ALPN. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
55 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
56 |
public class Http2TestServerConnection { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
57 |
final Http2TestServer server; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
58 |
@SuppressWarnings({"rawtypes","unchecked"}) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
59 |
final Map<Integer, Queue> streams; // input q per stream |
48083 | 60 |
final Map<Integer, BodyOutputStream> outStreams; // output q per stream |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
61 |
final HashSet<Integer> pushStreams; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
62 |
final Queue<Http2Frame> outputQ; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
63 |
volatile int nextstream; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
64 |
final Socket socket; |
48083 | 65 |
final Http2TestExchangeSupplier exchangeSupplier; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
66 |
final InputStream is; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
67 |
final OutputStream os; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
68 |
volatile Encoder hpackOut; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
69 |
volatile Decoder hpackIn; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
70 |
volatile SettingsFrame clientSettings; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
71 |
final SettingsFrame serverSettings; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
72 |
final ExecutorService exec; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
73 |
final boolean secure; |
56598
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
74 |
final Properties properties; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
75 |
volatile boolean stopping; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
76 |
volatile int nextPushStreamId = 2; |
48083 | 77 |
ConcurrentLinkedQueue<PingRequest> pings = new ConcurrentLinkedQueue<>(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
78 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
79 |
final static ByteBuffer EMPTY_BUFFER = ByteBuffer.allocate(0); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
80 |
final static byte[] EMPTY_BARRAY = new byte[0]; |
48083 | 81 |
final Random random; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
82 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
83 |
final static byte[] clientPreface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n".getBytes(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
84 |
|
48083 | 85 |
static class Sentinel extends Http2Frame { |
86 |
Sentinel() { super(-1,-1);} |
|
87 |
} |
|
88 |
||
89 |
static final Sentinel sentinel = new Sentinel(); |
|
90 |
||
91 |
class PingRequest { |
|
92 |
final byte[] pingData; |
|
93 |
final long pingStamp; |
|
94 |
final CompletableFuture<Long> response; |
|
95 |
||
96 |
PingRequest() { |
|
97 |
pingData = new byte[8]; |
|
98 |
random.nextBytes(pingData); |
|
99 |
pingStamp = System.currentTimeMillis(); |
|
100 |
response = new CompletableFuture<>(); |
|
101 |
} |
|
102 |
||
103 |
PingFrame frame() { |
|
104 |
return new PingFrame(0, pingData); |
|
105 |
} |
|
106 |
||
107 |
CompletableFuture<Long> response() { |
|
108 |
return response; |
|
109 |
} |
|
110 |
||
111 |
void success() { |
|
112 |
response.complete(System.currentTimeMillis() - pingStamp); |
|
113 |
} |
|
114 |
||
115 |
void fail(Throwable t) { |
|
116 |
response.completeExceptionally(t); |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
Http2TestServerConnection(Http2TestServer server, |
|
121 |
Socket socket, |
|
56598
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
122 |
Http2TestExchangeSupplier exchangeSupplier, |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
123 |
Properties properties) |
48083 | 124 |
throws IOException |
125 |
{ |
|
45713
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
126 |
if (socket instanceof SSLSocket) { |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
127 |
handshake(server.serverName(), (SSLSocket)socket); |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
128 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
129 |
System.err.println("TestServer: New connection from " + socket); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
130 |
this.server = server; |
48083 | 131 |
this.exchangeSupplier = exchangeSupplier; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
132 |
this.streams = Collections.synchronizedMap(new HashMap<>()); |
48083 | 133 |
this.outStreams = Collections.synchronizedMap(new HashMap<>()); |
134 |
this.outputQ = new Queue<>(sentinel); |
|
135 |
this.random = new Random(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
136 |
this.socket = socket; |
56598
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
137 |
this.properties = properties; |
48083 | 138 |
this.socket.setTcpNoDelay(true); |
56598
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
139 |
this.serverSettings = getServerSettingProperties(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
140 |
this.exec = server.exec; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
141 |
this.secure = server.secure; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
142 |
this.pushStreams = new HashSet<>(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
143 |
is = new BufferedInputStream(socket.getInputStream()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
144 |
os = new BufferedOutputStream(socket.getOutputStream()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
145 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
146 |
|
56598
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
147 |
static final String propPrefix = "http2server.settings."; |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
148 |
|
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
149 |
static final String[][] propIDs = { |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
150 |
{"header_table_size", Integer.toString(SettingsFrame.HEADER_TABLE_SIZE)}, |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
151 |
{"enable_push", Integer.toString(SettingsFrame.ENABLE_PUSH)}, |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
152 |
{"max_concurrent_streams", Integer.toString(SettingsFrame.MAX_CONCURRENT_STREAMS)}, |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
153 |
{"initial_window_size", Integer.toString(SettingsFrame.INITIAL_WINDOW_SIZE)}, |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
154 |
{"max_frame_size", Integer.toString(SettingsFrame.MAX_FRAME_SIZE)}, |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
155 |
{"max_header_list_size", Integer.toString(SettingsFrame.MAX_HEADER_LIST_SIZE)} |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
156 |
}; |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
157 |
|
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
158 |
private SettingsFrame getServerSettingProperties() { |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
159 |
SettingsFrame s = SettingsFrame.getDefaultSettings(); |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
160 |
if (properties == null) |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
161 |
return s; |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
162 |
for (int i=0; i<propIDs.length; i++) { |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
163 |
String key = propIDs[i][0]; |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
164 |
String numS = propIDs[i][1]; |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
165 |
String prop = properties.getProperty(propPrefix + key); |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
166 |
if (prop != null) { |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
167 |
try { |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
168 |
System.err.println("TestServer: setting " + key + " property to: " + |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
169 |
prop); |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
170 |
int num = Integer.parseInt(numS); |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
171 |
System.err.println("TestServer: num = " + num); |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
172 |
s.setParameter(num, Integer.parseInt(prop)); |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
173 |
} catch (NumberFormatException e) {/* ignore errors */} |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
174 |
} |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
175 |
} |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
176 |
return s; |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
177 |
} |
4c502e3991bf
8196389: Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
michaelm
parents:
56451
diff
changeset
|
178 |
|
48083 | 179 |
/** |
180 |
* Sends a PING frame on this connection, and completes the returned |
|
181 |
* CF when the PING ack is received. The CF is given |
|
182 |
* an integer, whose value is the number of milliseconds |
|
183 |
* between PING and ACK. |
|
184 |
*/ |
|
185 |
CompletableFuture<Long> sendPing() { |
|
186 |
PingRequest ping = null; |
|
187 |
try { |
|
188 |
ping = new PingRequest(); |
|
189 |
pings.add(ping); |
|
190 |
outputQ.put(ping.frame()); |
|
191 |
} catch (Throwable t) { |
|
192 |
ping.fail(t); |
|
193 |
} |
|
194 |
return ping.response(); |
|
195 |
} |
|
196 |
||
197 |
void goAway(int error) throws IOException { |
|
198 |
int laststream = nextstream >= 3 ? nextstream - 2 : 1; |
|
199 |
||
200 |
GoAwayFrame go = new GoAwayFrame(laststream, error); |
|
201 |
outputQ.put(go); |
|
202 |
} |
|
203 |
||
204 |
/** |
|
205 |
* Returns the first PingRequest from Queue |
|
206 |
*/ |
|
207 |
private PingRequest getNextRequest() { |
|
208 |
return pings.poll(); |
|
209 |
} |
|
210 |
||
211 |
/** |
|
212 |
* Handles incoming Ping, which could be an ack |
|
213 |
* or a client originated Ping |
|
214 |
*/ |
|
215 |
void handlePing(PingFrame ping) throws IOException { |
|
216 |
if (ping.streamid() != 0) { |
|
217 |
System.err.println("Invalid ping received"); |
|
218 |
close(ErrorFrame.PROTOCOL_ERROR); |
|
219 |
return; |
|
220 |
} |
|
221 |
if (ping.getFlag(PingFrame.ACK)) { |
|
222 |
// did we send a Ping? |
|
223 |
PingRequest request = getNextRequest(); |
|
224 |
if (request == null) { |
|
225 |
System.err.println("Invalid ping ACK received"); |
|
226 |
close(ErrorFrame.PROTOCOL_ERROR); |
|
227 |
return; |
|
228 |
} else if (!Arrays.equals(request.pingData, ping.getData())) { |
|
229 |
request.fail(new RuntimeException("Wrong ping data in ACK")); |
|
230 |
} else { |
|
231 |
request.success(); |
|
232 |
} |
|
233 |
} else { |
|
234 |
// client originated PING. Just send it back with ACK set |
|
235 |
ping.setFlag(PingFrame.ACK); |
|
236 |
outputQ.put(ping); |
|
237 |
} |
|
238 |
} |
|
239 |
||
45713
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
240 |
private static boolean compareIPAddrs(InetAddress addr1, String host) { |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
241 |
try { |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
242 |
InetAddress addr2 = InetAddress.getByName(host); |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
243 |
return addr1.equals(addr2); |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
244 |
} catch (IOException e) { |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
245 |
throw new UncheckedIOException(e); |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
246 |
} |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
247 |
} |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
248 |
|
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
249 |
private static void handshake(String name, SSLSocket sock) throws IOException { |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
250 |
if (name == null) { |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
251 |
// no name set. No need to check |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
252 |
return; |
49765 | 253 |
} else if (name.equals("localhost")) { |
45713
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
254 |
name = "localhost"; |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
255 |
} |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
256 |
final String fname = name; |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
257 |
final InetAddress addr1 = InetAddress.getByName(name); |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
258 |
SSLParameters params = sock.getSSLParameters(); |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
259 |
SNIMatcher matcher = new SNIMatcher(StandardConstants.SNI_HOST_NAME) { |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
260 |
public boolean matches (SNIServerName n) { |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
261 |
String host = ((SNIHostName)n).getAsciiName(); |
49765 | 262 |
if (host.equals("localhost")) |
45713
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
263 |
host = "localhost"; |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
264 |
boolean cmp = host.equalsIgnoreCase(fname); |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
265 |
if (cmp) |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
266 |
return true; |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
267 |
return compareIPAddrs(addr1, host); |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
268 |
} |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
269 |
}; |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
270 |
List<SNIMatcher> list = List.of(matcher); |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
271 |
params.setSNIMatchers(list); |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
272 |
sock.setSSLParameters(params); |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
273 |
sock.getSession(); // blocks until handshake done |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
274 |
} |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43788
diff
changeset
|
275 |
|
48083 | 276 |
void closeIncoming() { |
277 |
close(-1); |
|
278 |
} |
|
279 |
||
280 |
void close(int error) { |
|
281 |
if (stopping) |
|
282 |
return; |
|
46157 | 283 |
stopping = true; |
48083 | 284 |
System.err.printf("Server connection to %s stopping. %d streams\n", |
285 |
socket.getRemoteSocketAddress().toString(), streams.size()); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
286 |
streams.forEach((i, q) -> { |
48083 | 287 |
q.orderlyClose(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
288 |
}); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
289 |
try { |
48083 | 290 |
if (error != -1) |
291 |
goAway(error); |
|
292 |
outputQ.orderlyClose(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
293 |
socket.close(); |
48083 | 294 |
} catch (Exception e) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
295 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
296 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
297 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
298 |
private void readPreface() throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
299 |
int len = clientPreface.length; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
300 |
byte[] bytes = new byte[len]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
301 |
is.readNBytes(bytes, 0, len); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
302 |
if (Arrays.compare(clientPreface, bytes) != 0) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
303 |
throw new IOException("Invalid preface: " + new String(bytes, 0, len)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
304 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
305 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
306 |
|
49765 | 307 |
Http1InitialRequest doUpgrade() throws IOException { |
308 |
Http1InitialRequest upgrade = readHttp1Request(); |
|
309 |
String h2c = getHeader(upgrade.headers, "Upgrade"); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
310 |
if (h2c == null || !h2c.equals("h2c")) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
311 |
System.err.println("Server:HEADERS: " + upgrade); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
312 |
throw new IOException("Bad upgrade 1 " + h2c); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
313 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
314 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
315 |
sendHttp1Response(101, "Switching Protocols", "Connection", "Upgrade", |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
316 |
"Upgrade", "h2c"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
317 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
318 |
sendSettingsFrame(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
319 |
readPreface(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
320 |
|
49765 | 321 |
String clientSettingsString = getHeader(upgrade.headers, "HTTP2-Settings"); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
322 |
clientSettings = getSettingsFromString(clientSettingsString); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
323 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
324 |
return upgrade; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
325 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
326 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
327 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
328 |
* Decodes the given, Client, settings payload provided in base64 HTTP1 |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
329 |
* header value. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
330 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
331 |
private SettingsFrame getSettingsFromString(String s) throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
332 |
Base64.Decoder decoder = Base64.getUrlDecoder(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
333 |
byte[] payload = decoder.decode(s); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
334 |
ByteBuffer bb1 = ByteBuffer.wrap(payload); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
335 |
// simulate header of Settings Frame |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
336 |
ByteBuffer bb0 = ByteBuffer.wrap( |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
337 |
new byte[] {0, 0, (byte)payload.length, 4, 0, 0, 0, 0, 0}); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
338 |
List<Http2Frame> frames = new ArrayList<>(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
339 |
FramesDecoder reader = new FramesDecoder(frames::add); |
48083 | 340 |
reader.decode(bb0); |
341 |
reader.decode(bb1); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
342 |
if (frames.size()!=1) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
343 |
throw new IOException("Expected 1 frame got "+frames.size()) ; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
344 |
Http2Frame frame = frames.get(0); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
345 |
if (!(frame instanceof SettingsFrame)) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
346 |
throw new IOException("Expected SettingsFrame"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
347 |
return (SettingsFrame)frame; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
348 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
349 |
|
49765 | 350 |
public int getMaxFrameSize() { |
351 |
return clientSettings.getParameter(SettingsFrame.MAX_FRAME_SIZE); |
|
352 |
} |
|
353 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
354 |
void run() throws Exception { |
49765 | 355 |
Http1InitialRequest upgrade = null; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
356 |
if (!secure) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
357 |
upgrade = doUpgrade(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
358 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
359 |
readPreface(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
360 |
sendSettingsFrame(true); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
361 |
clientSettings = (SettingsFrame) readFrame(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
362 |
if (clientSettings.getFlag(SettingsFrame.ACK)) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
363 |
// we received the ack to our frame first |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
364 |
clientSettings = (SettingsFrame) readFrame(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
365 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
366 |
nextstream = 1; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
367 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
368 |
|
49765 | 369 |
// Uncomment if needed, but very noisy |
370 |
//System.out.println("ServerSettings: " + serverSettings); |
|
371 |
//System.out.println("ClientSettings: " + clientSettings); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
372 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
373 |
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
|
374 |
hpackIn = new Decoder(clientSettings.getParameter(HEADER_TABLE_SIZE)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
375 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
376 |
if (!secure) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
377 |
createPrimordialStream(upgrade); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
378 |
nextstream = 3; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
379 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
380 |
|
48083 | 381 |
(new ConnectionThread("readLoop", this::readLoop)).start(); |
382 |
(new ConnectionThread("writeLoop", this::writeLoop)).start(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
383 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
384 |
|
48083 | 385 |
class ConnectionThread extends Thread { |
386 |
final Runnable r; |
|
387 |
ConnectionThread(String name, Runnable r) { |
|
388 |
setName(name); |
|
389 |
setDaemon(true); |
|
390 |
this.r = r; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
391 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
392 |
|
48083 | 393 |
public void run() { |
394 |
r.run(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
395 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
396 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
397 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
398 |
private void writeFrame(Http2Frame frame) throws IOException { |
48083 | 399 |
List<ByteBuffer> bufs = new FramesEncoder().encodeFrame(frame); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
400 |
//System.err.println("TestServer: Writing frame " + frame.toString()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
401 |
int c = 0; |
48083 | 402 |
for (ByteBuffer buf : bufs) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
403 |
byte[] ba = buf.array(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
404 |
int start = buf.arrayOffset() + buf.position(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
405 |
c += buf.remaining(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
406 |
os.write(ba, start, buf.remaining()); |
48083 | 407 |
|
408 |
// System.out.println("writing byte at a time"); |
|
409 |
// while (buf.hasRemaining()) { |
|
410 |
// byte b = buf.get(); |
|
411 |
// os.write(b); |
|
412 |
// os.flush(); |
|
413 |
// try { |
|
414 |
// Thread.sleep(1); |
|
415 |
// } catch(InterruptedException e) { |
|
416 |
// UncheckedIOException uie = new UncheckedIOException(new IOException("")); |
|
417 |
// uie.addSuppressed(e); |
|
418 |
// throw uie; |
|
419 |
// } |
|
420 |
// } |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
421 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
422 |
os.flush(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
423 |
//System.err.printf("TestServer: wrote %d bytes\n", c); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
424 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
425 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
426 |
private void handleCommonFrame(Http2Frame f) throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
427 |
if (f instanceof SettingsFrame) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
428 |
SettingsFrame sf = (SettingsFrame) f; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
429 |
if (sf.getFlag(SettingsFrame.ACK)) // ignore |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
430 |
{ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
431 |
return; |
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 |
// otherwise acknowledge it |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
434 |
clientSettings = sf; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
435 |
SettingsFrame frame = new SettingsFrame(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
436 |
frame.setFlag(SettingsFrame.ACK); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
437 |
frame.streamid(0); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
438 |
outputQ.put(frame); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
439 |
return; |
48083 | 440 |
} else if (f instanceof GoAwayFrame) { |
441 |
System.err.println("Closing: "+ f.toString()); |
|
442 |
close(ErrorFrame.NO_ERROR); |
|
443 |
} else if (f instanceof PingFrame) { |
|
444 |
handlePing((PingFrame)f); |
|
445 |
} else |
|
446 |
throw new UnsupportedOperationException("Not supported yet: " + f.toString()); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
447 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
448 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
449 |
void sendWindowUpdates(int len, int streamid) throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
450 |
if (len == 0) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
451 |
return; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
452 |
WindowUpdateFrame wup = new WindowUpdateFrame(streamid, len); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
453 |
outputQ.put(wup); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
454 |
wup = new WindowUpdateFrame(0 , len); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
455 |
outputQ.put(wup); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
456 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
457 |
|
48083 | 458 |
HttpHeadersImpl decodeHeaders(List<HeaderFrame> frames) throws IOException { |
49765 | 459 |
HttpHeadersImpl headers = createNewResponseHeaders(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
460 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
461 |
DecodingCallback cb = (name, value) -> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
462 |
headers.addHeader(name.toString(), value.toString()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
463 |
}; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
464 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
465 |
for (HeaderFrame frame : frames) { |
48083 | 466 |
List<ByteBuffer> buffers = frame.getHeaderBlock(); |
467 |
for (ByteBuffer buffer : buffers) { |
|
468 |
hpackIn.decode(buffer, false, cb); |
|
42460
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 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
471 |
hpackIn.decode(EMPTY_BUFFER, true, cb); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
472 |
return headers; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
473 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
474 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
475 |
String getRequestLine(String request) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
476 |
int eol = request.indexOf(CRLF); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
477 |
return request.substring(0, eol); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
478 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
479 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
480 |
String getHeaders(String request) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
481 |
int start = request.indexOf(CRLF); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
482 |
int end = request.indexOf(CRLFCRLF); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
483 |
if (start == -1 || end == -1) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
484 |
throw new RuntimeException("Malformed request"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
485 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
486 |
return request.substring(start,end); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
487 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
488 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
489 |
void addHeaders(String headers, HttpHeadersImpl hdrs) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
490 |
String[] hh = headers.split(CRLF); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
491 |
for (String header : hh) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
492 |
int colon = header.indexOf(':'); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
493 |
if (colon == -1) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
494 |
continue; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
495 |
String name = header.substring(0, colon); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
496 |
String value = header.substring(colon+1); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
497 |
while (value.startsWith(" ")) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
498 |
value = value.substring(1); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
499 |
hdrs.addHeader(name, value); |
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 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
502 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
503 |
// First stream (1) comes from a plaintext HTTP/1.1 request |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
504 |
@SuppressWarnings({"rawtypes","unchecked"}) |
49765 | 505 |
void createPrimordialStream(Http1InitialRequest request) throws IOException { |
506 |
HttpHeadersImpl headers = createNewResponseHeaders(); |
|
507 |
String requestLine = getRequestLine(request.headers); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
508 |
String[] tokens = requestLine.split(" "); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
509 |
if (!tokens[2].equals("HTTP/1.1")) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
510 |
throw new IOException("bad request line"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
511 |
} |
49765 | 512 |
URI uri; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
513 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
514 |
uri = new URI(tokens[1]); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
515 |
} catch (URISyntaxException e) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
516 |
throw new IOException(e); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
517 |
} |
49765 | 518 |
String host = getHeader(request.headers, "Host"); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
519 |
if (host == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
520 |
throw new IOException("missing Host"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
521 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
522 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
523 |
headers.setHeader(":method", tokens[0]); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
524 |
headers.setHeader(":scheme", "http"); // always in this case |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
525 |
headers.setHeader(":authority", host); |
49765 | 526 |
String path = uri.getRawPath(); |
527 |
if (uri.getRawQuery() != null) |
|
528 |
path = path + "?" + uri.getRawQuery(); |
|
529 |
headers.setHeader(":path", path); |
|
530 |
||
48083 | 531 |
Queue q = new Queue(sentinel); |
49765 | 532 |
byte[] body = getRequestBody(request); |
533 |
addHeaders(getHeaders(request.headers), headers); |
|
534 |
headers.setHeader("Content-length", Integer.toString(body.length)); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
535 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
536 |
addRequestBodyToQueue(body, q); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
537 |
streams.put(1, q); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
538 |
exec.submit(() -> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
539 |
handleRequest(headers, q, 1, true /*complete request has been read*/); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
540 |
}); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
541 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
542 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
543 |
// all other streams created here |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
544 |
@SuppressWarnings({"rawtypes","unchecked"}) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
545 |
void createStream(HeaderFrame frame) throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
546 |
List<HeaderFrame> frames = new LinkedList<>(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
547 |
frames.add(frame); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
548 |
int streamid = frame.streamid(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
549 |
if (streamid != nextstream) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
550 |
throw new IOException("unexpected stream id"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
551 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
552 |
nextstream += 2; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
553 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
554 |
boolean endStream = false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
555 |
if (frame.getFlag(HeaderFrame.END_STREAM)) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
556 |
endStream = true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
557 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
558 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
559 |
while (!frame.getFlag(HeaderFrame.END_HEADERS)) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
560 |
Http2Frame f = readFrame(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
561 |
if (!(f instanceof HeaderFrame)) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
562 |
handleCommonFrame(f); // should only be error frames |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
563 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
564 |
frame = (HeaderFrame) f; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
565 |
if (frame.getFlag(HeaderFrame.END_STREAM)) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
566 |
endStream = true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
567 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
568 |
frames.add(frame); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
569 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
570 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
571 |
boolean endStreamReceived = endStream; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
572 |
HttpHeadersImpl headers = decodeHeaders(frames); |
49765 | 573 |
|
574 |
// Strict to assert Client correctness. Not all servers are as strict, |
|
575 |
// but some are known to be. |
|
576 |
Optional<?> disallowedHeader = headers.firstValue("Upgrade"); |
|
577 |
if (disallowedHeader.isPresent()) { |
|
578 |
throw new IOException("Unexpected Upgrade in headers:" + headers); |
|
579 |
} |
|
580 |
disallowedHeader = headers.firstValue("HTTP2-Settings"); |
|
581 |
if (disallowedHeader.isPresent()) |
|
582 |
throw new IOException("Unexpected HTTP2-Settings in headers:" + headers); |
|
583 |
||
584 |
||
48083 | 585 |
Queue q = new Queue(sentinel); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
586 |
streams.put(streamid, q); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
587 |
exec.submit(() -> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
588 |
handleRequest(headers, q, streamid, endStreamReceived); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
589 |
}); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
590 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
591 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
592 |
// runs in own thread. Handles request from start to finish. Incoming frames |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
593 |
// for this stream/request delivered on Q |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
594 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
595 |
@SuppressWarnings({"rawtypes","unchecked"}) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
596 |
void handleRequest(HttpHeadersImpl headers, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
597 |
Queue queue, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
598 |
int streamid, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
599 |
boolean endStreamReceived) |
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 |
String method = headers.firstValue(":method").orElse(""); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
602 |
//System.out.println("method = " + method); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
603 |
String path = headers.firstValue(":path").orElse(""); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
604 |
//System.out.println("path = " + path); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
605 |
String scheme = headers.firstValue(":scheme").orElse(""); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
606 |
//System.out.println("scheme = " + scheme); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
607 |
String authority = headers.firstValue(":authority").orElse(""); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
608 |
//System.out.println("authority = " + authority); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
609 |
System.err.printf("TestServer: %s %s\n", method, path); |
49765 | 610 |
HttpHeadersImpl rspheaders = createNewResponseHeaders(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
611 |
int winsize = clientSettings.getParameter( |
48083 | 612 |
SettingsFrame.INITIAL_WINDOW_SIZE); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
613 |
//System.err.println ("Stream window size = " + winsize); |
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 |
final InputStream bis; |
43788
22a618ec8268
8174833: java/net/httpclient/http2/BasicTest.java always fails but always report success
dfuchs
parents:
42460
diff
changeset
|
616 |
if (endStreamReceived && queue.size() == 0) { |
22a618ec8268
8174833: java/net/httpclient/http2/BasicTest.java always fails but always report success
dfuchs
parents:
42460
diff
changeset
|
617 |
System.err.println("Server: got END_STREAM for stream " + streamid); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
618 |
bis = NullInputStream.INSTANCE; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
619 |
} else { |
43788
22a618ec8268
8174833: java/net/httpclient/http2/BasicTest.java always fails but always report success
dfuchs
parents:
42460
diff
changeset
|
620 |
System.err.println("Server: creating input stream for stream " + streamid); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
621 |
bis = new BodyInputStream(queue, streamid, this); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
622 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
623 |
try (bis; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
624 |
BodyOutputStream bos = new BodyOutputStream(streamid, winsize, this)) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
625 |
{ |
48083 | 626 |
outStreams.put(streamid, bos); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
627 |
String us = scheme + "://" + authority + path; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
628 |
URI uri = new URI(us); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
629 |
boolean pushAllowed = clientSettings.getParameter(SettingsFrame.ENABLE_PUSH) == 1; |
48083 | 630 |
Http2TestExchange exchange = exchangeSupplier.get(streamid, method, |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
631 |
headers, rspheaders, uri, bis, getSSLSession(), |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
632 |
bos, this, pushAllowed); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
633 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
634 |
// give to user |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
635 |
Http2Handler handler = server.getHandlerFor(uri.getPath()); |
49765 | 636 |
try { |
637 |
handler.handle(exchange); |
|
638 |
} catch (IOException closed) { |
|
639 |
if (bos.closed) { |
|
640 |
Queue q = streams.get(streamid); |
|
641 |
if (q != null && (q.isClosed() || q.isClosing())) { |
|
642 |
System.err.println("TestServer: Stream " + streamid + " closed: " + closed); |
|
643 |
return; |
|
644 |
} |
|
645 |
} |
|
646 |
throw closed; |
|
647 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
648 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
649 |
// everything happens in the exchange from here. Hopefully will |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
650 |
// return though. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
651 |
} catch (Throwable e) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
652 |
System.err.println("TestServer: handleRequest exception: " + e); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
653 |
e.printStackTrace(); |
49765 | 654 |
close(-1); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
655 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
656 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
657 |
|
49765 | 658 |
protected HttpHeadersImpl createNewResponseHeaders() { |
659 |
return new HttpHeadersImpl(); |
|
660 |
} |
|
661 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
662 |
private SSLSession getSSLSession() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
663 |
if (! (socket instanceof SSLSocket)) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
664 |
return null; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
665 |
SSLSocket ssl = (SSLSocket)socket; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
666 |
return ssl.getSession(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
667 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
668 |
// Runs in own thread |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
669 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
670 |
@SuppressWarnings({"rawtypes","unchecked"}) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
671 |
void readLoop() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
672 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
673 |
while (!stopping) { |
48083 | 674 |
Http2Frame frame = readFrameImpl(); |
675 |
if (frame == null) { |
|
676 |
closeIncoming(); |
|
677 |
return; |
|
678 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
679 |
//System.err.printf("TestServer: received frame %s\n", frame); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
680 |
int stream = frame.streamid(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
681 |
if (stream == 0) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
682 |
if (frame.type() == WindowUpdateFrame.TYPE) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
683 |
WindowUpdateFrame wup = (WindowUpdateFrame) frame; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
684 |
updateConnectionWindow(wup.getUpdate()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
685 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
686 |
// other common frame types |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
687 |
handleCommonFrame(frame); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
688 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
689 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
690 |
Queue q = streams.get(stream); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
691 |
if (frame.type() == HeadersFrame.TYPE) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
692 |
if (q != null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
693 |
System.err.println("HEADERS frame for existing stream! Error."); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
694 |
// TODO: close connection |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
695 |
continue; |
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 |
createStream((HeadersFrame) frame); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
698 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
699 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
700 |
if (q == null && !pushStreams.contains(stream)) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
701 |
System.err.printf("Non Headers frame received with"+ |
48083 | 702 |
" non existing stream (%d) ", frame.streamid()); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
703 |
System.err.println(frame); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
704 |
continue; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
705 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
706 |
if (frame.type() == WindowUpdateFrame.TYPE) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
707 |
WindowUpdateFrame wup = (WindowUpdateFrame) frame; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
708 |
synchronized (updaters) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
709 |
Consumer<Integer> r = updaters.get(stream); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
710 |
r.accept(wup.getUpdate()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
711 |
} |
48083 | 712 |
} else if (frame.type() == ResetFrame.TYPE) { |
713 |
// do orderly close on input q |
|
714 |
// and close the output q immediately |
|
715 |
// This should mean depending on what the |
|
716 |
// handler is doing: either an EOF on read |
|
717 |
// or an IOException if writing the response. |
|
48523
b95b08f3e1a8
8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
48083
diff
changeset
|
718 |
if (q != null) { |
b95b08f3e1a8
8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
48083
diff
changeset
|
719 |
q.orderlyClose(); |
b95b08f3e1a8
8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
48083
diff
changeset
|
720 |
BodyOutputStream oq = outStreams.get(stream); |
b95b08f3e1a8
8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
48083
diff
changeset
|
721 |
if (oq != null) |
b95b08f3e1a8
8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
48083
diff
changeset
|
722 |
oq.closeInternal(); |
b95b08f3e1a8
8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
48083
diff
changeset
|
723 |
} else if (pushStreams.contains(stream)) { |
b95b08f3e1a8
8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
48083
diff
changeset
|
724 |
// we could interrupt the pushStream's output |
b95b08f3e1a8
8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
48083
diff
changeset
|
725 |
// but the continuation, even after a reset |
b95b08f3e1a8
8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
48083
diff
changeset
|
726 |
// should be handle gracefully by the client |
b95b08f3e1a8
8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
48083
diff
changeset
|
727 |
// anyway. |
b95b08f3e1a8
8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
48083
diff
changeset
|
728 |
} else { |
b95b08f3e1a8
8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
48083
diff
changeset
|
729 |
System.err.println("TestServer: Unexpected frame on: " + stream); |
b95b08f3e1a8
8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
48083
diff
changeset
|
730 |
System.err.println(frame); |
b95b08f3e1a8
8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
48083
diff
changeset
|
731 |
throw new IOException("Unexpected frame"); |
b95b08f3e1a8
8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
48083
diff
changeset
|
732 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
733 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
734 |
q.put(frame); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
735 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
736 |
} |
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 |
} catch (Throwable e) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
740 |
if (!stopping) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
741 |
System.err.println("Http server reader thread shutdown"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
742 |
e.printStackTrace(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
743 |
} |
48083 | 744 |
close(ErrorFrame.PROTOCOL_ERROR); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
745 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
746 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
747 |
|
48083 | 748 |
List<ByteBuffer> encodeHeaders(HttpHeadersImpl headers) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
749 |
List<ByteBuffer> buffers = new LinkedList<>(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
750 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
751 |
ByteBuffer buf = getBuffer(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
752 |
boolean encoded; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
753 |
for (Map.Entry<String, List<String>> entry : headers.map().entrySet()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
754 |
List<String> values = entry.getValue(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
755 |
String key = entry.getKey().toLowerCase(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
756 |
for (String value : values) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
757 |
do { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
758 |
hpackOut.header(key, value); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
759 |
encoded = hpackOut.encode(buf); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
760 |
if (!encoded) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
761 |
buf.flip(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
762 |
buffers.add(buf); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
763 |
buf = getBuffer(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
764 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
765 |
} while (!encoded); |
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 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
768 |
buf.flip(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
769 |
buffers.add(buf); |
48083 | 770 |
return buffers; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
771 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
772 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
773 |
static void closeIgnore(Closeable c) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
774 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
775 |
c.close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
776 |
} catch (IOException e) {} |
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 |
// Runs in own thread |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
780 |
void writeLoop() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
781 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
782 |
while (!stopping) { |
46157 | 783 |
Http2Frame frame; |
784 |
try { |
|
785 |
frame = outputQ.take(); |
|
48083 | 786 |
if (stopping) |
787 |
break; |
|
46157 | 788 |
} catch(IOException x) { |
789 |
if (stopping && x.getCause() instanceof InterruptedException) { |
|
790 |
break; |
|
791 |
} else throw x; |
|
792 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
793 |
if (frame instanceof ResponseHeaders) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
794 |
ResponseHeaders rh = (ResponseHeaders)frame; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
795 |
HeadersFrame hf = new HeadersFrame(rh.streamid(), rh.getFlags(), encodeHeaders(rh.headers)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
796 |
writeFrame(hf); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
797 |
} else if (frame instanceof OutgoingPushPromise) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
798 |
handlePush((OutgoingPushPromise)frame); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
799 |
} else |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
800 |
writeFrame(frame); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
801 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
802 |
System.err.println("TestServer: Connection writer stopping"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
803 |
} catch (Throwable e) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
804 |
e.printStackTrace(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
805 |
/*close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
806 |
if (!stopping) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
807 |
e.printStackTrace(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
808 |
System.err.println("TestServer: writeLoop exception: " + e); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
809 |
}*/ |
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 handlePush(OutgoingPushPromise op) throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
814 |
int promisedStreamid = nextPushStreamId; |
48083 | 815 |
PushPromiseFrame pp = new PushPromiseFrame(op.parentStream, |
816 |
HeaderFrame.END_HEADERS, |
|
817 |
promisedStreamid, |
|
818 |
encodeHeaders(op.headers), |
|
819 |
0); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
820 |
pushStreams.add(promisedStreamid); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
821 |
nextPushStreamId += 2; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
822 |
pp.streamid(op.parentStream); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
823 |
writeFrame(pp); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
824 |
final InputStream ii = op.is; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
825 |
final BodyOutputStream oo = new BodyOutputStream( |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
826 |
promisedStreamid, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
827 |
clientSettings.getParameter( |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
828 |
SettingsFrame.INITIAL_WINDOW_SIZE), this); |
48083 | 829 |
outStreams.put(promisedStreamid, oo); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
830 |
oo.goodToGo(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
831 |
exec.submit(() -> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
832 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
833 |
ResponseHeaders oh = getPushResponse(promisedStreamid); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
834 |
outputQ.put(oh); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
835 |
ii.transferTo(oo); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
836 |
} catch (Throwable ex) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
837 |
System.err.printf("TestServer: pushing response error: %s\n", |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
838 |
ex.toString()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
839 |
} finally { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
840 |
closeIgnore(ii); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
841 |
closeIgnore(oo); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
842 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
843 |
}); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
844 |
|
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
847 |
// returns a minimal response with status 200 |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
848 |
// that is the response to the push promise just sent |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
849 |
private ResponseHeaders getPushResponse(int streamid) { |
49765 | 850 |
HttpHeadersImpl h = createNewResponseHeaders(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
851 |
h.addHeader(":status", "200"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
852 |
ResponseHeaders oh = new ResponseHeaders(h); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
853 |
oh.streamid(streamid); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
854 |
oh.setFlag(HeaderFrame.END_HEADERS); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
855 |
return oh; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
856 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
857 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
858 |
private ByteBuffer getBuffer() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
859 |
return ByteBuffer.allocate(8 * 1024); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
860 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
861 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
862 |
private Http2Frame readFrame() throws IOException { |
48083 | 863 |
Http2Frame f = readFrameImpl(); |
864 |
if (f == null) |
|
865 |
throw new IOException("connection closed"); |
|
866 |
return f; |
|
867 |
} |
|
868 |
||
869 |
// does not throw an exception for EOF |
|
870 |
private Http2Frame readFrameImpl() throws IOException { |
|
871 |
try { |
|
872 |
byte[] buf = new byte[9]; |
|
873 |
int ret; |
|
874 |
ret=is.readNBytes(buf, 0, 9); |
|
875 |
if (ret == 0) { |
|
876 |
return null; |
|
877 |
} else if (ret != 9) { |
|
878 |
throw new IOException("readFrame: connection closed"); |
|
879 |
} |
|
880 |
int len = 0; |
|
881 |
for (int i = 0; i < 3; i++) { |
|
882 |
int n = buf[i] & 0xff; |
|
883 |
//System.err.println("n = " + n); |
|
884 |
len = (len << 8) + n; |
|
885 |
} |
|
886 |
byte[] rest = new byte[len]; |
|
887 |
int n = is.readNBytes(rest, 0, len); |
|
888 |
if (n != len) |
|
889 |
throw new IOException("Error reading frame"); |
|
890 |
List<Http2Frame> frames = new ArrayList<>(); |
|
891 |
FramesDecoder reader = new FramesDecoder(frames::add); |
|
892 |
reader.decode(ByteBuffer.wrap(buf)); |
|
893 |
reader.decode(ByteBuffer.wrap(rest)); |
|
894 |
if (frames.size()!=1) |
|
895 |
throw new IOException("Expected 1 frame got "+frames.size()) ; |
|
896 |
||
897 |
return frames.get(0); |
|
898 |
} catch (IOException ee) { |
|
899 |
if (stopping) |
|
900 |
return null; |
|
901 |
throw ee; |
|
42460
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 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
904 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
905 |
void sendSettingsFrame() throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
906 |
sendSettingsFrame(false); |
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 |
void sendSettingsFrame(boolean now) throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
910 |
if (now) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
911 |
writeFrame(serverSettings); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
912 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
913 |
outputQ.put(serverSettings); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
914 |
} |
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 |
String readUntil(String end) throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
918 |
int number = end.length(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
919 |
int found = 0; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
920 |
StringBuilder sb = new StringBuilder(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
921 |
while (found < number) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
922 |
char expected = end.charAt(found); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
923 |
int c = is.read(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
924 |
if (c == -1) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
925 |
throw new IOException("Connection closed"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
926 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
927 |
char c0 = (char) c; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
928 |
sb.append(c0); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
929 |
if (c0 != expected) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
930 |
found = 0; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
931 |
continue; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
932 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
933 |
found++; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
934 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
935 |
return sb.toString(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
936 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
937 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
938 |
private int getContentLength(String headers) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
939 |
return getIntHeader(headers, "Content-length"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
940 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
941 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
942 |
private int getIntHeader(String headers, String name) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
943 |
String val = getHeader(headers, name); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
944 |
if (val == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
945 |
return -1; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
946 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
947 |
return Integer.parseInt(val); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
948 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
949 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
950 |
private String getHeader(String headers, String name) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
951 |
String headers1 = headers.toLowerCase(); // not efficient |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
952 |
name = CRLF + name.toLowerCase(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
953 |
int start = headers1.indexOf(name); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
954 |
if (start == -1) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
955 |
return null; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
956 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
957 |
start += 2; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
958 |
int end = headers1.indexOf(CRLF, start); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
959 |
String line = headers.substring(start, end); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
960 |
start = line.indexOf(':'); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
961 |
if (start == -1) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
962 |
return null; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
963 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
964 |
return line.substring(start + 1).trim(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
965 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
966 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
967 |
final static String CRLF = "\r\n"; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
968 |
final static String CRLFCRLF = "\r\n\r\n"; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
969 |
|
49765 | 970 |
static class Http1InitialRequest { |
971 |
final String headers; |
|
972 |
final byte[] body; |
|
973 |
Http1InitialRequest(String headers, byte[] body) { |
|
974 |
this.headers = headers; |
|
975 |
this.body = body.clone(); |
|
976 |
} |
|
977 |
} |
|
978 |
||
979 |
Http1InitialRequest readHttp1Request() throws IOException { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
980 |
String headers = readUntil(CRLF + CRLF); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
981 |
int clen = getContentLength(headers); |
48083 | 982 |
String te = getHeader(headers, "Transfer-encoding"); |
983 |
byte[] buf = new byte[0]; |
|
984 |
try { |
|
985 |
if (clen >= 0) { |
|
986 |
// HTTP/1.1 fixed length content ( may be 0 ), read it |
|
987 |
buf = new byte[clen]; |
|
988 |
is.readNBytes(buf, 0, clen); |
|
989 |
} else if ("chunked".equalsIgnoreCase(te)) { |
|
990 |
// HTTP/1.1 chunked data, read it |
|
991 |
buf = readChunkedInputStream(is); |
|
992 |
} |
|
49765 | 993 |
return new Http1InitialRequest(headers, buf); |
48083 | 994 |
} catch (IOException e) { |
995 |
System.err.println("TestServer: headers read: [ " + headers + " ]"); |
|
996 |
throw e; |
|
997 |
} |
|
998 |
} |
|
999 |
||
1000 |
// This is a quick hack to get a chunked input stream reader. |
|
1001 |
private static byte[] readChunkedInputStream(InputStream is) throws IOException { |
|
1002 |
ChunkedInputStream cis = new ChunkedInputStream(is, new HttpClient() {}, null); |
|
1003 |
return cis.readAllBytes(); |
|
42460
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 |
void sendHttp1Response(int code, String msg, String... headers) throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1007 |
StringBuilder sb = new StringBuilder(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1008 |
sb.append("HTTP/1.1 ") |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1009 |
.append(code) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1010 |
.append(' ') |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1011 |
.append(msg) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1012 |
.append(CRLF); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1013 |
int numheaders = headers.length; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1014 |
for (int i = 0; i < numheaders; i += 2) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1015 |
sb.append(headers[i]) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1016 |
.append(": ") |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1017 |
.append(headers[i + 1]) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1018 |
.append(CRLF); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1019 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1020 |
sb.append(CRLF); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1021 |
String s = sb.toString(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1022 |
os.write(s.getBytes("US-ASCII")); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1023 |
os.flush(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1024 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1025 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1026 |
private void unexpectedFrame(Http2Frame frame) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1027 |
System.err.println("OOPS. Unexpected"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1028 |
assert false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1029 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1030 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1031 |
final static ByteBuffer[] bbarray = new ByteBuffer[0]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1032 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1033 |
// wrapper around a BlockingQueue that throws an exception when it's closed |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1034 |
// Each stream has one of these |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1035 |
|
49765 | 1036 |
byte[] getRequestBody(Http1InitialRequest request) { |
1037 |
return request.body; |
|
42460
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 |
@SuppressWarnings({"rawtypes","unchecked"}) |
49765 | 1041 |
void addRequestBodyToQueue(byte[] body, Queue q) throws IOException { |
1042 |
ByteBuffer buf = ByteBuffer.wrap(body); |
|
48083 | 1043 |
DataFrame df = new DataFrame(1, DataFrame.END_STREAM, buf); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1044 |
// only used for primordial stream |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1045 |
q.put(df); |
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1048 |
// window updates done in main reader thread because they may |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1049 |
// be used to unblock BodyOutputStreams waiting for WUPs |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1050 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1051 |
HashMap<Integer,Consumer<Integer>> updaters = new HashMap<>(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1052 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1053 |
void registerStreamWindowUpdater(int streamid, Consumer<Integer> r) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1054 |
synchronized(updaters) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1055 |
updaters.put(streamid, r); |
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 |
int sendWindow = 64 * 1024 - 1; // connection level send window |
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 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1062 |
* BodyOutputStreams call this to get the connection window first. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1063 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1064 |
* @param amount |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1065 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1066 |
synchronized void obtainConnectionWindow(int amount) throws InterruptedException { |
48083 | 1067 |
while (amount > 0) { |
1068 |
int n = Math.min(amount, sendWindow); |
|
1069 |
amount -= n; |
|
1070 |
sendWindow -= n; |
|
1071 |
if (amount > 0) |
|
1072 |
wait(); |
|
1073 |
} |
|
42460
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 |
synchronized void updateConnectionWindow(int amount) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1077 |
sendWindow += amount; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1078 |
notifyAll(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1079 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1080 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1081 |
// simplified output headers class. really just a type safe container |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1082 |
// for the hashmap. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1083 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1084 |
static class ResponseHeaders extends Http2Frame { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1085 |
HttpHeadersImpl headers; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1086 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1087 |
ResponseHeaders(HttpHeadersImpl headers) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1088 |
super(0, 0); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1089 |
this.headers = headers; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1090 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1091 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1092 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1093 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1094 |
static class NullInputStream extends InputStream { |
48083 | 1095 |
static final NullInputStream INSTANCE = new NullInputStream(); |
1096 |
private NullInputStream() {} |
|
1097 |
public int read() { return -1; } |
|
1098 |
public int available() { return 0; } |
|
1099 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1100 |
} |