public static Exception ConvertWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
        {
            switch (webException.Status)
            {
                case WebExceptionStatus.NameResolutionFailure:
                case WebExceptionStatus.ConnectFailure:
                case WebExceptionStatus.ProxyNameResolutionFailure:
                    return new EndpointNotFoundException(System.ServiceModel.SR.GetString("EndpointNotFound", new object[] { request.RequestUri.AbsoluteUri }), webException);

                case WebExceptionStatus.ReceiveFailure:
                    return new CommunicationException(System.ServiceModel.SR.GetString("HttpReceiveFailure", new object[] { request.RequestUri }), webException);

                case WebExceptionStatus.SendFailure:
                    return new CommunicationException(System.ServiceModel.SR.GetString("HttpSendFailure", new object[] { request.RequestUri }), webException);

                case WebExceptionStatus.RequestCanceled:
                    return CreateRequestCanceledException(webException, request, abortReason);

                case WebExceptionStatus.TrustFailure:
                    return new SecurityNegotiationException(System.ServiceModel.SR.GetString("TrustFailure", new object[] { request.RequestUri.Authority }), webException);

                case WebExceptionStatus.SecureChannelFailure:
                    return new SecurityNegotiationException(System.ServiceModel.SR.GetString("SecureChannelFailure", new object[] { request.RequestUri.Authority }), webException);

                case WebExceptionStatus.Timeout:
                    return new TimeoutException(CreateRequestTimedOutMessage(request), webException);
            }
            return null;
        }
 public virtual void Abort(HttpAbortReason reason)
 {
     if (!this.isDisposed)
     {
         this.abortReason = reason;
         if (DiagnosticUtility.ShouldTraceWarning)
         {
             TraceUtility.TraceEvent(TraceEventType.Warning, this.isRequest ? 0x4000d : 0x4000e, this.isRequest ? System.ServiceModel.SR.GetString("TraceCodeHttpChannelRequestAborted") : System.ServiceModel.SR.GetString("TraceCodeHttpChannelResponseAborted"), this.message);
         }
         this.CleanupBuffer();
     }
 }
Esempio n. 3
0
 public static HttpResponseMessage ProcessGetResponseWebException(HttpRequestException requestException, HttpRequestMessage request, HttpAbortReason abortReason)
 {
     var inner = requestException.InnerException;
     if (inner != null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ConvertHttpRequestException(requestException, request, abortReason));
     }
     else
     {
         // There is no inner exception so there's not enough information to be able to convert to the correct WCF exception.
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(requestException.Message, requestException));
     }
 }
Esempio n. 4
0
        public static Exception ConvertHttpRequestException(HttpRequestException exception, HttpRequestMessage request, HttpAbortReason abortReason)
        {
            Contract.Assert(exception.InnerException != null, "InnerException must be set to be able to convert");

            uint hresult = (uint)exception.InnerException.HResult;
            switch (hresult)
            {
                // .Net Native HttpClientHandler sometimes reports an incorrect handle state when a connection is aborted, so we treat it as a connection reset error
                case WININET_E_INCORRECT_HANDLE_STATE:
                    goto case WININET_E_CONNECTION_RESET;
                case WININET_E_CONNECTION_RESET:
                    return new CommunicationException(SR.Format(SR.HttpReceiveFailure, request.RequestUri), exception);
                case WININET_E_NAME_NOT_RESOLVED:
                    return new EndpointNotFoundException(SR.Format(SR.EndpointNotFound, request.RequestUri.AbsoluteUri), exception);
                default:
                    return new CommunicationException(exception.Message, exception);
            }
        }
Esempio n. 5
0
        public virtual void Abort(HttpAbortReason reason)
        {
            if (isDisposed)
            {
                return;
            }

            this.abortReason = reason;

            TraceRequestResponseAborted(reason);

            CleanupBuffer();
        }
Esempio n. 6
0
        public static Exception ConvertHttpRequestException(HttpRequestException exception, HttpRequestMessage request, HttpAbortReason abortReason)
        {
            Contract.Assert(exception.InnerException != null, "InnerException must be set to be able to convert");

            uint hresult = (uint)exception.InnerException.HResult;

            switch (hresult)
            {
            // .Net Native HttpClientHandler sometimes reports an incorrect handle state when a connection is aborted, so we treat it as a connection reset error
            case WININET_E_INCORRECT_HANDLE_STATE:
                goto case WININET_E_CONNECTION_RESET;

            case WININET_E_CONNECTION_RESET:
                return(new CommunicationException(string.Format(SRServiceModel.HttpReceiveFailure, request.RequestUri), exception));

            case WININET_E_NAME_NOT_RESOLVED:
                return(new EndpointNotFoundException(string.Format(SRServiceModel.EndpointNotFound, request.RequestUri.AbsoluteUri), exception));

            case ERROR_WINHTTP_SECURE_FAILURE:
                return(new SecurityNegotiationException(string.Format(SRServiceModel.TrustFailure, request.RequestUri.Authority), exception));

            default:
                return(new CommunicationException(exception.Message, exception));
            }
        }
Esempio n. 7
0
        public static HttpResponseMessage ProcessGetResponseWebException(HttpRequestException requestException, HttpRequestMessage request, HttpAbortReason abortReason)
        {
            var inner = requestException.InnerException;

            if (inner != null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ConvertHttpRequestException(requestException, request, abortReason));
            }
            else
            {
                // There is no inner exception so there's not enough information to be able to convert to the correct WCF exception.
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(requestException.Message, requestException));
            }
        }
Esempio n. 8
0
        public static Exception ConvertHttpRequestException(HttpRequestException exception, HttpRequestMessage request, HttpAbortReason abortReason)
        {
            Contract.Assert(exception.InnerException != null, "InnerException must be set to be able to convert");

            uint hresult = (uint)exception.InnerException.HResult;
            var  innerSocketException = exception.InnerException as SocketException;

            if (innerSocketException != null)
            {
                var socketErrorCode = innerSocketException.SocketErrorCode;
                switch (socketErrorCode)
                {
                case SocketError.HostNotFound:
                    return(new EndpointNotFoundException(SR.Format(SR.EndpointNotFound, request.RequestUri.AbsoluteUri), exception));

                default:
                    break;
                }
            }

            if (exception.InnerException is AuthenticationException)
            {
                return(new SecurityNegotiationException(SR.Format(SR.TrustFailure, request.RequestUri.Authority), exception));
            }

            switch (hresult)
            {
            // .Net Native HttpClientHandler sometimes reports an incorrect handle state when a connection is aborted, so we treat it as a connection reset error
            case WININET_E_INCORRECT_HANDLE_STATE:
                goto case WININET_E_CONNECTION_RESET;

            case WININET_E_CONNECTION_RESET:
                return(new CommunicationException(SR.Format(SR.HttpReceiveFailure, request.RequestUri), exception));

            // Linux HttpClient returns ERROR_INVALID_HANDLE in the endpoint-not-found case, so map to EndpointNotFoundException
            case UnsafeNativeMethods.ERROR_INVALID_HANDLE:
            case WININET_E_NAME_NOT_RESOLVED:
                return(new EndpointNotFoundException(SR.Format(SR.EndpointNotFound, request.RequestUri.AbsoluteUri), exception));

            case CURLE_SSL_CACERT:
            case CURLE_SSL_CERTPROBLEM:
            case ERROR_WINHTTP_SECURE_FAILURE:
                return(new SecurityNegotiationException(SR.Format(SR.TrustFailure, request.RequestUri.Authority), exception));

            default:
                return(new CommunicationException(exception.Message, exception));
            }
        }
Esempio n. 9
0
 public static Exception CreateHttpRequestException(HttpRequestException requestException, HttpRequestMessage request, HttpAbortReason abortReason)
 {
     var baseException = requestException.GetBaseException();
     return new CommunicationException(baseException.Message, baseException);
 }
Esempio n. 10
0
 public static Exception ConvertWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
 {
     switch (webException.Status)
     {
         case WebExceptionStatus.ConnectFailure:
         case WebExceptionStatus.NameResolutionFailure:
         case WebExceptionStatus.ProxyNameResolutionFailure:
             return new EndpointNotFoundException(SR.GetString(SR.EndpointNotFound, request.RequestUri.AbsoluteUri), webException);
         case WebExceptionStatus.SecureChannelFailure:
             return new SecurityNegotiationException(SR.GetString(SR.SecureChannelFailure, request.RequestUri.Authority), webException);
         case WebExceptionStatus.TrustFailure:
             return new SecurityNegotiationException(SR.GetString(SR.TrustFailure, request.RequestUri.Authority), webException);
         case WebExceptionStatus.Timeout:
             return new TimeoutException(CreateRequestTimedOutMessage(request), webException);
         case WebExceptionStatus.ReceiveFailure:
             return new CommunicationException(SR.GetString(SR.HttpReceiveFailure, request.RequestUri), webException);
         case WebExceptionStatus.SendFailure:
             return new CommunicationException(SR.GetString(SR.HttpSendFailure, request.RequestUri), webException);
         case WebExceptionStatus.RequestCanceled:
             return CreateRequestCanceledException(webException, request, abortReason);
         case WebExceptionStatus.ProtocolError:
             HttpWebResponse response = (HttpWebResponse)webException.Response;
             Fx.Assert(response != null, "'response' MUST NOT be NULL for WebExceptionStatus=='ProtocolError'.");
             if (response.StatusCode == HttpStatusCode.InternalServerError &&
                 string.Compare(response.StatusDescription, HttpChannelUtilities.StatusDescriptionStrings.HttpStatusServiceActivationException, StringComparison.OrdinalIgnoreCase) == 0)
             {
                 return new ServiceActivationException(SR.GetString(SR.Hosting_ServiceActivationFailed, request.RequestUri));
             }
             else
             {
                 return null;
             }
         default:
             return null;
     }
 }
Esempio n. 11
0
 public override void Abort(HttpAbortReason abortReason)
 {
     listenerResponse.Abort();
     base.Abort(abortReason);
 }
 public static HttpWebResponse ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
 {
     HttpWebResponse response = null;
     if ((webException.Status == WebExceptionStatus.Success) || (webException.Status == WebExceptionStatus.ProtocolError))
     {
         response = (HttpWebResponse) webException.Response;
     }
     if (response == null)
     {
         Exception exception = ConvertWebException(webException, request, abortReason);
         if (exception != null)
         {
             throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception);
         }
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(webException.Message, webException));
     }
     bool flag = false;
     try
     {
         if (response.StatusCode == HttpStatusCode.NotFound)
         {
             throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new EndpointNotFoundException(System.ServiceModel.SR.GetString("EndpointNotFound", new object[] { request.RequestUri.AbsoluteUri }), webException));
         }
         if (response.StatusCode == HttpStatusCode.ServiceUnavailable)
         {
             throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ServerTooBusyException(System.ServiceModel.SR.GetString("HttpServerTooBusy", new object[] { request.RequestUri.AbsoluteUri }), webException));
         }
         if (response.StatusCode == HttpStatusCode.UnsupportedMediaType)
         {
             string statusDescription = response.StatusDescription;
             if (!string.IsNullOrEmpty(statusDescription) && (string.Compare(statusDescription, "Missing Content Type", StringComparison.OrdinalIgnoreCase) == 0))
             {
                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("MissingContentType", new object[] { request.RequestUri }), webException));
             }
             throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("FramingContentTypeMismatch", new object[] { request.ContentType, request.RequestUri }), webException));
         }
         if (response.StatusCode == HttpStatusCode.GatewayTimeout)
         {
             throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(webException.Message, webException));
         }
         if (response.StatusCode == HttpStatusCode.BadRequest)
         {
             string str2 = null;
             if (response.ContentLength == "<h1>Bad Request (Invalid Hostname)</h1>".Length)
             {
                 str2 = "<h1>Bad Request (Invalid Hostname)</h1>";
             }
             else if (response.ContentLength == "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\"http://www.w3.org/TR/html4/strict.dtd\">\r\n<HTML><HEAD><TITLE>Bad Request</TITLE>\r\n<META HTTP-EQUIV=\"Content-Type\" Content=\"text/html; charset=us-ascii\"></HEAD>\r\n<BODY><h2>Bad Request - Invalid Hostname</h2>\r\n<hr><p>HTTP Error 400. The request hostname is invalid.</p>\r\n</BODY></HTML>\r\n".Length)
             {
                 str2 = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\"http://www.w3.org/TR/html4/strict.dtd\">\r\n<HTML><HEAD><TITLE>Bad Request</TITLE>\r\n<META HTTP-EQUIV=\"Content-Type\" Content=\"text/html; charset=us-ascii\"></HEAD>\r\n<BODY><h2>Bad Request - Invalid Hostname</h2>\r\n<hr><p>HTTP Error 400. The request hostname is invalid.</p>\r\n</BODY></HTML>\r\n";
             }
             if (str2 != null)
             {
                 Stream responseStream = response.GetResponseStream();
                 byte[] buffer = new byte[str2.Length];
                 if ((responseStream.Read(buffer, 0, buffer.Length) == str2.Length) && (str2 == Encoding.ASCII.GetString(buffer)))
                 {
                     throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new EndpointNotFoundException(System.ServiceModel.SR.GetString("EndpointNotFound", new object[] { request.RequestUri.AbsoluteUri }), webException));
                 }
             }
         }
         flag = true;
     }
     finally
     {
         if (!flag)
         {
             response.Close();
         }
     }
     return response;
 }
 public static Exception CreateRequestWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
 {
     Exception exception = ConvertWebException(webException, request, abortReason);
     if (webException.Response != null)
     {
         webException.Response.Close();
     }
     if (exception != null)
     {
         return exception;
     }
     if (webException.InnerException is IOException)
     {
         return CreateRequestIOException((IOException) webException.InnerException, request, webException);
     }
     if (webException.InnerException is SocketException)
     {
         return SocketConnectionInitiator.ConvertConnectException((SocketException) webException.InnerException, request.RequestUri, TimeSpan.MaxValue, webException);
     }
     return new EndpointNotFoundException(System.ServiceModel.SR.GetString("EndpointNotFound", new object[] { request.RequestUri.AbsoluteUri }), webException);
 }
        public static Exception CreateRequestCanceledException(Exception webException, HttpWebRequest request, HttpAbortReason abortReason)
        {
            switch (abortReason)
            {
            case HttpAbortReason.Aborted:
                return(new CommunicationObjectAbortedException(System.ServiceModel.SR.GetString("HttpRequestAborted", new object[] { request.RequestUri }), webException));

            case HttpAbortReason.TimedOut:
                return(new TimeoutException(CreateRequestTimedOutMessage(request), webException));
            }
            return(new CommunicationException(System.ServiceModel.SR.GetString("HttpTransferError", new object[] { webException.Message }), webException));
        }
        public static Exception ConvertWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
        {
            switch (webException.Status)
            {
            case WebExceptionStatus.NameResolutionFailure:
            case WebExceptionStatus.ConnectFailure:
            case WebExceptionStatus.ProxyNameResolutionFailure:
                return(new EndpointNotFoundException(System.ServiceModel.SR.GetString("EndpointNotFound", new object[] { request.RequestUri.AbsoluteUri }), webException));

            case WebExceptionStatus.ReceiveFailure:
                return(new CommunicationException(System.ServiceModel.SR.GetString("HttpReceiveFailure", new object[] { request.RequestUri }), webException));

            case WebExceptionStatus.SendFailure:
                return(new CommunicationException(System.ServiceModel.SR.GetString("HttpSendFailure", new object[] { request.RequestUri }), webException));

            case WebExceptionStatus.RequestCanceled:
                return(CreateRequestCanceledException(webException, request, abortReason));

            case WebExceptionStatus.TrustFailure:
                return(new SecurityNegotiationException(System.ServiceModel.SR.GetString("TrustFailure", new object[] { request.RequestUri.Authority }), webException));

            case WebExceptionStatus.SecureChannelFailure:
                return(new SecurityNegotiationException(System.ServiceModel.SR.GetString("SecureChannelFailure", new object[] { request.RequestUri.Authority }), webException));

            case WebExceptionStatus.Timeout:
                return(new TimeoutException(CreateRequestTimedOutMessage(request), webException));
            }
            return(null);
        }
        public static HttpWebResponse ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
        {
            HttpWebResponse response = null;

            if ((webException.Status == WebExceptionStatus.Success) || (webException.Status == WebExceptionStatus.ProtocolError))
            {
                response = (HttpWebResponse)webException.Response;
            }
            if (response == null)
            {
                Exception exception = ConvertWebException(webException, request, abortReason);
                if (exception != null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception);
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(webException.Message, webException));
            }
            bool flag = false;

            try
            {
                if (response.StatusCode == HttpStatusCode.NotFound)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new EndpointNotFoundException(System.ServiceModel.SR.GetString("EndpointNotFound", new object[] { request.RequestUri.AbsoluteUri }), webException));
                }
                if (response.StatusCode == HttpStatusCode.ServiceUnavailable)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ServerTooBusyException(System.ServiceModel.SR.GetString("HttpServerTooBusy", new object[] { request.RequestUri.AbsoluteUri }), webException));
                }
                if (response.StatusCode == HttpStatusCode.UnsupportedMediaType)
                {
                    string statusDescription = response.StatusDescription;
                    if (!string.IsNullOrEmpty(statusDescription) && (string.Compare(statusDescription, "Missing Content Type", StringComparison.OrdinalIgnoreCase) == 0))
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("MissingContentType", new object[] { request.RequestUri }), webException));
                    }
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("FramingContentTypeMismatch", new object[] { request.ContentType, request.RequestUri }), webException));
                }
                if (response.StatusCode == HttpStatusCode.GatewayTimeout)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(webException.Message, webException));
                }
                if (response.StatusCode == HttpStatusCode.BadRequest)
                {
                    string str2 = null;
                    if (response.ContentLength == "<h1>Bad Request (Invalid Hostname)</h1>".Length)
                    {
                        str2 = "<h1>Bad Request (Invalid Hostname)</h1>";
                    }
                    else if (response.ContentLength == "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\"http://www.w3.org/TR/html4/strict.dtd\">\r\n<HTML><HEAD><TITLE>Bad Request</TITLE>\r\n<META HTTP-EQUIV=\"Content-Type\" Content=\"text/html; charset=us-ascii\"></HEAD>\r\n<BODY><h2>Bad Request - Invalid Hostname</h2>\r\n<hr><p>HTTP Error 400. The request hostname is invalid.</p>\r\n</BODY></HTML>\r\n".Length)
                    {
                        str2 = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\"http://www.w3.org/TR/html4/strict.dtd\">\r\n<HTML><HEAD><TITLE>Bad Request</TITLE>\r\n<META HTTP-EQUIV=\"Content-Type\" Content=\"text/html; charset=us-ascii\"></HEAD>\r\n<BODY><h2>Bad Request - Invalid Hostname</h2>\r\n<hr><p>HTTP Error 400. The request hostname is invalid.</p>\r\n</BODY></HTML>\r\n";
                    }
                    if (str2 != null)
                    {
                        Stream responseStream = response.GetResponseStream();
                        byte[] buffer         = new byte[str2.Length];
                        if ((responseStream.Read(buffer, 0, buffer.Length) == str2.Length) && (str2 == Encoding.ASCII.GetString(buffer)))
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new EndpointNotFoundException(System.ServiceModel.SR.GetString("EndpointNotFound", new object[] { request.RequestUri.AbsoluteUri }), webException));
                        }
                    }
                }
                flag = true;
            }
            finally
            {
                if (!flag)
                {
                    response.Close();
                }
            }
            return(response);
        }
        public static Exception CreateRequestWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
        {
            Exception exception = ConvertWebException(webException, request, abortReason);

            if (webException.Response != null)
            {
                webException.Response.Close();
            }
            if (exception != null)
            {
                return(exception);
            }
            if (webException.InnerException is IOException)
            {
                return(CreateRequestIOException((IOException)webException.InnerException, request, webException));
            }
            if (webException.InnerException is SocketException)
            {
                return(SocketConnectionInitiator.ConvertConnectException((SocketException)webException.InnerException, request.RequestUri, TimeSpan.MaxValue, webException));
            }
            return(new EndpointNotFoundException(System.ServiceModel.SR.GetString("EndpointNotFound", new object[] { request.RequestUri.AbsoluteUri }), webException));
        }
Esempio n. 18
0
 private void TraceRequestResponseAborted(HttpAbortReason reason)
 {
 }
Esempio n. 19
0
        private void TraceRequestResponseAborted(HttpAbortReason reason)
        {
            if (isRequest)
            {
                if (TD.HttpChannelRequestAbortedIsEnabled())
                {
                    TD.HttpChannelRequestAborted(this.eventTraceActivity);
                }
            }
            else if (TD.HttpChannelResponseAbortedIsEnabled())
            {
                TD.HttpChannelResponseAborted(this.eventTraceActivity);
            }

            if (DiagnosticUtility.ShouldTraceWarning)
            {
                TraceUtility.TraceEvent(TraceEventType.Warning,
                                        isRequest ? TraceCode.HttpChannelRequestAborted : TraceCode.HttpChannelResponseAborted,
                                        isRequest ? SR.GetString(SR.TraceCodeHttpChannelRequestAborted) : SR.GetString(SR.TraceCodeHttpChannelResponseAborted),
                                        this.message);
            }
        }
Esempio n. 20
0
 public override void Abort(HttpAbortReason abortReason)
 {
     httpWebRequest.Abort();
     base.Abort(abortReason);
 }
Esempio n. 21
0
        public static Exception CreateRequestWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
        {
            Exception convertedException = ConvertWebException(webException, request, abortReason);

            if (webException.Response != null)
            {
                //free the connection for use by another request
                webException.Response.Close();
            }

            if (convertedException != null)
            {
                return convertedException;
            }

            if (webException.InnerException is IOException)
            {
                return CreateRequestIOException((IOException)webException.InnerException, request, webException);
            }

            if (webException.InnerException is SocketException)
            {
                return SocketConnectionInitiator.ConvertConnectException((SocketException)webException.InnerException, request.RequestUri, TimeSpan.MaxValue, webException);
            }

            return new EndpointNotFoundException(SR.GetString(SR.EndpointNotFound, request.RequestUri.AbsoluteUri), webException);
        }
Esempio n. 22
0
        public static HttpWebResponse ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
        {
            HttpWebResponse response = null;

            if (webException.Status == WebExceptionStatus.Success ||
                webException.Status == WebExceptionStatus.ProtocolError)
            {
                response = (HttpWebResponse)webException.Response;
            }

            if (response == null)
            {
                Exception convertedException = ConvertWebException(webException, request, abortReason);

                if (convertedException != null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(convertedException);
                }

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(webException.Message,
                    webException));
            }

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new EndpointNotFoundException(SR.GetString(SR.EndpointNotFound, request.RequestUri.AbsoluteUri), webException));
            }

            if (response.StatusCode == HttpStatusCode.ServiceUnavailable)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ServerTooBusyException(SR.GetString(SR.HttpServerTooBusy, request.RequestUri.AbsoluteUri), webException));
            }

            if (response.StatusCode == HttpStatusCode.UnsupportedMediaType)
            {
                string statusDescription = response.StatusDescription;
                if (!string.IsNullOrEmpty(statusDescription))
                {
                    if (string.Compare(statusDescription, HttpChannelUtilities.StatusDescriptionStrings.HttpContentTypeMissing, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(SR.GetString(SR.MissingContentType, request.RequestUri), webException));
                    }
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(SR.GetString(SR.FramingContentTypeMismatch, request.ContentType, request.RequestUri), webException));
            }

            if (response.StatusCode == HttpStatusCode.GatewayTimeout)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(webException.Message, webException));
            }

            // if http.sys has a request queue on the TCP port, then if the path fails to match it will send
            // back "<h1>Bad Request (Invalid Hostname)</h1>" in the body of a 400 response.
            // See code at \\index1\sddnsrv\net\http\sys\httprcv.c for details
            if (response.StatusCode == HttpStatusCode.BadRequest)
            {
                const string httpSysRequestQueueNotFound = "<h1>Bad Request (Invalid Hostname)</h1>";
                const string httpSysRequestQueueNotFoundVista = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\"http://www.w3.org/TR/html4/strict.dtd\">\r\n<HTML><HEAD><TITLE>Bad Request</TITLE>\r\n<META HTTP-EQUIV=\"Content-Type\" Content=\"text/html; charset=us-ascii\"></HEAD>\r\n<BODY><h2>Bad Request - Invalid Hostname</h2>\r\n<hr><p>HTTP Error 400. The request hostname is invalid.</p>\r\n</BODY></HTML>\r\n";
                string notFoundTestString = null;

                if (response.ContentLength == httpSysRequestQueueNotFound.Length)
                {
                    notFoundTestString = httpSysRequestQueueNotFound;
                }
                else if (response.ContentLength == httpSysRequestQueueNotFoundVista.Length)
                {
                    notFoundTestString = httpSysRequestQueueNotFoundVista;
                }

                if (notFoundTestString != null)
                {
                    Stream responseStream = response.GetResponseStream();
                    byte[] responseBytes = new byte[notFoundTestString.Length];
                    int bytesRead = responseStream.Read(responseBytes, 0, responseBytes.Length);

                    // since the response is buffered by System.Net (it's an error response), we should have read
                    // the amount we were expecting
                    if (bytesRead == notFoundTestString.Length
                        && notFoundTestString == UTF8Encoding.ASCII.GetString(responseBytes))
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new EndpointNotFoundException(SR.GetString(SR.EndpointNotFound, request.RequestUri.AbsoluteUri), webException));
                    }
                }
            }

            return response;
        }
Esempio n. 23
0
 public void Abort(RequestChannel channel)
 {
     Cleanup();
     _abortReason = HttpAbortReason.Aborted;
 }
Esempio n. 24
0
 public static Exception CreateRequestCanceledException(Exception webException, HttpWebRequest request, HttpAbortReason abortReason)
 {
     switch (abortReason)
     {
         case HttpAbortReason.Aborted:
             return new CommunicationObjectAbortedException(SR.GetString(SR.HttpRequestAborted, request.RequestUri), webException);
         case HttpAbortReason.TimedOut:
             return new TimeoutException(CreateRequestTimedOutMessage(request), webException);
         default:
             return new CommunicationException(SR.GetString(SR.HttpTransferError, webException.Message), webException);
     }
 }
Esempio n. 25
0
 public override void Abort(HttpAbortReason abortReason)
 {
     base._timeoutHelper.CancelCancellationToken();
     base.Abort(abortReason);
 }
Esempio n. 26
0
        public static Exception ConvertHttpRequestException(HttpRequestException exception, HttpRequestMessage request, HttpAbortReason abortReason)
        {
            Contract.Assert(exception.InnerException != null, "InnerException must be set to be able to convert");

            uint hresult = (uint)exception.InnerException.HResult;
            switch (hresult)
            {
                // .Net Native HttpClientHandler sometimes reports an incorrect handle state when a connection is aborted, so we treat it as a connection reset error
                case WININET_E_INCORRECT_HANDLE_STATE:
                    goto case WININET_E_CONNECTION_RESET;
                case WININET_E_CONNECTION_RESET:
                    return new CommunicationException(SR.Format(SR.HttpReceiveFailure, request.RequestUri), exception);
                // Linux HttpClient returns ERROR_INVALID_HANDLE in the endpoint-not-found case, so map to EndpointNotFoundException
                case UnsafeNativeMethods.ERROR_INVALID_HANDLE:
                case WININET_E_NAME_NOT_RESOLVED:
                    return new EndpointNotFoundException(SR.Format(SR.EndpointNotFound, request.RequestUri.AbsoluteUri), exception);
                case CURLE_SSL_CACERT:
                case CURLE_SSL_CERTPROBLEM:
                case ERROR_WINHTTP_SECURE_FAILURE:
                    return new SecurityNegotiationException(SR.Format(SR.TrustFailure, request.RequestUri.Authority), exception);
                default:
                    return new CommunicationException(exception.Message, exception);
            }
        }
        public static Exception CreateRequestCanceledException(Exception webException, HttpWebRequest request, HttpAbortReason abortReason)
        {
            switch (abortReason)
            {
                case HttpAbortReason.Aborted:
                    return new CommunicationObjectAbortedException(System.ServiceModel.SR.GetString("HttpRequestAborted", new object[] { request.RequestUri }), webException);

                case HttpAbortReason.TimedOut:
                    return new TimeoutException(CreateRequestTimedOutMessage(request), webException);
            }
            return new CommunicationException(System.ServiceModel.SR.GetString("HttpTransferError", new object[] { webException.Message }), webException);
        }