Use curl options where appropriate

This commit is contained in:
Sam Ford 2021-09-06 22:56:25 -04:00
parent d44979fa67
commit f88966a8a5
No known key found for this signature in database
GPG Key ID: 95209E46C7FFDEFE
4 changed files with 35 additions and 20 deletions

View File

@ -27,7 +27,7 @@ module Homebrew
return cache[endpoint] if cache.present? && cache.key?(endpoint) return cache[endpoint] if cache.present? && cache.key?(endpoint)
api_url = "#{API_DOMAIN}/#{endpoint}" api_url = "#{API_DOMAIN}/#{endpoint}"
output = Utils::Curl.curl_output("--fail", "--max-time", "5", api_url) output = Utils::Curl.curl_output("--fail", api_url, max_time: 5)
raise ArgumentError, "No file found at #{Tty.underline}#{api_url}#{Tty.reset}" unless output.success? raise ArgumentError, "No file found at #{Tty.underline}#{api_url}#{Tty.reset}" unless output.success?
cache[endpoint] = if json cache[endpoint] = if json

View File

@ -558,7 +558,7 @@ class CurlDownloadStrategy < AbstractFileDownloadStrategy
end end
def curl(*args, **options) def curl(*args, **options)
args << "--connect-timeout" << "15" unless mirrors.empty? options[:connect_timeout] = 15 unless mirrors.empty?
super(*_curl_args, *args, **_curl_opts, **command_output_options, **options) super(*_curl_args, *args, **_curl_opts, **command_output_options, **options)
end end
end end

View File

@ -39,8 +39,6 @@ module Homebrew
DEFAULT_CURL_ARGS = [ DEFAULT_CURL_ARGS = [
# Follow redirections to handle mirrors, relocations, etc. # Follow redirections to handle mirrors, relocations, etc.
"--location", "--location",
"--connect-timeout", CURL_CONNECT_TIMEOUT,
"--max-time", CURL_MAX_TIME
].freeze ].freeze
# `curl` arguments used in `Strategy#page_headers` method. # `curl` arguments used in `Strategy#page_headers` method.
@ -62,12 +60,14 @@ module Homebrew
# Baseline `curl` options used in {Strategy} methods. # Baseline `curl` options used in {Strategy} methods.
DEFAULT_CURL_OPTIONS = { DEFAULT_CURL_OPTIONS = {
print_stdout: false, print_stdout: false,
print_stderr: false, print_stderr: false,
debug: false, debug: false,
verbose: false, verbose: false,
timeout: CURL_PROCESS_TIMEOUT, timeout: CURL_PROCESS_TIMEOUT,
retries: 0, connect_timeout: CURL_CONNECT_TIMEOUT,
max_time: CURL_MAX_TIME,
retries: 0,
}.freeze }.freeze
# HTTP response head(s) and body are typically separated by a double # HTTP response head(s) and body are typically separated by a double

View File

@ -214,8 +214,13 @@ module Utils
if url != secure_url if url != secure_url
user_agents.each do |user_agent| user_agents.each do |user_agent|
secure_details = begin secure_details = begin
curl_http_content_headers_and_checksum(secure_url, specs: specs, hash_needed: true, curl_http_content_headers_and_checksum(
user_agent: user_agent, use_homebrew_curl: use_homebrew_curl) secure_url,
specs: specs,
hash_needed: true,
use_homebrew_curl: use_homebrew_curl,
user_agent: user_agent,
)
rescue Timeout::Error rescue Timeout::Error
next next
end end
@ -231,8 +236,13 @@ module Utils
details = nil details = nil
user_agents.each do |user_agent| user_agents.each do |user_agent|
details = details =
curl_http_content_headers_and_checksum(url, specs: specs, hash_needed: hash_needed, curl_http_content_headers_and_checksum(
user_agent: user_agent, use_homebrew_curl: use_homebrew_curl) url,
specs: specs,
hash_needed: hash_needed,
use_homebrew_curl: use_homebrew_curl,
user_agent: user_agent,
)
break if http_status_ok?(details[:status]) break if http_status_ok?(details[:status])
end end
@ -297,16 +307,21 @@ module Utils
"The #{url_type} #{url} may be able to use HTTPS rather than HTTP. Please verify it in a browser." "The #{url_type} #{url} may be able to use HTTPS rather than HTTP. Please verify it in a browser."
end end
def curl_http_content_headers_and_checksum(url, specs: {}, hash_needed: false, def curl_http_content_headers_and_checksum(
user_agent: :default, use_homebrew_curl: false) url, specs: {}, hash_needed: false,
use_homebrew_curl: false, user_agent: :default
)
file = Tempfile.new.tap(&:close) file = Tempfile.new.tap(&:close)
specs = specs.flat_map { |option, argument| ["--#{option.to_s.tr("_", "-")}", argument] } specs = specs.flat_map { |option, argument| ["--#{option.to_s.tr("_", "-")}", argument] }
max_time = hash_needed ? "600" : "25" max_time = hash_needed ? 600 : 25
output, _, status = curl_output( output, _, status = curl_output(
*specs, "--dump-header", "-", "--output", file.path, "--location", *specs, "--dump-header", "-", "--output", file.path, "--location", url,
"--connect-timeout", "15", "--max-time", max_time, "--retry-max-time", max_time, url, use_homebrew_curl: use_homebrew_curl,
user_agent: user_agent, use_homebrew_curl: use_homebrew_curl connect_timeout: 15,
max_time: max_time,
retry_max_time: max_time,
user_agent: user_agent
) )
status_code = :unknown status_code = :unknown