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);
}
}
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
All the redirects are resolved in line 1378
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
-MaximumRedirectionflag.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
-MaximumRedirectionflag