Example #1
0
        public void validate(UpgradeRequest request)
        {
            // check for 101 - Switching Protocols
            int statusCode = getStatusCode();

            if (statusCode != 101)
            {
                throw new UpgradeException(statusCode, getStatusReason());
            }

            // Connection header must be "upgrade"
            String connection = getHeader("Connection");

            if (String.Compare("upgrade", connection, StringComparison.CurrentCultureIgnoreCase) != 0)
            {
                throw new UpgradeException(statusCode,
                                           "expected Connection header value of [upgrade], received [" + connection + "]");
            }

            // Upgrade header must be "websocket"
            String upgrade = getHeader("Upgrade");

            if (String.Compare("websocket", upgrade, StringComparison.CurrentCultureIgnoreCase) != 0)
            {
                throw new UpgradeException(statusCode,
                                           "expected Upgrade header value of [websocket], received [" + upgrade + "]");
            }

            // check for properly hashed request key
            String key     = request.getKey();
            String keyHash = getHeader("Sec-WebSocket-Accept");

            /*String expectedKeyHash = generateKeyHash(key);
             * if (String.Compare(expectedKeyHash, keyHash) != 0) {
             *  throw new UpgradeException(statusCode,
             *                             "invalid Sec-WebSocket-Accept value");
             * }*/
        }
Example #2
0
        public void Run()
        {
            try
            {
                socket = new Windows.Networking.Sockets.StreamSocket();
                UpgradeRequest request = null;
                try
                {
                    // connect to the eftlServer
                    if (String.Compare("ws", uri.Scheme, StringComparison.CurrentCultureIgnoreCase) == 0)
                    {
                        socket.ConnectAsync(remoteHostName, getPort().ToString(), Windows.Networking.Sockets.SocketProtectionLevel.PlainSocket).AsTask().Wait(5000);
                    }
                    else if (String.Compare("wss", uri.Scheme, StringComparison.CurrentCultureIgnoreCase) == 0)
                    {
                        socket.ConnectAsync(remoteHostName, getPort().ToString(), Windows.Networking.Sockets.SocketProtectionLevel.Ssl).AsTask().Wait(5000);
                    }

                    Windows.Networking.Sockets.SocketProtectionLevel l = socket.Information.ProtectionLevel;
                    Console.WriteLine("ProtectionLevel = " + l);

                    // send HTTP upgrade request
                    request = new UpgradeRequest(uri, protocols);
                    DataWriter writer = new DataWriter(socket.OutputStream);

                    String s = request.toString();
                    writer.WriteString(s);

                    // Call StoreAsync method to store the data to a backing stream
                    try
                    {
                        writer.StoreAsync().AsTask().Wait();
                        writer.FlushAsync().AsTask().Wait();
                    }
                    catch (Exception e) {
                        Console.WriteLine(e.StackTrace);
                    }
                    writer.DetachStream();
                }
                catch (Exception e)
                {
                    Exception exp = new Exception("failed to connect" + ((e.InnerException != null) ? e.InnerException.Message : ""));
                    notifyError(exp);
                }

                byte[]       buffer      = new byte[32768];
                IInputStream inputStream = socket.InputStream;

                try {
                    inputStream.ReadAsync(buffer.AsBuffer(), (uint)buffer.Length, InputStreamOptions.Partial).AsTask().Wait();
                    System.IO.Stream stream = new System.IO.MemoryStream(buffer);

                    // read HTTP upgrade response
                    UpgradeResponse response = UpgradeResponse.read(stream);
                    response.validate(request);

                    // get the agreed upon protocol
                    protocol = response.getProtocol();
                }
                catch (Exception e)
                {
                    notifyError(e);
                }

                // notify listener
                notifyOpen();

                // dispatch frames
                dispatch();
            }
            catch (Exception e)
            {
                notifyError(e);
            }
            finally
            {
                socket.Dispose();
            }
        }