< prev index next >

test/jdk/java/net/httpclient/websocket/DummyWebSocketServer.java

Print this page

        

*** 103,113 **** log.log(INFO, "Accepted: " + channel); try { channel.configureBlocking(true); StringBuilder request = new StringBuilder(); if (!readRequest(channel, request)) { ! throw new IOException("Bad 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 --- 103,113 ---- log.log(INFO, "Accepted: " + channel); try { channel.configureBlocking(true); StringBuilder request = new StringBuilder(); if (!readRequest(channel, 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,163 **** --- 154,164 ---- @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,189 **** private boolean readRequest(SocketChannel channel, StringBuilder request) throws IOException { ByteBuffer buffer = ByteBuffer.allocate(512); ! int num = channel.read(buffer); ! if (num == -1) { ! return false; ! } 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(); } private void writeResponse(SocketChannel channel, List<String> response) throws IOException { --- 168,192 ---- private boolean readRequest(SocketChannel channel, StringBuilder request) throws IOException { ByteBuffer buffer = ByteBuffer.allocate(512); ! 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); ! 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 >