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
This commit is contained in:
Michael Cho 2024-10-05 02:21:45 -04:00
parent 1e29665e62
commit 43ee408793
No known key found for this signature in database
GPG Key ID: 55E85E28A7CD1E85
3 changed files with 15 additions and 2 deletions

View File

@ -27,6 +27,8 @@ RSpec.describe CurlGitHubPackagesDownloadStrategy do
before do before do
stub_const("HOMEBREW_GITHUB_PACKAGES_AUTH", authorization) if authorization.present? 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) allow(strategy).to receive(:system_command)
.with( .with(
/curl/, /curl/,

View File

@ -18,6 +18,8 @@ RSpec.describe CurlPostDownloadStrategy do
describe "#fetch" do describe "#fetch" do
before do before do
allow(strategy).to receive(:curl_version).and_return(Version.new("8.6.0"))
allow(strategy).to receive(:system_command) allow(strategy).to receive(:system_command)
.with( .with(
/curl/, /curl/,

View File

@ -234,7 +234,11 @@ module Utils
end end
def curl_headers(*args, wanted_headers: [], **options) 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( result = curl_output(
"--fail", "--location", "--silent", "--head", *request_args, *args, "--fail", "--location", "--silent", "--head", *request_args, *args,
**options **options
@ -494,9 +498,14 @@ module Utils
T.must(file).unlink T.must(file).unlink
end 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? def curl_supports_fail_with_body?
@curl_supports_fail_with_body ||= Hash.new do |h, key| @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 end
@curl_supports_fail_with_body[curl_path] @curl_supports_fail_with_body[curl_path]
end end