author | chegar |
Mon, 28 Jan 2019 13:51:16 +0000 | |
changeset 53521 | 41fa3e6f2785 |
parent 49944 | 4690a2871b44 |
permissions | -rw-r--r-- |
37874 | 1 |
/* |
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
2 |
* Copyright (c) 2016, 2019, 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; |
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
49 |
import java.util.function.BiFunction; |
37874 | 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; |
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
56 |
import static java.nio.charset.StandardCharsets.UTF_8; |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
57 |
import static java.util.Arrays.asList; |
37874 | 58 |
import static java.util.Objects.requireNonNull; |
59 |
||
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
60 |
/** |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
61 |
* Dummy WebSocket Server. |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
62 |
* |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
63 |
* 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
|
64 |
* 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
|
65 |
* 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
|
66 |
* disconnect first. |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
67 |
* |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
68 |
* Expected client request: |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
69 |
* |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
70 |
* GET /chat HTTP/1.1 |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
71 |
* Host: server.example.com |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
72 |
* Upgrade: websocket |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
73 |
* Connection: Upgrade |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
74 |
* Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
75 |
* Origin: http://example.com |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
76 |
* Sec-WebSocket-Protocol: chat, superchat |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
77 |
* Sec-WebSocket-Version: 13 |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
78 |
* |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
79 |
* This server response: |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
80 |
* |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
81 |
* HTTP/1.1 101 Switching Protocols |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
82 |
* Upgrade: websocket |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
83 |
* Connection: Upgrade |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
84 |
* Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
85 |
* Sec-WebSocket-Protocol: chat |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
86 |
*/ |
49765 | 87 |
public class DummyWebSocketServer implements Closeable { |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
88 |
|
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
89 |
private final AtomicBoolean started = new AtomicBoolean(); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
90 |
private final Thread thread; |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
91 |
private volatile ServerSocketChannel ssc; |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
92 |
private volatile InetSocketAddress address; |
49765 | 93 |
private ByteBuffer read = ByteBuffer.allocate(16384); |
94 |
private final CountDownLatch readReady = new CountDownLatch(1); |
|
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
95 |
|
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
96 |
private static class Credentials { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
97 |
private final String name; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
98 |
private final String password; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
99 |
private Credentials(String name, String password) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
100 |
this.name = name; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
101 |
this.password = password; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
102 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
103 |
public String name() { return name; } |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
104 |
public String password() { return password; } |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
105 |
} |
37874 | 106 |
|
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
107 |
public DummyWebSocketServer() { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
108 |
this(defaultMapping(), null, null); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
109 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
110 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
111 |
public DummyWebSocketServer(String username, String password) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
112 |
this(defaultMapping(), username, password); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
113 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
114 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
115 |
public DummyWebSocketServer(BiFunction<List<String>,Credentials,List<String>> mapping, |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
116 |
String username, |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
117 |
String password) { |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
118 |
requireNonNull(mapping); |
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
119 |
Credentials credentials = username != null ? |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
120 |
new Credentials(username, password) : null; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
121 |
|
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
122 |
thread = new Thread(() -> { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
123 |
try { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
124 |
while (!Thread.currentThread().isInterrupted()) { |
48083 | 125 |
err.println("Accepting next connection at: " + ssc); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
126 |
SocketChannel channel = ssc.accept(); |
48083 | 127 |
err.println("Accepted: " + channel); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
128 |
try { |
49765 | 129 |
channel.setOption(StandardSocketOptions.TCP_NODELAY, true); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
130 |
channel.configureBlocking(true); |
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
131 |
while (true) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
132 |
StringBuilder request = new StringBuilder(); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
133 |
if (!readRequest(channel, request)) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
134 |
throw new IOException("Bad request:[" + request + "]"); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
135 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
136 |
List<String> strings = asList(request.toString().split("\r\n")); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
137 |
List<String> response = mapping.apply(strings, credentials); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
138 |
writeResponse(channel, response); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
139 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
140 |
if (response.get(0).startsWith("HTTP/1.1 401")) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
141 |
err.println("Sent 401 Authentication response " + channel); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
142 |
continue; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
143 |
} else { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
144 |
serve(channel); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
145 |
break; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
146 |
} |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
147 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
148 |
} catch (IOException e) { |
48083 | 149 |
err.println("Error in connection: " + channel + ", " + e); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
150 |
} finally { |
48083 | 151 |
err.println("Closed: " + channel); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
152 |
close(channel); |
49765 | 153 |
readReady.countDown(); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
154 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
155 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
156 |
} catch (ClosedByInterruptException ignored) { |
49765 | 157 |
} catch (Exception e) { |
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
158 |
e.printStackTrace(err); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
159 |
} finally { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
160 |
close(ssc); |
48083 | 161 |
err.println("Stopped at: " + getURI()); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
162 |
} |
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 |
thread.setName("DummyWebSocketServer"); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
165 |
thread.setDaemon(false); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
166 |
} |
37874 | 167 |
|
49765 | 168 |
protected void read(SocketChannel ch) throws IOException { |
169 |
// Read until the thread is interrupted or an error occurred |
|
170 |
// or the input is shutdown |
|
171 |
ByteBuffer b = ByteBuffer.allocate(65536); |
|
172 |
while (ch.read(b) != -1) { |
|
173 |
b.flip(); |
|
174 |
if (read.remaining() < b.remaining()) { |
|
175 |
int required = read.capacity() - read.remaining() + b.remaining(); |
|
176 |
int log2required = 32 - Integer.numberOfLeadingZeros(required - 1); |
|
177 |
ByteBuffer newBuffer = ByteBuffer.allocate(1 << log2required); |
|
178 |
newBuffer.put(read.flip()); |
|
179 |
read = newBuffer; |
|
180 |
} |
|
181 |
read.put(b); |
|
182 |
b.clear(); |
|
183 |
} |
|
184 |
} |
|
185 |
||
186 |
protected void write(SocketChannel ch) throws IOException { } |
|
187 |
||
188 |
protected final void serve(SocketChannel channel) |
|
189 |
throws InterruptedException |
|
190 |
{ |
|
191 |
Thread reader = new Thread(() -> { |
|
192 |
try { |
|
193 |
read(channel); |
|
194 |
} catch (IOException ignored) { } |
|
195 |
}); |
|
196 |
Thread writer = new Thread(() -> { |
|
197 |
try { |
|
198 |
write(channel); |
|
199 |
} catch (IOException ignored) { } |
|
200 |
}); |
|
201 |
reader.start(); |
|
202 |
writer.start(); |
|
203 |
try { |
|
204 |
reader.join(); |
|
205 |
} finally { |
|
206 |
reader.interrupt(); |
|
207 |
try { |
|
208 |
writer.join(); |
|
209 |
} finally { |
|
210 |
writer.interrupt(); |
|
211 |
} |
|
212 |
} |
|
213 |
} |
|
214 |
||
215 |
public ByteBuffer read() throws InterruptedException { |
|
216 |
readReady.await(); |
|
217 |
return read.duplicate().asReadOnlyBuffer().flip(); |
|
218 |
} |
|
219 |
||
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
220 |
public void open() throws IOException { |
48083 | 221 |
err.println("Starting"); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
222 |
if (!started.compareAndSet(false, true)) { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
223 |
throw new IllegalStateException("Already started"); |
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 |
ssc = ServerSocketChannel.open(); |
37874 | 226 |
try { |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
227 |
ssc.configureBlocking(true); |
49765 | 228 |
ssc.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
229 |
address = (InetSocketAddress) ssc.getLocalAddress(); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
230 |
thread.start(); |
37874 | 231 |
} catch (IOException e) { |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
232 |
close(ssc); |
49944 | 233 |
throw e; |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
234 |
} |
48083 | 235 |
err.println("Started at: " + getURI()); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
236 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
237 |
|
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
238 |
@Override |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
239 |
public void close() { |
48083 | 240 |
err.println("Stopping: " + getURI()); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
241 |
thread.interrupt(); |
47266
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
242 |
close(ssc); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
243 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
244 |
|
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
245 |
URI getURI() { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
246 |
if (!started.get()) { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
247 |
throw new IllegalStateException("Not yet started"); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
248 |
} |
49765 | 249 |
return URI.create("ws://localhost:" + address.getPort()); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
250 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
251 |
|
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
252 |
private boolean readRequest(SocketChannel channel, StringBuilder request) |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
253 |
throws IOException |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
254 |
{ |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
255 |
ByteBuffer buffer = ByteBuffer.allocate(512); |
47266
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
256 |
while (channel.read(buffer) != -1) { |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
257 |
// 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
|
258 |
CharBuffer decoded; |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
259 |
buffer.flip(); |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
260 |
try { |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
261 |
decoded = ISO_8859_1.newDecoder().decode(buffer); |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
262 |
} catch (CharacterCodingException e) { |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
263 |
throw new UncheckedIOException(e); |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
264 |
} |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
265 |
request.append(decoded); |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
266 |
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
|
267 |
return true; |
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
268 |
buffer.clear(); |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
269 |
} |
47266
b841be61b9d9
8181176: java/net/httpclient/websocket/ConnectionHandover.java times out
chegar
parents:
47216
diff
changeset
|
270 |
return false; |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
271 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
272 |
|
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
273 |
private void writeResponse(SocketChannel channel, List<String> response) |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
274 |
throws IOException |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
275 |
{ |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
276 |
String s = response.stream().collect(Collectors.joining("\r\n")) |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
277 |
+ "\r\n\r\n"; |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
278 |
ByteBuffer encoded; |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
279 |
try { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
280 |
encoded = ISO_8859_1.newEncoder().encode(CharBuffer.wrap(s)); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
281 |
} catch (CharacterCodingException e) { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
282 |
throw new UncheckedIOException(e); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
283 |
} |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
284 |
while (encoded.hasRemaining()) { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
285 |
channel.write(encoded); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
286 |
} |
37874 | 287 |
} |
288 |
||
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
289 |
private static BiFunction<List<String>,Credentials,List<String>> defaultMapping() { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
290 |
return (request, credentials) -> { |
37874 | 291 |
List<String> response = new LinkedList<>(); |
292 |
Iterator<String> iterator = request.iterator(); |
|
293 |
if (!iterator.hasNext()) { |
|
294 |
throw new IllegalStateException("The request is empty"); |
|
295 |
} |
|
48083 | 296 |
String statusLine = iterator.next(); |
297 |
if (!(statusLine.startsWith("GET /") && statusLine.endsWith(" HTTP/1.1"))) { |
|
37874 | 298 |
throw new IllegalStateException |
299 |
("Unexpected status line: " + request.get(0)); |
|
300 |
} |
|
301 |
response.add("HTTP/1.1 101 Switching Protocols"); |
|
48083 | 302 |
Map<String, List<String>> requestHeaders = new HashMap<>(); |
37874 | 303 |
while (iterator.hasNext()) { |
304 |
String header = iterator.next(); |
|
305 |
String[] split = header.split(": "); |
|
306 |
if (split.length != 2) { |
|
307 |
throw new IllegalStateException |
|
308 |
("Unexpected header: " + header |
|
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
309 |
+ ", split=" + Arrays.toString(split)); |
37874 | 310 |
} |
48083 | 311 |
requestHeaders.computeIfAbsent(split[0], k -> new ArrayList<>()).add(split[1]); |
312 |
||
37874 | 313 |
} |
314 |
if (requestHeaders.containsKey("Sec-WebSocket-Protocol")) { |
|
315 |
throw new IllegalStateException("Subprotocols are not expected"); |
|
316 |
} |
|
317 |
if (requestHeaders.containsKey("Sec-WebSocket-Extensions")) { |
|
318 |
throw new IllegalStateException("Extensions are not expected"); |
|
319 |
} |
|
320 |
expectHeader(requestHeaders, "Connection", "Upgrade"); |
|
321 |
response.add("Connection: Upgrade"); |
|
322 |
expectHeader(requestHeaders, "Upgrade", "websocket"); |
|
323 |
response.add("Upgrade: websocket"); |
|
324 |
expectHeader(requestHeaders, "Sec-WebSocket-Version", "13"); |
|
48083 | 325 |
List<String> key = requestHeaders.get("Sec-WebSocket-Key"); |
326 |
if (key == null || key.isEmpty()) { |
|
37874 | 327 |
throw new IllegalStateException("Sec-WebSocket-Key is missing"); |
328 |
} |
|
48083 | 329 |
if (key.size() != 1) { |
330 |
throw new IllegalStateException("Sec-WebSocket-Key has too many values : " + key); |
|
331 |
} |
|
37874 | 332 |
MessageDigest sha1 = null; |
333 |
try { |
|
334 |
sha1 = MessageDigest.getInstance("SHA-1"); |
|
335 |
} catch (NoSuchAlgorithmException e) { |
|
336 |
throw new InternalError(e); |
|
337 |
} |
|
48083 | 338 |
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
|
339 |
sha1.update(x.getBytes(ISO_8859_1)); |
37874 | 340 |
String v = Base64.getEncoder().encodeToString(sha1.digest()); |
341 |
response.add("Sec-WebSocket-Accept: " + v); |
|
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
342 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
343 |
// check authorization credentials, if required by the server |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
344 |
if (credentials != null && !authorized(credentials, requestHeaders)) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
345 |
response.clear(); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
346 |
response.add("HTTP/1.1 401 Unauthorized"); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
347 |
response.add("Content-Length: 0"); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
348 |
response.add("WWW-Authenticate: Basic realm=\"dummy server realm\""); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
349 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
350 |
|
37874 | 351 |
return response; |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
352 |
}; |
37874 | 353 |
} |
354 |
||
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
355 |
// Checks credentials in the request against those allowable by the server. |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
356 |
private static boolean authorized(Credentials credentials, |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
357 |
Map<String,List<String>> requestHeaders) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
358 |
List<String> authorization = requestHeaders.get("Authorization"); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
359 |
if (authorization == null) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
360 |
return false; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
361 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
362 |
if (authorization.size() != 1) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
363 |
throw new IllegalStateException("Authorization unexpected count:" + authorization); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
364 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
365 |
String header = authorization.get(0); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
366 |
if (!header.startsWith("Basic ")) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
367 |
throw new IllegalStateException("Authorization not Basic: " + header); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
368 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
369 |
header = header.substring("Basic ".length()); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
370 |
String values = new String(Base64.getDecoder().decode(header), UTF_8); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
371 |
int sep = values.indexOf(':'); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
372 |
if (sep < 1) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
373 |
throw new IllegalStateException("Authorization not colon: " + values); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
374 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
375 |
String name = values.substring(0, sep); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
376 |
String password = values.substring(sep + 1); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
377 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
378 |
if (name.equals(credentials.name()) && password.equals(credentials.password())) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
379 |
return true; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
380 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
381 |
return false; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
382 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
383 |
|
48083 | 384 |
protected static String expectHeader(Map<String, List<String>> headers, |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
385 |
String name, |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
386 |
String value) { |
48083 | 387 |
List<String> v = headers.get(name); |
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
388 |
if (v == null) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
389 |
throw new IllegalStateException( |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
390 |
format("Expected '%s' header, not present in %s", |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
391 |
name, headers)); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
392 |
} |
48083 | 393 |
if (!v.contains(value)) { |
37874 | 394 |
throw new IllegalStateException( |
395 |
format("Expected '%s: %s', actual: '%s: %s'", |
|
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
396 |
name, value, name, v) |
37874 | 397 |
); |
398 |
} |
|
48083 | 399 |
return value; |
37874 | 400 |
} |
401 |
||
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
402 |
private static void close(AutoCloseable... acs) { |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
403 |
for (AutoCloseable ac : acs) { |
37874 | 404 |
try { |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
405 |
ac.close(); |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
37874
diff
changeset
|
406 |
} catch (Exception ignored) { } |
37874 | 407 |
} |
408 |
} |
|
409 |
} |