jdk/test/java/net/httpclient/HandshakePhase.java
author michaelm
Fri, 09 Dec 2016 11:35:02 +0000
changeset 42460 7133f144981a
parent 37874 02589df0999a
permissions -rw-r--r--
8170648: Move java.net.http package out of Java SE to incubator namespace Reviewed-by: chegar, dfuchs, michaelm, prappo, skuksenko Contributed-by: Chris Hegarty <chris.hegarty@oracle.com>, Daniel Fuchs <daniel.fuchs@oracle.com>, Michael McMahon <michael.x.mcmahon@oracle.com>, Pavel Rappo <pavel.rappo@oracle.com>, Sergey Kuksenko <sergey.kuksenko@oracle.com>, anthony.vanelverdinghe@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     1
/*
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
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
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    24
import java.io.IOException;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    25
import java.io.UncheckedIOException;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    26
import java.net.InetSocketAddress;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    27
import java.net.URI;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    28
import java.nio.ByteBuffer;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    29
import java.nio.CharBuffer;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    30
import java.nio.channels.ServerSocketChannel;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    31
import java.nio.channels.SocketChannel;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    32
import java.nio.charset.CharacterCodingException;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    33
import java.nio.charset.StandardCharsets;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    34
import java.security.MessageDigest;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    35
import java.security.NoSuchAlgorithmException;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    36
import java.util.Arrays;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    37
import java.util.Base64;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    38
import java.util.HashMap;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    39
import java.util.Iterator;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    40
import java.util.LinkedList;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    41
import java.util.List;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    42
import java.util.Map;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    43
import java.util.concurrent.CompletableFuture;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    44
import java.util.function.Function;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    45
import java.util.regex.Pattern;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    46
import java.util.stream.Collectors;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    47
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    48
import static java.lang.String.format;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    49
import static java.util.Objects.requireNonNull;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    50
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    51
//
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    52
// Performs a simple opening handshake and yields the channel.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    53
//
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    54
// Client Request:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    55
//
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    56
//    GET /chat HTTP/1.1
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    57
//    Host: server.example.com
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    58
//    Upgrade: websocket
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    59
//    Connection: Upgrade
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    60
//    Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    61
//    Origin: http://example.com
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    62
//    Sec-WebSocket-Protocol: chat, superchat
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    63
//    Sec-WebSocket-Version: 13
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    64
//
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    65
//
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    66
// Server Response:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    67
//
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    68
//    HTTP/1.1 101 Switching Protocols
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    69
//    Upgrade: websocket
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    70
//    Connection: Upgrade
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    71
//    Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    72
//    Sec-WebSocket-Protocol: chat
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    73
//
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    74
final class HandshakePhase {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    75
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    76
    private final ServerSocketChannel ssc;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    77
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    78
    HandshakePhase(InetSocketAddress address) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    79
        requireNonNull(address);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    80
        try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    81
            ssc = ServerSocketChannel.open();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    82
            ssc.bind(address);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    83
        } catch (IOException e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    84
            throw new UncheckedIOException(e);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    85
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    86
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    87
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    88
    //
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    89
    // Returned CF completes normally after the handshake has been performed
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    90
    //
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    91
    CompletableFuture<SocketChannel> afterHandshake(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    92
            Function<List<String>, List<String>> mapping) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    93
        return CompletableFuture.supplyAsync(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    94
                () -> {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    95
                    SocketChannel socketChannel = accept();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    96
                    try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    97
                        StringBuilder request = new StringBuilder();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    98
                        if (!readRequest(socketChannel, request)) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    99
                            throw new IllegalStateException();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   100
                        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   101
                        List<String> strings = Arrays.asList(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   102
                                request.toString().split("\r\n")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   103
                        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   104
                        List<String> response = mapping.apply(strings);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   105
                        writeResponse(socketChannel, response);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   106
                        return socketChannel;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   107
                    } catch (Throwable t) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   108
                        try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   109
                            socketChannel.close();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   110
                        } catch (IOException ignored) { }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   111
                        throw t;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   112
                    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   113
                });
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   114
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   115
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   116
    CompletableFuture<SocketChannel> afterHandshake() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   117
        return afterHandshake((request) -> {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   118
            List<String> response = new LinkedList<>();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   119
            Iterator<String> iterator = request.iterator();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   120
            if (!iterator.hasNext()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   121
                throw new IllegalStateException("The request is empty");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   122
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   123
            if (!"GET / HTTP/1.1".equals(iterator.next())) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   124
                throw new IllegalStateException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   125
                        ("Unexpected status line: " + request.get(0));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   126
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   127
            response.add("HTTP/1.1 101 Switching Protocols");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   128
            Map<String, String> requestHeaders = new HashMap<>();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   129
            while (iterator.hasNext()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   130
                String header = iterator.next();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   131
                String[] split = header.split(": ");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   132
                if (split.length != 2) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   133
                    throw new IllegalStateException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   134
                            ("Unexpected header: " + header
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   135
                                    + ", split=" + Arrays.toString(split));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   136
                }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   137
                if (requestHeaders.put(split[0], split[1]) != null) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   138
                    throw new IllegalStateException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   139
                            ("Duplicating headers: " + Arrays.toString(split));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   140
                }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   141
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   142
            if (requestHeaders.containsKey("Sec-WebSocket-Protocol")) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   143
                throw new IllegalStateException("Subprotocols are not expected");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   144
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   145
            if (requestHeaders.containsKey("Sec-WebSocket-Extensions")) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   146
                throw new IllegalStateException("Extensions are not expected");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   147
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   148
            expectHeader(requestHeaders, "Connection", "Upgrade");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   149
            response.add("Connection: Upgrade");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   150
            expectHeader(requestHeaders, "Upgrade", "websocket");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   151
            response.add("Upgrade: websocket");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   152
            expectHeader(requestHeaders, "Sec-WebSocket-Version", "13");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   153
            String key = requestHeaders.get("Sec-WebSocket-Key");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   154
            if (key == null) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   155
                throw new IllegalStateException("Sec-WebSocket-Key is missing");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   156
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   157
            MessageDigest sha1 = null;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   158
            try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   159
                sha1 = MessageDigest.getInstance("SHA-1");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   160
            } catch (NoSuchAlgorithmException e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   161
                throw new InternalError(e);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   162
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   163
            String x = key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   164
            sha1.update(x.getBytes(StandardCharsets.ISO_8859_1));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   165
            String v = Base64.getEncoder().encodeToString(sha1.digest());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   166
            response.add("Sec-WebSocket-Accept: " + v);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   167
            return response;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   168
        });
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   169
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   170
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   171
    private String expectHeader(Map<String, String> headers,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   172
                                String name,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   173
                                String value) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   174
        String v = headers.get(name);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   175
        if (!value.equals(v)) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   176
            throw new IllegalStateException(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   177
                    format("Expected '%s: %s', actual: '%s: %s'",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   178
                            name, value, name, v)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   179
            );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   180
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   181
        return v;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   182
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   183
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   184
    URI getURI() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   185
        InetSocketAddress a;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   186
        try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   187
            a = (InetSocketAddress) ssc.getLocalAddress();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   188
        } catch (IOException e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   189
            throw new UncheckedIOException(e);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   190
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   191
        return URI.create("ws://" + a.getHostName() + ":" + a.getPort());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   192
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   193
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   194
    private int read(SocketChannel socketChannel, ByteBuffer buffer) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   195
        try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   196
            int num = socketChannel.read(buffer);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   197
            if (num == -1) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   198
                throw new IllegalStateException("Unexpected EOF");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   199
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   200
            assert socketChannel.isBlocking() && num > 0;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   201
            return num;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   202
        } catch (IOException e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   203
            throw new UncheckedIOException(e);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   204
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   205
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   206
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   207
    private SocketChannel accept() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   208
        SocketChannel socketChannel = null;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   209
        try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   210
            socketChannel = ssc.accept();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   211
            socketChannel.configureBlocking(true);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   212
        } catch (IOException e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   213
            if (socketChannel != null) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   214
                try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   215
                    socketChannel.close();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   216
                } catch (IOException ignored) { }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   217
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   218
            throw new UncheckedIOException(e);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   219
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   220
        return socketChannel;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   221
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   222
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   223
    private boolean readRequest(SocketChannel socketChannel,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   224
                                StringBuilder request) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   225
        ByteBuffer buffer = ByteBuffer.allocateDirect(512);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   226
        read(socketChannel, buffer);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   227
        CharBuffer decoded;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   228
        buffer.flip();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   229
        try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   230
            decoded =
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   231
                    StandardCharsets.ISO_8859_1.newDecoder().decode(buffer);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   232
        } catch (CharacterCodingException e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   233
            throw new UncheckedIOException(e);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   234
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   235
        request.append(decoded);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   236
        return Pattern.compile("\r\n\r\n").matcher(request).find();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   237
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   238
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   239
    private void writeResponse(SocketChannel socketChannel,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   240
                               List<String> response) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   241
        String s = response.stream().collect(Collectors.joining("\r\n"))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   242
                + "\r\n\r\n";
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   243
        ByteBuffer encoded;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   244
        try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   245
            encoded =
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   246
                    StandardCharsets.ISO_8859_1.newEncoder().encode(CharBuffer.wrap(s));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   247
        } catch (CharacterCodingException e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   248
            throw new UncheckedIOException(e);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   249
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   250
        write(socketChannel, encoded);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   251
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   252
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   253
    private void write(SocketChannel socketChannel, ByteBuffer buffer) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   254
        try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   255
            while (buffer.hasRemaining()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   256
                socketChannel.write(buffer);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   257
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   258
        } catch (IOException e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   259
            try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   260
                socketChannel.close();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   261
            } catch (IOException ignored) { }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   262
            throw new UncheckedIOException(e);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   263
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   264
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   265
}