Пример #1
0
        private bool SendResponse(TcpClient client, ClientHeaderData CData)
        {
            // here we send him response header data
            string response;

            response = "HTTP/1.1 101 \r\n" +
                       "Upgrade: websocket\r\n" +
                       "Sec-WebSocket-Accept: " + Convert.ToBase64String(
                SHA1.Create().ComputeHash(
                    Encoding.UTF8.GetBytes(
                        CData.get("Sec-WebSocket-Key").Trim() + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
                        )
                    )
                ) + "\r\nConnection: upgrade\r\n\r\n";

            NetworkStream stream = client.GetStream();

            byte[] buffer = Encoding.UTF8.GetBytes(response);

            try
            {
                stream.Write(buffer, 0, buffer.Length);
                return(true);
            }
            catch (System.IO.IOException)
            {
                Client.Kill(client);
                return(false);
            }
        }
Пример #2
0
        private bool ClientIsValid(string request, out ClientHeaderData CData)
        {
            CData = new ClientHeaderData(1);// it is temporary

            string[] tempLines = request.Split("\n\r".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            string[] tmp;
            if (tempLines.Length < 1)
            {
                return(false);
            }
            CData = new ClientHeaderData(tempLines.Length - 1);
            for (int i = 0; i < tempLines.Length; i++)
            {
                if (i == 0)
                {
                    tmp = tempLines[i].Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    if (tmp[0] != "GET")
                    {
                        return(false);
                    }
                    if (tmp[1][0] != '/')
                    {
                        return(false);
                    }
                }
                else
                {
                    tmp = tempLines[i].Split(":".ToCharArray(), 2, StringSplitOptions.RemoveEmptyEntries);
                    if (!CData.add(tmp[0], tmp[1]))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }