diff --git a/src/chocolatey.resources/helpers/functions/Get-ChocolateyWebFile.ps1 b/src/chocolatey.resources/helpers/functions/Get-ChocolateyWebFile.ps1 index 0b3fb35575..a5d6e7ec0e 100644 --- a/src/chocolatey.resources/helpers/functions/Get-ChocolateyWebFile.ps1 +++ b/src/chocolatey.resources/helpers/functions/Get-ChocolateyWebFile.ps1 @@ -301,7 +301,7 @@ Get-FtpFile $fileFullPath = $fileFullPath -replace '\\chocolatey\\chocolatey\\', '\chocolatey\' $fileDirectory = [System.IO.Path]::GetDirectoryName($fileFullPath) $originalFileName = [System.IO.Path]::GetFileName($fileFullPath) - $fileFullPath = Get-WebFileName -Url $url -DefaultName $originalFileName + $fileFullPath = Get-WebFileName -Url $url -DefaultName $originalFileName -Options $options $fileFullPath = Join-Path $fileDirectory $fileFullPath $fileFullPath = [System.IO.Path]::GetFullPath($fileFullPath) } @@ -324,7 +324,7 @@ Get-FtpFile $headers = @{} if ($url.StartsWith('http')) { try { - $headers = Get-WebHeaders -Url $url -ErrorAction "Stop" + $headers = Get-WebHeaders -Url $url -ErrorAction "Stop" -Options $options } catch { if ($PSVersionTable.PSVersion -lt (New-Object 'Version' 3, 0)) { @@ -333,7 +333,7 @@ Get-FtpFile $originalProtocol = [System.Net.ServicePointManager]::SecurityProtocol [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Ssl3 try { - $headers = Get-WebHeaders -Url $url -ErrorAction "Stop" + $headers = Get-WebHeaders -Url $url -ErrorAction "Stop" -Options $options } catch { Write-Host "Attempt to get headers for $url failed.`n $($_.Exception.Message)" diff --git a/src/chocolatey.resources/helpers/functions/Get-WebHeaders.ps1 b/src/chocolatey.resources/helpers/functions/Get-WebHeaders.ps1 index 8fd96503bf..f33fd53e64 100644 --- a/src/chocolatey.resources/helpers/functions/Get-WebHeaders.ps1 +++ b/src/chocolatey.resources/helpers/functions/Get-WebHeaders.ps1 @@ -52,6 +52,7 @@ Get-WebFile param( [parameter(Mandatory = $false, Position = 0)][string] $url = '', [parameter(Mandatory = $false, Position = 1)][string] $userAgent = 'chocolatey command line', + [parameter(Mandatory = $false, Position = 2)][hashtable] $options = @{Headers = @{} }, [parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments ) @@ -61,7 +62,7 @@ Get-WebFile return @{} } - $request = [System.Net.HttpWebRequest]::Create($url); + [System.Net.HttpWebRequest] $request = [System.Net.HttpWebRequest]::Create($url); $defaultCreds = [System.Net.CredentialCache]::DefaultCredentials if ($defaultCreds -ne $null) { $request.Credentials = $defaultCreds @@ -128,7 +129,33 @@ Get-WebFile #http://stackoverflow.com/questions/518181/too-many-automatic-redirections-were-attempted-error-message-when-using-a-httpw $request.CookieContainer = New-Object System.Net.CookieContainer - if ($userAgent -ne $null) { + + if ($options.Headers -ne $null) { + foreach ($key in $options.Headers.Keys) { + # https://learn.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.headers?view=net-8.0#remarks + switch ($key.ToLower()) { + 'accept' { $request.Accept = $options.Headers[$key] } + 'connection' { } + 'content-length' { $request.ContentLength = $options.Headers[$key] } + 'content-type' { $request.ContentType = $options.Headers[$key] } + 'expect' { $request.Expect = $options.Headers[$key] } + 'date' { $request.Date = $options.Headers[$key] } + 'host' { $request.Host = $options.Headers[$key] } + 'if-modified-since' { $request.IfModifiedSince = $options.Headers[$key] } + 'range' { } + 'referer' { $request.Referer = $options.Headers[$key] } + 'transfer-encoding' { } + 'user-agent' { $request.UserAgent = $options.Headers[$key] } + + Default { + # Only add headers that don't match a request property + $request.Headers.Add($key, $options.Headers[$key]) + } + } + } + } + + if ($userAgent -ne $null -and $options.Headers['User-Agent'] -eq $null) { Write-Debug "Setting the UserAgent to `'$userAgent`'" $request.UserAgent = $userAgent }