From 4c4ebc8820a8fb316dbae7e3cf7c941da6989898 Mon Sep 17 00:00:00 2001 From: Stephen Valdinger Date: Thu, 6 Feb 2020 16:54:27 -0500 Subject: [PATCH 1/3] (GH-1998) Fix authorization header * Reworks headers to not use GetEnumerator() method, and appropriately adds headers to `$options` hashtable. --- .../helpers/functions/Get-WebFile.ps1 | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 b/src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 index 9a33819119..71670a6732 100644 --- a/src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 +++ b/src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 @@ -177,17 +177,16 @@ param( $req.UserAgent = $userAgent } - if ($options.Headers.Count -gt 0) { + 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){ + 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.Autorization = $options.headers.$key} + Default {$req.Headers.Add($key,$options.headers.$key)} } } } @@ -209,7 +208,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 +314,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 +330,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() } From 827e3ec2fc4424ad55b64175cc399d99b5fd3131 Mon Sep 17 00:00:00 2001 From: Stephen Valdinger Date: Thu, 6 Feb 2020 21:05:20 -0500 Subject: [PATCH 2/3] (GH-1998) Add back $uri in Foreach loop --- src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 b/src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 index 71670a6732..9a8ed71642 100644 --- a/src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 +++ b/src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 @@ -180,6 +180,7 @@ param( if($options.Headers.Count -gt 0){ Write-Debug "Setting custom headers" foreach($key in $options.headers.keys){ + $uri = (New-Object sysetem.uri $url) switch($key){ 'Accept' {$req.Accept = $options.headers.$key} 'Cookie' {$req.CookieContainer.SetCookies($uri,$options.headers.$key)} From 883eb940a16747f50af39cb6ba4bbaa928f8b544 Mon Sep 17 00:00:00 2001 From: Stephen Valdinger Date: Fri, 7 Feb 2020 09:15:45 -0500 Subject: [PATCH 3/3] (GH-1998) Fix typo --- src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 b/src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 index 9a8ed71642..275276f7b2 100644 --- a/src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 +++ b/src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 @@ -180,7 +180,7 @@ param( if($options.Headers.Count -gt 0){ Write-Debug "Setting custom headers" foreach($key in $options.headers.keys){ - $uri = (New-Object sysetem.uri $url) + $uri = (New-Object -Typename system.uri $url) switch($key){ 'Accept' {$req.Accept = $options.headers.$key} 'Cookie' {$req.CookieContainer.SetCookies($uri,$options.headers.$key)}