private void ReadHttpRequest(NetworkConnection clientStream, WebSocketHandshake handshake)
        {
            if (clientStream == null)
            {
                throw new ArgumentNullException(nameof(clientStream));
            }
            if (handshake == null)
            {
                throw new ArgumentNullException(nameof(handshake));
            }

            using (var sr = new StreamReader(clientStream.AsStream(), Encoding.ASCII, false, 1024, true))
            {
                var line = sr.ReadLine();

                ParseGET(line, handshake);

                while (!string.IsNullOrWhiteSpace(line = sr.ReadLine()))
                {
                    handshake.Request.Headers.TryParseAndAdd(line);
                }

                ParseCookies(handshake);
            }
        }
        private async Task <WebSocket> NegotiateRequestAsync(WebSocketHandshake handshake, NetworkConnection connection, CancellationToken cancellation)
        {
            if (handshake == null)
            {
                throw new ArgumentNullException(nameof(handshake));
            }
            if (connection == null)
            {
                throw new ArgumentNullException(nameof(connection));
            }

            cancellation.ThrowIfCancellationRequested();

            var stream = connection.AsStream();

            if (handshake.Request.IsSecure)
            {
                var protocols    = this.options.SupportedSslProtocols;
                var host         = handshake.Request.RequestUri.DnsSafeHost;
                var secureStream = new SslStream(stream, false, this.options.CertificateValidationHandler);
                await secureStream.AuthenticateAsClientAsync(host, null, protocols, checkCertificateRevocation : false).ConfigureAwait(false);

                connection = new SslNetworkConnection(secureStream, connection);
                stream     = secureStream;
            }

            handshake.Factory = this.options.Standards.GetLast();

            await this.WriteRequestAsync(handshake, stream).ConfigureAwait(false);

            cancellation.ThrowIfCancellationRequested();

            await this.ReadResponseAsync(handshake, stream).ConfigureAwait(false);

            cancellation.ThrowIfCancellationRequested();

            if (await(this.options.HttpAuthenticationHandler?.Invoke(handshake.Request, handshake.Response) ?? Task.FromResult(false)).ConfigureAwait(false))
            {
                throw new WebSocketException("HTTP authentication failed.");
            }

            var webSocket = handshake.Factory.CreateWebSocket(connection, this.options, handshake.Request, handshake.Response, handshake.NegotiatedMessageExtensions);

            return(webSocket);
        }
        private void WriteHttpResponse(WebSocketHandshake handshake, NetworkConnection networkConnection)
        {
            if (handshake == null)
            {
                throw new ArgumentNullException(nameof(handshake));
            }
            if (networkConnection == null)
            {
                throw new ArgumentNullException(nameof(networkConnection));
            }

            handshake.IsResponseSent = true;
            using (var writer = new StreamWriter(networkConnection.AsStream(), Encoding.ASCII, 1024, true))
            {
                WriteResponseInternal(handshake, writer);
                writer.Flush();
            }
        }
Esempio n. 4
0
        public async Task <NetworkConnection> ExtendConnectionAsync(NetworkConnection networkConnection)
        {
            if (networkConnection == null)
            {
                throw new ArgumentNullException(nameof(networkConnection));
            }

            var ssl = new SslStream(networkConnection.AsStream(), false, _validation);

            try
            {
                await ssl.AuthenticateAsServerAsync(_certificate, _validation != null, _protocols, false).ConfigureAwait(false);

                return(new SslNetworkConnection(ssl, networkConnection));
            }
            catch
            {
                SafeEnd.Dispose(ssl);
                throw;
            }
        }
        private async Task WriteHttpResponseAsync(WebSocketHandshake handshake, NetworkConnection networkConnection)
        {
            if (handshake == null)
            {
                throw new ArgumentNullException(nameof(handshake));
            }
            if (networkConnection == null)
            {
                throw new ArgumentNullException(nameof(networkConnection));
            }

            if (!handshake.IsWebSocketRequest && handshake.IsValidHttpRequest && this.options.HttpFallback != null)
            {
                return;
            }

            handshake.IsResponseSent = true;
            using (var writer = new StreamWriter(networkConnection.AsStream(), Encoding.ASCII, 1024, true))
            {
                WriteResponseInternal(handshake, writer);
                await writer.FlushAsync().ConfigureAwait(false);
            }
        }