< prev index next >
test/jdk/java/net/httpclient/websocket/DummyWebSocketServer.java
Print this page
@@ -103,11 +103,11 @@
log.log(INFO, "Accepted: " + channel);
try {
channel.configureBlocking(true);
StringBuilder request = new StringBuilder();
if (!readRequest(channel, request)) {
- throw new IOException("Bad request");
+ throw new IOException("Bad request:" + request);
}
List<String> strings = asList(request.toString().split("\r\n"));
List<String> response = mapping.apply(strings);
writeResponse(channel, response);
// Read until the thread is interrupted or an error occurred
@@ -154,10 +154,11 @@
@Override
public void close() {
log.log(INFO, "Stopping: " + getURI());
thread.interrupt();
+ close(ssc);
}
URI getURI() {
if (!started.get()) {
throw new IllegalStateException("Not yet started");
@@ -167,23 +168,25 @@
private boolean readRequest(SocketChannel channel, StringBuilder request)
throws IOException
{
ByteBuffer buffer = ByteBuffer.allocate(512);
- int num = channel.read(buffer);
- if (num == -1) {
- return false;
- }
+ while (channel.read(buffer) != -1) {
+ // read the complete HTTP request headers, there should be no body
CharBuffer decoded;
buffer.flip();
try {
decoded = ISO_8859_1.newDecoder().decode(buffer);
} catch (CharacterCodingException e) {
throw new UncheckedIOException(e);
}
request.append(decoded);
- return Pattern.compile("\r\n\r\n").matcher(request).find();
+ if (Pattern.compile("\r\n\r\n").matcher(request).find())
+ return true;
+ buffer.clear();
+ }
+ return false;
}
private void writeResponse(SocketChannel channel, List<String> response)
throws IOException
{
< prev index next >