<commit_before>﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net;

namespace NuGet {
    public class ProxyFinder : CredentialProviderRegistry, IProxyFinder {
        /// <summary>
        /// Local Cache of Proxy objects that will store the Proxy that was discovered during the session
        /// and will return the cached proxy object instead of trying to perform proxy detection logic
        /// for the same Uri again.
        /// </summary>
        private readonly ConcurrentDictionary<Uri, IWebProxy> _proxyCache = new ConcurrentDictionary<Uri, IWebProxy>();
        ///// <summary>
        ///// Local cache of registered proxy providers to use when locating a valid proxy
        ///// to use for the given Uri.
        ///// </summary>
        //private readonly ISet<IProxyProvider> _providerCache = new HashSet<IProxyProvider>();
        /// <summary>
        /// Returns an instance of a IWebProxy interface that is to be used for creating requests to the given Uri
        /// </summary>
        /// <param name="uri">The Uri object that the proxy is to be used for.</param>
        /// <returns></returns>
        public virtual IWebProxy GetProxy(Uri uri) {
            if (uri == null) {
                throw new ArgumentNullException("uri");
            }

            bool isSystemProxySet = IsSystemProxySet(uri);
            return isSystemProxySet ? GetProxyInternal(uri) : null;
        }

        /// <summary>
        /// Internal method that handles the logic of going through the Proxy detection logic and returns the
        /// correct instance of the IWebProxy object.
        /// </summary>
        /// <param name="uri">The Uri object that the proxy is to be used for.</param>
        /// <returns></returns>
        private IWebProxy GetProxyInternal(Uri uri) {
            IWebProxy result = null;
            WebProxy systemProxy = GetSystemProxy(uri);

            IWebProxy cachedProxy;
            if (_proxyCache.TryGetValue(systemProxy.Address, out cachedProxy)) {
                return cachedProxy;
            }

            systemProxy.Credentials = GetProxyCredentials(uri);

            // TODO: If the proxy that is returned is null do we really want to cache this?
            // PRO: Subsequent requests for the given Uri should automatically return a null
            //      proxy instance without going through the proxy detection logic.
            // CON: If the user incorrectly types the password or an invalid proxy instance
            //      is cached then the user has to re-start the "Client" to be able to re-try
            //      connecting to a valid proxy.
            _proxyCache.TryAdd(systemProxy.Address, result);

            return result;
        }

        private ICredentials GetProxyCredentials(Uri uri) {
            ICredentials result = null;

            foreach (var provider in RegisteredProviders) {
                var credentials = ExecuteProvider(provider, uri);
                if (credentials != null) {
                    result = credentials;
                    break;
                }
            }
            return result;
        }
<commit_msg>ProxyFinder was not caching the correct instance of the proxy.<commit_after>﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net;

namespace NuGet {
    public class ProxyFinder : CredentialProviderRegistry, IProxyFinder {
        /// <summary>
        /// Local Cache of Proxy objects that will store the Proxy that was discovered during the session
        /// and will return the cached proxy object instead of trying to perform proxy detection logic
        /// for the same Uri again.
        /// </summary>
        private readonly ConcurrentDictionary<Uri, IWebProxy> _proxyCache = new ConcurrentDictionary<Uri, IWebProxy>();
        ///// <summary>
        ///// Local cache of registered proxy providers to use when locating a valid proxy
        ///// to use for the given Uri.
        ///// </summary>
        //private readonly ISet<IProxyProvider> _providerCache = new HashSet<IProxyProvider>();
        /// <summary>
        /// Returns an instance of a IWebProxy interface that is to be used for creating requests to the given Uri
        /// </summary>
        /// <param name="uri">The Uri object that the proxy is to be used for.</param>
        /// <returns></returns>
        public virtual IWebProxy GetProxy(Uri uri) {
            if (uri == null) {
                throw new ArgumentNullException("uri");
            }

            bool isSystemProxySet = IsSystemProxySet(uri);
            return isSystemProxySet ? GetProxyInternal(uri) : null;
        }

        /// <summary>
        /// Internal method that handles the logic of going through the Proxy detection logic and returns the
        /// correct instance of the IWebProxy object.
        /// </summary>
        /// <param name="uri">The Uri object that the proxy is to be used for.</param>
        /// <returns></returns>
        private IWebProxy GetProxyInternal(Uri uri) {
            WebProxy systemProxy = GetSystemProxy(uri);

            IWebProxy cachedProxy;
            if (_proxyCache.TryGetValue(systemProxy.Address, out cachedProxy)) {
                return cachedProxy;
            }

            systemProxy.Credentials = GetProxyCredentials(uri);

            // TODO: If the proxy that is returned is null do we really want to cache this?
            // PRO: Subsequent requests for the given Uri should automatically return a null
            //      proxy instance without going through the proxy detection logic.
            // CON: If the user incorrectly types the password or an invalid proxy instance
            //      is cached then the user has to re-start the "Client" to be able to re-try
            //      connecting to a valid proxy.
            _proxyCache.TryAdd(systemProxy.Address, systemProxy);

            return systemProxy;
        }

        private ICredentials GetProxyCredentials(Uri uri) {
            ICredentials result = null;

            foreach (var provider in RegisteredProviders) {
                var credentials = ExecuteProvider(provider, uri);
                if (credentials != null) {
                    result = credentials;
                    break;
                }
            }
            return result;
        }

{'fraction_non_alphanumeric': 0.080765639589169, 'fraction_numerical': 0.0, 'mean_word_length': 1.9387288523090993, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4309552', 'n_tokens_mistral': 1501, 'n_tokens_neox': 1464, 'n_words': 596}