2016-06-03 13:05:18 +01:00
|
|
|
require "pathname"
|
2016-07-12 19:46:29 +01:00
|
|
|
require "open3"
|
2016-06-03 13:05:18 +01:00
|
|
|
|
2017-08-08 18:10:13 +02:00
|
|
|
def curl_executable
|
2016-06-03 13:05:18 +01:00
|
|
|
curl = Pathname.new ENV["HOMEBREW_CURL"]
|
|
|
|
curl = Pathname.new "/usr/bin/curl" unless curl.exist?
|
2017-08-08 18:10:13 +02:00
|
|
|
return curl if curl.executable?
|
|
|
|
raise "#{curl} is not executable"
|
|
|
|
end
|
2016-06-03 13:05:18 +01:00
|
|
|
|
2017-08-08 18:10:13 +02:00
|
|
|
def curl_args(*extra_args, show_output: false, user_agent: :default)
|
2016-12-25 23:01:40 +00:00
|
|
|
args = [
|
2017-08-08 18:10:13 +02:00
|
|
|
curl_executable.to_s,
|
|
|
|
"--show-error",
|
2016-12-25 23:01:40 +00:00
|
|
|
]
|
2016-06-03 13:05:18 +01:00
|
|
|
|
2017-08-08 18:10:13 +02:00
|
|
|
args << "--user-agent" << case user_agent
|
|
|
|
when :browser, :fake
|
|
|
|
HOMEBREW_USER_AGENT_FAKE_SAFARI
|
|
|
|
when :default
|
|
|
|
HOMEBREW_USER_AGENT_CURL
|
2017-01-07 14:03:08 +00:00
|
|
|
else
|
2017-08-08 18:10:13 +02:00
|
|
|
user_agent
|
2016-12-25 23:01:40 +00:00
|
|
|
end
|
|
|
|
|
2017-08-08 18:10:13 +02:00
|
|
|
unless show_output
|
2017-08-11 12:28:58 +02:00
|
|
|
args << "--fail"
|
2016-12-25 23:01:40 +00:00
|
|
|
args << "--progress-bar" unless ARGV.verbose?
|
|
|
|
args << "--verbose" if ENV["HOMEBREW_CURL_VERBOSE"]
|
2017-11-27 10:48:03 +00:00
|
|
|
args << "--silent" if !$stdout.tty? || ENV["TRAVIS"]
|
2016-12-25 23:01:40 +00:00
|
|
|
end
|
|
|
|
|
2017-08-08 18:10:13 +02:00
|
|
|
args + extra_args
|
2016-06-03 13:05:18 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def curl(*args)
|
2017-11-03 18:58:59 +00:00
|
|
|
# SSL_CERT_FILE can be incorrectly set by users or portable-ruby and screw
|
|
|
|
# with SSL downloads so unset it here.
|
|
|
|
with_env SSL_CERT_FILE: nil do
|
|
|
|
safe_system(*curl_args(*args))
|
|
|
|
end
|
2017-08-04 16:24:29 +02:00
|
|
|
end
|
|
|
|
|
2017-08-21 19:32:16 +02:00
|
|
|
def curl_download(*args, to: nil, continue_at: "-", **options)
|
2017-09-10 07:23:18 +02:00
|
|
|
had_incomplete_download ||= File.exist?(to)
|
2017-08-21 19:32:16 +02:00
|
|
|
curl("--location", "--remote-time", "--continue-at", continue_at.to_s, "--output", to, *args, **options)
|
2017-08-10 18:53:23 +02:00
|
|
|
rescue ErrorDuringExecution
|
|
|
|
# `curl` error 33: HTTP server doesn't seem to support byte ranges. Cannot resume.
|
2017-09-10 07:23:18 +02:00
|
|
|
# HTTP status 416: Requested range not satisfiable
|
|
|
|
if ($CHILD_STATUS.exitstatus == 33 || had_incomplete_download) && continue_at == "-"
|
2017-08-21 19:32:16 +02:00
|
|
|
continue_at = 0
|
2017-09-10 07:23:18 +02:00
|
|
|
had_incomplete_download = false
|
2017-08-10 18:53:23 +02:00
|
|
|
retry
|
|
|
|
end
|
|
|
|
|
|
|
|
raise
|
2017-08-08 18:10:13 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def curl_output(*args, **options)
|
|
|
|
Open3.capture3(*curl_args(*args, show_output: true, **options))
|
2016-06-03 13:05:18 +01:00
|
|
|
end
|