Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2014 Red Hat, Inc., and individual contributors
   * as indicated by the @author tags.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 
 package io.undertow.websockets.client;
 
 import org.xnio.Pool;
 
 import java.net.URI;
 import java.util.List;
 import java.util.Map;

Author(s):
Stuart Douglas
 
 
     public static final String MAGIC_NUMBER = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
 
     private final WebSocketClientNegotiation negotiation;
 
     public WebSocket13ClientHandshake(final URI urlWebSocketClientNegotiation negotiation) {
         super(url);
         this. = negotiation;
     }
 
     public WebSocket13ClientHandshake(final URI url) {
         this(urlnull);
     }
 
     @Override
     public WebSocketChannel createChannel(final StreamConnection channelfinal String wsUrifinal Pool<ByteBufferbufferPool) {
         return new WebSocket13Channel(channelbufferPoolwsUri != null ? .getSelectedSubProtocol() : ""truefalsenew HashSet<WebSocketChannel>());
     }
 
 
     public Map<StringStringcreateHeaders() {
         Map<StringStringheaders = new HashMap<>();
         headers.put(."websocket");
         headers.put(."upgrade");
         String key = createSecKey();
         headers.put(.key);
         if ( != null) {
             List<StringsubProtocols = .getSupportedSubProtocols();
             if (subProtocols != null && !subProtocols.isEmpty()) {
                 StringBuilder sb = new StringBuilder();
                 Iterator<Stringit = subProtocols.iterator();
                 while (it.hasNext()) {
                     sb.append(it.next());
                     if (it.hasNext()) {
                         sb.append(", ");
                     }
                 }
                 headers.put(.sb.toString());
             }
             List<WebSocketExtensionextensions = .getSupportedExtensions();
             if (extensions != null && !extensions.isEmpty()) {
                 StringBuilder sb = new StringBuilder();
                 Iterator<WebSocketExtensionit = extensions.iterator();
                 while (it.hasNext()) {
                     WebSocketExtension next = it.next();
                     sb.append(next.getName());
                     for (WebSocketExtension.Parameter param : next.getParameters()) {
                         sb.append("; ");
                        sb.append(param.getName());
                        sb.append("=");
                        sb.append(param.getValue());
                    }
                    if (it.hasNext()) {
                        sb.append(", ");
                    }
                }
                headers.put(.sb.toString());
            }
        }
        return headers;
    }
    protected String createSecKey() {
        SecureRandom random = new SecureRandom();
        byte[] data = new byte[16];
        for (int i = 0; i < 4; ++i) {
            int val = random.nextInt();
            data[i * 4] = (byteval;
            data[i * 4 + 1] = (byte) ((val >> 8) & 0xFF);
            data[i * 4 + 2] = (byte) ((val >> 16) & 0xFF);
            data[i * 4 + 3] = (byte) ((val >> 24) & 0xFF);
        }
        return FlexBase64.encodeString(datafalse);
    }
    @Override
    public ExtendedHandshakeChecker handshakeChecker(final URI urifinal Map<StringList<String>> requestHeaders) {
        final String sentKey = requestHeaders.containsKey(.) ? requestHeaders.get(.).get(0) : null;
        return new ExtendedHandshakeChecker() {
            @Override
            public void checkHandshakeExtended(Map<StringList<String>> headersthrows IOException {
                try {
                    if ( != null) {
                        .afterRequest(headers);
                    }
                    String upgrade = getFirst(.headers);
                    if (upgrade == null || !upgrade.trim().equalsIgnoreCase("websocket")) {
                        throw ..noWebSocketUpgradeHeader();
                    }
                    String connHeader = getFirst(.headers);
                    if (connHeader == null || !connHeader.trim().equalsIgnoreCase("upgrade")) {
                        throw ..noWebSocketConnectionHeader();
                    }
                    String acceptKey = getFirst(.headers);
                    final String dKey = solve(sentKey);
                    if (!dKey.equals(acceptKey)) {
                        throw ..webSocketAcceptKeyMismatch(dKeyacceptKey);
                    }
                    if ( != null) {
                        String subProto = getFirst(.headers);
                        if (subProto != null && !subProto.isEmpty() && !.getSupportedSubProtocols().contains(subProto)) {
                            throw ..unsupportedProtocol(subProto.getSupportedSubProtocols());
                        }
                        List<WebSocketExtensionextensions = Collections.emptyList();
                        String extHeader = getFirst(.headers);
                        if (extHeader != null) {
                            extensions = WebSocketExtension.parse(extHeader);
                        }
                        .handshakeComplete(subProtoextensions);
                    }
                } catch (IOException e) {
                    throw e;
                } catch (Exception e) {
                    throw new IOException(e);
                }
            }
        };
    }
    private String getFirst(String keyMap<StringList<String>> map) {
        List<Stringlist = map.get(key.toLowerCase(.));
        if(list == null || list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }
    protected final String solve(final String nonceBase64) {
        try {
            final String concat = nonceBase64 + ;
            final MessageDigest digest = MessageDigest.getInstance("SHA1");
            digest.update(concat.getBytes(.));
            final byte[] bytes = digest.digest();
            return FlexBase64.encodeString(bytesfalse);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
    public WebSocketVersion getVersion() {
        return .;
    }
New to GrepCode? Check out our FAQ X