Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  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() : ""truefalse);
     }
 
 
     public Map<StringStringcreateHeaders() {
         Map<StringStringheaders = new HashMap<StringString>();
         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);
                     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 HandshakeChecker handshakeChecker(final URI urifinal Map<StringStringrequestHeaders) {
        final String sentKey = requestHeaders.get(.);
        return new HandshakeChecker() {
            @Override
            public void checkHandshake(Map<StringStringheadersthrows IOException {
                if( != null) {
                    .afterRequest(headers);
                }
                String upgrade = headers.get(..toLowerCase(.));
                if (upgrade == null || !upgrade.trim().equalsIgnoreCase("websocket")) {
                    throw ..noWebSocketUpgradeHeader();
                }
                String connHeader = headers.get(..toLowerCase(.));
                if (connHeader == null || !connHeader.trim().equalsIgnoreCase("upgrade")) {
                    throw ..noWebSocketConnectionHeader();
                }
                String acceptKey = headers.get(..toLowerCase(.));
                final String dKey = solve(sentKey);
                if (!dKey.equals(acceptKey)) {
                    throw ..webSocketAcceptKeyMismatch(dKeyacceptKey);
                }
                if ( != null) {
                    String subProto = headers.get(..toLowerCase(.));
                    if (subProto != null && !subProto.isEmpty() && !.getSupportedSubProtocols().contains(subProto)) {
                        throw ..unsupportedProtocol(subProto.getSupportedSubProtocols());
                    }
                    List<WebSocketExtensionextensions = Collections.emptyList();
                    String extHeader = headers.get(..toLowerCase(.));
                    if (extHeader != null) {
                        extensions = WebSocketExtension.parse(extHeader);
                    }
                    .handshakeComplete(subProtoextensions);
                }
            }
        };
    }
    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