Skip to content

Commit 003d81f

Browse files
committed
perf: pass $wc into Get-Encoding function
1 parent 6d03cbf commit 003d81f

7 files changed

Lines changed: 24 additions & 25 deletions

File tree

bin/checkver.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ $Queue | ForEach-Object {
113113
} else {
114114
$wc.Headers.Add('User-Agent', (Get-UserAgent))
115115
}
116-
Register-ObjectEvent $wc downloadstringcompleted -ErrorAction Stop | Out-Null
116+
Register-ObjectEvent $wc downloadDataCompleted -ErrorAction Stop | Out-Null
117117

118118
$githubRegex = '\/releases\/tag\/(?:v|V)?([\d.]+)'
119119

@@ -190,8 +190,8 @@ $Queue | ForEach-Object {
190190
}
191191

192192
$wc.Headers.Add('Referer', (strip_filename $url))
193-
$wc.Encoding = Get-Encoding($url)
194-
$wc.DownloadStringAsync($url, $state)
193+
$data = $wc.DownloadDataAsync($url, $state)
194+
(Get-Encoding($wc)).GetString($data)
195195
}
196196

197197
function next($er) {

bin/describe.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ $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)
48-
$home_html = $wc.DownloadString($manifest.homepage)
47+
$data = $wc.DownloadData($manifest.homepage)
48+
$home_html = (Get-Encoding($wc)).GetString($data)
4949
} catch {
5050
Write-Host "`n$($_.Exception.Message)" -ForegroundColor Red
5151
return

lib/autoupdate.ps1

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
# Must included with 'json.ps1'
22
function find_hash_in_rdf([String] $url, [String] $basename) {
3-
$data = $null
3+
$xml = $null
44
try {
55
# Download and parse RDF XML file
66
$wc = New-Object Net.Webclient
77
$wc.Headers.Add('Referer', (strip_filename $url))
88
$wc.Headers.Add('User-Agent', (Get-UserAgent))
9-
$wc.Encoding = Get-Encoding($url)
10-
[xml]$data = $wc.downloadstring($url)
9+
$data = $wc.DownloadData($url)
10+
[xml]$xml = (Get-Encoding($wc)).GetString($data)
1111
} catch [system.net.webexception] {
1212
write-host -f darkred $_
1313
write-host -f darkred "URL $url is not valid"
1414
return $null
1515
}
1616

1717
# Find file content
18-
$digest = $data.RDF.Content | Where-Object { [String]$_.about -eq $basename }
18+
$digest = $xml.RDF.Content | Where-Object { [String]$_.about -eq $basename }
1919

2020
return format_hash $digest.sha256
2121
}
@@ -36,8 +36,8 @@ function find_hash_in_textfile([String] $url, [Hashtable] $substitutions, [Strin
3636
$wc = New-Object Net.Webclient
3737
$wc.Headers.Add('Referer', (strip_filename $url))
3838
$wc.Headers.Add('User-Agent', (Get-UserAgent))
39-
$wc.Encoding = Get-Encoding($url)
40-
$hashfile = $wc.downloadstring($url)
39+
$data = $wc.DownloadData($url)
40+
$hashfile = (Get-Encoding($wc)).GetString($data)
4141
} catch [system.net.webexception] {
4242
write-host -f darkred $_
4343
write-host -f darkred "URL $url is not valid"
@@ -90,8 +90,8 @@ function find_hash_in_json([String] $url, [Hashtable] $substitutions, [String] $
9090
$wc = New-Object Net.Webclient
9191
$wc.Headers.Add('Referer', (strip_filename $url))
9292
$wc.Headers.Add('User-Agent', (Get-UserAgent))
93-
$wc.Encoding = Get-Encoding($url)
94-
$json = $wc.downloadstring($url)
93+
$data = $wc.DownloadData($url)
94+
$json = (Get-Encoding($wc)).GetString($data)
9595
} catch [system.net.webexception] {
9696
write-host -f darkred $_
9797
write-host -f darkred "URL $url is not valid"
@@ -111,8 +111,8 @@ function find_hash_in_xml([String] $url, [Hashtable] $substitutions, [String] $x
111111
$wc = New-Object Net.Webclient
112112
$wc.Headers.Add('Referer', (strip_filename $url))
113113
$wc.Headers.Add('User-Agent', (Get-UserAgent))
114-
$wc.Encoding = Get-Encoding($url)
115-
$xml = [xml]$wc.downloadstring($url)
114+
$data = $wc.DownloadData($url)
115+
$xml = [xml]((Get-Encoding($wc)).GetString($data))
116116
} catch [system.net.webexception] {
117117
write-host -f darkred $_
118118
write-host -f darkred "URL $url is not valid"

lib/core.ps1

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ function Optimize-SecurityProtocol {
1515
}
1616
}
1717

18-
function Get-Encoding($url) {
19-
$rawData = $wc.downloadData($url)
18+
function Get-Encoding($wc) {
2019
if($wc.ResponseHeaders["Content-Type"] -match 'charset=([^;]*)') {
2120
return [System.Text.Encoding]::GetEncoding($Matches[1])
2221
} else {

lib/description.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ 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)
22-
$html = $wc.downloadstring($refresh)
21+
$data = $wc.DownloadData($refresh)
22+
$html = (Get-Encoding($wc)).GetString($data)
2323
return find_description $refresh $html $true
2424
}
2525

lib/manifest.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ function url_manifest($url) {
1212
try {
1313
$wc = New-Object Net.Webclient
1414
$wc.Headers.Add('User-Agent', (Get-UserAgent))
15-
$wc.Encoding = Get-Encoding($url)
16-
$str = $wc.downloadstring($url)
15+
$data = $wc.DownloadData($url)
16+
$str = (Get-Encoding($wc)).GetString($data)
1717
} catch [system.management.automation.methodinvocationexception] {
1818
warn "error: $($_.exception.innerexception.message)"
1919
} catch {
@@ -32,8 +32,8 @@ function save_installed_manifest($app, $bucket, $dir, $url) {
3232
if($url) {
3333
$wc = New-Object Net.Webclient
3434
$wc.Headers.Add('User-Agent', (Get-UserAgent))
35-
$wc.Encoding = Get-Encoding($url)
36-
$wc.downloadstring($url) > "$dir\manifest.json"
35+
$data = $wc.DownloadData($url)
36+
(Get-Encoding($wc)).GetString($data) > "$dir\manifest.json"
3737
} else {
3838
Copy-Item (manifest_path $app $bucket) "$dir\manifest.json"
3939
}

libexec/scoop-virustotal.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ Function Get-VirusTotalResult($hash, $app) {
8585
$url = "https://www.virustotal.com/ui/files/$hash"
8686
$wc = New-Object Net.Webclient
8787
$wc.Headers.Add('User-Agent', (Get-UserAgent))
88-
$wc.Encoding = Get-Encoding($url)
89-
$result = $wc.downloadstring($url)
88+
$data = $wc.DownloadData($url)
89+
$result = (Get-Encoding($wc)).GetString($data)
9090
$stats = json_path $result '$.data.attributes.last_analysis_stats'
9191
$malicious = json_path $stats '$.malicious'
9292
$suspicious = json_path $stats '$.suspicious'

0 commit comments

Comments
 (0)