From 5580558c52c423f578a9a68d13e19e42161c5dcd Mon Sep 17 00:00:00 2001 From: Stephen Valdinger Date: Thu, 6 Feb 2020 16:54:27 -0500 Subject: [PATCH] (#1998) Add authorization header to Get-WebFile 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. --- .../helpers/functions/Get-WebFile.ps1 | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 b/src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 index 5bcb20c6cc..353172d2c7 100644 --- a/src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 +++ b/src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 @@ -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)} } } } @@ -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 @@ -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 @@ -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() }