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