Skip to content

Apparently unused code WebRequestPSCmdlet.Common.cs #18541

Closed

Description

@CarloToso

Summary of the new feature / enhancement

I was working on the WebCmdlets trying to understand how to fix issue #2896 and it seems that lines 1380-1406 in WebRequestPSCmdlet.Common.cs are never used

                if (keepAuthorization && IsRedirectCode(response.StatusCode) && response.Headers.Location != null)
                {
                    _cancelToken.Cancel();
                    _cancelToken = null;

                    // if explicit count was provided, reduce it for this redirection.
                    if (WebSession.MaximumRedirection > 0)
                    {
                        WebSession.MaximumRedirection--;
                    }
                    // For selected redirects that used POST, GET must be used with the
                    // redirected Location.
                    // Since GET is the default; POST only occurs when -Method POST is used.
                    if (Method == WebRequestMethod.Post && IsRedirectToGet(response.StatusCode))
                    {
                        // See https://msdn.microsoft.com/library/system.net.httpstatuscode(v=vs.110).aspx
                        Method = WebRequestMethod.Get;
                    }

                    currentUri = new Uri(request.RequestUri, response.Headers.Location);
                    // Continue to handle redirection
                    using (client = GetHttpClient(handleRedirect: true))
                    using (HttpRequestMessage redirectRequest = GetRequest(currentUri))
                    {
                        response = GetResponse(client, redirectRequest, keepAuthorization);
                    }
                }

All the redirects are resolved in line 1378

response = client.SendAsync(req, HttpCompletionOption.ResponseHeadersRead, _cancelToken.Token).GetAwaiter().GetResult();

Proposed technical implementation details (optional)

I think lines 1380-1406 could be repurposed to follow HTTP redirects from HTTPS. Unfortunately I encountered some issues with the -MaximumRedirection flag.
Can someone test if those lines are actually never used?
Does someone know how to check how many redirects are resolved in line 1378?

Sample code that follows HTTPS->HTTP redirects but can't stop at -MaximumRedirection flag

          if (WebSession.MaximumRedirection > 0 && IsRedirectCode(response.StatusCode) && response.Headers.Location != null)
                {
                    _cancelToken.Cancel();
                    _cancelToken = null;

                    // if explicit count was provided, reduce it for this redirection.
                    if (WebSession.MaximumRedirection > 0)
                    {
                        WebSession.MaximumRedirection--;
                    }
                    // For selected redirects that used POST, GET must be used with the
                    // redirected Location.
                    // Since GET is the default; POST only occurs when -Method POST is used.
                    if (Method == WebRequestMethod.Post && IsRedirectToGet(response.StatusCode))
                    {
                        // See https://msdn.microsoft.com/library/system.net.httpstatuscode(v=vs.110).aspx
                        Method = WebRequestMethod.Get;
                    }

                    currentUri = new Uri(request.RequestUri, response.Headers.Location);
                    // Continue to handle redirection
                    using (client = GetHttpClient(handleRedirect: true))
                    using (HttpRequestMessage redirectRequest = GetRequest(currentUri))
                    {
                        response = GetResponse(client, redirectRequest, keepAuthorization);
                    }
                }

Activity

  1. added
    Issue-Enhancementthe issue is more of a feature request than a bug
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    on Nov 12, 2022
  2. iSazonov commented on Nov 12, 2022

    @iSazonov
    Collaborator

    I guess it works only if there are PreserveAuthorizationOnRedirect and Authorization header.

    It is very old code and it was ported ~6 years ago. I believe we could do things easier with modern HttpClient but it is not trivial. We could compare with wget but in any case we should have internal tests.

  3. ghost removed
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    on Jan 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions