author | chegar |
Sun, 05 Nov 2017 21:19:55 +0000 | |
branch | http-client-branch |
changeset 55764 | 34d7cc00f87a |
parent 47266 | b841be61b9d9 |
child 49765 | ee6f7a61f3a5 |
child 56045 | 5c6e3b76d2ad |
permissions | -rw-r--r-- |
37874 | 1 |
/* |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
2 |
* Copyright (c) 2016, 2017, 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; |
|
27 |
import java.net.InetSocketAddress; |
|
28 |
import java.net.URI; |
|
29 |
import java.nio.ByteBuffer; |
|
30 |
import java.nio.CharBuffer; |
|
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
31 |
import java.nio.channels.ClosedByInterruptException; |
37874 | 32 |
import java.nio.channels.ServerSocketChannel; |
33 |
import java.nio.channels.SocketChannel; |
|
34 |
import java.nio.charset.CharacterCodingException; |
|
35 |
import java.security.MessageDigest; |
|
36 |
import java.security.NoSuchAlgorithmException; |
|
55764
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
37 |
import java.util.ArrayList; |
37874 | 38 |
import java.util.Arrays; |
39 |
import java.util.Base64; |
|
40 |
import java.util.HashMap; |
|
41 |
import java.util.Iterator; |
|
42 |
import java.util.LinkedList; |
|
43 |
import java.util.List; |
|
44 |
import java.util.Map; |
|
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
45 |
import java.util.concurrent.atomic.AtomicBoolean; |
37874 | 46 |
import java.util.function.Function; |
47 |
import java.util.regex.Pattern; |
|
48 |
import java.util.stream.Collectors; |
|
49 |
||
50 |
import static java.lang.String.format; |
|
55764
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
51 |
import static java.lang.System.err; |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
52 |
import static java.nio.charset.StandardCharsets.ISO_8859_1; |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
53 |
import static java.util.Arrays.asList; |
37874 | 54 |
import static java.util.Objects.requireNonNull; |
55 |
||
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
56 |
/** |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
57 |
* Dummy WebSocket Server. |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
58 |
* |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
59 |
* 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
|
60 |
* 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
|
61 |
* 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
|
62 |
* disconnect first. |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
63 |
* |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
64 |
* Expected client request: |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
65 |
* |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
66 |
* GET /chat HTTP/1.1 |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
67 |
* Host: server.example.com |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
68 |
* Upgrade: websocket |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
69 |
* Connection: Upgrade |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
70 |
* Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
71 |
* Origin: http://example.com |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
72 |
* Sec-WebSocket-Protocol: chat, superchat |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
73 |
* Sec-WebSocket-Version: 13 |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
74 |
* |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
75 |
* This server response: |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
76 |
* |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
77 |
* HTTP/1.1 101 Switching Protocols |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
78 |
* Upgrade: websocket |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
79 |
* Connection: Upgrade |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
80 |
* Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
81 |
* Sec-WebSocket-Protocol: chat |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
82 |
*/ |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
83 |
public final class DummyWebSocketServer implements Closeable { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
84 |
|
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
85 |
private final AtomicBoolean started = new AtomicBoolean(); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
86 |
private final Thread thread; |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
87 |
private volatile ServerSocketChannel ssc; |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
88 |
private volatile InetSocketAddress address; |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
89 |
|
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
90 |
public DummyWebSocketServer() { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
91 |
this(defaultMapping()); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
92 |
} |
37874 | 93 |
|
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
94 |
public DummyWebSocketServer(Function<List<String>, List<String>> mapping) { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
95 |
requireNonNull(mapping); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
96 |
thread = new Thread(() -> { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
97 |
try { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
98 |
while (!Thread.currentThread().isInterrupted()) { |
55764
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
99 |
err.println("Accepting next connection at: " + ssc); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
100 |
SocketChannel channel = ssc.accept(); |
55764
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
101 |
err.println("Accepted: " + channel); |
43999
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 |
channel.configureBlocking(true); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
104 |
StringBuilder request = new StringBuilder(); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
105 |
if (!readRequest(channel, request)) { |
47266
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
106 |
throw new IOException("Bad request:" + request); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
107 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
108 |
List<String> strings = asList(request.toString().split("\r\n")); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
109 |
List<String> response = mapping.apply(strings); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
110 |
writeResponse(channel, response); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
111 |
// Read until the thread is interrupted or an error occurred |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
112 |
// or the input is shutdown |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
113 |
ByteBuffer b = ByteBuffer.allocate(1024); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
114 |
while (channel.read(b) != -1) { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
115 |
b.clear(); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
116 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
117 |
} catch (IOException e) { |
55764
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
118 |
err.println("Error in connection: " + channel + ", " + e); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
119 |
} finally { |
55764
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
120 |
err.println("Closed: " + channel); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
121 |
close(channel); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
122 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
123 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
124 |
} catch (ClosedByInterruptException ignored) { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
125 |
} catch (IOException e) { |
55764
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
126 |
err.println(e); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
127 |
} finally { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
128 |
close(ssc); |
55764
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
129 |
err.println("Stopped at: " + getURI()); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
130 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
131 |
}); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
132 |
thread.setName("DummyWebSocketServer"); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
133 |
thread.setDaemon(false); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
134 |
} |
37874 | 135 |
|
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
136 |
public void open() throws IOException { |
55764
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
137 |
err.println("Starting"); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
138 |
if (!started.compareAndSet(false, true)) { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
139 |
throw new IllegalStateException("Already started"); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
140 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
141 |
ssc = ServerSocketChannel.open(); |
37874 | 142 |
try { |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
143 |
ssc.configureBlocking(true); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
144 |
ssc.bind(new InetSocketAddress("localhost", 0)); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
145 |
address = (InetSocketAddress) ssc.getLocalAddress(); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
146 |
thread.start(); |
37874 | 147 |
} catch (IOException e) { |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
148 |
close(ssc); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
149 |
} |
55764
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
150 |
err.println("Started at: " + getURI()); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
151 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
152 |
|
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
153 |
@Override |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
154 |
public void close() { |
55764
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
155 |
err.println("Stopping: " + getURI()); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
156 |
thread.interrupt(); |
47266
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
157 |
close(ssc); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
158 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
159 |
|
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
160 |
URI getURI() { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
161 |
if (!started.get()) { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
162 |
throw new IllegalStateException("Not yet started"); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
163 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
164 |
return URI.create("ws://" + address.getHostName() + ":" + address.getPort()); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
165 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
166 |
|
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
167 |
private boolean readRequest(SocketChannel channel, StringBuilder request) |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
168 |
throws IOException |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
169 |
{ |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
170 |
ByteBuffer buffer = ByteBuffer.allocate(512); |
47266
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
171 |
while (channel.read(buffer) != -1) { |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
172 |
// 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
|
173 |
CharBuffer decoded; |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
174 |
buffer.flip(); |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
175 |
try { |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
176 |
decoded = ISO_8859_1.newDecoder().decode(buffer); |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
177 |
} catch (CharacterCodingException e) { |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
178 |
throw new UncheckedIOException(e); |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
179 |
} |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
180 |
request.append(decoded); |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
181 |
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
|
182 |
return true; |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
183 |
buffer.clear(); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
184 |
} |
47266
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
185 |
return false; |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
186 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
187 |
|
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
188 |
private void writeResponse(SocketChannel channel, List<String> response) |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
189 |
throws IOException |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
190 |
{ |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
191 |
String s = response.stream().collect(Collectors.joining("\r\n")) |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
192 |
+ "\r\n\r\n"; |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
193 |
ByteBuffer encoded; |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
194 |
try { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
195 |
encoded = ISO_8859_1.newEncoder().encode(CharBuffer.wrap(s)); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
196 |
} catch (CharacterCodingException e) { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
197 |
throw new UncheckedIOException(e); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
198 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
199 |
while (encoded.hasRemaining()) { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
200 |
channel.write(encoded); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
201 |
} |
37874 | 202 |
} |
203 |
||
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
204 |
private static Function<List<String>, List<String>> defaultMapping() { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
205 |
return request -> { |
37874 | 206 |
List<String> response = new LinkedList<>(); |
207 |
Iterator<String> iterator = request.iterator(); |
|
208 |
if (!iterator.hasNext()) { |
|
209 |
throw new IllegalStateException("The request is empty"); |
|
210 |
} |
|
55764
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
211 |
String statusLine = iterator.next(); |
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
212 |
if (!(statusLine.startsWith("GET /") && statusLine.endsWith(" HTTP/1.1"))) { |
37874 | 213 |
throw new IllegalStateException |
214 |
("Unexpected status line: " + request.get(0)); |
|
215 |
} |
|
216 |
response.add("HTTP/1.1 101 Switching Protocols"); |
|
55764
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
217 |
Map<String, List<String>> requestHeaders = new HashMap<>(); |
37874 | 218 |
while (iterator.hasNext()) { |
219 |
String header = iterator.next(); |
|
220 |
String[] split = header.split(": "); |
|
221 |
if (split.length != 2) { |
|
222 |
throw new IllegalStateException |
|
223 |
("Unexpected header: " + header |
|
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
224 |
+ ", split=" + Arrays.toString(split)); |
37874 | 225 |
} |
55764
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
226 |
requestHeaders.computeIfAbsent(split[0], k -> new ArrayList<>()).add(split[1]); |
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
227 |
|
37874 | 228 |
} |
229 |
if (requestHeaders.containsKey("Sec-WebSocket-Protocol")) { |
|
230 |
throw new IllegalStateException("Subprotocols are not expected"); |
|
231 |
} |
|
232 |
if (requestHeaders.containsKey("Sec-WebSocket-Extensions")) { |
|
233 |
throw new IllegalStateException("Extensions are not expected"); |
|
234 |
} |
|
235 |
expectHeader(requestHeaders, "Connection", "Upgrade"); |
|
236 |
response.add("Connection: Upgrade"); |
|
237 |
expectHeader(requestHeaders, "Upgrade", "websocket"); |
|
238 |
response.add("Upgrade: websocket"); |
|
239 |
expectHeader(requestHeaders, "Sec-WebSocket-Version", "13"); |
|
55764
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
240 |
List<String> key = requestHeaders.get("Sec-WebSocket-Key"); |
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
241 |
if (key == null || key.isEmpty()) { |
37874 | 242 |
throw new IllegalStateException("Sec-WebSocket-Key is missing"); |
243 |
} |
|
55764
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
244 |
if (key.size() != 1) { |
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
245 |
throw new IllegalStateException("Sec-WebSocket-Key has too many values : " + key); |
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
246 |
} |
37874 | 247 |
MessageDigest sha1 = null; |
248 |
try { |
|
249 |
sha1 = MessageDigest.getInstance("SHA-1"); |
|
250 |
} catch (NoSuchAlgorithmException e) { |
|
251 |
throw new InternalError(e); |
|
252 |
} |
|
55764
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
253 |
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
|
254 |
sha1.update(x.getBytes(ISO_8859_1)); |
37874 | 255 |
String v = Base64.getEncoder().encodeToString(sha1.digest()); |
256 |
response.add("Sec-WebSocket-Accept: " + v); |
|
257 |
return response; |
|
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
258 |
}; |
37874 | 259 |
} |
260 |
||
55764
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
261 |
protected static String expectHeader(Map<String, List<String>> headers, |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
262 |
String name, |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
263 |
String value) { |
55764
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
264 |
List<String> v = headers.get(name); |
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
265 |
if (!v.contains(value)) { |
37874 | 266 |
throw new IllegalStateException( |
267 |
format("Expected '%s: %s', actual: '%s: %s'", |
|
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
268 |
name, value, name, v) |
37874 | 269 |
); |
270 |
} |
|
55764
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47266
diff
changeset
|
271 |
return value; |
37874 | 272 |
} |
273 |
||
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
274 |
private static void close(AutoCloseable... acs) { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
275 |
for (AutoCloseable ac : acs) { |
37874 | 276 |
try { |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
277 |
ac.close(); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
278 |
} catch (Exception ignored) { } |
37874 | 279 |
} |
280 |
} |
|
281 |
} |