예제 #1
0
            async Task HandleListenerContextAsync(HttpListenerContext context)
            {
                WebSocket webSocket = null;

                try
                {
                    var wsContext = await context.AcceptWebSocketAsync(WebSocketTransport.WebSocketSubProtocol);

                    var wsTransport = new ListenerWebSocketTransport(wsContext);
                    await this.listener.HandleTransportAsync(wsTransport);
                }
                catch (Exception exception)
                {
                    Trace.WriteLine(TraceLevel.Error, exception.ToString());
                    if (webSocket != null)
                    {
                        webSocket.Abort();
                    }
                }
            }
예제 #2
0
            async Task <int> CreateTransportAsync(HttpListenerContext context)
            {
                X509Certificate2 clientCertificate = null;

                if (this.listener.sslSettings != null && this.listener.sslSettings.ClientCertificateRequired)
                {
                    clientCertificate = await context.Request.GetClientCertificateAsync();;
                    if (clientCertificate == null)
                    {
                        return(40300);
                    }

                    if (this.listener.sslSettings.RemoteCertificateValidationCallback != null)
                    {
                        SslPolicyErrors sslError = SslPolicyErrors.None;
                        X509Chain       chain    = new X509Chain();
                        chain.ChainPolicy.RevocationMode = this.listener.sslSettings.CheckCertificateRevocation ?
                                                           X509RevocationMode.Online : X509RevocationMode.NoCheck;
                        chain.Build(clientCertificate);
                        if (chain.ChainStatus.Length > 0)
                        {
                            sslError = SslPolicyErrors.RemoteCertificateChainErrors;
                        }

                        bool success = this.listener.sslSettings.RemoteCertificateValidationCallback(
                            this, clientCertificate, chain, sslError);
                        if (!success)
                        {
                            return(40301);
                        }
                    }
                    else if (context.Request.ClientCertificateError != 0)
                    {
                        return(40302);
                    }
                }

                IPrincipal principal = context.User;

                if (principal == null && clientCertificate != null)
                {
                    principal = new GenericPrincipal(new X509Identity(clientCertificate), new string[0]);
                }

                string subProtocol = null;

                string[] subProtocols = context.Request.Headers.GetValues("Sec-WebSocket-Protocol");
                for (int i = 0; i < subProtocols.Length; i++)
                {
                    if (subProtocols[i].Equals(WebSocketTransport.WebSocketSubProtocol) ||
                        subProtocols[i].Equals("AMQPWSB10")     // defined by the previous draft
                        )
                    {
                        subProtocol = subProtocols[i];
                        break;
                    }
                }

                if (subProtocol == null)
                {
                    return(40003);
                }

                var wsContext = await context.AcceptWebSocketAsync(subProtocol);

                var wsTransport = new ListenerWebSocketTransport(wsContext.WebSocket, principal);

                await this.listener.HandleTransportAsync(wsTransport);

                return(0);
            }
예제 #3
0
 async Task HandleListenerContextAsync(HttpListenerContext context)
 {
     WebSocket webSocket = null;
     try
     {
         var wsContext = await context.AcceptWebSocketAsync(WebSocketTransport.WebSocketSubProtocol);
         var wsTransport = new ListenerWebSocketTransport(wsContext);
         await this.listener.HandleTransportAsync(wsTransport);
     }
     catch(Exception exception)
     {
         Trace.WriteLine(TraceLevel.Error, exception.ToString());
         if (webSocket != null)
         {
             webSocket.Abort();
         }
     }
 }
예제 #4
0
            async Task<int> CreateTransportAsync(HttpListenerContext context)
            {
                X509Certificate2 clientCertificate = null;

                if (this.listener.sslSettings != null && this.listener.sslSettings.ClientCertificateRequired)
                {
                    clientCertificate = await context.Request.GetClientCertificateAsync(); ;
                    if (clientCertificate == null)
                    {
                        return 403;
                    }

                    if (this.listener.sslSettings.RemoteCertificateValidationCallback != null)
                    {
                        SslPolicyErrors sslError = SslPolicyErrors.None;
                        X509Chain chain = new X509Chain();
                        chain.ChainPolicy.RevocationMode = this.listener.sslSettings.CheckCertificateRevocation ?
                            X509RevocationMode.Online : X509RevocationMode.NoCheck;
                        chain.Build(clientCertificate);
                        if (chain.ChainStatus.Length > 0)
                        {
                            sslError = SslPolicyErrors.RemoteCertificateChainErrors;
                        }

                        bool success = this.listener.sslSettings.RemoteCertificateValidationCallback(
                            this, clientCertificate, chain, sslError);
                        if (!success)
                        {
                            return 403;
                        }
                    }
                    else if (context.Request.ClientCertificateError != 0)
                    {
                        return 403;
                    }
                }

                IPrincipal principal = context.User;
                if (principal == null && clientCertificate != null)
                {
                    principal = new GenericPrincipal(new X509Identity(clientCertificate), new string[0]);
                }

                var wsContext = await context.AcceptWebSocketAsync(WebSocketTransport.WebSocketSubProtocol);
                var wsTransport = new ListenerWebSocketTransport(wsContext.WebSocket, principal);
                await this.listener.HandleTransportAsync(wsTransport);

                return 0;
            }