From fb22f7f47d30536a77447a750963b96a3433f43d Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 3 May 2023 20:27:57 +0200 Subject: [PATCH 1/6] Replace Microsoft.PowerShell.Commands.WebProxy with System.Net.WebProxy --- .../Common/WebRequestPSCmdlet.Common.cs | 5 +- .../utility/WebCmdlet/CoreCLR/WebProxy.cs | 63 ------------------- 2 files changed, 3 insertions(+), 65 deletions(-) delete mode 100644 src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebProxy.cs 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..b03dbf969d8 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 @@ -387,6 +387,7 @@ public virtual string CustomMethod /// [Parameter(ParameterSetName = "StandardMethod")] [Parameter(ParameterSetName = "CustomMethod")] + [ValidateNotNullOrEmpty] public virtual Uri Proxy { get; set; } /// @@ -962,8 +963,8 @@ internal virtual void PrepareSession() { if (Proxy is not null) { - WebProxy webProxy = new(Proxy); - webProxy.BypassProxyOnLocal = false; + WebProxy webProxy = new(Proxy, BypassOnLocal: false); + if (ProxyCredential is not null) { webProxy.Credentials = ProxyCredential.GetNetworkCredential(); 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 deleted file mode 100644 index 6890599b51b..00000000000 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebProxy.cs +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -using System; -using System.Net; - -namespace Microsoft.PowerShell.Commands -{ - internal class WebProxy : IWebProxy, IEquatable - { - private ICredentials _credentials; - private readonly Uri _proxyAddress; - - internal WebProxy(Uri address) - { - ArgumentNullException.ThrowIfNull(address); - - _proxyAddress = address; - } - - public override bool Equals(object obj) => Equals(obj as WebProxy); - - public override int GetHashCode() => HashCode.Combine(_proxyAddress, _credentials, BypassProxyOnLocal); - - public bool Equals(WebProxy other) - { - if (other is null) - { - return false; - } - - // _proxyAddress cannot be null as it is set in the constructor - return other._credentials == _credentials - && _proxyAddress.Equals(other._proxyAddress) - && BypassProxyOnLocal == other.BypassProxyOnLocal; - } - - 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; - } -} From 437513818372648e12522f7af12478a39c151207 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sat, 6 May 2023 21:34:41 +0200 Subject: [PATCH 2/6] add back Microsoft.PowerShell.Commands.WebProxy --- .../utility/WebCmdlet/CoreCLR/WebProxy.cs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebProxy.cs 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 new file mode 100644 index 00000000000..7375d131758 --- /dev/null +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebProxy.cs @@ -0,0 +1,33 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +#nullable enable + +using System; + +namespace Microsoft.PowerShell.Commands +{ + internal class WebProxy : System.Net.WebProxy, IEquatable + { + internal WebProxy(Uri address, bool BypassOnLocal) + { + Address = address; + BypassProxyOnLocal = BypassOnLocal; + } + + public override bool Equals(object? obj) => Equals(obj as WebProxy); + + public override int GetHashCode() => HashCode.Combine(Address, Credentials, BypassProxyOnLocal); + + public bool Equals(WebProxy? other) + { + if (other is null) + { + return false; + } + + //return Credentials == other.Credentials && Address.Equals(other.Address) && BypassProxyOnLocal == other.BypassProxyOnLocal; + return GetHashCode() == other.GetHashCode(); + } + } +} From f474bca6ee3c402b663c9bb966d688bc80c4a6cd Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sat, 6 May 2023 23:11:30 +0200 Subject: [PATCH 3/6] use == operator for Address --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 1 - .../commands/utility/WebCmdlet/CoreCLR/WebProxy.cs | 5 ++--- 2 files changed, 2 insertions(+), 4 deletions(-) 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 b03dbf969d8..62b167258c9 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 @@ -387,7 +387,6 @@ public virtual string CustomMethod /// [Parameter(ParameterSetName = "StandardMethod")] [Parameter(ParameterSetName = "CustomMethod")] - [ValidateNotNullOrEmpty] public virtual Uri Proxy { get; set; } /// 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 7375d131758..354dd6c6f57 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 @@ -9,7 +9,7 @@ namespace Microsoft.PowerShell.Commands { internal class WebProxy : System.Net.WebProxy, IEquatable { - internal WebProxy(Uri address, bool BypassOnLocal) + internal WebProxy(Uri? address, bool BypassOnLocal) { Address = address; BypassProxyOnLocal = BypassOnLocal; @@ -26,8 +26,7 @@ public bool Equals(WebProxy? other) return false; } - //return Credentials == other.Credentials && Address.Equals(other.Address) && BypassProxyOnLocal == other.BypassProxyOnLocal; - return GetHashCode() == other.GetHashCode(); + return Credentials == other.Credentials && Address == other.Address && BypassProxyOnLocal == other.BypassProxyOnLocal; } } } From 25c6ef893d23f51144088fc6db4c7e4824ab787f Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 8 May 2023 13:29:20 +0200 Subject: [PATCH 4/6] More in depth WebProxy comparison --- .../commands/utility/WebCmdlet/CoreCLR/WebProxy.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 354dd6c6f57..3690e916c09 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 @@ -17,7 +17,7 @@ internal WebProxy(Uri? address, bool BypassOnLocal) public override bool Equals(object? obj) => Equals(obj as WebProxy); - public override int GetHashCode() => HashCode.Combine(Address, Credentials, BypassProxyOnLocal); + public override int GetHashCode() => HashCode.Combine(Address, Credentials, BypassProxyOnLocal, UseDefaultCredentials, BypassArrayList); public bool Equals(WebProxy? other) { @@ -26,7 +26,7 @@ public bool Equals(WebProxy? other) return false; } - return Credentials == other.Credentials && Address == other.Address && BypassProxyOnLocal == other.BypassProxyOnLocal; + return GetHashCode() == other.GetHashCode(); } } } From bebe5b35c8e5999df952fc9c089eab05897f7125 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 8 May 2023 19:41:30 +0200 Subject: [PATCH 5/6] more in depth equality fix; cleanup nested if --- .../Common/WebRequestPSCmdlet.Common.cs | 35 +++++++++---------- .../utility/WebCmdlet/CoreCLR/WebProxy.cs | 11 +++--- 2 files changed, 23 insertions(+), 23 deletions(-) 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 62b167258c9..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, BypassOnLocal: false); + WebProxy webProxy = new(Proxy); - if (ProxyCredential is not null) - { - webProxy.Credentials = ProxyCredential.GetNetworkCredential(); - } - else - { - webProxy.UseDefaultCredentials = ProxyUseDefaultCredentials; - } + if (ProxyCredential is not null) + { + 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 3690e916c09..50bb5604330 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 @@ -9,15 +9,14 @@ namespace Microsoft.PowerShell.Commands { internal class WebProxy : System.Net.WebProxy, IEquatable { - internal WebProxy(Uri? address, bool BypassOnLocal) + internal WebProxy(Uri? address) { Address = address; - BypassProxyOnLocal = BypassOnLocal; } public override bool Equals(object? obj) => Equals(obj as WebProxy); - public override int GetHashCode() => HashCode.Combine(Address, Credentials, BypassProxyOnLocal, UseDefaultCredentials, BypassArrayList); + public override int GetHashCode() => HashCode.Combine(Address, Credentials, BypassProxyOnLocal); public bool Equals(WebProxy? other) { @@ -26,7 +25,11 @@ public bool Equals(WebProxy? other) return false; } - return GetHashCode() == other.GetHashCode(); + return Address == other.Address + && Credentials == other.Credentials + && BypassProxyOnLocal == other.BypassProxyOnLocal + && UseDefaultCredentials == other.UseDefaultCredentials + && BypassArrayList == other.BypassArrayList; } } } From 8be91a27ff2e9b42111259a4e9b2df9111aed62c Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 8 May 2023 21:28:20 +0200 Subject: [PATCH 6/6] compare BypassList --- .../commands/utility/WebCmdlet/CoreCLR/WebProxy.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 50bb5604330..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,6 +4,8 @@ #nullable enable using System; +using System.Collections; +using System.Collections.Generic; namespace Microsoft.PowerShell.Commands { @@ -29,7 +31,7 @@ public bool Equals(WebProxy? other) && Credentials == other.Credentials && BypassProxyOnLocal == other.BypassProxyOnLocal && UseDefaultCredentials == other.UseDefaultCredentials - && BypassArrayList == other.BypassArrayList; + && (BypassList as IStructuralEquatable).Equals(other.BypassList, EqualityComparer.Default); } } }