Skip to content

Commit

Permalink
(#3191) Pass options to Get-WebHeaders
Browse files Browse the repository at this point in the history
Fixes #3191
  • Loading branch information
flcdrg committed Sep 15, 2024
1 parent dd1de8f commit e9e2702
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)) {
Expand All @@ -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)"
Expand Down
31 changes: 29 additions & 2 deletions src/chocolatey.resources/helpers/functions/Get-WebHeaders.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit e9e2702

Please sign in to comment.