Skip to content

Commit

Permalink
(gh-181) Add Chocolatey helper module
Browse files Browse the repository at this point in the history
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
dgalbraith committed Jun 7, 2021
1 parent 7b66886 commit 081afcf
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
Binary file added scripts/chocolatey-helpers/Chocolatey-Helpers.psd1
Binary file not shown.
59 changes: 59 additions & 0 deletions scripts/chocolatey-helpers/functions/Get-RedirectedUri.ps1
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
}
}

0 comments on commit 081afcf

Please sign in to comment.