Search in sources :

Example 1 with ExtendedHandshakeChecker

use of org.xnio.http.ExtendedHandshakeChecker in project undertow by undertow-io.

the class WebSocket13ClientHandshake method handshakeChecker.

@Override
public ExtendedHandshakeChecker handshakeChecker(final URI uri, final Map<String, List<String>> requestHeaders) {
    final String sentKey = requestHeaders.containsKey(Headers.SEC_WEB_SOCKET_KEY_STRING) ? requestHeaders.get(Headers.SEC_WEB_SOCKET_KEY_STRING).get(0) : null;
    return new ExtendedHandshakeChecker() {

        @Override
        public void checkHandshakeExtended(Map<String, List<String>> headers) throws IOException {
            try {
                if (negotiation != null) {
                    negotiation.afterRequest(headers);
                }
                String upgrade = getFirst(Headers.UPGRADE_STRING, headers);
                if (upgrade == null || !upgrade.trim().equalsIgnoreCase("websocket")) {
                    throw WebSocketMessages.MESSAGES.noWebSocketUpgradeHeader();
                }
                String connHeader = getFirst(Headers.CONNECTION_STRING, headers);
                if (connHeader == null || !connHeader.trim().equalsIgnoreCase("upgrade")) {
                    throw WebSocketMessages.MESSAGES.noWebSocketConnectionHeader();
                }
                String acceptKey = getFirst(Headers.SEC_WEB_SOCKET_ACCEPT_STRING, headers);
                final String dKey = solve(sentKey);
                if (!dKey.equals(acceptKey)) {
                    throw WebSocketMessages.MESSAGES.webSocketAcceptKeyMismatch(dKey, acceptKey);
                }
                if (negotiation != null) {
                    String subProto = getFirst(Headers.SEC_WEB_SOCKET_PROTOCOL_STRING, headers);
                    if (subProto != null && !subProto.isEmpty() && !negotiation.getSupportedSubProtocols().contains(subProto)) {
                        throw WebSocketMessages.MESSAGES.unsupportedProtocol(subProto, negotiation.getSupportedSubProtocols());
                    }
                    List<WebSocketExtension> extensions = Collections.emptyList();
                    String extHeader = getFirst(Headers.SEC_WEB_SOCKET_EXTENSIONS_STRING, headers);
                    if (extHeader != null) {
                        extensions = WebSocketExtension.parse(extHeader);
                    }
                    negotiation.handshakeComplete(subProto, extensions);
                }
            } catch (IOException e) {
                throw e;
            } catch (Exception e) {
                throw new IOException(e);
            }
        }
    };
}
Also used : WebSocketExtension(io.undertow.websockets.WebSocketExtension) ExtendedHandshakeChecker(org.xnio.http.ExtendedHandshakeChecker) IOException(java.io.IOException) HashMap(java.util.HashMap) OptionMap(org.xnio.OptionMap) Map(java.util.Map) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

WebSocketExtension (io.undertow.websockets.WebSocketExtension)1 IOException (java.io.IOException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 OptionMap (org.xnio.OptionMap)1 ExtendedHandshakeChecker (org.xnio.http.ExtendedHandshakeChecker)1