diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 60ee931591a..0cc82345d5c 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -958,28 +958,25 @@ internal virtual void PrepareSession() { WebSession.NoProxy = true; } - else + else if (Proxy is not null) { - if (Proxy is not null) + WebProxy webProxy = new(Proxy); + + if (ProxyCredential is not null) { - WebProxy webProxy = new(Proxy); - webProxy.BypassProxyOnLocal = false; - if (ProxyCredential is not null) - { - webProxy.Credentials = ProxyCredential.GetNetworkCredential(); - } - else - { - webProxy.UseDefaultCredentials = ProxyUseDefaultCredentials; - } + webProxy.Credentials = ProxyCredential.GetNetworkCredential(); + } + else + { + webProxy.UseDefaultCredentials = ProxyUseDefaultCredentials; + } - // We don't want to update the WebSession unless the proxies are different - // as that will require us to create a new HttpClientHandler and lose connection - // persistence. - if (!webProxy.Equals(WebSession.Proxy)) - { - WebSession.Proxy = webProxy; - } + // We don't want to update the WebSession unless the proxies are different + // as that will require us to create a new HttpClientHandler and lose connection + // persistence. + if (!webProxy.Equals(WebSession.Proxy)) + { + WebSession.Proxy = webProxy; } } diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebProxy.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebProxy.cs index 6e9f6bdf98e..e806b595a55 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebProxy.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebProxy.cs @@ -4,25 +4,21 @@ #nullable enable using System; -using System.Net; +using System.Collections; +using System.Collections.Generic; namespace Microsoft.PowerShell.Commands { - internal class WebProxy : IWebProxy, IEquatable + internal class WebProxy : System.Net.WebProxy, IEquatable { - private ICredentials? _credentials; - private readonly Uri _proxyAddress; - - internal WebProxy(Uri address) + internal WebProxy(Uri? address) { - ArgumentNullException.ThrowIfNull(address); - - _proxyAddress = address; + Address = address; } public override bool Equals(object? obj) => Equals(obj as WebProxy); - public override int GetHashCode() => HashCode.Combine(_proxyAddress, _credentials, BypassProxyOnLocal); + public override int GetHashCode() => HashCode.Combine(Address, Credentials, BypassProxyOnLocal); public bool Equals(WebProxy? other) { @@ -30,36 +26,12 @@ public bool Equals(WebProxy? other) { return false; } - - // _proxyAddress cannot be null as it is set in the constructor - return other._credentials == _credentials - && _proxyAddress.Equals(other._proxyAddress) - && BypassProxyOnLocal == other.BypassProxyOnLocal; + + return Address == other.Address + && Credentials == other.Credentials + && BypassProxyOnLocal == other.BypassProxyOnLocal + && UseDefaultCredentials == other.UseDefaultCredentials + && (BypassList as IStructuralEquatable).Equals(other.BypassList, EqualityComparer.Default); } - - public ICredentials? Credentials - { - get => _credentials; - - set => _credentials = value; - } - - internal bool BypassProxyOnLocal { get; set; } - - internal bool UseDefaultCredentials - { - get => _credentials == CredentialCache.DefaultCredentials; - - set => _credentials = value ? CredentialCache.DefaultCredentials : null; - } - - public Uri GetProxy(Uri destination) - { - ArgumentNullException.ThrowIfNull(destination); - - return destination.IsLoopback ? destination : _proxyAddress; - } - - public bool IsBypassed(Uri host) => host.IsLoopback; } }