From 98cf8ae4daf8e41249709cd43e66d41b8740248a Mon Sep 17 00:00:00 2001 From: Hsiao-nan Cheung Date: Wed, 17 Apr 2024 22:29:20 +0800 Subject: [PATCH] fix(autoupdate): Fix bug that 'WebClient' doesn't auto-extract 'gzip' (#5901) --- CHANGELOG.md | 1 + bin/checkver.ps1 | 9 ++++++++- lib/autoupdate.ps1 | 24 +++++++++++++++++++++--- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4955a93f7f..fcb7d9adeb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,7 @@ - **scoop-virustotal:** Fix the issue that escape character not available in PowerShell 5.1 ([#5870](https://github.com/ScoopInstaller/Scoop/issues/5870)) - **decompress:** Use `wix.exe` in WiX Toolset v4+ as primary extractor of `Expand-DarkArchive()` ([#5871](https://github.com/ScoopInstaller/Scoop/issues/5871)) - **shim:** Run JAR file from app's root directory ([#5872](https://github.com/ScoopInstaller/Scoop/issues/5872)) +- **autoupdate:** Fix bug that 'WebClient' doesn't auto-extract 'gzip' ([#5901](https://github.com/ScoopInstaller/Scoop/issues/5901)) ### Performance Improvements diff --git a/bin/checkver.ps1 b/bin/checkver.ps1 index e740208d99..07d954c671 100644 --- a/bin/checkver.ps1 +++ b/bin/checkver.ps1 @@ -260,6 +260,7 @@ while ($in_progress -gt 0) { $in_progress-- $state = $ev.SourceEventArgs.UserState + $result = $ev.SourceEventArgs.Result $app = $state.app $file = $state.file $json = $state.json @@ -285,7 +286,13 @@ while ($in_progress -gt 0) { } if ($url) { - $page = (Get-Encoding($wc)).GetString($ev.SourceEventArgs.Result) + $ms = New-Object System.IO.MemoryStream + $ms.Write($result, 0, $result.Length) + $ms.Seek(0, 0) | Out-Null + if ($result[0] -eq 0x1F -and $result[1] -eq 0x8B) { + $ms = New-Object System.IO.Compression.GZipStream($ms, [System.IO.Compression.CompressionMode]::Decompress) + } + $page = (New-Object System.IO.StreamReader($ms, (Get-Encoding $wc))).ReadToEnd() } if ($script) { $page = Invoke-Command ([scriptblock]::Create($script -join "`r`n")) diff --git a/lib/autoupdate.ps1 b/lib/autoupdate.ps1 index c07fe6ba8a..bd24e04015 100644 --- a/lib/autoupdate.ps1 +++ b/lib/autoupdate.ps1 @@ -37,7 +37,13 @@ function find_hash_in_textfile([String] $url, [Hashtable] $substitutions, [Strin $wc.Headers.Add('Referer', (strip_filename $url)) $wc.Headers.Add('User-Agent', (Get-UserAgent)) $data = $wc.DownloadData($url) - $hashfile = (Get-Encoding($wc)).GetString($data) + $ms = New-Object System.IO.MemoryStream + $ms.Write($data, 0, $data.Length) + $ms.Seek(0, 0) | Out-Null + if ($data[0] -eq 0x1F -and $data[1] -eq 0x8B) { + $ms = New-Object System.IO.Compression.GZipStream($ms, [System.IO.Compression.CompressionMode]::Decompress) + } + $hashfile = (New-Object System.IO.StreamReader($ms, (Get-Encoding $wc))).ReadToEnd() } catch [system.net.webexception] { Write-Host $_ -ForegroundColor DarkRed Write-Host "URL $url is not valid" -ForegroundColor DarkRed @@ -93,7 +99,13 @@ function find_hash_in_json([String] $url, [Hashtable] $substitutions, [String] $ $wc.Headers.Add('Referer', (strip_filename $url)) $wc.Headers.Add('User-Agent', (Get-UserAgent)) $data = $wc.DownloadData($url) - $json = (Get-Encoding($wc)).GetString($data) + $ms = New-Object System.IO.MemoryStream + $ms.Write($data, 0, $data.Length) + $ms.Seek(0, 0) | Out-Null + if ($data[0] -eq 0x1F -and $data[1] -eq 0x8B) { + $ms = New-Object System.IO.Compression.GZipStream($ms, [System.IO.Compression.CompressionMode]::Decompress) + } + $json = (New-Object System.IO.StreamReader($ms, (Get-Encoding $wc))).ReadToEnd() } catch [System.Net.WebException] { Write-Host $_ -ForegroundColor DarkRed Write-Host "URL $url is not valid" -ForegroundColor DarkRed @@ -115,7 +127,13 @@ function find_hash_in_xml([String] $url, [Hashtable] $substitutions, [String] $x $wc.Headers.Add('Referer', (strip_filename $url)) $wc.Headers.Add('User-Agent', (Get-UserAgent)) $data = $wc.DownloadData($url) - $xml = [xml]((Get-Encoding($wc)).GetString($data)) + $ms = New-Object System.IO.MemoryStream + $ms.Write($data, 0, $data.Length) + $ms.Seek(0, 0) | Out-Null + if ($data[0] -eq 0x1F -and $data[1] -eq 0x8B) { + $ms = New-Object System.IO.Compression.GZipStream($ms, [System.IO.Compression.CompressionMode]::Decompress) + } + $xml = [xml]((New-Object System.IO.StreamReader($ms, (Get-Encoding $wc))).ReadToEnd()) } catch [system.net.webexception] { Write-Host $_ -ForegroundColor DarkRed Write-Host "URL $url is not valid" -ForegroundColor DarkRed