Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use curl_headers and curl_output for Livecheck strategies. #15351

Merged
merged 2 commits into from
May 8, 2023
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
2 changes: 1 addition & 1 deletion Library/Homebrew/download_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ def resolve_url_basename_time_file_size(url, timeout: nil)
url = url.sub(%r{^https?://#{GitHubPackages::URL_DOMAIN}/}o, "#{domain.chomp("/")}/")
end

parsed_output = curl_head(url.to_s, timeout: timeout)
parsed_output = curl_headers(url.to_s, wanted_headers: ["content-disposition"], timeout: timeout)
parsed_headers = parsed_output.fetch(:responses).map { |r| r.fetch(:headers) }

final_url = curl_response_follow_redirections(parsed_output.fetch(:responses), url)
Expand Down
21 changes: 8 additions & 13 deletions Library/Homebrew/livecheck/strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module Livecheck
#
# @api private
module Strategy
extend Utils::Curl

module_function

# {Strategy} priorities informally range from 1 to 10, where 10 is the
Expand Down Expand Up @@ -53,14 +55,6 @@ module Strategy
"--silent"
].freeze

# `curl` arguments used in `Strategy#page_headers` method.
PAGE_HEADERS_CURL_ARGS = ([
# We only need the response head (not the body)
"--head",
# Some servers may not allow a HEAD request, so we use GET
"--request", "GET"
] + DEFAULT_CURL_ARGS).freeze

# `curl` arguments used in `Strategy#page_content` method.
PAGE_CONTENT_CURL_ARGS = ([
"--compressed",
Expand Down Expand Up @@ -188,11 +182,12 @@ def self.page_headers(url, homebrew_curl: false)
headers = []

[:default, :browser].each do |user_agent|
output, _, status = curl_with_workarounds(
*PAGE_HEADERS_CURL_ARGS, url,
**DEFAULT_CURL_OPTIONS,
output, _, status = curl_headers(
url,
wanted_headers: ["location", "content-disposition"],
use_homebrew_curl: homebrew_curl,
user_agent: user_agent
user_agent: user_agent,
**DEFAULT_CURL_OPTIONS,
)
next unless status.success?

Expand All @@ -216,7 +211,7 @@ def self.page_headers(url, homebrew_curl: false)
def self.page_content(url, homebrew_curl: false)
stderr = T.let(nil, T.nilable(String))
[:default, :browser].each do |user_agent|
stdout, stderr, status = curl_with_workarounds(
stdout, stderr, status = curl_output(
*PAGE_CONTENT_CURL_ARGS, url,
**DEFAULT_CURL_OPTIONS,
use_homebrew_curl: homebrew_curl,
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/test/download_strategies/curl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
let(:artifact_domain) { nil }

before do
allow(strategy).to receive(:curl_head).and_return({ responses: [{ headers: {} }] })
allow(strategy).to receive(:curl_headers).and_return({ responses: [{ headers: {} }] })
end

it "parses the opts and sets the corresponding args" do
Expand Down
8 changes: 4 additions & 4 deletions Library/Homebrew/utils/curl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def curl_output(*args, **options)
curl_with_workarounds(*args, print_stderr: false, show_output: true, **options)
end

def curl_head(*args, **options)
def curl_headers(*args, wanted_headers: [], **options)
[[], ["--request", "GET"]].each do |request_args|
result = curl_output(
"--fail", "--location", "--silent", "--head", *request_args, *args,
Expand All @@ -216,9 +216,9 @@ def curl_head(*args, **options)
if result.success? || result.exit_status == 22
parsed_output = parse_curl_output(result.stdout)

# If we didn't get a `Content-Disposition` header yet, retry using `GET`.
next if request_args.empty? &&
parsed_output.fetch(:responses).none? { |r| r.fetch(:headers).key?("content-disposition") }
# If we didn't get any wanted header yet, retry using `GET`.
next if request_args.empty? && wanted_headers.any? &&
parsed_output.fetch(:responses).none? { |r| (r.fetch(:headers).keys & wanted_headers).any? }

return parsed_output if result.success?
end
Expand Down