Skip to content

Commit ac79116

Browse files
committed
fix(core): add Get-Encoding function to fix missing webClient encoding ([ScoopInstaller#4956](ScoopInstaller#4956))
Add Get-Encoding function in core.ps1 close ScoopInstaller#4911
1 parent 0f6d012 commit ac79116

8 files changed

Lines changed: 25 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- **core:** Using `Invoke-Command` instead of `Invoke-Expression` ([#4941](https://github.com/ScoopInstaller/Scoop/issues/4941))
77
- **core:** Load config file before initialization ([#4932](https://github.com/ScoopInstaller/Scoop/issues/4932))
88
- **core:** Allow to use '_' and '.' in bucket name ([#4952](https://github.com/ScoopInstaller/Scoop/pull/4952))
9+
- **core:** Add `Get-Encoding` function to fix missing webclient encoding ([#4956](https://github.com/ScoopInstaller/Scoop/pull/4956))
910
- **depends:** Avoid digits in archive file extension (except for .7z and .001) ([#4915](https://github.com/ScoopInstaller/Scoop/issues/4915))
1011
- **bucket:** Don't check remote URL of non-git buckets ([#4923](https://github.com/ScoopInstaller/Scoop/issues/4923))
1112
- **bucket:** Don't write message OK before bucket is cloned ([#4925](https://github.com/ScoopInstaller/Scoop/issues/4925))

bin/checkver.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ $Queue | ForEach-Object {
190190
}
191191

192192
$wc.Headers.Add('Referer', (strip_filename $url))
193+
$wc.Encoding = Get-Encoding($url)
193194
$wc.DownloadStringAsync($url, $state)
194195
}
195196

bin/describe.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ $Queue | ForEach-Object {
4444
try {
4545
$wc = New-Object Net.Webclient
4646
$wc.Headers.Add('User-Agent', (Get-UserAgent))
47+
$wc.Encoding = Get-Encoding($url)
4748
$home_html = $wc.DownloadString($manifest.homepage)
4849
} catch {
4950
Write-Host "`n$($_.Exception.Message)" -ForegroundColor Red

lib/autoupdate.ps1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
# Must included with 'json.ps1'
2+
3+
. "$PSScriptRoot\..\lib\core.ps1"
4+
25
function find_hash_in_rdf([String] $url, [String] $basename) {
36
$data = $null
47
try {
58
# Download and parse RDF XML file
69
$wc = New-Object Net.Webclient
710
$wc.Headers.Add('Referer', (strip_filename $url))
811
$wc.Headers.Add('User-Agent', (Get-UserAgent))
12+
$wc.Encoding = Get-Encoding($url)
913
[xml]$data = $wc.downloadstring($url)
1014
} catch [system.net.webexception] {
1115
write-host -f darkred $_
@@ -35,6 +39,7 @@ function find_hash_in_textfile([String] $url, [Hashtable] $substitutions, [Strin
3539
$wc = New-Object Net.Webclient
3640
$wc.Headers.Add('Referer', (strip_filename $url))
3741
$wc.Headers.Add('User-Agent', (Get-UserAgent))
42+
$wc.Encoding = Get-Encoding($url)
3843
$hashfile = $wc.downloadstring($url)
3944
} catch [system.net.webexception] {
4045
write-host -f darkred $_
@@ -88,6 +93,7 @@ function find_hash_in_json([String] $url, [Hashtable] $substitutions, [String] $
8893
$wc = New-Object Net.Webclient
8994
$wc.Headers.Add('Referer', (strip_filename $url))
9095
$wc.Headers.Add('User-Agent', (Get-UserAgent))
96+
$wc.Encoding = Get-Encoding($url)
9197
$json = $wc.downloadstring($url)
9298
} catch [system.net.webexception] {
9399
write-host -f darkred $_
@@ -108,6 +114,7 @@ function find_hash_in_xml([String] $url, [Hashtable] $substitutions, [String] $x
108114
$wc = New-Object Net.Webclient
109115
$wc.Headers.Add('Referer', (strip_filename $url))
110116
$wc.Headers.Add('User-Agent', (Get-UserAgent))
117+
$wc.Encoding = Get-Encoding($url)
111118
$xml = [xml]$wc.downloadstring($url)
112119
} catch [system.net.webexception] {
113120
write-host -f darkred $_

lib/core.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ function Optimize-SecurityProtocol {
1515
}
1616
}
1717

18+
function Get-Encoding($url) {
19+
$rawData = $wc.downloadData($url)
20+
if($wc.ResponseHeaders["Content-Type"] -match 'charset=([^;]*)') {
21+
return [System.Text.Encoding]::GetEncoding($Matches[1])
22+
} else {
23+
return [System.Text.Encoding]::GetEncoding('utf-8')
24+
}
25+
}
26+
1827
function Get-UserAgent() {
1928
return "Scoop/1.0 (+http://scoop.sh/) PowerShell/$($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor) (Windows NT $([System.Environment]::OSVersion.Version.Major).$([System.Environment]::OSVersion.Version.Minor); $(if($env:PROCESSOR_ARCHITECTURE -eq 'AMD64'){'Win64; x64; '})$(if($env:PROCESSOR_ARCHITEW6432 -eq 'AMD64'){'WOW64; '})$PSEdition)"
2029
}

lib/description.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function find_description($url, $html, $redir = $false) {
1818
if($refresh -and !$redir) {
1919
$wc = New-Object Net.Webclient
2020
$wc.Headers.Add('User-Agent', (Get-UserAgent))
21+
$wc.Encoding = Get-Encoding($refresh)
2122
$html = $wc.downloadstring($refresh)
2223
return find_description $refresh $html $true
2324
}

lib/manifest.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
. "$PSScriptRoot\..\lib\core.ps1"
12
function manifest_path($app, $bucket) {
23
fullpath "$(Find-BucketDirectory $bucket)\$(sanitary_path $app).json"
34
}
@@ -12,6 +13,7 @@ function url_manifest($url) {
1213
try {
1314
$wc = New-Object Net.Webclient
1415
$wc.Headers.Add('User-Agent', (Get-UserAgent))
16+
$wc.Encoding = Get-Encoding($url)
1517
$str = $wc.downloadstring($url)
1618
} catch [system.management.automation.methodinvocationexception] {
1719
warn "error: $($_.exception.innerexception.message)"
@@ -31,6 +33,7 @@ function save_installed_manifest($app, $bucket, $dir, $url) {
3133
if($url) {
3234
$wc = New-Object Net.Webclient
3335
$wc.Headers.Add('User-Agent', (Get-UserAgent))
36+
$wc.Encoding = Get-Encoding($url)
3437
$wc.downloadstring($url) > "$dir\manifest.json"
3538
} else {
3639
Copy-Item (manifest_path $app $bucket) "$dir\manifest.json"

libexec/scoop-virustotal.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
# -n, --no-depends By default, all dependencies are checked too. This flag avoids it.
3535
# -u, --no-update-scoop Don't update Scoop before checking if it's outdated
3636

37+
. "$PSScriptRoot\..\lib\core.ps1"
3738
. "$PSScriptRoot\..\lib\getopt.ps1"
3839
. "$PSScriptRoot\..\lib\manifest.ps1" # 'Find-Manifest' (indirectly)
3940
. "$PSScriptRoot\..\lib\json.ps1" # 'json_path'
@@ -85,6 +86,7 @@ Function Get-VirusTotalResult($hash, $app) {
8586
$url = "https://www.virustotal.com/ui/files/$hash"
8687
$wc = New-Object Net.Webclient
8788
$wc.Headers.Add('User-Agent', (Get-UserAgent))
89+
$wc.Encoding = Get-Encoding($url)
8890
$result = $wc.downloadstring($url)
8991
$stats = json_path $result '$.data.attributes.last_analysis_stats'
9092
$malicious = json_path $stats '$.malicious'

0 commit comments

Comments
 (0)