Skip to content

Commit

Permalink
(#1998) Add authorization header to Get-WebFile
Browse files Browse the repository at this point in the history
Passing authorization headers through Install-ChocolateyPackage does not
attach them to Get-WebFile downstream properly.

Reworks headers to not use GetEnumerator() method, and appropriately
adds headers to `$options` hashtable.
  • Loading branch information
steviecoaster authored and gep13 committed Apr 18, 2021
1 parent 2b00069 commit 5580558
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/chocolatey.resources/helpers/functions/Get-WebFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,15 @@ param(

if ($options.Headers.Count -gt 0) {
Write-Debug "Setting custom headers"
foreach ($item in $options.Headers.GetEnumerator()) {
$uri = (new-object system.uri $url)
Write-Debug($item.Key + ':' + $item.Value)
switch ($item.Key) {
'Accept' {$req.Accept = $item.Value}
'Cookie' {$req.CookieContainer.SetCookies($uri, $item.Value)}
'Referer' {$req.Referer = $item.Value}
'User-Agent' {$req.UserAgent = $item.Value}
Default {$req.Headers.Add($item.Key, $item.Value)}
foreach ($key in $options.headers.keys) {
$uri = (New-Object -Typename system.uri $url)
switch ($key) {
'Accept' {$req.Accept = $options.headers.$key}
'Cookie' {$req.CookieContainer.SetCookies($uri, $options.headers.$key)}
'Referer' {$req.Referer = $options.headers.$key}
'User-Agent' {$req.UserAgent = $options.headers.$key}
'Authorization' {$re.Authorization = $options.headers.$key}
Default {$req.Headers.Add($key, $options.headers.$key)}
}
}
}
Expand All @@ -209,7 +209,7 @@ param(

if ($headers.ContainsKey("Content-Type")) {
$contentType = $headers['Content-Type']
if ($contentType -ne $null) {
if ($null -ne $contentType) {
if ($contentType.ToLower().Contains("text/html") -or $contentType.ToLower().Contains("text/plain")) {
Write-Warning "$fileName is of content type $contentType"
Set-Content -Path $binaryIsTextCheckFile -Value "$fileName has content type $contentType" -Encoding UTF8 -Force
Expand Down Expand Up @@ -315,7 +315,7 @@ param(
}
}
} catch {
if ($req -ne $null) {
if ($null -ne $req) {
$req.ServicePoint.MaxIdleTime = 0
$req.Abort();
# ruthlessly remove $req to ensure it isn't reused
Expand All @@ -331,7 +331,7 @@ param(
throw "The remote file either doesn't exist, is unauthorized, or is forbidden for url '$url'. $($_.Exception.Message)"
}
} finally {
if ($res -ne $null) {
if ($null -ne $res) {
$res.Close()
}

Expand Down

0 comments on commit 5580558

Please sign in to comment.