/** * Perform the sub-protocol negotiation based on requested and supported sub-protocols. * For the list of supported sub-protocols, this method first checks if the target * WebSocketHandler is a {@link SubProtocolCapable} and then also checks if any * sub-protocols have been explicitly configured with * {@link #setSupportedProtocols(String...)}. * @param requestedProtocols the requested sub-protocols * @param webSocketHandler the WebSocketHandler that will be used * @return the selected protocols or {@code null} * @see #determineHandlerSupportedProtocols(WebSocketHandler) */ @Nullable protected String selectProtocol(List<String> requestedProtocols, WebSocketHandler webSocketHandler) { List<String> handlerProtocols = determineHandlerSupportedProtocols(webSocketHandler); for (String protocol : requestedProtocols) { if (handlerProtocols.contains(protocol.toLowerCase())) { return protocol; } if (this.supportedProtocols.contains(protocol.toLowerCase())) { return protocol; } } return null; }
handleInvalidUpgradeHeader(request, response); return false; handleInvalidConnectHeader(request, response); return false; if (!isWebSocketVersionSupported(headers)) { handleWebSocketVersionNotSupported(request, response); return false; if (!isValidOrigin(request)) { response.setStatusCode(HttpStatus.FORBIDDEN); return false; String subProtocol = selectProtocol(headers.getSecWebSocketProtocol(), wsHandler); List<WebSocketExtension> requested = headers.getSecWebSocketExtensions(); List<WebSocketExtension> supported = this.requestUpgradeStrategy.getSupportedExtensions(request); List<WebSocketExtension> extensions = filterRequestedExtensions(request, requested, supported); Principal user = determineUser(request, wsHandler, attributes);
protected boolean isWebSocketVersionSupported(WebSocketHttpHeaders httpHeaders) { String version = httpHeaders.getSecWebSocketVersion(); String[] supportedVersions = getSupportedVersions(); for (String supportedVersion : supportedVersions) { if (supportedVersion.trim().equals(version)) { return true; } } return false; }
/** * Default constructor that auto-detects and instantiates a * {@link RequestUpgradeStrategy} suitable for the runtime container. * @throws IllegalStateException if no {@link RequestUpgradeStrategy} can be found. */ protected AbstractHandshakeHandler() { this(initRequestUpgradeStrategy()); }
handleInvalidUpgradeHeader(request, response); return false; handleInvalidConnectHeader(request, response); return false; if (!isWebSocketVersionSupported(headers)) { handleWebSocketVersionNotSupported(request, response); return false; if (!isValidOrigin(request)) { response.setStatusCode(HttpStatus.FORBIDDEN); return false; String subProtocol = selectProtocol(headers.getSecWebSocketProtocol(), wsHandler); List<WebSocketExtension> requested = headers.getSecWebSocketExtensions(); List<WebSocketExtension> supported = this.requestUpgradeStrategy.getSupportedExtensions(request); List<WebSocketExtension> extensions = filterRequestedExtensions(request, requested, supported); Principal user = determineUser(request, wsHandler, attributes);
protected void handleWebSocketVersionNotSupported(ServerHttpRequest request, ServerHttpResponse response) { if (logger.isErrorEnabled()) { String version = request.getHeaders().getFirst("Sec-WebSocket-Version"); logger.error("Handshake failed due to unsupported WebSocket version: " + version + ". Supported versions: " + Arrays.toString(getSupportedVersions())); } response.setStatusCode(HttpStatus.UPGRADE_REQUIRED); response.getHeaders().set(WebSocketHttpHeaders.SEC_WEBSOCKET_VERSION, StringUtils.arrayToCommaDelimitedString(getSupportedVersions())); }
/** * Default constructor that auto-detects and instantiates a * {@link RequestUpgradeStrategy} suitable for the runtime container. * @throws IllegalStateException if no {@link RequestUpgradeStrategy} can be found. */ protected AbstractHandshakeHandler() { this(initRequestUpgradeStrategy()); }
handleInvalidUpgradeHeader(request, response); return false; handleInvalidConnectHeader(request, response); return false; if (!isWebSocketVersionSupported(headers)) { handleWebSocketVersionNotSupported(request, response); return false; if (!isValidOrigin(request)) { response.setStatusCode(HttpStatus.FORBIDDEN); return false; String subProtocol = selectProtocol(headers.getSecWebSocketProtocol(), wsHandler); List<WebSocketExtension> requested = headers.getSecWebSocketExtensions(); List<WebSocketExtension> supported = this.requestUpgradeStrategy.getSupportedExtensions(request); List<WebSocketExtension> extensions = filterRequestedExtensions(request, requested, supported); Principal user = determineUser(request, wsHandler, attributes);
protected boolean isWebSocketVersionSupported(WebSocketHttpHeaders httpHeaders) { String version = httpHeaders.getSecWebSocketVersion(); String[] supportedVersions = getSupportedVersions(); for (String supportedVersion : supportedVersions) { if (supportedVersion.trim().equals(version)) { return true; } } return false; }
/** * Default constructor that auto-detects and instantiates a * {@link RequestUpgradeStrategy} suitable for the runtime container. * @throws IllegalStateException if no {@link RequestUpgradeStrategy} can be found. */ protected AbstractHandshakeHandler() { this(initRequestUpgradeStrategy()); }
/** * Perform the sub-protocol negotiation based on requested and supported sub-protocols. * For the list of supported sub-protocols, this method first checks if the target * WebSocketHandler is a {@link SubProtocolCapable} and then also checks if any * sub-protocols have been explicitly configured with * {@link #setSupportedProtocols(String...)}. * @param requestedProtocols the requested sub-protocols * @param webSocketHandler the WebSocketHandler that will be used * @return the selected protocols or {@code null} * @see #determineHandlerSupportedProtocols(WebSocketHandler) */ @Nullable protected String selectProtocol(List<String> requestedProtocols, WebSocketHandler webSocketHandler) { List<String> handlerProtocols = determineHandlerSupportedProtocols(webSocketHandler); for (String protocol : requestedProtocols) { if (handlerProtocols.contains(protocol.toLowerCase())) { return protocol; } if (this.supportedProtocols.contains(protocol.toLowerCase())) { return protocol; } } return null; }
protected boolean isWebSocketVersionSupported(WebSocketHttpHeaders httpHeaders) { String version = httpHeaders.getSecWebSocketVersion(); String[] supportedVersions = getSupportedVersions(); for (String supportedVersion : supportedVersions) { if (supportedVersion.trim().equals(version)) { return true; } } return false; }
/** * Perform the sub-protocol negotiation based on requested and supported sub-protocols. * For the list of supported sub-protocols, this method first checks if the target * WebSocketHandler is a {@link SubProtocolCapable} and then also checks if any * sub-protocols have been explicitly configured with * {@link #setSupportedProtocols(String...)}. * @param requestedProtocols the requested sub-protocols * @param webSocketHandler the WebSocketHandler that will be used * @return the selected protocols or {@code null} * @see #determineHandlerSupportedProtocols(WebSocketHandler) */ @Nullable protected String selectProtocol(List<String> requestedProtocols, WebSocketHandler webSocketHandler) { List<String> handlerProtocols = determineHandlerSupportedProtocols(webSocketHandler); for (String protocol : requestedProtocols) { if (handlerProtocols.contains(protocol.toLowerCase())) { return protocol; } if (this.supportedProtocols.contains(protocol.toLowerCase())) { return protocol; } } return null; }
protected void handleWebSocketVersionNotSupported(ServerHttpRequest request, ServerHttpResponse response) { if (logger.isErrorEnabled()) { String version = request.getHeaders().getFirst("Sec-WebSocket-Version"); logger.error("Handshake failed due to unsupported WebSocket version: " + version + ". Supported versions: " + Arrays.toString(getSupportedVersions())); } response.setStatusCode(HttpStatus.UPGRADE_REQUIRED); response.getHeaders().set(WebSocketHttpHeaders.SEC_WEBSOCKET_VERSION, StringUtils.arrayToCommaDelimitedString(getSupportedVersions())); }