Skip to content
Merged
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
42 changes: 42 additions & 0 deletions scripts/get-dotnetup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,37 @@ function Get-RuntimeId {

# --- Main ---

# Map a 'channel' such as 'daily' to specific version url for the binary and its .sha512 to prevent release race condition mismatches
function Resolve-FinalUrl([string]$Url) {
# Require an actual curl executable; on Windows PowerShell 5.1 'curl' is an alias for Invoke-WebRequest, so -CommandType Application excludes it.
$curl = Get-Command curl.exe -CommandType Application -ErrorAction SilentlyContinue
if (-not $curl) { $curl = Get-Command curl -CommandType Application -ErrorAction SilentlyContinue }
if ($curl) {
$sink = [System.IO.Path]::GetTempFileName()
try {
# --head resolves redirects without downloading the body.
$final = & $curl.Source --silent --show-error --location --head `
--output $sink --write-out '%{url_effective}' $Url 2>$null
if ($LASTEXITCODE -eq 0 -and $final) { return "$final".Trim() }
}
catch { }
finally { Remove-Item $sink -Force -ErrorAction SilentlyContinue }
}

# Fallback for hosts without a curl executable (e.g. Windows PowerShell 5.1):
try {
$req = [System.Net.WebRequest]::Create($Url)
$req.Method = "HEAD"
$req.AllowAutoRedirect = $true
$resp = $req.GetResponse()
try { return $resp.ResponseUri.AbsoluteUri }
finally { $resp.Dispose() }
}
catch {
return $null
}
}

$rid = Get-RuntimeId
Write-Host "Detected runtime: $rid" -ForegroundColor Cyan

Expand All @@ -148,6 +179,17 @@ $fileName = if ($rid -like "win-*") { "dotnetup-$rid.exe" } else { "dotnetup-$ri
$downloadUrl = "$BaseUrl/$fileName"
$checksumUrl = "$downloadUrl.sha512"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Any reason to be setting the checksum (and really the download too) URL here since they're just going to be overwritten?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is done in case curl/wget fails then we can still try to use the old link (hoping that they don't race)


$resolvedUrl = Resolve-FinalUrl $downloadUrl
if ($resolvedUrl -and $resolvedUrl -like "*/public/*") {
Write-Host "Resolved '$Quality' to concrete build: $resolvedUrl" -ForegroundColor DarkGray
$downloadUrl = $resolvedUrl
# Checksums live under the sibling 'public-checksums' path with a .sha512 suffix.
$checksumUrl = ($resolvedUrl -replace '/public/', '/public-checksums/') + ".sha512"
Comment on lines +186 to +187

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we want to hard-code this? I'm not sure if there's a way around it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I agree with the hesitancy to hard-code though don't see a way around it.

}
else {
Write-Host "Could not resolve '$Quality' shortlink to a concrete build; using shortlink URLs directly." -ForegroundColor DarkGray
}

$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) "dotnetup-install-$([System.IO.Path]::GetRandomFileName())"
New-Item -ItemType Directory -Path $tempDir -Force | Out-Null

Expand Down
26 changes: 26 additions & 0 deletions scripts/get-dotnetup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,32 @@ FILE_NAME="dotnetup-${RID}"
DOWNLOAD_URL="${BASE_URL}/${FILE_NAME}"
CHECKSUM_URL="${DOWNLOAD_URL}.sha512"

# Map a 'channel' such as 'daily' to specific version url for the binary and its .sha512 to prevent release race condition mismatches
resolve_final_url() {
local url="$1"
if [ "$DOWNLOADER" = "curl" ]; then
# --head resolves redirects without downloading the body.
curl --silent --show-error --location --head --output /dev/null \
--write-out '%{url_effective}' "$url" 2>/dev/null
elif [ "$DOWNLOADER" = "wget" ]; then
# wget lacks --write-out; --spider -S prints the redirect headers, whose final 'Location:' is the concrete build URL. tolower() keeps awk portable.
wget --spider -S "$url" 2>&1 \
| awk 'tolower($1) == "location:" { u = $2 } END { if (u != "") print u }'
fi
# An empty result falls back to the shortlink URLs below (previous behavior).
}

RESOLVED_URL="$(resolve_final_url "$DOWNLOAD_URL" || true)"
if [ -n "$RESOLVED_URL" ] && [[ "$RESOLVED_URL" == *"/public/"* ]]; then
info "Resolved '${QUALITY}' to concrete build: $RESOLVED_URL"
DOWNLOAD_URL="$RESOLVED_URL"
# Checksums live under the sibling 'public-checksums' path. Use sed, not bash
# ${var/p/r}, since bash 3.2 (macOS) keeps the escaped backslashes literally.
CHECKSUM_URL="$(printf '%s' "$RESOLVED_URL" | sed 's#/public/#/public-checksums/#').sha512"
else
gray "Could not resolve '${QUALITY}' shortlink to a concrete build; using shortlink URLs directly."
fi

INSTALLED_BINARY="${INSTALL_DIR}/dotnetup"

TEMP_DIR=$(mktemp -d)
Expand Down
Loading