From caf2b36854d99ab5345ab27eeda405540983350b Mon Sep 17 00:00:00 2001 From: robmoss2k <15984835+robmoss2k@users.noreply.github.com> Date: Tue, 17 Dec 2024 10:08:05 +0000 Subject: [PATCH 1/2] Refactor azcopy update script to handle URL redirection in PowerShell Core --- automatic/azcopy10/update.ps1 | 54 ++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/automatic/azcopy10/update.ps1 b/automatic/azcopy10/update.ps1 index 61efacfa8b5..040b3301e62 100644 --- a/automatic/azcopy10/update.ps1 +++ b/automatic/azcopy10/update.ps1 @@ -2,23 +2,36 @@ $releases = 'https://aka.ms/downloadazcopy-v10-windows' -function global:au_SearchReplace { - @{ - ".\tools\chocolateyInstall.ps1" = @{ - "(?i)(^\s*url64\s*=\s*)('.*')" = "`$1'$($Latest.URL64)'" - "(?i)(^\s*checksum64\s*=\s*)('.*')" = "`$1'$($Latest.Checksum64)'" - "(?i)(^\s*checksumType64\s*=\s*)('.*')" = "`$1'$($Latest.ChecksumType64)'" - "(?i)(^\s*url\s*=\s*)('.*')" = "`$1'$($Latest.URL32)'" - "(?i)(^\s*checksum\s*=\s*)('.*')" = "`$1'$($Latest.Checksum32)'" - "(?i)(^\s*checksumType\s*=\s*)('.*')" = "`$1'$($Latest.ChecksumType32)'" - } - } +# Define a function to handle the 301 redirection and retrieve headers +function Get-RedirectedUrl { + param ( + [string]$Url + ) + # Use .NET HttpClient for better control + $handler = New-Object System.Net.Http.HttpClientHandler + $handler.AllowAutoRedirect = $false # Prevent auto-following redirects + + $client = New-Object System.Net.Http.HttpClient($handler) + + try { + # Create a HEAD request + $request = [System.Net.Http.HttpRequestMessage]::new([System.Net.Http.HttpMethod]::Head, $Url) + $response = $client.SendAsync($request).Result + + if ($response.StatusCode -eq [System.Net.HttpStatusCode]::MovedPermanently -or ` + $response.StatusCode -eq [System.Net.HttpStatusCode]::Found) { + return $response.Headers.Location.AbsoluteUri + } else { + throw "Unexpected status code: $($response.StatusCode)" + } + } + finally { + $client.Dispose() + } } function global:au_GetLatest { - $download_page = Invoke-WebRequest -Uri $releases -MaximumRedirection 0 -ErrorAction SilentlyContinue - - $url64 = $download_page.Headers.Location + $url64 = Get-RedirectedUrl -Url $releases $url32 = $url64 -replace "amd64", "386" $version = $url64 -replace ".zip", "" -split "_" | Select-Object -Last 1 @@ -29,4 +42,17 @@ function global:au_GetLatest { } } +function global:au_SearchReplace { + @{ + ".\tools\chocolateyInstall.ps1" = @{ + "(?i)(^\s*url64\s*=\s*)('.*')" = "`$1'$($Latest.URL64)'" + "(?i)(^\s*checksum64\s*=\s*)('.*')" = "`$1'$($Latest.Checksum64)'" + "(?i)(^\s*checksumType64\s*=\s*)('.*')" = "`$1'$($Latest.ChecksumType64)'" + "(?i)(^\s*url\s*=\s*)('.*')" = "`$1'$($Latest.URL32)'" + "(?i)(^\s*checksum\s*=\s*)('.*')" = "`$1'$($Latest.Checksum32)'" + "(?i)(^\s*checksumType\s*=\s*)('.*')" = "`$1'$($Latest.ChecksumType32)'" + } + } +} + update -ChecksumFor all From eee96e55930cf94d272609dd9929d3d6a8b4b1ee Mon Sep 17 00:00:00 2001 From: robmoss2k <15984835+robmoss2k@users.noreply.github.com> Date: Tue, 17 Dec 2024 12:44:30 +0000 Subject: [PATCH 2/2] Remove redundant URL redirection function from azcopy update script --- automatic/azcopy10/update.ps1 | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/automatic/azcopy10/update.ps1 b/automatic/azcopy10/update.ps1 index 040b3301e62..03af12ac2d2 100644 --- a/automatic/azcopy10/update.ps1 +++ b/automatic/azcopy10/update.ps1 @@ -2,34 +2,6 @@ $releases = 'https://aka.ms/downloadazcopy-v10-windows' -# Define a function to handle the 301 redirection and retrieve headers -function Get-RedirectedUrl { - param ( - [string]$Url - ) - # Use .NET HttpClient for better control - $handler = New-Object System.Net.Http.HttpClientHandler - $handler.AllowAutoRedirect = $false # Prevent auto-following redirects - - $client = New-Object System.Net.Http.HttpClient($handler) - - try { - # Create a HEAD request - $request = [System.Net.Http.HttpRequestMessage]::new([System.Net.Http.HttpMethod]::Head, $Url) - $response = $client.SendAsync($request).Result - - if ($response.StatusCode -eq [System.Net.HttpStatusCode]::MovedPermanently -or ` - $response.StatusCode -eq [System.Net.HttpStatusCode]::Found) { - return $response.Headers.Location.AbsoluteUri - } else { - throw "Unexpected status code: $($response.StatusCode)" - } - } - finally { - $client.Dispose() - } -} - function global:au_GetLatest { $url64 = Get-RedirectedUrl -Url $releases $url32 = $url64 -replace "amd64", "386"