< prev index next >
test/jdk/java/net/httpclient/websocket/DummyWebSocketServer.java
Print this page
@@ -32,10 +32,11 @@
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.CharacterCodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
@@ -45,13 +46,11 @@
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static java.lang.String.format;
-import static java.lang.System.Logger.Level.ERROR;
-import static java.lang.System.Logger.Level.INFO;
-import static java.lang.System.Logger.Level.TRACE;
+import static java.lang.System.err;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static java.util.Arrays.asList;
import static java.util.Objects.requireNonNull;
/**
@@ -81,11 +80,10 @@
* Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
* Sec-WebSocket-Protocol: chat
*/
public final class DummyWebSocketServer implements Closeable {
- private final static System.Logger log = System.getLogger(DummyWebSocketServer.class.getName());
private final AtomicBoolean started = new AtomicBoolean();
private final Thread thread;
private volatile ServerSocketChannel ssc;
private volatile InetSocketAddress address;
@@ -96,13 +94,13 @@
public DummyWebSocketServer(Function<List<String>, List<String>> mapping) {
requireNonNull(mapping);
thread = new Thread(() -> {
try {
while (!Thread.currentThread().isInterrupted()) {
- log.log(INFO, "Accepting next connection at: " + ssc);
+ err.println("Accepting next connection at: " + ssc);
SocketChannel channel = ssc.accept();
- log.log(INFO, "Accepted: " + channel);
+ err.println("Accepted: " + channel);
try {
channel.configureBlocking(true);
StringBuilder request = new StringBuilder();
if (!readRequest(channel, request)) {
throw new IOException("Bad request:" + request);
@@ -115,30 +113,30 @@
ByteBuffer b = ByteBuffer.allocate(1024);
while (channel.read(b) != -1) {
b.clear();
}
} catch (IOException e) {
- log.log(TRACE, () -> "Error in connection: " + channel, e);
+ err.println("Error in connection: " + channel + ", " + e);
} finally {
- log.log(INFO, "Closed: " + channel);
+ err.println("Closed: " + channel);
close(channel);
}
}
} catch (ClosedByInterruptException ignored) {
} catch (IOException e) {
- log.log(ERROR, e);
+ err.println(e);
} finally {
close(ssc);
- log.log(INFO, "Stopped at: " + getURI());
+ err.println("Stopped at: " + getURI());
}
});
thread.setName("DummyWebSocketServer");
thread.setDaemon(false);
}
public void open() throws IOException {
- log.log(INFO, "Starting");
+ err.println("Starting");
if (!started.compareAndSet(false, true)) {
throw new IllegalStateException("Already started");
}
ssc = ServerSocketChannel.open();
try {
@@ -147,16 +145,16 @@
address = (InetSocketAddress) ssc.getLocalAddress();
thread.start();
} catch (IOException e) {
close(ssc);
}
- log.log(INFO, "Started at: " + getURI());
+ err.println("Started at: " + getURI());
}
@Override
public void close() {
- log.log(INFO, "Stopping: " + getURI());
+ err.println("Stopping: " + getURI());
thread.interrupt();
close(ssc);
}
URI getURI() {
@@ -208,28 +206,27 @@
List<String> response = new LinkedList<>();
Iterator<String> iterator = request.iterator();
if (!iterator.hasNext()) {
throw new IllegalStateException("The request is empty");
}
- if (!"GET / HTTP/1.1".equals(iterator.next())) {
+ String statusLine = iterator.next();
+ if (!(statusLine.startsWith("GET /") && statusLine.endsWith(" HTTP/1.1"))) {
throw new IllegalStateException
("Unexpected status line: " + request.get(0));
}
response.add("HTTP/1.1 101 Switching Protocols");
- Map<String, String> requestHeaders = new HashMap<>();
+ Map<String, List<String>> requestHeaders = new HashMap<>();
while (iterator.hasNext()) {
String header = iterator.next();
String[] split = header.split(": ");
if (split.length != 2) {
throw new IllegalStateException
("Unexpected header: " + header
+ ", split=" + Arrays.toString(split));
}
- if (requestHeaders.put(split[0], split[1]) != null) {
- throw new IllegalStateException
- ("Duplicating headers: " + Arrays.toString(split));
- }
+ requestHeaders.computeIfAbsent(split[0], k -> new ArrayList<>()).add(split[1]);
+
}
if (requestHeaders.containsKey("Sec-WebSocket-Protocol")) {
throw new IllegalStateException("Subprotocols are not expected");
}
if (requestHeaders.containsKey("Sec-WebSocket-Extensions")) {
@@ -238,39 +235,42 @@
expectHeader(requestHeaders, "Connection", "Upgrade");
response.add("Connection: Upgrade");
expectHeader(requestHeaders, "Upgrade", "websocket");
response.add("Upgrade: websocket");
expectHeader(requestHeaders, "Sec-WebSocket-Version", "13");
- String key = requestHeaders.get("Sec-WebSocket-Key");
- if (key == null) {
+ List<String> key = requestHeaders.get("Sec-WebSocket-Key");
+ if (key == null || key.isEmpty()) {
throw new IllegalStateException("Sec-WebSocket-Key is missing");
}
+ if (key.size() != 1) {
+ throw new IllegalStateException("Sec-WebSocket-Key has too many values : " + key);
+ }
MessageDigest sha1 = null;
try {
sha1 = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
throw new InternalError(e);
}
- String x = key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
+ String x = key.get(0) + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
sha1.update(x.getBytes(ISO_8859_1));
String v = Base64.getEncoder().encodeToString(sha1.digest());
response.add("Sec-WebSocket-Accept: " + v);
return response;
};
}
- protected static String expectHeader(Map<String, String> headers,
+ protected static String expectHeader(Map<String, List<String>> headers,
String name,
String value) {
- String v = headers.get(name);
- if (!value.equals(v)) {
+ List<String> v = headers.get(name);
+ if (!v.contains(value)) {
throw new IllegalStateException(
format("Expected '%s: %s', actual: '%s: %s'",
name, value, name, v)
);
}
- return v;
+ return value;
}
private static void close(AutoCloseable... acs) {
for (AutoCloseable ac : acs) {
try {
< prev index next >