Пример #1
0
        private async Task <IWSConnectionParameters> ClientHandshake(Stream stream, Uri uri)
        {
            var clientHeaders = CreateClientHeaders();
            var serverHeaders = CreateServerHeaders();

            var streamWriter = new StreamWriter(stream);
            var streamReader = new StreamReader(stream);

            var key = WSKey.FromID(ID);

            clientHeaders["Host"]                  = uri.Host;
            clientHeaders["Upgrade"]               = "WebSocket";
            clientHeaders["Connection"]            = "Upgrade";
            clientHeaders["Sec-WebSocket-Key"]     = key.Text;
            clientHeaders["Sec-WebSocket-Version"] = "13";

            var clientDirective = new HTTPClientDirective {
                Method       = "GET",
                Version      = new Version(1, 1),
                PathAndQuery = uri.PathAndQuery,
            };

            await SendResponse(clientHeaders, streamWriter, clientDirective);

            var serverDirective = HTTPServerDirective.Parse(streamReader.ReadLine());

            if (serverDirective.StatusCode != 101)
            {
                throw new NotSupportedException("Server Handshake Response");
            }

            await ReadHeaders(streamReader, serverHeaders);

            return(new WSHTTPConnectionParameters(stream, clientHeaders, serverHeaders, WSHTTPConnectionType.Client));
        }
Пример #2
0
        public static bool TryParse(string text, out HTTPServerDirective value)
        {
            try {
                value = Parse(text);

                return(true);
            }
            catch (FormatException) {
                value = default;

                return(false);
            }
        }
Пример #3
0
        private async Task <IWSConnectionParameters> ServerHandshake(Stream stream)
        {
            var clientHeaders = CreateClientHeaders();
            var serverHeaders = CreateServerHeaders();

            var streamWriter = new StreamWriter(stream);
            var streamReader = new StreamReader(stream);

            var clientDirective = HTTPClientDirective.Parse(streamReader.ReadLine());

            if (clientDirective.Method != "GET" || clientDirective.PathAndQuery != "/ws")
            {
                throw new NotSupportedException("Client Handshake Request");
            }

            await ReadHeaders(streamReader, clientHeaders);

            var key = WSKey.FromString(clientHeaders["Sec-WebSocket-Key"]);

            ID = key.ID;

            UpdateHeaders(clientHeaders, serverHeaders);

            serverHeaders["Upgrade"]              = "WebSocket";
            serverHeaders["Connection"]           = "Upgrade";
            serverHeaders["Sec-WebSocket-Accept"] = key.ComputeAccept();

            var serverDirective = new HTTPServerDirective {
                Version    = new Version(1, 1),
                StatusText = "Switch Protocol WebSocket",
                StatusCode = 101,
            };

            await SendResponse(serverHeaders, streamWriter, serverDirective);

            return(new WSHTTPConnectionParameters(stream, clientHeaders, serverHeaders, WSHTTPConnectionType.Server));
        }