Ejemplo n.º 1
0
 public override int ReadByte()
 {
     try
     {
         return(base.ReadByte());
     }
     catch (HttpListenerException listenerException)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                   HttpChannelUtilities.CreateCommunicationException(listenerException));
     }
 }
Ejemplo n.º 2
0
 protected override void OnClose(TimeSpan timeout)
 {
     base.OnClose(new TimeoutHelper(timeout).RemainingTime());
     try
     {
         this.listenerContext.Response.Close();
     }
     catch (HttpListenerException exception)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(HttpChannelUtilities.CreateCommunicationException(exception));
     }
 }
Ejemplo n.º 3
0
 public override int Read(byte[] buffer, int offset, int count)
 {
     try
     {
         return(base.Read(buffer, offset, count));
     }
     catch (HttpListenerException listenerException)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                   HttpChannelUtilities.CreateCommunicationException(listenerException));
     }
 }
Ejemplo n.º 4
0
 public override int EndRead(IAsyncResult result)
 {
     try
     {
         return(base.EndRead(result));
     }
     catch (HttpListenerException listenerException)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                   HttpChannelUtilities.CreateCommunicationException(listenerException));
     }
 }
Ejemplo n.º 5
0
 public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
 {
     try
     {
         return(base.BeginRead(buffer, offset, count, callback, state));
     }
     catch (HttpListenerException listenerException)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                   HttpChannelUtilities.CreateCommunicationException(listenerException));
     }
 }
Ejemplo n.º 6
0
        internal static Exception ConvertAggregateExceptionToCommunicationException(AggregateException ex)
        {
            Exception          exception          = FxTrace.Exception.AsError <WebSocketException>(ex);
            WebSocketException webSocketException = exception as WebSocketException;

            if (webSocketException != null && webSocketException.InnerException != null)
            {
                HttpListenerException httpListenerException = webSocketException.InnerException as HttpListenerException;
                if (httpListenerException != null)
                {
                    return(HttpChannelUtilities.CreateCommunicationException(httpListenerException));
                }
            }

            ObjectDisposedException objectDisposedException = exception as ObjectDisposedException;

            if (objectDisposedException != null)
            {
                return(new CommunicationObjectAbortedException(exception.Message, exception));
            }

            return(new CommunicationException(exception.Message, exception));
        }
Ejemplo n.º 7
0
                    public override int ReadByte()
                    {
                        int num;

                        try
                        {
                            num = base.ReadByte();
                        }
                        catch (HttpListenerException exception)
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(HttpChannelUtilities.CreateCommunicationException(exception));
                        }
                        return(num);
                    }
Ejemplo n.º 8
0
        internal override void OnOpen()
        {
            listener = new HttpListener();

            string host;

            switch (HostNameComparisonMode)
            {
            case HostNameComparisonMode.Exact:
                // Uri.DnsSafeHost strips the [], but preserves the scopeid for IPV6 addresses.
                if (ListenUri.HostNameType == UriHostNameType.IPv6)
                {
                    host = string.Concat("[", ListenUri.DnsSafeHost, "]");
                }
                else
                {
                    host = ListenUri.NormalizedHost();
                }
                break;

            case HostNameComparisonMode.StrongWildcard:
                host = "+";
                break;

            case HostNameComparisonMode.WeakWildcard:
                host = "*";
                break;

            default:
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.UnrecognizedHostNameComparisonMode, HostNameComparisonMode.ToString())));
            }

            string path = ListenUri.GetComponents(UriComponents.Path, UriFormat.Unescaped);

            if (!path.StartsWith("/", StringComparison.Ordinal))
            {
                path = "/" + path;
            }

            if (!path.EndsWith("/", StringComparison.Ordinal))
            {
                path = path + "/";
            }

            string httpListenUrl = string.Concat(Scheme, "://", host, ":", ListenUri.Port, path);

            listener.UnsafeConnectionNtlmAuthentication   = this.unsafeConnectionNtlmAuthentication;
            listener.AuthenticationSchemeSelectorDelegate =
                new AuthenticationSchemeSelector(SelectAuthenticationScheme);

            if (ExtendedProtectionPolicy.OSSupportsExtendedProtection)
            {
                //This API will throw if on an unsupported platform.
                listener.ExtendedProtectionSelectorDelegate =
                    new HttpListener.ExtendedProtectionSelector(SelectExtendedProtectionPolicy);
            }

            if (this.Realm != null)
            {
                listener.Realm = this.Realm;
            }

            bool success = false;

            try
            {
                listener.Prefixes.Add(httpListenUrl);
                listener.Start();

                bool startedListening = false;
                try
                {
                    if (Thread.CurrentThread.IsThreadPoolThread)
                    {
                        StartListening();
                    }
                    else
                    {
                        // If we're not on a threadpool thread, then we need to post a callback to start our accepting loop
                        // Otherwise if the calling thread aborts then the async I/O will get inadvertantly cancelled
                        listenStartedEvent = new ManualResetEvent(false);
                        ActionItem.Schedule(OnListening, null);
                        listenStartedEvent.WaitOne();
                        listenStartedEvent.Close();
                        listenStartedEvent = null;
                        if (listenStartedException != null)
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(listenStartedException);
                        }
                    }
                    startedListening = true;
                }
                finally
                {
                    if (!startedListening)
                    {
                        listener.Stop();
                    }
                }

                success = true;
            }
            catch (HttpListenerException listenerException)
            {
                switch (listenerException.NativeErrorCode)
                {
                case UnsafeNativeMethods.ERROR_ALREADY_EXISTS:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AddressAlreadyInUseException(SR.GetString(SR.HttpRegistrationAlreadyExists, httpListenUrl), listenerException));

                case UnsafeNativeMethods.ERROR_SHARING_VIOLATION:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AddressAlreadyInUseException(SR.GetString(SR.HttpRegistrationPortInUse, httpListenUrl, ListenUri.Port), listenerException));

                case UnsafeNativeMethods.ERROR_ACCESS_DENIED:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AddressAccessDeniedException(SR.GetString(SR.HttpRegistrationAccessDenied, httpListenUrl), listenerException));

                case UnsafeNativeMethods.ERROR_ALLOTTED_SPACE_EXCEEDED:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(SR.GetString(SR.HttpRegistrationLimitExceeded, httpListenUrl), listenerException));

                case UnsafeNativeMethods.ERROR_INVALID_PARAMETER:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.HttpInvalidListenURI, ListenUri.OriginalString), listenerException));

                default:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                              HttpChannelUtilities.CreateCommunicationException(listenerException));
                }
            }
            finally
            {
                if (!success)
                {
                    listener.Abort();
                }
            }
        }
        internal override void OnOpen()
        {
            string dnsSafeHost;

            this.listener = new HttpListener();
            switch (base.HostNameComparisonMode)
            {
            case HostNameComparisonMode.StrongWildcard:
                dnsSafeHost = "+";
                break;

            case HostNameComparisonMode.Exact:
                if (base.ListenUri.HostNameType != UriHostNameType.IPv6)
                {
                    dnsSafeHost = base.ListenUri.DnsSafeHost;
                    break;
                }
                dnsSafeHost = "[" + base.ListenUri.DnsSafeHost + "]";
                break;

            case HostNameComparisonMode.WeakWildcard:
                dnsSafeHost = "*";
                break;

            default:
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("UnrecognizedHostNameComparisonMode", new object[] { base.HostNameComparisonMode.ToString() })));
            }
            string components = base.ListenUri.GetComponents(UriComponents.Path, UriFormat.Unescaped);

            if (!components.StartsWith("/", StringComparison.Ordinal))
            {
                components = "/" + components;
            }
            if (!components.EndsWith("/", StringComparison.Ordinal))
            {
                components = components + "/";
            }
            string uriPrefix = string.Concat(new object[] { this.Scheme, "://", dnsSafeHost, ":", base.ListenUri.Port, components });

            this.listener.UnsafeConnectionNtlmAuthentication   = this.unsafeConnectionNtlmAuthentication;
            this.listener.AuthenticationSchemeSelectorDelegate = new AuthenticationSchemeSelector(this.SelectAuthenticationScheme);
            if (ExtendedProtectionPolicy.OSSupportsExtendedProtection)
            {
                this.listener.ExtendedProtectionSelectorDelegate = new HttpListener.ExtendedProtectionSelector(this.SelectExtendedProtectionPolicy);
            }
            if (base.Realm != null)
            {
                this.listener.Realm = base.Realm;
            }
            bool flag = false;

            try
            {
                this.listener.Prefixes.Add(uriPrefix);
                this.listener.Start();
                bool flag2 = false;
                try
                {
                    if (Thread.CurrentThread.IsThreadPoolThread)
                    {
                        this.StartListening();
                    }
                    else
                    {
                        this.listenStartedEvent = new ManualResetEvent(false);
                        ActionItem.Schedule(new Action <object>(this.OnListening), null);
                        this.listenStartedEvent.WaitOne();
                        this.listenStartedEvent.Close();
                        this.listenStartedEvent = null;
                        if (this.listenStartedException != null)
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(this.listenStartedException);
                        }
                    }
                    flag2 = true;
                }
                finally
                {
                    if (!flag2)
                    {
                        this.listener.Stop();
                    }
                }
                flag = true;
            }
            catch (HttpListenerException exception)
            {
                switch (exception.NativeErrorCode)
                {
                case 0x57:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("HttpInvalidListenURI", new object[] { base.ListenUri.OriginalString }), exception));

                case 0xb7:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AddressAlreadyInUseException(System.ServiceModel.SR.GetString("HttpRegistrationAlreadyExists", new object[] { uriPrefix }), exception));

                case 0x540:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(System.ServiceModel.SR.GetString("HttpRegistrationLimitExceeded", new object[] { uriPrefix }), exception));

                case 5:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AddressAccessDeniedException(System.ServiceModel.SR.GetString("HttpRegistrationAccessDenied", new object[] { uriPrefix }), exception));

                case 0x20:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AddressAlreadyInUseException(System.ServiceModel.SR.GetString("HttpRegistrationPortInUse", new object[] { uriPrefix, base.ListenUri.Port }), exception));
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(HttpChannelUtilities.CreateCommunicationException(exception));
            }
            finally
            {
                if (!flag)
                {
                    this.listener.Abort();
                }
            }
        }