-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Microsoft.PowerShell.Commands.WebProxy replace System.Net.IWebProxy with System.Net.WebProxy #19609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
fb22f7f
dc452cc
4375138
f474bca
25c6ef8
bebe5b3
8be91a2
adbafe8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,62 +4,34 @@ | |
| #nullable enable | ||
|
|
||
| using System; | ||
| using System.Net; | ||
| using System.Collections; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace Microsoft.PowerShell.Commands | ||
| { | ||
| internal class WebProxy : IWebProxy, IEquatable<WebProxy> | ||
| internal class WebProxy : System.Net.WebProxy, IEquatable<WebProxy> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would assume that reference equality is enough to make the cache work for most real-world scenarios. Then we could still remove this type.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using ReferenceEquals does not work, the tests fail
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is still possible to remove the class - the Address property on The Address property on System.Net.WebProxy is also settable, and can be changed without causing exceptions between requests from
Updating the proxy Address property does cause a new connection to be made (to connect to the new proxy), but this doesn't require creation of a new Note that no new connection is required if the user updates the Address property to the same value it already had.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't see the commit.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tested in in local
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see no reason why it wouldn't work. Unless there happens to be wrapping in PSObject which can be bypassed.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't work because there is a test that checks that passing the same -Proxy repeatedly with a persistent websession doesn't cause a new httpclienthandler to be created. If you use referenceequals then this will not be true. The code from the Equals() could be moved into the CmdLet if you want to retain the existing behaviour but remove the internal WebProxy class completely. Also note that you can modify properties on the WebProxy without impacting the HttpClientHandler so there's no need to really check all the properties. The only thing that really makes a difference is the Proxy Address. |
||
| { | ||
| 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) | ||
| { | ||
| 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; | ||
|
|
||
| return Address == other.Address | ||
| && Credentials == other.Credentials | ||
| && BypassProxyOnLocal == other.BypassProxyOnLocal | ||
| && UseDefaultCredentials == other.UseDefaultCredentials | ||
| && (BypassList as IStructuralEquatable).Equals(other.BypassList, EqualityComparer<string>.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; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really we use 3 parameters to create webProxy. So we could keep them in WebSession
to track WebSession.Proxy changes. This also exclude creating proxy in line 963 if no need.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At simplest level, couldn't the comparison be something like this (excuse the possible namespace confusion, I've typed this directly into GitHub comment uncompiled/untested)?
This may not pass the existing tests because reassigning Address from Proxy parameter when they're different won't cause the HttpClient to be recreated, but it should work, and that test's expected result could be updated accordingly.
If you're also trying to deal with the
BypassProxyOnLocalparameter (did you add that to the CmdLet because there's no way in current PowerShell to affect that setting), you'll need some extra logic for that too, but it can also be modifieddirectly on the Proxy class without needing to recreate it (provided the stored WebSession.Proxy is a System.Net.WebProxy object and not some custom Proxy class designed by the user).
Edit: I just noticed I used Address instead of Proxy in the
new System.Net.WebProxy(Proxy, true);(third last line)