-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(gh-181) Add Chocolatey helper module
Add Chocolatey helper module as a placeholder for utility funcitons to assist in the prodcution of Chocolatey packages. In the first instance a helper function to retrieve a redirected url has been added.
- Loading branch information
1 parent
7b66886
commit 081afcf
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
59 changes: 59 additions & 0 deletions
59
scripts/chocolatey-helpers/functions/Get-RedirectedUri.ps1
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
Function Get-RedirectedUri() { | ||
<# | ||
.SYNOPSIS | ||
Get the uri that is being redirected to when using the supplied uri parameter. | ||
.DESCRIPTION | ||
Get-RedirectedUri is a Powershell function to obtain the URI that is being redirected to from the supplied URI. | ||
The URI to obtain the redirection for is specified using the Uri parameter. The redirected URI will be returned | ||
or the supplied URI if no redirection has taken place. | ||
.PARAMETER $Uri | ||
The URI to obtain the redirection for | ||
.EXAMPLE | ||
PS C:\> Get-RedirectedUri -Uri 'https://download.mozilla.org/?product=firefox-latest&os=win64' | ||
Obtain the redirected URI for the Windows 64-bit Mozilla Firefox download. | ||
#> | ||
[CmdletBinding()] | ||
Param( | ||
[parameter(Position = 0, | ||
Mandatory = $true, | ||
ValueFromPipeline = $true, | ||
HelpMessage = 'The Uri to obtain the redirection for')] | ||
[ValidateNotNullOrEmpty()] | ||
[String] | ||
$Uri | ||
) | ||
process { | ||
do { | ||
try { | ||
|
||
Write-Verbose('Attempting to retrieve URL redirect for ' + $Uri) | ||
$response = Invoke-WebRequest -Method Head -Uri $Uri | ||
|
||
if ($null -ne $response.BaseResponse.ResponseUri) { | ||
Write-Verbose('Extracting redirected URI from BaseResponse') | ||
$redirectUri = $response.BaseResponse.ResponseUri.AbsoluteUri | ||
} elseif ($null -ne $response.BaseResponse.RequestMessage.RequestUri) { | ||
Write-Verbose('Extracting redirected URI from RequestMessage') | ||
$redirectUri = $response.BaseResponse.RequestMessage.RequestUri.AbsoluteUri | ||
Write-Verbose('Redirected URL ' + $redirectUrl) | ||
} | ||
$retry = $false | ||
} catch { | ||
if (($_.Exception.GetType() -match 'HttpResponseException') -and ($_.Exception -match '302')) { | ||
$Uri = $_.Exception.Response.Headers.Location.AbsoluteUri | ||
$retry = $true | ||
} else { | ||
throw $_ | ||
} | ||
} | ||
} while ($retry) | ||
|
||
Write-Verbose('Returning redirected URL ' + $redirectUrl) | ||
$redirectUri | ||
} | ||
} |