-
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
Conversation
|
@CarloToso Please resolve merge conflicts. |
|
@stevenebutler could you help me with the failing test? |
With the change made I would expect the test to fail. The test is ensuring that supplying the Proxy parameter only causes a new connection to be created if the new proxy is different to the old proxy because changing the proxy is not permitted once a request has been issued. If you don't use the pwsh WebProxy class with the Equality operator defined then the equality check will always fail so a new connection will be used regardless of providing the same Proxy parameter. You can trade-off the performance impact of creating a new connection and change the test or implement another way to avoid recreating the Why did pwsh have its own WebProxy class in the first place, and why do we want to get rid of it now? |
|
Thank you very much for your explanation. |
You can probably re-implement the point of the comparison Equals operator in the WebProxy class I added directly into the body of the method. i.e. only create a new WebProxy if the Proxy is provided on the command line and the proxy string is different to the one stored in the existing WebProxy in the WebSession (assuming there is one of course). I'm assuming you are able to retrieve that value from the system WebProxy through a property. If you can do that the test should pass. This may be a minor breaking change if anyone is checking the class name of the existing webproxy or if it has properties that don't exist on the system one that they're using to drive logic in their scripts. I can't imagine why anybody would want to do that though. |
|
Thank you, I'll look into it |
| namespace Microsoft.PowerShell.Commands | ||
| { | ||
| internal class WebProxy : IWebProxy, IEquatable<WebProxy> | ||
| internal class WebProxy : System.Net.WebProxy, IEquatable<WebProxy> |
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.
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.
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.
Using ReferenceEquals does not work, the tests fail
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.
I think it is still possible to remove the class - the Address property on System.Net.WebProxy is available and could be used to compare with the passed in Proxy parameter (if one is provided).
The Address property on System.Net.WebProxy is also settable, and can be changed without causing exceptions between requests from HttpClient so it may be possible to simply update the Address property on the existing Proxy provided:
- the WebSession already has a Proxy (i.e. this is not a brand new WebSession)
- the type of the Proxy is
System.Net.WebProxy
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 HttpClientHandler.
Note that no new connection is required if the user updates the Address property to the same value it already had.
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.
Using ReferenceEquals does not work, the tests fail
I don't see the commit.
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.
I tested in in local
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.
I see no reason why it wouldn't work. Unless there happens to be wrapping in PSObject which can be bypassed.
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.
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.
|
This PR has Quantification details
Why proper sizing of changes matters
Optimal pull request sizes drive a better predictable PR flow as they strike a
What can I do to optimize my changes
How to interpret the change counts in git diff output
Was this comment helpful? 👍 :ok_hand: :thumbsdown: (Email) |
| // 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)) |
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.
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)?
if (!string.IsNullOrEmpty(Proxy) && Proxy != (WebSession?.Proxy as System.Net.WebProxy)?.Address)
{
if (WebSession.Proxy is System.Net.WebProxy wsProxy)
{
wsProxy.Address = Proxy;
}
else
{
// Proxy type is unexpected so replace with standard proxy (since the user supplied the Proxy parameter)
WebSession.Proxy = new System.Net.WebProxy(Proxy, true);
}
}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 BypassProxyOnLocal parameter (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 modified
directly 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)
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
|
@iSazonov have your questions/concerns been satisfied? |
|
As I see it, this PR has not been completed. We need to delete the internal Web Proxy class and for this to correctly track changes to the argument in the Proxy parameter. There's a suggestion from @stevenebutler on how to do it. @CarloToso Do you want to continue? |
I'll try to come back to this PR as soon as I have some spare time |
PR Summary
It seems we can't remove
Microsoft.PowerShell.Commands.WebProxybecause we need to implementIEquatable<WebProxy>Replaces
System.Net.IWebProxywithSystem.Net.WebProxyPR Context
Discussed with @iSazonov in another PR #19359 (comment)
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.(which runs in a different PS Host).