test/jdk/java/net/httpclient/websocket/DummyWebSocketServer.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 43999 jdk/test/java/net/httpclient/websocket/DummyWebSocketServer.java@4cc44dd9f14f
child 47266 b841be61b9d9
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     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
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     4
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     7
 * published by the Free Software Foundation.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     8
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    13
 * accompanied this code).
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    14
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    18
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    21
 * questions.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    22
 */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    23
43999
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    24
import java.io.Closeable;
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    25
import java.io.IOException;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    26
import java.io.UncheckedIOException;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    27
import java.net.InetSocketAddress;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    28
import java.net.URI;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    29
import java.nio.ByteBuffer;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    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
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    32
import java.nio.channels.ServerSocketChannel;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    33
import java.nio.channels.SocketChannel;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    34
import java.nio.charset.CharacterCodingException;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    35
import java.security.MessageDigest;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    36
import java.security.NoSuchAlgorithmException;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    37
import java.util.Arrays;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    38
import java.util.Base64;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    39
import java.util.HashMap;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    40
import java.util.Iterator;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    41
import java.util.LinkedList;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    42
import java.util.List;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    43
import java.util.Map;
43999
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    44
import java.util.concurrent.atomic.AtomicBoolean;
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    45
import java.util.function.Function;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    46
import java.util.regex.Pattern;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    47
import java.util.stream.Collectors;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    48
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    49
import static java.lang.String.format;
43999
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    50
import static java.lang.System.Logger.Level.ERROR;
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    51
import static java.lang.System.Logger.Level.INFO;
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    52
import static java.lang.System.Logger.Level.TRACE;
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    53
import static java.nio.charset.StandardCharsets.ISO_8859_1;
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    54
import static java.util.Arrays.asList;
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    55
import static java.util.Objects.requireNonNull;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    56
43999
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    57
/**
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    58
 * Dummy WebSocket Server.
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    59
 *
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    60
 * 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
    61
 * 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
    62
 * 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
    63
 * disconnect first.
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    64
 *
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    65
 * Expected client request:
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    66
 *
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    67
 *     GET /chat HTTP/1.1
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    68
 *     Host: server.example.com
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    69
 *     Upgrade: websocket
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    70
 *     Connection: Upgrade
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    71
 *     Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    72
 *     Origin: http://example.com
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    73
 *     Sec-WebSocket-Protocol: chat, superchat
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    74
 *     Sec-WebSocket-Version: 13
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    75
 *
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    76
 * This server response:
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    77
 *
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    78
 *     HTTP/1.1 101 Switching Protocols
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    79
 *     Upgrade: websocket
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    80
 *     Connection: Upgrade
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    81
 *     Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    82
 *     Sec-WebSocket-Protocol: chat
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    83
 */
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    84
public final class DummyWebSocketServer implements Closeable {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    85
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    86
    private final static System.Logger log = System.getLogger(DummyWebSocketServer.class.getName());
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    87
    private final AtomicBoolean started = new AtomicBoolean();
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    88
    private final Thread thread;
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    89
    private volatile ServerSocketChannel ssc;
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    90
    private volatile InetSocketAddress address;
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    91
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    92
    public DummyWebSocketServer() {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    93
        this(defaultMapping());
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    94
    }
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    95
43999
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    96
    public DummyWebSocketServer(Function<List<String>, List<String>> mapping) {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    97
        requireNonNull(mapping);
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    98
        thread = new Thread(() -> {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
    99
            try {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   100
                while (!Thread.currentThread().isInterrupted()) {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   101
                    log.log(INFO, "Accepting next connection at: " + ssc);
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   102
                    SocketChannel channel = ssc.accept();
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   103
                    log.log(INFO, "Accepted: " + channel);
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   104
                    try {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   105
                        channel.configureBlocking(true);
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   106
                        StringBuilder request = new StringBuilder();
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   107
                        if (!readRequest(channel, request)) {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   108
                            throw new IOException("Bad request");
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   109
                        }
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   110
                        List<String> strings = asList(request.toString().split("\r\n"));
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   111
                        List<String> response = mapping.apply(strings);
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   112
                        writeResponse(channel, response);
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   113
                        // Read until the thread is interrupted or an error occurred
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   114
                        // or the input is shutdown
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   115
                        ByteBuffer b = ByteBuffer.allocate(1024);
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   116
                        while (channel.read(b) != -1) {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   117
                            b.clear();
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   118
                        }
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   119
                    } catch (IOException e) {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   120
                        log.log(TRACE, () -> "Error in connection: " + channel, e);
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   121
                    } finally {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   122
                        log.log(INFO, "Closed: " + channel);
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   123
                        close(channel);
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   124
                    }
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   125
                }
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   126
            } catch (ClosedByInterruptException ignored) {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   127
            } catch (IOException e) {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   128
                log.log(ERROR, e);
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   129
            } finally {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   130
                close(ssc);
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   131
                log.log(INFO, "Stopped at: " + getURI());
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   132
            }
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   133
        });
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   134
        thread.setName("DummyWebSocketServer");
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   135
        thread.setDaemon(false);
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   136
    }
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   137
43999
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   138
    public void open() throws IOException {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   139
        log.log(INFO, "Starting");
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   140
        if (!started.compareAndSet(false, true)) {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   141
            throw new IllegalStateException("Already started");
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   142
        }
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   143
        ssc = ServerSocketChannel.open();
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   144
        try {
43999
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   145
            ssc.configureBlocking(true);
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   146
            ssc.bind(new InetSocketAddress("localhost", 0));
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   147
            address = (InetSocketAddress) ssc.getLocalAddress();
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   148
            thread.start();
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   149
        } catch (IOException e) {
43999
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   150
            close(ssc);
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
        log.log(INFO, "Started at: " + getURI());
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   153
    }
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
    @Override
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   156
    public void close() {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   157
        log.log(INFO, "Stopping: " + getURI());
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   158
        thread.interrupt();
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
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   161
    URI getURI() {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   162
        if (!started.get()) {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   163
            throw new IllegalStateException("Not yet started");
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   164
        }
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   165
        return URI.create("ws://" + address.getHostName() + ":" + address.getPort());
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
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   168
    private boolean readRequest(SocketChannel channel, StringBuilder request)
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   169
            throws IOException
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   170
    {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   171
        ByteBuffer buffer = ByteBuffer.allocate(512);
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   172
        int num = channel.read(buffer);
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   173
        if (num == -1) {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   174
            return false;
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   175
        }
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   176
        CharBuffer decoded;
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   177
        buffer.flip();
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   178
        try {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   179
            decoded = ISO_8859_1.newDecoder().decode(buffer);
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   180
        } catch (CharacterCodingException e) {
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   181
            throw new UncheckedIOException(e);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   182
        }
43999
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   183
        request.append(decoded);
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   184
        return Pattern.compile("\r\n\r\n").matcher(request).find();
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   185
    }
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
    private void writeResponse(SocketChannel channel, List<String> response)
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   188
            throws IOException
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   189
    {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   190
        String s = response.stream().collect(Collectors.joining("\r\n"))
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   191
                + "\r\n\r\n";
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   192
        ByteBuffer encoded;
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   193
        try {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   194
            encoded = ISO_8859_1.newEncoder().encode(CharBuffer.wrap(s));
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   195
        } catch (CharacterCodingException e) {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   196
            throw new UncheckedIOException(e);
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   197
        }
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   198
        while (encoded.hasRemaining()) {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   199
            channel.write(encoded);
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   200
        }
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   201
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   202
43999
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   203
    private static Function<List<String>, List<String>> defaultMapping() {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   204
        return request -> {
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   205
            List<String> response = new LinkedList<>();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   206
            Iterator<String> iterator = request.iterator();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   207
            if (!iterator.hasNext()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   208
                throw new IllegalStateException("The request is empty");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   209
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   210
            if (!"GET / HTTP/1.1".equals(iterator.next())) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   211
                throw new IllegalStateException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   212
                        ("Unexpected status line: " + request.get(0));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   213
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   214
            response.add("HTTP/1.1 101 Switching Protocols");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   215
            Map<String, String> requestHeaders = new HashMap<>();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   216
            while (iterator.hasNext()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   217
                String header = iterator.next();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   218
                String[] split = header.split(": ");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   219
                if (split.length != 2) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   220
                    throw new IllegalStateException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   221
                            ("Unexpected header: " + header
43999
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   222
                                     + ", split=" + Arrays.toString(split));
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   223
                }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   224
                if (requestHeaders.put(split[0], split[1]) != null) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   225
                    throw new IllegalStateException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   226
                            ("Duplicating headers: " + Arrays.toString(split));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   227
                }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   228
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   229
            if (requestHeaders.containsKey("Sec-WebSocket-Protocol")) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   230
                throw new IllegalStateException("Subprotocols are not expected");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   231
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   232
            if (requestHeaders.containsKey("Sec-WebSocket-Extensions")) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   233
                throw new IllegalStateException("Extensions are not expected");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   234
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   235
            expectHeader(requestHeaders, "Connection", "Upgrade");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   236
            response.add("Connection: Upgrade");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   237
            expectHeader(requestHeaders, "Upgrade", "websocket");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   238
            response.add("Upgrade: websocket");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   239
            expectHeader(requestHeaders, "Sec-WebSocket-Version", "13");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   240
            String key = requestHeaders.get("Sec-WebSocket-Key");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   241
            if (key == null) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   242
                throw new IllegalStateException("Sec-WebSocket-Key is missing");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   243
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   244
            MessageDigest sha1 = null;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   245
            try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   246
                sha1 = MessageDigest.getInstance("SHA-1");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   247
            } catch (NoSuchAlgorithmException e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   248
                throw new InternalError(e);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   249
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   250
            String x = key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
43999
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   251
            sha1.update(x.getBytes(ISO_8859_1));
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   252
            String v = Base64.getEncoder().encodeToString(sha1.digest());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   253
            response.add("Sec-WebSocket-Accept: " + v);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   254
            return response;
43999
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   255
        };
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   256
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   257
43999
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   258
    protected static String expectHeader(Map<String, String> headers,
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   259
                                         String name,
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   260
                                         String value) {
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   261
        String v = headers.get(name);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   262
        if (!value.equals(v)) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   263
            throw new IllegalStateException(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   264
                    format("Expected '%s: %s', actual: '%s: %s'",
43999
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   265
                           name, value, name, v)
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   266
            );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   267
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   268
        return v;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   269
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   270
43999
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   271
    private static void close(AutoCloseable... acs) {
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   272
        for (AutoCloseable ac : acs) {
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   273
            try {
43999
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   274
                ac.close();
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 37874
diff changeset
   275
            } catch (Exception ignored) { }
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   276
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   277
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   278
}