Skip to content

Commit af8a9bc

Browse files
committed
(#3191) Pass options to Get-WebHeaders
Fixes #3191
1 parent a7f72e0 commit af8a9bc

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/chocolatey.resources/helpers/functions/Get-ChocolateyWebFile.ps1

+3-3
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ Get-FtpFile
301301
$fileFullPath = $fileFullPath -replace '\\chocolatey\\chocolatey\\', '\chocolatey\'
302302
$fileDirectory = [System.IO.Path]::GetDirectoryName($fileFullPath)
303303
$originalFileName = [System.IO.Path]::GetFileName($fileFullPath)
304-
$fileFullPath = Get-WebFileName -Url $url -DefaultName $originalFileName
304+
$fileFullPath = Get-WebFileName -Url $url -DefaultName $originalFileName -Options $options
305305
$fileFullPath = Join-Path $fileDirectory $fileFullPath
306306
$fileFullPath = [System.IO.Path]::GetFullPath($fileFullPath)
307307
}
@@ -324,7 +324,7 @@ Get-FtpFile
324324
$headers = @{}
325325
if ($url.StartsWith('http')) {
326326
try {
327-
$headers = Get-WebHeaders -Url $url -ErrorAction "Stop"
327+
$headers = Get-WebHeaders -Url $url -ErrorAction "Stop" -Options $options
328328
}
329329
catch {
330330
if ($PSVersionTable.PSVersion -lt (New-Object 'Version' 3, 0)) {
@@ -333,7 +333,7 @@ Get-FtpFile
333333
$originalProtocol = [System.Net.ServicePointManager]::SecurityProtocol
334334
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Ssl3
335335
try {
336-
$headers = Get-WebHeaders -Url $url -ErrorAction "Stop"
336+
$headers = Get-WebHeaders -Url $url -ErrorAction "Stop" -Options $options
337337
}
338338
catch {
339339
Write-Host "Attempt to get headers for $url failed.`n $($_.Exception.Message)"

src/chocolatey.resources/helpers/functions/Get-WebHeaders.ps1

+29-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Get-WebFile
5252
param(
5353
[parameter(Mandatory = $false, Position = 0)][string] $url = '',
5454
[parameter(Mandatory = $false, Position = 1)][string] $userAgent = 'chocolatey command line',
55+
[parameter(Mandatory = $false, Position = 2)][hashtable] $options = @{Headers = @{} },
5556
[parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments
5657
)
5758

@@ -61,7 +62,7 @@ Get-WebFile
6162
return @{}
6263
}
6364

64-
$request = [System.Net.HttpWebRequest]::Create($url);
65+
[System.Net.HttpWebRequest] $request = [System.Net.HttpWebRequest]::Create($url);
6566
$defaultCreds = [System.Net.CredentialCache]::DefaultCredentials
6667
if ($defaultCreds -ne $null) {
6768
$request.Credentials = $defaultCreds
@@ -128,7 +129,33 @@ Get-WebFile
128129

129130
#http://stackoverflow.com/questions/518181/too-many-automatic-redirections-were-attempted-error-message-when-using-a-httpw
130131
$request.CookieContainer = New-Object System.Net.CookieContainer
131-
if ($userAgent -ne $null) {
132+
133+
if ($options.Headers -ne $null) {
134+
foreach ($key in $options.Headers.Keys) {
135+
# https://learn.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.headers?view=net-8.0#remarks
136+
switch ($key.ToLower()) {
137+
'accept' { $request.Accept = $options.Headers[$key] }
138+
'connection' { }
139+
'content-length' { $request.ContentLength = $options.Headers[$key] }
140+
'content-type' { $request.ContentType = $options.Headers[$key] }
141+
'expect' { $request.Expect = $options.Headers[$key] }
142+
'date' { $request.Date = $options.Headers[$key] }
143+
'host' { $request.Host = $options.Headers[$key] }
144+
'if-modified-since' { $request.IfModifiedSince = $options.Headers[$key] }
145+
'range' { }
146+
'referer' { $request.Referer = $options.Headers[$key] }
147+
'transfer-encoding' { }
148+
'user-agent' { $request.UserAgent = $options.Headers[$key] }
149+
150+
Default {
151+
# Only add headers that don't match a request property
152+
$request.Headers.Add($key, $options.Headers[$key])
153+
}
154+
}
155+
}
156+
}
157+
158+
if ($userAgent -ne $null -and $options.Headers['User-Agent'] -eq $null) {
132159
Write-Debug "Setting the UserAgent to `'$userAgent`'"
133160
$request.UserAgent = $userAgent
134161
}

0 commit comments

Comments
 (0)