Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
7 changes: 6 additions & 1 deletion src/chocolatey.resources/helpers/functions/Get-WebFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,12 @@ Get-WebFileName
$req.UserAgent = $options.headers.$key
}
Default {
$req.Headers.Add($key, $options.headers.$key)
if ([System.Net.WebHeaderCollection]::IsRestricted($key)) {
Write-Warning "Skipping restricted header `'$key`' not currently supported by Chocolatey"
}
else {
$req.Headers.Add($key, $options.headers.$key)
}
}
}
}
Expand Down
36 changes: 34 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,38 @@ 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 {
if ([System.Net.WebHeaderCollection]::IsRestricted($key)) {
Write-Warning "Skipping restricted header `'$key`' not currently supported by Chocolatey"
}
else {
# 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