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 @@ -269,7 +269,7 @@ param(
if ($url.StartsWith('http:')) {
try {
$httpsUrl = $url.Replace("http://", "https://")
Get-WebHeaders -Url $httpsUrl -ErrorAction "Stop" | Out-Null
Get-WebHeaders -Url $httpsUrl -ErrorAction "Stop" -Options $options | Out-Null
$url = $httpsUrl
Write-Warning "Url has SSL/TLS available, switching to HTTPS for download"
} catch {
Expand Down Expand Up @@ -303,15 +303,15 @@ param(
$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)) {
Write-Debug "Converting Security Protocol to SSL3 only for Powershell v2"
# this should last for the entire duration
$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)"
[System.Net.ServicePointManager]::SecurityProtocol = $originalProtocol
Expand Down
28 changes: 25 additions & 3 deletions src/chocolatey.resources/helpers/functions/Get-WebHeaders.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ command line'.
.PARAMETER IgnoredArguments
Allows splatting with arguments that do not apply. Do not use directly.

.PARAMETER Options
Specify request headers. Please note that UserAgent function parameter, if specified, overwrite header present in this parameter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also specify the first version this was available in. I would venture a guess that it is 0.10.13. At least include that here (search other parameters for examples of this and keep it consistent.). Thanks!

.LINK
Get-ChocolateyWebFile

Expand All @@ -54,8 +57,9 @@ Get-WebFile
#>
param(
[parameter(Mandatory=$false, Position=0)][string] $url = '',
[parameter(Mandatory=$false, Position=1)][string] $userAgent = 'chocolatey command line',
[parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments
[parameter(Mandatory=$false, Position=1)][string] $userAgent,
[parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments,
[parameter(Mandatory=$false)][hashtable] $options = @{Headers=@{}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be before $ignoredArguments. Those are the last items to be passed.

)

Write-FunctionCallLogMessage -Invocation $MyInvocation -Parameters $PSBoundParameters
Expand Down Expand Up @@ -127,7 +131,25 @@ param(

#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) {

#Mimic previous behaviour with default User-Agent
$request.UserAgent = 'chocolatey command line'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting please


if ($Options.Headers) {
$Options.Headers.Keys | % {
if ([System.Net.WebHeaderCollection]::IsRestricted($_)) {
$key = $_.Replace('-','')
$request.$key = $Options.Headers[$_]
}
else {
$request.Headers.add($_, $Options.Headers[$_])
}
}
}

#User-Agent, if specified explicitly as function parameter, should overwrite header from Headers hashtable
#`$userAgent` is typed parameter, when it's not passed value is '' (empty string)
if ($userAgent -ne '') {
Write-Debug "Setting the UserAgent to `'$userAgent`'"
$request.UserAgent = $userAgent
}
Expand Down