Esempio n. 1
0
        private void ValidateAuthentication()
        {
            if (_httpResponseMessage.StatusCode == HttpStatusCode.Unauthorized)
            {
                string message = SR.Format(SR.HttpAuthorizationFailed, _factory.AuthenticationScheme,
                                           _httpResponseMessage.Headers.WwwAuthenticate.ToString());
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                          HttpChannelUtilities.TraceResponseException(new MessageSecurityException(message)));
            }

            if (_httpResponseMessage.StatusCode == HttpStatusCode.Forbidden)
            {
                string message = SR.Format(SR.HttpAuthorizationForbidden, _factory.AuthenticationScheme);
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                          HttpChannelUtilities.TraceResponseException(new MessageSecurityException(message)));
            }
        }
Esempio n. 2
0
        private async Task <bool> ValidateContentTypeAsync(TimeoutHelper timeoutHelper)
        {
            var content = _httpResponseMessage.Content;

            if (content != null)
            {
                var mediaValueContentType = content.Headers.ContentType;
                _contentType   = mediaValueContentType == null ? string.Empty : mediaValueContentType.ToString();
                _contentLength = content.Headers.ContentLength.HasValue ? content.Headers.ContentLength.Value : -1;
            }

            if (string.IsNullOrEmpty(_contentType))
            {
                Stream contentStream = await GetStreamAsync(timeoutHelper);

                if (contentStream != null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(SR.HttpContentTypeHeaderRequired));
                }
                return(false);
            }
            else if (_contentLength != 0)
            {
                if (!_encoder.IsContentTypeSupported(_contentType))
                {
                    int    bytesToRead   = (int)_contentLength;
                    Stream contentStream = await GetStreamAsync(timeoutHelper);

                    string responseExcerpt = HttpChannelUtilities.GetResponseStreamExcerptString(contentStream, ref bytesToRead);
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(HttpChannelUtilities.TraceResponseException(
                                                                                  new ProtocolException(
                                                                                      SR.Format(
                                                                                          SR.ResponseContentTypeMismatch,
                                                                                          _contentType,
                                                                                          _encoder.ContentType,
                                                                                          bytesToRead,
                                                                                          responseExcerpt))));
                }
            }
            return(true);
        }