From 43ee40879363c65ce0afb08f5e292c14b45e66c6 Mon Sep 17 00:00:00 2001 From: Michael Cho Date: Sat, 5 Oct 2024 02:21:45 -0400 Subject: [PATCH] utils/curl: workaround curl bug for `--head --request GET` The `curl --head --request GET` causes a full download to happen on `curl` from 8.7.0 to 8.9.1[^1] which causes poor UX due to slow Cask downloads that can take almost twice as long as they should. [^1]: https://github.com/Homebrew/brew/issues/18213 --- .../curl_github_packages_spec.rb | 2 ++ .../test/download_strategies/curl_post_spec.rb | 2 ++ Library/Homebrew/utils/curl.rb | 13 +++++++++++-- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/test/download_strategies/curl_github_packages_spec.rb b/Library/Homebrew/test/download_strategies/curl_github_packages_spec.rb index 07d7d7ada1..0868a77aa1 100644 --- a/Library/Homebrew/test/download_strategies/curl_github_packages_spec.rb +++ b/Library/Homebrew/test/download_strategies/curl_github_packages_spec.rb @@ -27,6 +27,8 @@ RSpec.describe CurlGitHubPackagesDownloadStrategy do before do stub_const("HOMEBREW_GITHUB_PACKAGES_AUTH", authorization) if authorization.present? + allow(strategy).to receive(:curl_version).and_return(Version.new("8.7.1")) + allow(strategy).to receive(:system_command) .with( /curl/, diff --git a/Library/Homebrew/test/download_strategies/curl_post_spec.rb b/Library/Homebrew/test/download_strategies/curl_post_spec.rb index 20b382736c..121334fbca 100644 --- a/Library/Homebrew/test/download_strategies/curl_post_spec.rb +++ b/Library/Homebrew/test/download_strategies/curl_post_spec.rb @@ -18,6 +18,8 @@ RSpec.describe CurlPostDownloadStrategy do describe "#fetch" do before do + allow(strategy).to receive(:curl_version).and_return(Version.new("8.6.0")) + allow(strategy).to receive(:system_command) .with( /curl/, diff --git a/Library/Homebrew/utils/curl.rb b/Library/Homebrew/utils/curl.rb index 724b4b0fd6..72e6f79501 100644 --- a/Library/Homebrew/utils/curl.rb +++ b/Library/Homebrew/utils/curl.rb @@ -234,7 +234,11 @@ module Utils end def curl_headers(*args, wanted_headers: [], **options) - [[], ["--request", "GET"]].each do |request_args| + get_retry_args = ["--request", "GET"] + # This is a workaround for https://github.com/Homebrew/brew/issues/18213 + get_retry_args << "--http1.1" if curl_version >= Version.new("8.7") && curl_version < Version.new("8.10") + + [[], get_retry_args].each do |request_args| result = curl_output( "--fail", "--location", "--silent", "--head", *request_args, *args, **options @@ -494,9 +498,14 @@ module Utils T.must(file).unlink end + def curl_version + @curl_version ||= {} + @curl_version[curl_path] ||= Version.new(curl_output("-V").stdout[/curl (\d+(\.\d+)+)/, 1]) + end + def curl_supports_fail_with_body? @curl_supports_fail_with_body ||= Hash.new do |h, key| - h[key] = Version.new(curl_output("-V").stdout[/curl (\d+(\.\d+)+)/, 1]) >= Version.new("7.76.0") + h[key] = curl_version >= Version.new("7.76.0") end @curl_supports_fail_with_body[curl_path] end