jdk/src/java.httpclient/share/classes/java/net/http/WSOpeningHandshake.java
author prappo
Tue, 21 Jun 2016 18:51:18 +0100
changeset 39133 b5641ce64cf7
parent 38322 f6f9d3ec14ba
child 39730 196f4e25d9f5
permissions -rw-r--r--
8156742: Miscellaneous WebSocket API improvements Reviewed-by: chegar, rriggs
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) 2015, 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.  Oracle designates this
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    10
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    15
 * accompanied this code).
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    16
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    20
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    23
 * questions.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    24
 */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    25
package java.net.http;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    26
38322
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37874
diff changeset
    27
import java.io.UncheckedIOException;
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37874
diff changeset
    28
import java.io.IOException;
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    29
import java.net.URI;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    30
import java.net.URISyntaxException;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    31
import java.nio.charset.StandardCharsets;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    32
import java.security.MessageDigest;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    33
import java.security.NoSuchAlgorithmException;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    34
import java.security.SecureRandom;
39133
b5641ce64cf7 8156742: Miscellaneous WebSocket API improvements
prappo
parents: 38322
diff changeset
    35
import java.time.Duration;
b5641ce64cf7 8156742: Miscellaneous WebSocket API improvements
prappo
parents: 38322
diff changeset
    36
import java.time.temporal.ChronoUnit;
37874
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.Collection;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    40
import java.util.List;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    41
import java.util.concurrent.CompletableFuture;
39133
b5641ce64cf7 8156742: Miscellaneous WebSocket API improvements
prappo
parents: 38322
diff changeset
    42
import java.util.concurrent.TimeUnit;
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    43
import java.util.function.Predicate;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    44
import java.util.stream.Collectors;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    45
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    46
import static java.lang.String.format;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    47
import static java.lang.System.Logger.Level.TRACE;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    48
import static java.net.http.WSUtils.logger;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    49
import static java.net.http.WSUtils.webSocketSpecViolation;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    50
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    51
final class WSOpeningHandshake {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    52
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    53
    private static final String HEADER_CONNECTION = "Connection";
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    54
    private static final String HEADER_UPGRADE = "Upgrade";
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    55
    private static final String HEADER_ACCEPT = "Sec-WebSocket-Accept";
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    56
    private static final String HEADER_EXTENSIONS = "Sec-WebSocket-Extensions";
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    57
    private static final String HEADER_KEY = "Sec-WebSocket-Key";
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    58
    private static final String HEADER_PROTOCOL = "Sec-WebSocket-Protocol";
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    59
    private static final String HEADER_VERSION = "Sec-WebSocket-Version";
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    60
    private static final String VALUE_VERSION = "13"; // WebSocket's lucky number
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    61
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    62
    private static final SecureRandom srandom = new SecureRandom();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    63
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    64
    private final MessageDigest sha1;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    65
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    66
    {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    67
        try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    68
            sha1 = MessageDigest.getInstance("SHA-1");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    69
        } catch (NoSuchAlgorithmException e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    70
            // Shouldn't happen:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    71
            // SHA-1 must be available in every Java platform implementation
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    72
            throw new InternalError("Minimum platform requirements are not met", e);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    73
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    74
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    75
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    76
    private final HttpRequest request;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    77
    private final Collection<String> subprotocols;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    78
    private final String nonce;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    79
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    80
    WSOpeningHandshake(WSBuilder b) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    81
        URI httpURI = createHttpUri(b.getUri());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    82
        HttpRequest.Builder requestBuilder = b.getClient().request(httpURI);
39133
b5641ce64cf7 8156742: Miscellaneous WebSocket API improvements
prappo
parents: 38322
diff changeset
    83
        Duration connectTimeout = b.getConnectTimeout();
b5641ce64cf7 8156742: Miscellaneous WebSocket API improvements
prappo
parents: 38322
diff changeset
    84
        if (connectTimeout != null) {
b5641ce64cf7 8156742: Miscellaneous WebSocket API improvements
prappo
parents: 38322
diff changeset
    85
            requestBuilder.timeout(
b5641ce64cf7 8156742: Miscellaneous WebSocket API improvements
prappo
parents: 38322
diff changeset
    86
                    TimeUnit.of(ChronoUnit.MILLIS),
b5641ce64cf7 8156742: Miscellaneous WebSocket API improvements
prappo
parents: 38322
diff changeset
    87
                    connectTimeout.get(ChronoUnit.MILLIS)
b5641ce64cf7 8156742: Miscellaneous WebSocket API improvements
prappo
parents: 38322
diff changeset
    88
            );
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    89
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    90
        Collection<String> s = b.getSubprotocols();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    91
        if (!s.isEmpty()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    92
            String p = s.stream().collect(Collectors.joining(", "));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    93
            requestBuilder.header(HEADER_PROTOCOL, p);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    94
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    95
        requestBuilder.header(HEADER_VERSION, VALUE_VERSION);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    96
        this.nonce = createNonce();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    97
        requestBuilder.header(HEADER_KEY, this.nonce);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    98
        this.request = requestBuilder.GET();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    99
        HttpRequestImpl r = (HttpRequestImpl) this.request;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   100
        r.isWebSocket(true);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   101
        r.setSystemHeader(HEADER_UPGRADE, "websocket");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   102
        r.setSystemHeader(HEADER_CONNECTION, "Upgrade");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   103
        this.subprotocols = s;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   104
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   105
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   106
    private URI createHttpUri(URI webSocketUri) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   107
        // FIXME: check permission for WebSocket URI and translate it into http/https permission
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   108
        logger.log(TRACE, "->createHttpUri(''{0}'')", webSocketUri);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   109
        String httpScheme = webSocketUri.getScheme().equalsIgnoreCase("ws")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   110
                ? "http"
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   111
                : "https";
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   112
        try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   113
            URI uri = new URI(httpScheme,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   114
                    webSocketUri.getUserInfo(),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   115
                    webSocketUri.getHost(),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   116
                    webSocketUri.getPort(),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   117
                    webSocketUri.getPath(),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   118
                    webSocketUri.getQuery(),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   119
                    null);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   120
            logger.log(TRACE, "<-createHttpUri: ''{0}''", uri);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   121
            return uri;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   122
        } catch (URISyntaxException e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   123
            // Shouldn't happen: URI invariant
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   124
            throw new InternalError("Error translating WebSocket URI to HTTP URI", e);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   125
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   126
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   127
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   128
    CompletableFuture<Result> performAsync() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   129
        // The whole dancing with thenCompose instead of thenApply is because
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   130
        // WebSocketHandshakeException is a checked exception
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   131
        return request.responseAsync()
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   132
                .thenCompose(response -> {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   133
                    try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   134
                        Result result = handleResponse(response);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   135
                        return CompletableFuture.completedFuture(result);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   136
                    } catch (WebSocketHandshakeException e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   137
                        return CompletableFuture.failedFuture(e);
38322
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37874
diff changeset
   138
                    } catch (UncheckedIOException ee) {
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37874
diff changeset
   139
                        return CompletableFuture.failedFuture(ee.getCause());
37874
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
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   143
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   144
    private Result handleResponse(HttpResponse response) throws WebSocketHandshakeException {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   145
        // By this point all redirects, authentications, etc. (if any) must have
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   146
        // been done by the httpClient used by the WebSocket; so only 101 is
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   147
        // expected
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   148
        int statusCode = response.statusCode();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   149
        if (statusCode != 101) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   150
            String m = webSocketSpecViolation("1.3.",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   151
                    "Unable to complete handshake; HTTP response status code "
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   152
                            + statusCode
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   153
            );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   154
            throw new WebSocketHandshakeException(m, response);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   155
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   156
        HttpHeaders h = response.headers();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   157
        checkHeader(h, response, HEADER_UPGRADE, v -> v.equalsIgnoreCase("websocket"));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   158
        checkHeader(h, response, HEADER_CONNECTION, v -> v.equalsIgnoreCase("Upgrade"));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   159
        checkVersion(response, h);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   160
        checkAccept(response, h);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   161
        checkExtensions(response, h);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   162
        String subprotocol = checkAndReturnSubprotocol(response, h);
38322
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37874
diff changeset
   163
        RawChannel channel = null;
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37874
diff changeset
   164
        try {
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37874
diff changeset
   165
            channel = ((HttpResponseImpl) response).rawChannel();
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37874
diff changeset
   166
        } catch (IOException e) {
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37874
diff changeset
   167
            throw new UncheckedIOException(e);
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37874
diff changeset
   168
        }
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   169
        return new Result(subprotocol, channel);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   170
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   171
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   172
    private void checkExtensions(HttpResponse response, HttpHeaders headers)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   173
            throws WebSocketHandshakeException {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   174
        List<String> ext = headers.allValues(HEADER_EXTENSIONS);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   175
        if (!ext.isEmpty()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   176
            String m = webSocketSpecViolation("4.1.",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   177
                    "Server responded with extension(s) though none were requested "
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   178
                            + Arrays.toString(ext.toArray())
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   179
            );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   180
            throw new WebSocketHandshakeException(m, response);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   181
        }
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
    private String checkAndReturnSubprotocol(HttpResponse response, HttpHeaders headers)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   185
            throws WebSocketHandshakeException {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   186
        assert response.statusCode() == 101 : response.statusCode();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   187
        List<String> sp = headers.allValues(HEADER_PROTOCOL);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   188
        int size = sp.size();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   189
        if (size == 0) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   190
            // In this case the subprotocol requested (if any) by the client
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   191
            // doesn't matter. If there is no such header in the response, then
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   192
            // the server doesn't want to use any subprotocol
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   193
            return null;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   194
        } else if (size > 1) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   195
            // We don't know anything about toString implementation of this
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   196
            // list, so let's create an array
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   197
            String m = webSocketSpecViolation("4.1.",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   198
                    "Server responded with multiple subprotocols: "
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   199
                            + Arrays.toString(sp.toArray())
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   200
            );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   201
            throw new WebSocketHandshakeException(m, response);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   202
        } else {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   203
            String selectedSubprotocol = sp.get(0);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   204
            if (this.subprotocols.contains(selectedSubprotocol)) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   205
                return selectedSubprotocol;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   206
            } else {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   207
                String m = webSocketSpecViolation("4.1.",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   208
                        format("Server responded with a subprotocol " +
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   209
                                        "not among those requested: '%s'",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   210
                                selectedSubprotocol));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   211
                throw new WebSocketHandshakeException(m, response);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   212
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   213
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   214
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   215
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   216
    private void checkAccept(HttpResponse response, HttpHeaders headers)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   217
            throws WebSocketHandshakeException {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   218
        assert response.statusCode() == 101 : response.statusCode();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   219
        String x = nonce + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   220
        sha1.update(x.getBytes(StandardCharsets.ISO_8859_1));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   221
        String expected = Base64.getEncoder().encodeToString(sha1.digest());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   222
        checkHeader(headers, response, HEADER_ACCEPT, actual -> actual.trim().equals(expected));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   223
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   224
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   225
    private void checkVersion(HttpResponse response, HttpHeaders headers)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   226
            throws WebSocketHandshakeException {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   227
        assert response.statusCode() == 101 : response.statusCode();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   228
        List<String> versions = headers.allValues(HEADER_VERSION);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   229
        if (versions.isEmpty()) { // That's normal and expected
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   230
            return;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   231
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   232
        String m = webSocketSpecViolation("4.4.",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   233
                "Server responded with version(s) "
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   234
                        + Arrays.toString(versions.toArray()));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   235
        throw new WebSocketHandshakeException(m, response);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   236
    }
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
    // Checks whether there's only one value for the header with the given name
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   240
    // and the value satisfies the predicate.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   241
    //
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   242
    private static void checkHeader(HttpHeaders headers,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   243
                                    HttpResponse response,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   244
                                    String headerName,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   245
                                    Predicate<? super String> valuePredicate)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   246
            throws WebSocketHandshakeException {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   247
        assert response.statusCode() == 101 : response.statusCode();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   248
        List<String> values = headers.allValues(headerName);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   249
        if (values.isEmpty()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   250
            String m = webSocketSpecViolation("4.1.",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   251
                    format("Server response field '%s' is missing", headerName)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   252
            );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   253
            throw new WebSocketHandshakeException(m, response);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   254
        } else if (values.size() > 1) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   255
            String m = webSocketSpecViolation("4.1.",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   256
                    format("Server response field '%s' has multiple values", headerName)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   257
            );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   258
            throw new WebSocketHandshakeException(m, response);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   259
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   260
        if (!valuePredicate.test(values.get(0))) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   261
            String m = webSocketSpecViolation("4.1.",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   262
                    format("Server response field '%s' is incorrect", headerName)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   263
            );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   264
            throw new WebSocketHandshakeException(m, response);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   265
        }
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
    private static String createNonce() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   269
        byte[] bytes = new byte[16];
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   270
        srandom.nextBytes(bytes);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   271
        return Base64.getEncoder().encodeToString(bytes);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   272
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   273
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   274
    static final class Result {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   275
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   276
        final String subprotocol;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   277
        final RawChannel channel;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   278
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   279
        private Result(String subprotocol, RawChannel channel) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   280
            this.subprotocol = subprotocol;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   281
            this.channel = channel;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   282
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   283
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   284
}