author | chegar |
Wed, 02 May 2018 02:36:17 -0700 | |
changeset 49944 | 4690a2871b44 |
parent 49765 | ee6f7a61f3a5 |
child 53521 | 41fa3e6f2785 |
child 56507 | 2294c51eae30 |
permissions | -rw-r--r-- |
37874 | 1 |
/* |
49765 | 2 |
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. |
37874 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
24 |
import java.io.Closeable; |
37874 | 25 |
import java.io.IOException; |
26 |
import java.io.UncheckedIOException; |
|
49765 | 27 |
import java.net.InetAddress; |
37874 | 28 |
import java.net.InetSocketAddress; |
49765 | 29 |
import java.net.StandardSocketOptions; |
37874 | 30 |
import java.net.URI; |
31 |
import java.nio.ByteBuffer; |
|
32 |
import java.nio.CharBuffer; |
|
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
33 |
import java.nio.channels.ClosedByInterruptException; |
37874 | 34 |
import java.nio.channels.ServerSocketChannel; |
35 |
import java.nio.channels.SocketChannel; |
|
36 |
import java.nio.charset.CharacterCodingException; |
|
37 |
import java.security.MessageDigest; |
|
38 |
import java.security.NoSuchAlgorithmException; |
|
48083 | 39 |
import java.util.ArrayList; |
37874 | 40 |
import java.util.Arrays; |
41 |
import java.util.Base64; |
|
42 |
import java.util.HashMap; |
|
43 |
import java.util.Iterator; |
|
44 |
import java.util.LinkedList; |
|
45 |
import java.util.List; |
|
46 |
import java.util.Map; |
|
49765 | 47 |
import java.util.concurrent.CountDownLatch; |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
48 |
import java.util.concurrent.atomic.AtomicBoolean; |
37874 | 49 |
import java.util.function.Function; |
50 |
import java.util.regex.Pattern; |
|
51 |
import java.util.stream.Collectors; |
|
52 |
||
53 |
import static java.lang.String.format; |
|
48083 | 54 |
import static java.lang.System.err; |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
55 |
import static java.nio.charset.StandardCharsets.ISO_8859_1; |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
56 |
import static java.util.Arrays.asList; |
37874 | 57 |
import static java.util.Objects.requireNonNull; |
58 |
||
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
59 |
/** |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
60 |
* Dummy WebSocket Server. |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
61 |
* |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
62 |
* Performs simpler version of the WebSocket Opening Handshake over HTTP (i.e. |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
63 |
* no proxying, cookies, etc.) Supports sequential connections, one at a time, |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
64 |
* i.e. in order for a client to connect to the server the previous client must |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
65 |
* disconnect first. |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
66 |
* |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
67 |
* Expected client request: |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
68 |
* |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
69 |
* GET /chat HTTP/1.1 |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
70 |
* Host: server.example.com |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
71 |
* Upgrade: websocket |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
72 |
* Connection: Upgrade |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
73 |
* Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
74 |
* Origin: http://example.com |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
75 |
* Sec-WebSocket-Protocol: chat, superchat |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
76 |
* Sec-WebSocket-Version: 13 |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
77 |
* |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
78 |
* This server response: |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
79 |
* |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
80 |
* HTTP/1.1 101 Switching Protocols |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
81 |
* Upgrade: websocket |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
82 |
* Connection: Upgrade |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
83 |
* Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
84 |
* Sec-WebSocket-Protocol: chat |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
85 |
*/ |
49765 | 86 |
public class DummyWebSocketServer implements Closeable { |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
87 |
|
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
88 |
private final AtomicBoolean started = new AtomicBoolean(); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
89 |
private final Thread thread; |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
90 |
private volatile ServerSocketChannel ssc; |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
91 |
private volatile InetSocketAddress address; |
49765 | 92 |
private ByteBuffer read = ByteBuffer.allocate(16384); |
93 |
private final CountDownLatch readReady = new CountDownLatch(1); |
|
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
94 |
|
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
95 |
public DummyWebSocketServer() { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
96 |
this(defaultMapping()); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
97 |
} |
37874 | 98 |
|
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
99 |
public DummyWebSocketServer(Function<List<String>, List<String>> mapping) { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
100 |
requireNonNull(mapping); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
101 |
thread = new Thread(() -> { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
102 |
try { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
103 |
while (!Thread.currentThread().isInterrupted()) { |
48083 | 104 |
err.println("Accepting next connection at: " + ssc); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
105 |
SocketChannel channel = ssc.accept(); |
48083 | 106 |
err.println("Accepted: " + channel); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
107 |
try { |
49765 | 108 |
channel.setOption(StandardSocketOptions.TCP_NODELAY, true); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
109 |
channel.configureBlocking(true); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
110 |
StringBuilder request = new StringBuilder(); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
111 |
if (!readRequest(channel, request)) { |
47266
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
112 |
throw new IOException("Bad request:" + request); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
113 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
114 |
List<String> strings = asList(request.toString().split("\r\n")); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
115 |
List<String> response = mapping.apply(strings); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
116 |
writeResponse(channel, response); |
49765 | 117 |
serve(channel); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
118 |
} catch (IOException e) { |
48083 | 119 |
err.println("Error in connection: " + channel + ", " + e); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
120 |
} finally { |
48083 | 121 |
err.println("Closed: " + channel); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
122 |
close(channel); |
49765 | 123 |
readReady.countDown(); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
124 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
125 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
126 |
} catch (ClosedByInterruptException ignored) { |
49765 | 127 |
} catch (Exception e) { |
48083 | 128 |
err.println(e); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
129 |
} finally { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
130 |
close(ssc); |
48083 | 131 |
err.println("Stopped at: " + getURI()); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
132 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
133 |
}); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
134 |
thread.setName("DummyWebSocketServer"); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
135 |
thread.setDaemon(false); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
136 |
} |
37874 | 137 |
|
49765 | 138 |
protected void read(SocketChannel ch) throws IOException { |
139 |
// Read until the thread is interrupted or an error occurred |
|
140 |
// or the input is shutdown |
|
141 |
ByteBuffer b = ByteBuffer.allocate(65536); |
|
142 |
while (ch.read(b) != -1) { |
|
143 |
b.flip(); |
|
144 |
if (read.remaining() < b.remaining()) { |
|
145 |
int required = read.capacity() - read.remaining() + b.remaining(); |
|
146 |
int log2required = 32 - Integer.numberOfLeadingZeros(required - 1); |
|
147 |
ByteBuffer newBuffer = ByteBuffer.allocate(1 << log2required); |
|
148 |
newBuffer.put(read.flip()); |
|
149 |
read = newBuffer; |
|
150 |
} |
|
151 |
read.put(b); |
|
152 |
b.clear(); |
|
153 |
} |
|
154 |
} |
|
155 |
||
156 |
protected void write(SocketChannel ch) throws IOException { } |
|
157 |
||
158 |
protected final void serve(SocketChannel channel) |
|
159 |
throws InterruptedException |
|
160 |
{ |
|
161 |
Thread reader = new Thread(() -> { |
|
162 |
try { |
|
163 |
read(channel); |
|
164 |
} catch (IOException ignored) { } |
|
165 |
}); |
|
166 |
Thread writer = new Thread(() -> { |
|
167 |
try { |
|
168 |
write(channel); |
|
169 |
} catch (IOException ignored) { } |
|
170 |
}); |
|
171 |
reader.start(); |
|
172 |
writer.start(); |
|
173 |
try { |
|
174 |
reader.join(); |
|
175 |
} finally { |
|
176 |
reader.interrupt(); |
|
177 |
try { |
|
178 |
writer.join(); |
|
179 |
} finally { |
|
180 |
writer.interrupt(); |
|
181 |
} |
|
182 |
} |
|
183 |
} |
|
184 |
||
185 |
public ByteBuffer read() throws InterruptedException { |
|
186 |
readReady.await(); |
|
187 |
return read.duplicate().asReadOnlyBuffer().flip(); |
|
188 |
} |
|
189 |
||
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
190 |
public void open() throws IOException { |
48083 | 191 |
err.println("Starting"); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
192 |
if (!started.compareAndSet(false, true)) { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
193 |
throw new IllegalStateException("Already started"); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
194 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
195 |
ssc = ServerSocketChannel.open(); |
37874 | 196 |
try { |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
197 |
ssc.configureBlocking(true); |
49765 | 198 |
ssc.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
199 |
address = (InetSocketAddress) ssc.getLocalAddress(); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
200 |
thread.start(); |
37874 | 201 |
} catch (IOException e) { |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
202 |
close(ssc); |
49944 | 203 |
throw e; |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
204 |
} |
48083 | 205 |
err.println("Started at: " + getURI()); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
206 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
207 |
|
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
208 |
@Override |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
209 |
public void close() { |
48083 | 210 |
err.println("Stopping: " + getURI()); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
211 |
thread.interrupt(); |
47266
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
212 |
close(ssc); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
213 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
214 |
|
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
215 |
URI getURI() { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
216 |
if (!started.get()) { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
217 |
throw new IllegalStateException("Not yet started"); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
218 |
} |
49765 | 219 |
return URI.create("ws://localhost:" + address.getPort()); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
220 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
221 |
|
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
222 |
private boolean readRequest(SocketChannel channel, StringBuilder request) |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
223 |
throws IOException |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
224 |
{ |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
225 |
ByteBuffer buffer = ByteBuffer.allocate(512); |
47266
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
226 |
while (channel.read(buffer) != -1) { |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
227 |
// read the complete HTTP request headers, there should be no body |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
228 |
CharBuffer decoded; |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
229 |
buffer.flip(); |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
230 |
try { |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
231 |
decoded = ISO_8859_1.newDecoder().decode(buffer); |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
232 |
} catch (CharacterCodingException e) { |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
233 |
throw new UncheckedIOException(e); |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
234 |
} |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
235 |
request.append(decoded); |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
236 |
if (Pattern.compile("\r\n\r\n").matcher(request).find()) |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
237 |
return true; |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
238 |
buffer.clear(); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
239 |
} |
47266
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
240 |
return false; |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
241 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
242 |
|
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
243 |
private void writeResponse(SocketChannel channel, List<String> response) |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
244 |
throws IOException |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
245 |
{ |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
246 |
String s = response.stream().collect(Collectors.joining("\r\n")) |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
247 |
+ "\r\n\r\n"; |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
248 |
ByteBuffer encoded; |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
249 |
try { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
250 |
encoded = ISO_8859_1.newEncoder().encode(CharBuffer.wrap(s)); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
251 |
} catch (CharacterCodingException e) { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
252 |
throw new UncheckedIOException(e); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
253 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
254 |
while (encoded.hasRemaining()) { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
255 |
channel.write(encoded); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
256 |
} |
37874 | 257 |
} |
258 |
||
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
259 |
private static Function<List<String>, List<String>> defaultMapping() { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
260 |
return request -> { |
37874 | 261 |
List<String> response = new LinkedList<>(); |
262 |
Iterator<String> iterator = request.iterator(); |
|
263 |
if (!iterator.hasNext()) { |
|
264 |
throw new IllegalStateException("The request is empty"); |
|
265 |
} |
|
48083 | 266 |
String statusLine = iterator.next(); |
267 |
if (!(statusLine.startsWith("GET /") && statusLine.endsWith(" HTTP/1.1"))) { |
|
37874 | 268 |
throw new IllegalStateException |
269 |
("Unexpected status line: " + request.get(0)); |
|
270 |
} |
|
271 |
response.add("HTTP/1.1 101 Switching Protocols"); |
|
48083 | 272 |
Map<String, List<String>> requestHeaders = new HashMap<>(); |
37874 | 273 |
while (iterator.hasNext()) { |
274 |
String header = iterator.next(); |
|
275 |
String[] split = header.split(": "); |
|
276 |
if (split.length != 2) { |
|
277 |
throw new IllegalStateException |
|
278 |
("Unexpected header: " + header |
|
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
279 |
+ ", split=" + Arrays.toString(split)); |
37874 | 280 |
} |
48083 | 281 |
requestHeaders.computeIfAbsent(split[0], k -> new ArrayList<>()).add(split[1]); |
282 |
||
37874 | 283 |
} |
284 |
if (requestHeaders.containsKey("Sec-WebSocket-Protocol")) { |
|
285 |
throw new IllegalStateException("Subprotocols are not expected"); |
|
286 |
} |
|
287 |
if (requestHeaders.containsKey("Sec-WebSocket-Extensions")) { |
|
288 |
throw new IllegalStateException("Extensions are not expected"); |
|
289 |
} |
|
290 |
expectHeader(requestHeaders, "Connection", "Upgrade"); |
|
291 |
response.add("Connection: Upgrade"); |
|
292 |
expectHeader(requestHeaders, "Upgrade", "websocket"); |
|
293 |
response.add("Upgrade: websocket"); |
|
294 |
expectHeader(requestHeaders, "Sec-WebSocket-Version", "13"); |
|
48083 | 295 |
List<String> key = requestHeaders.get("Sec-WebSocket-Key"); |
296 |
if (key == null || key.isEmpty()) { |
|
37874 | 297 |
throw new IllegalStateException("Sec-WebSocket-Key is missing"); |
298 |
} |
|
48083 | 299 |
if (key.size() != 1) { |
300 |
throw new IllegalStateException("Sec-WebSocket-Key has too many values : " + key); |
|
301 |
} |
|
37874 | 302 |
MessageDigest sha1 = null; |
303 |
try { |
|
304 |
sha1 = MessageDigest.getInstance("SHA-1"); |
|
305 |
} catch (NoSuchAlgorithmException e) { |
|
306 |
throw new InternalError(e); |
|
307 |
} |
|
48083 | 308 |
String x = key.get(0) + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
309 |
sha1.update(x.getBytes(ISO_8859_1)); |
37874 | 310 |
String v = Base64.getEncoder().encodeToString(sha1.digest()); |
311 |
response.add("Sec-WebSocket-Accept: " + v); |
|
312 |
return response; |
|
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
313 |
}; |
37874 | 314 |
} |
315 |
||
48083 | 316 |
protected static String expectHeader(Map<String, List<String>> headers, |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
317 |
String name, |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
318 |
String value) { |
48083 | 319 |
List<String> v = headers.get(name); |
320 |
if (!v.contains(value)) { |
|
37874 | 321 |
throw new IllegalStateException( |
322 |
format("Expected '%s: %s', actual: '%s: %s'", |
|
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
323 |
name, value, name, v) |
37874 | 324 |
); |
325 |
} |
|
48083 | 326 |
return value; |
37874 | 327 |
} |
328 |
||
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
329 |
private static void close(AutoCloseable... acs) { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
330 |
for (AutoCloseable ac : acs) { |
37874 | 331 |
try { |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
332 |
ac.close(); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
333 |
} catch (Exception ignored) { } |
37874 | 334 |
} |
335 |
} |
|
336 |
} |