Last active
November 16, 2022 23:58
-
-
Save sdwheeler/d5e80f2b88472214583cd03571fd1665 to your computer and use it in GitHub Desktop.
Script to show each step of a redirection chain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[string]$startURL, | |
[switch]$showall | |
) | |
$ErrorActionPreference = 'Stop' | |
$Error.Clear() | |
$lastError = $null | |
function getUrl { | |
param([uri]$url) | |
$wr = [System.Net.WebRequest]::Create($url) | |
$wr.Method= 'GET' | |
$wr.Timeout = 25000 | |
$wr.AllowAutoRedirect = $false | |
$wr.UserAgent = 'Redirect crawler' | |
try { | |
$resp = [System.Net.HttpWebResponse]$wr.GetResponse() | |
$resp | |
} | |
catch [System.Net.WebException] { | |
$script:lastError = $error[0].Exception.InnerException | |
$error[0].Exception.InnerException.Response | |
$Error.Clear() | |
} | |
} | |
$locationlist = @() | |
while ($startURL -ne '') { | |
$response = getURL $startURL | |
if ($response -eq $null) { | |
$result = new-object -type psobject -prop ([ordered]@{ | |
requestURL=$startURL | |
statusCode=$lastError.Status | |
status= $lastError.Message | |
location=$startURL | |
}) | |
break | |
} | |
if ($locationlist.Contains($response.Headers['Location'])) { | |
$result = new-object -type psobject -prop ([ordered]@{ | |
requestURL=$startURL | |
statusCode='RedirLoop' | |
status= 'Redirection loop!' | |
location=$response.Headers['Location'] | |
}) | |
break | |
} | |
if ($Error.Count -ne 0) { | |
break | |
} | |
switch ($response.StatusCode.value__) { | |
301 { | |
$result = new-object -type psobject -prop ([ordered]@{ | |
requestURL=$startURL | |
statusCode=$response.StatusCode.value__ | |
status= $response.StatusDescription | |
location=$response.Headers['Location'] | |
}) | |
if ($response.Headers['Location'].StartsWith('/')) { | |
$baseURL = [uri]$response.ResponseUri | |
$startURL = $baseURL.Scheme + '://'+ $baseURL.Host + $response.Headers['Location'] | |
} elseif ($response.Headers['Location'].StartsWith('http')) { | |
$startURL = $response.Headers['Location'] | |
} else { | |
$baseURL = [uri]$response.ResponseUri | |
$startURL = $baseURL.Scheme + '://'+ $baseURL.Host + $baseURL.AbsolutePath + $response.Headers['Location'] | |
} | |
break | |
} | |
302 { | |
$result = new-object -type psobject -prop ([ordered]@{ | |
requestURL=$startURL | |
statusCode=$response.StatusCode.value__ | |
status= $response.StatusDescription | |
location=$response.Headers['Location'] | |
}) | |
if ($response.Headers['Location'].StartsWith('/')) { | |
$baseURL = [uri]$response.ResponseUri | |
$startURL = $baseURL.Scheme + '://'+ $baseURL.Host + $response.Headers['Location'] | |
} elseif ($response.Headers['Location'].StartsWith('http')) { | |
$startURL = $response.Headers['Location'] | |
} else { | |
$baseURL = [uri]$response.ResponseUri | |
$startURL = $baseURL.Scheme + '://'+ $baseURL.Host + $baseURL.AbsolutePath + $response.Headers['Location'] | |
} | |
break | |
} | |
304 { | |
$result = new-object -type psobject -prop ([ordered]@{ | |
requestURL=$startURL | |
statusCode=$response.StatusCode.value__ | |
status= $response.StatusDescription | |
location=$response.Headers['Location'] | |
}) | |
if ($response.Headers['Location'].StartsWith('/')) { | |
$baseURL = [uri]$response.ResponseUri | |
$startURL = $baseURL.Scheme + '://'+ $baseURL.Host + $response.Headers['Location'] | |
} elseif ($response.Headers['Location'].StartsWith('http')) { | |
$startURL = $response.Headers['Location'] | |
} else { | |
$baseURL = [uri]$response.ResponseUri | |
$startURL = $baseURL.Scheme + '://'+ $baseURL.Host + $baseURL.AbsolutePath + $response.Headers['Location'] | |
} | |
break | |
} | |
404 { | |
$result = new-object -type psobject -prop ([ordered]@{ | |
requestURL=$startURL | |
statusCode=$response.StatusCode.value__ | |
status= $response.StatusDescription | |
location=$startURL | |
}) | |
$startURL = '' | |
break | |
} | |
200 { | |
$result = new-object -type psobject -prop ([ordered]@{ | |
requestURL=$response.ResponseUri | |
statusCode=$response.StatusCode.value__ | |
status= $response.StatusDescription | |
location=$response.ResponseUri | |
}) | |
$startURL = '' | |
break | |
} | |
default { | |
$result = new-object -type psobject -prop ([ordered]@{ | |
requestURL=$startURL | |
statusCode=$response.StatusCode.value__ | |
status= $response.StatusDescription | |
location=$response.ResponseUri | |
}) | |
$startURL = '' | |
break | |
} | |
} | |
$locationlist += $response.Headers['Location'] | |
if ($showall) { | |
write-output $result | |
$result = $null | |
} | |
} | |
if ($result) { write-output $result } | |
$locationlist = @() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment