示例#1
0
        static HttpMessageHandler CreateHttpMessageHandler(PackageSource packageSource, ICredentialService credentialService)
        {
            var proxy = ProxyCache.Instance.GetProxy(packageSource.SourceUri);

            var clientHandler = new HttpClientHandler {
                Proxy = proxy,
                AutomaticDecompression = (DecompressionMethods.GZip | DecompressionMethods.Deflate)
            };

            HttpMessageHandler messageHandler = clientHandler;

            if (proxy != null)
            {
                messageHandler = new ProxyAuthenticationHandler(clientHandler, credentialService, ProxyCache.Instance);
            }

            HttpMessageHandler innerHandler = messageHandler;

            messageHandler = new StsAuthenticationHandler(packageSource, TokenStore.Instance)
            {
                InnerHandler = messageHandler
            };

            innerHandler = messageHandler;

            messageHandler = new HttpSourceAuthenticationHandler(packageSource, clientHandler, credentialService)
            {
                InnerHandler = innerHandler
            };

            return(messageHandler);
        }
示例#2
0
        public ApiPortService(string endpoint, ProductInformation info, IProxyProvider proxyProvider)
        {
            if (string.IsNullOrWhiteSpace(endpoint))
            {
                throw new ArgumentOutOfRangeException(nameof(endpoint), endpoint, "Must be a valid endpoint");
            }
            if (proxyProvider == null)
            {
                throw new ArgumentNullException(nameof(proxyProvider));
            }

            var uri   = new Uri(endpoint);
            var proxy = proxyProvider.GetProxy(uri);

            // replace the handler with the proxy aware handler
            var clientHandler = new HttpClientHandler
            {
                Proxy = proxy,
                AutomaticDecompression = (DecompressionMethods.GZip | DecompressionMethods.Deflate)
            };

            // HTTP handler pipeline can be injected here, around the client handler
            HttpMessageHandler messageHandler = clientHandler;

            if (proxy != null)
            {
                messageHandler = new ProxyAuthenticationHandler(clientHandler, proxyProvider);
            }

            _client = new CompressedHttpClient(info, messageHandler)
            {
                BaseAddress = new Uri(endpoint),
                Timeout     = Timeout
            };
        }
        public async Task SendAsync_WithMissingCredentials_Returns407()
        {
            var defaultClientHandler = GetDefaultClientHandler();

            var service = Mock.Of <ICredentialService>();
            var handler = new ProxyAuthenticationHandler(defaultClientHandler, service, ProxyCache.Instance)
            {
                InnerHandler = GetLambdaHandler(HttpStatusCode.ProxyAuthenticationRequired)
            };

            var response = await SendAsync(handler);

            Assert.NotNull(response);
            Assert.Equal(HttpStatusCode.ProxyAuthenticationRequired, response.StatusCode);

            Mock.Get(service)
            .Verify(
                x => x.GetCredentialsAsync(
                    ProxyAddress,
                    It.IsAny <IWebProxy>(),
                    CredentialRequestType.Proxy,
                    It.IsAny <string>(),
                    It.IsAny <CancellationToken>()),
                Times.Once());
        }
        public async Task SendAsync_RetryWithClonedRequest()
        {
            var defaultClientHandler = GetDefaultClientHandler();

            var service = Mock.Of <ICredentialService>();

            Mock.Get(service)
            .Setup(
                x => x.GetCredentialsAsync(
                    ProxyAddress,
                    It.IsAny <IWebProxy>(),
                    CredentialRequestType.Proxy,
                    It.IsAny <string>(),
                    It.IsAny <CancellationToken>()))
            .Returns(() => Task.FromResult <ICredentials>(new NetworkCredential()));

            var requests = 0;
            var handler  = new ProxyAuthenticationHandler(defaultClientHandler, service, ProxyCache.Instance)
            {
                InnerHandler = new LambdaMessageHandler(
                    request =>
                {
                    Assert.Null(request.Headers.Authorization);
                    request.Headers.Authorization = new AuthenticationHeaderValue("Basic", "TEST");
                    requests++;
                    return(new HttpResponseMessage(HttpStatusCode.ProxyAuthenticationRequired));
                })
            };

            var response = await SendAsync(handler);

            Assert.True(requests > 1, "No retries");
            Assert.NotNull(response);
            Assert.Equal(HttpStatusCode.ProxyAuthenticationRequired, response.StatusCode);
        }
        public async Task SendAsync_WithUnauthenticatedProxy_PassesThru()
        {
            var defaultClientHandler = GetDefaultClientHandler();

            var service = Mock.Of <ICredentialService>();
            var handler = new ProxyAuthenticationHandler(defaultClientHandler, service, ProxyCache.Instance)
            {
                InnerHandler = GetLambdaHandler(HttpStatusCode.OK)
            };

            var response = await SendAsync(handler);

            Assert.NotNull(response);
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        }
        public async Task SendAsync_WhenCancelledDuringAcquiringCredentials_Throws()
        {
            // Arrange
            var defaultClientHandler = GetDefaultClientHandler();

            var cts = new CancellationTokenSource();

            var service = Mock.Of <ICredentialService>();

            Mock.Get(service)
            .Setup(
                x => x.GetCredentialsAsync(
                    ProxyAddress,
                    It.IsAny <IWebProxy>(),
                    CredentialRequestType.Proxy,
                    It.IsAny <string>(),
                    It.IsAny <CancellationToken>()))
            .ThrowsAsync(new TaskCanceledException())
            .Callback(() => cts.Cancel());

            var handler = new ProxyAuthenticationHandler(defaultClientHandler, service, ProxyCache.Instance);

            var responses = new Queue <HttpStatusCode>(
                new[] { HttpStatusCode.ProxyAuthenticationRequired, HttpStatusCode.OK });
            var innerHandler = new LambdaMessageHandler(
                _ => new HttpResponseMessage(responses.Dequeue()));

            handler.InnerHandler = innerHandler;

            // Act
            await Assert.ThrowsAsync <TaskCanceledException>(
                () => SendAsync(handler, cancellationToken : cts.Token));

            Mock.Get(service)
            .Verify(
                x => x.GetCredentialsAsync(
                    ProxyAddress,
                    It.IsAny <IWebProxy>(),
                    CredentialRequestType.Proxy,
                    It.IsAny <string>(),
                    It.IsAny <CancellationToken>()),
                Times.Once);
        }
示例#7
0
        private HttpHandlerResourceV3 CreateResource(PackageSource packageSource)
        {
            var sourceUri = packageSource.SourceUri;
            var proxy     = ProxyCache.Instance.GetProxy(sourceUri);

            // replace the handler with the proxy aware handler
            var clientHandler = new HttpClientHandler
            {
                Proxy = proxy,
                AutomaticDecompression = (DecompressionMethods.GZip | DecompressionMethods.Deflate)
            };

            // HTTP handler pipeline can be injected here, around the client handler
            HttpMessageHandler messageHandler = new ProgressHttpMessageHandler(clientHandler, _progressAction);

            if (proxy != null)
            {
                messageHandler = new ProxyAuthenticationHandler(clientHandler, HttpHandlerResourceV3.CredentialService.Value, ProxyCache.Instance);
            }

            {
                //var innerHandler = messageHandler;

                // TODO: Investigate what changed in this type
                //    messageHandler = new StsAuthenticationHandler(packageSource, TokenStore.Instance)
                //    {
                //        InnerHandler = innerHandler
                //    };
            }
            {
                var innerHandler = messageHandler;

                messageHandler = new HttpSourceAuthenticationHandler(packageSource, clientHandler, HttpHandlerResourceV3.CredentialService.Value)
                {
                    InnerHandler = innerHandler
                };
            }

            var resource = new HttpHandlerResourceV3(clientHandler, messageHandler);

            return(resource);
        }
示例#8
0
        public ApiPortService(string endpoint, ProductInformation info, IProxyProvider proxyProvider)
        {
            if (string.IsNullOrWhiteSpace(endpoint))
            {
                throw new ArgumentOutOfRangeException(nameof(endpoint), endpoint, LocalizedStrings.MustBeValidEndpoint);
            }
            if (proxyProvider == null)
            {
                throw new ArgumentNullException(nameof(proxyProvider));
            }

            // Create the URI directly from a string (rather than using a hard-coded scheme or port) because
            // even though production use of ApiPort should always use HTTPS, developers using a non-production
            // portability service URL (via the -e command line parameter) may need to specify a different
            // scheme or port.
            var uri   = new Uri(endpoint);
            var proxy = proxyProvider.GetProxy(uri);

            // replace the handler with the proxy aware handler
            var clientHandler = new HttpClientHandler
            {
#if !FEATURE_SERVICE_POINT_MANAGER
                SslProtocols = CompressedHttpClient.SupportedSSLProtocols,
#endif
                Proxy = proxy,
                AutomaticDecompression = (DecompressionMethods.GZip | DecompressionMethods.Deflate)
            };

            // HTTP handler pipeline can be injected here, around the client handler
            HttpMessageHandler messageHandler = clientHandler;

            if (proxy != null)
            {
                messageHandler = new ProxyAuthenticationHandler(clientHandler, proxyProvider);
            }

            _client = new CompressedHttpClient(info, messageHandler)
            {
                BaseAddress = new Uri(endpoint),
                Timeout     = Timeout
            };
        }
        public async Task SendAsync_WithWrongCredentials_StopsRetryingAfter3Times()
        {
            var defaultClientHandler = GetDefaultClientHandler();

            var service = Mock.Of <ICredentialService>();

            Mock.Get(service)
            .Setup(
                x => x.GetCredentialsAsync(
                    ProxyAddress,
                    It.IsAny <IWebProxy>(),
                    CredentialRequestType.Proxy,
                    It.IsAny <string>(),
                    It.IsAny <CancellationToken>()))
            .Returns(() => Task.FromResult <ICredentials>(new NetworkCredential()));

            var handler = new ProxyAuthenticationHandler(defaultClientHandler, service, ProxyCache.Instance);

            int retryCount   = 0;
            var innerHandler = new LambdaMessageHandler(
                _ => { retryCount++; return(new HttpResponseMessage(HttpStatusCode.ProxyAuthenticationRequired)); });

            handler.InnerHandler = innerHandler;

            var response = await SendAsync(handler);

            Assert.NotNull(response);
            Assert.Equal(HttpStatusCode.ProxyAuthenticationRequired, response.StatusCode);

            Assert.Equal(ProxyAuthenticationHandler.MaxAuthRetries, retryCount);

            Mock.Get(service)
            .Verify(
                x => x.GetCredentialsAsync(
                    ProxyAddress,
                    It.IsAny <IWebProxy>(),
                    CredentialRequestType.Proxy,
                    It.IsAny <string>(),
                    It.IsAny <CancellationToken>()),
                Times.Exactly(2));
        }
        public async Task SendAsync_WithAcquiredCredentials_RetriesRequest()
        {
            var defaultClientHandler = GetDefaultClientHandler();

            var service = Mock.Of <ICredentialService>();

            Mock.Get(service)
            .Setup(
                x => x.GetCredentialsAsync(
                    ProxyAddress,
                    It.IsAny <IWebProxy>(),
                    CredentialRequestType.Proxy,
                    It.IsAny <string>(),
                    It.IsAny <CancellationToken>()))
            .Returns(() => Task.FromResult <ICredentials>(new NetworkCredential()));

            var handler = new ProxyAuthenticationHandler(defaultClientHandler, service, ProxyCache.Instance);

            var responses = new Queue <HttpStatusCode>(
                new[] { HttpStatusCode.ProxyAuthenticationRequired, HttpStatusCode.OK });
            var innerHandler = new LambdaMessageHandler(
                _ => new HttpResponseMessage(responses.Dequeue()));

            handler.InnerHandler = innerHandler;

            var response = await SendAsync(handler);

            Assert.NotNull(response);
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);

            Mock.Get(service)
            .Verify(
                x => x.GetCredentialsAsync(
                    ProxyAddress,
                    It.IsAny <IWebProxy>(),
                    CredentialRequestType.Proxy,
                    It.IsAny <string>(),
                    It.IsAny <CancellationToken>()),
                Times.Once());
        }