2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2019-04-22 07:56:16 -04:00
|
|
|
require_relative '../rubygems'
|
|
|
|
require_relative 'request'
|
|
|
|
require_relative 'request/connection_pools'
|
|
|
|
require_relative 's3_uri_signer'
|
|
|
|
require_relative 'uri_formatter'
|
2021-08-24 06:02:29 -04:00
|
|
|
require_relative 'uri'
|
2019-04-22 07:56:16 -04:00
|
|
|
require_relative 'user_interaction'
|
2007-11-10 02:48:56 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# RemoteFetcher handles the details of fetching gems and gem information from
|
|
|
|
# a remote source.
|
|
|
|
|
|
|
|
class Gem::RemoteFetcher
|
2008-03-31 18:40:06 -04:00
|
|
|
include Gem::UserInteraction
|
2019-12-13 06:19:08 -05:00
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
##
|
|
|
|
# A FetchError exception wraps up the various possible IO and HTTP failures
|
|
|
|
# that could happen while downloading from the internet.
|
|
|
|
|
|
|
|
class FetchError < Gem::Exception
|
|
|
|
##
|
|
|
|
# The URI which was being accessed when the exception happened.
|
2021-08-23 09:02:57 -04:00
|
|
|
|
2021-08-21 11:40:21 -04:00
|
|
|
attr_accessor :uri, :original_uri
|
2021-01-03 20:09:05 -05:00
|
|
|
|
2021-08-22 06:06:02 -04:00
|
|
|
def initialize(message, uri)
|
2021-08-24 06:02:29 -04:00
|
|
|
uri = Gem::Uri.new(uri)
|
2021-08-22 06:06:02 -04:00
|
|
|
|
2021-08-24 06:02:29 -04:00
|
|
|
super uri.redact_credentials_from(message)
|
2019-12-13 06:19:08 -05:00
|
|
|
|
2021-08-24 06:02:29 -04:00
|
|
|
@original_uri = uri.to_s
|
|
|
|
@uri = uri.redacted.to_s
|
2008-06-17 18:04:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s # :nodoc:
|
|
|
|
"#{super} (#{uri})"
|
|
|
|
end
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
##
|
|
|
|
# A FetchError that indicates that the reason for not being
|
|
|
|
# able to fetch data was that the host could not be contacted
|
|
|
|
|
|
|
|
class UnknownHostError < FetchError
|
|
|
|
end
|
2021-04-14 23:47:18 -04:00
|
|
|
deprecate_constant(:UnknownHostError)
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
@fetcher = nil
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
##
|
2007-11-10 02:48:56 -05:00
|
|
|
# Cached RemoteFetcher instance.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def self.fetcher
|
2007-12-20 03:39:12 -05:00
|
|
|
@fetcher ||= self.new Gem.configuration[:http_proxy]
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2015-10-29 20:54:12 -04:00
|
|
|
attr_accessor :headers
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
##
|
2007-11-10 02:48:56 -05:00
|
|
|
# Initialize a remote fetcher using the source URI and possible proxy
|
|
|
|
# information.
|
|
|
|
#
|
|
|
|
# +proxy+
|
|
|
|
# * [String]: explicit specification of proxy; overrides any environment
|
|
|
|
# variable setting
|
|
|
|
# * nil: respect environment variables (HTTP_PROXY, HTTP_PROXY_USER,
|
|
|
|
# HTTP_PROXY_PASS)
|
|
|
|
# * <tt>:no_proxy</tt>: ignore environment variables and _don't_ use a proxy
|
2012-11-29 01:52:18 -05:00
|
|
|
#
|
2015-10-29 20:54:12 -04:00
|
|
|
# +headers+: A set of additional HTTP headers to be sent to the server when
|
|
|
|
# fetching the gem.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2018-10-21 20:27:02 -04:00
|
|
|
def initialize(proxy=nil, dns=nil, headers={})
|
2021-10-10 11:10:49 -04:00
|
|
|
require_relative 'core_ext/tcpsocket_init' if Gem.configuration.ipv4_fallback_enabled
|
2011-01-18 19:08:49 -05:00
|
|
|
require 'net/http'
|
|
|
|
require 'stringio'
|
|
|
|
require 'uri'
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
Socket.do_not_reverse_lookup = true
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
@proxy = proxy
|
2014-09-13 23:30:02 -04:00
|
|
|
@pools = {}
|
2021-07-07 01:07:29 -04:00
|
|
|
@pool_lock = Thread::Mutex.new
|
2014-09-13 23:30:02 -04:00
|
|
|
@cert_files = Gem::Request.get_cert_files
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2015-10-29 20:54:12 -04:00
|
|
|
@headers = headers
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
2011-03-01 04:41:32 -05:00
|
|
|
##
|
|
|
|
# Given a name and requirement, downloads this gem into cache and returns the
|
|
|
|
# filename. Returns nil if the gem cannot be located.
|
|
|
|
#--
|
|
|
|
# Should probably be integrated with #download below, but that will be a
|
2015-01-09 09:20:10 -05:00
|
|
|
# larger, more encompassing effort. -erikh
|
2011-03-01 04:41:32 -05:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def download_to_cache(dependency)
|
2012-11-29 01:52:18 -05:00
|
|
|
found, _ = Gem::SpecFetcher.fetcher.spec_for_dependency dependency
|
2011-03-01 04:41:32 -05:00
|
|
|
|
|
|
|
return if found.empty?
|
|
|
|
|
2020-06-10 13:46:05 -04:00
|
|
|
spec, source = found.max_by {|(s,_)| s.version }
|
2011-03-01 04:41:32 -05:00
|
|
|
|
2019-12-13 06:19:08 -05:00
|
|
|
download spec, source.uri
|
2011-03-01 04:41:32 -05:00
|
|
|
end
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
##
|
|
|
|
# Moves the gem +spec+ from +source_uri+ to the cache dir unless it is
|
|
|
|
# already there. If the source_uri is local the gem cache dir copy is
|
|
|
|
# always replaced.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
def download(spec, source_uri, install_dir = Gem.dir)
|
2020-08-04 01:25:59 -04:00
|
|
|
install_cache_dir = File.join install_dir, "cache"
|
2012-11-29 01:52:18 -05:00
|
|
|
cache_dir =
|
2019-05-03 13:56:58 -04:00
|
|
|
if Dir.pwd == install_dir # see fetch_command
|
2012-11-29 01:52:18 -05:00
|
|
|
install_dir
|
2020-08-04 01:25:59 -04:00
|
|
|
elsif File.writable?(install_cache_dir) || (File.writable?(install_dir) && (not File.exist?(install_cache_dir)))
|
|
|
|
install_cache_dir
|
2012-11-29 01:52:18 -05:00
|
|
|
else
|
|
|
|
File.join Gem.user_dir, "cache"
|
|
|
|
end
|
2008-06-25 22:06:00 -04:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
gem_file_name = File.basename spec.cache_file
|
2008-06-17 18:04:18 -04:00
|
|
|
local_gem_path = File.join cache_dir, gem_file_name
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2020-07-02 09:11:18 -04:00
|
|
|
require "fileutils"
|
2008-06-17 18:04:18 -04:00
|
|
|
FileUtils.mkdir_p cache_dir rescue nil unless File.exist? cache_dir
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2021-08-24 06:02:29 -04:00
|
|
|
source_uri = Gem::Uri.new(source_uri)
|
2009-06-09 17:38:59 -04:00
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
scheme = source_uri.scheme
|
|
|
|
|
|
|
|
# URI.parse gets confused by MS Windows paths with forward slashes.
|
|
|
|
scheme = nil if scheme =~ /^[a-z]$/i
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
# REFACTOR: split this up and dispatch on scheme (eg download_http)
|
|
|
|
# REFACTOR: be sure to clean up fake fetcher when you do this... cleaner
|
2008-03-31 18:40:06 -04:00
|
|
|
case scheme
|
2014-09-13 23:30:02 -04:00
|
|
|
when 'http', 'https', 's3' then
|
2018-11-21 05:20:47 -05:00
|
|
|
unless File.exist? local_gem_path
|
2008-03-31 18:40:06 -04:00
|
|
|
begin
|
2014-09-13 23:30:02 -04:00
|
|
|
verbose "Downloading gem #{gem_file_name}"
|
2008-03-31 18:40:06 -04:00
|
|
|
|
|
|
|
remote_gem_path = source_uri + "gems/#{gem_file_name}"
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
self.cache_update_path remote_gem_path, local_gem_path
|
2019-12-13 06:19:08 -05:00
|
|
|
rescue FetchError
|
2008-03-31 18:40:06 -04:00
|
|
|
raise if spec.original_platform == spec.platform
|
|
|
|
|
|
|
|
alternate_name = "#{spec.original_name}.gem"
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
verbose "Failed, downloading gem #{alternate_name}"
|
2008-03-31 18:40:06 -04:00
|
|
|
|
|
|
|
remote_gem_path = source_uri + "gems/#{alternate_name}"
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
self.cache_update_path remote_gem_path, local_gem_path
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
when 'file' then
|
|
|
|
begin
|
|
|
|
path = source_uri.path
|
|
|
|
path = File.dirname(path) if File.extname(path) == '.gem'
|
|
|
|
|
2019-06-01 05:45:11 -04:00
|
|
|
remote_gem_path = Gem::Util.correct_for_windows_path(File.join(path, 'gems', gem_file_name))
|
2009-06-09 17:38:59 -04:00
|
|
|
|
|
|
|
FileUtils.cp(remote_gem_path, local_gem_path)
|
|
|
|
rescue Errno::EACCES
|
|
|
|
local_gem_path = source_uri.to_s
|
|
|
|
end
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
verbose "Using local gem #{local_gem_path}"
|
2009-06-09 17:38:59 -04:00
|
|
|
when nil then # TODO test for local overriding cache
|
2010-02-21 21:52:35 -05:00
|
|
|
source_path = if Gem.win_platform? && source_uri.scheme &&
|
2018-11-21 05:20:47 -05:00
|
|
|
!source_uri.path.include?(':')
|
2010-02-21 21:52:35 -05:00
|
|
|
"#{source_uri.scheme}:#{source_uri.path}"
|
|
|
|
else
|
|
|
|
source_uri.path
|
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
source_path = Gem::UriFormatter.new(source_path).unescape
|
2010-02-21 21:52:35 -05:00
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
begin
|
2010-02-21 21:52:35 -05:00
|
|
|
FileUtils.cp source_path, local_gem_path unless
|
2012-04-15 20:44:47 -04:00
|
|
|
File.identical?(source_path, local_gem_path)
|
2008-03-31 18:40:06 -04:00
|
|
|
rescue Errno::EACCES
|
|
|
|
local_gem_path = source_uri.to_s
|
|
|
|
end
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
verbose "Using local gem #{local_gem_path}"
|
2008-03-31 18:40:06 -04:00
|
|
|
else
|
2012-11-29 01:52:18 -05:00
|
|
|
raise ArgumentError, "unsupported URI scheme #{source_uri.scheme}"
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
local_gem_path
|
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
##
|
|
|
|
# File Fetcher. Dispatched by +fetch_path+. Use it instead.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def fetch_file(uri, *_)
|
2019-06-01 05:45:11 -04:00
|
|
|
Gem.read_binary Gem::Util.correct_for_windows_path uri.path
|
2011-05-31 23:45:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# HTTP Fetcher. Dispatched by +fetch_path+. Use it instead.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def fetch_http(uri, last_modified = nil, head = false, depth = 0)
|
2011-05-31 23:45:05 -04:00
|
|
|
fetch_type = head ? Net::HTTP::Head : Net::HTTP::Get
|
2015-10-29 20:54:12 -04:00
|
|
|
response = request uri, fetch_type, last_modified do |req|
|
2020-06-10 13:46:05 -04:00
|
|
|
headers.each {|k,v| req.add_field(k,v) }
|
2015-10-29 20:54:12 -04:00
|
|
|
end
|
2011-05-31 23:45:05 -04:00
|
|
|
|
|
|
|
case response
|
|
|
|
when Net::HTTPOK, Net::HTTPNotModified then
|
2020-12-22 18:45:19 -05:00
|
|
|
response.uri = uri
|
2011-05-31 23:45:05 -04:00
|
|
|
head ? response : response.body
|
|
|
|
when Net::HTTPMovedPermanently, Net::HTTPFound, Net::HTTPSeeOther,
|
|
|
|
Net::HTTPTemporaryRedirect then
|
2021-08-22 06:06:02 -04:00
|
|
|
raise FetchError.new('too many redirects', uri) if depth > 10
|
2011-05-31 23:45:05 -04:00
|
|
|
|
2016-09-27 20:57:53 -04:00
|
|
|
unless location = response['Location']
|
2021-08-22 06:06:02 -04:00
|
|
|
raise FetchError.new("redirecting but no redirect location was given", uri)
|
2016-09-27 20:57:53 -04:00
|
|
|
end
|
2021-08-24 06:02:29 -04:00
|
|
|
location = Gem::Uri.new location
|
2012-04-19 19:07:48 -04:00
|
|
|
|
|
|
|
if https?(uri) && !https?(location)
|
2021-08-22 06:06:02 -04:00
|
|
|
raise FetchError.new("redirecting to non-https resource: #{location}", uri)
|
2012-04-19 19:07:48 -04:00
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
fetch_http(location, last_modified, head, depth + 1)
|
|
|
|
else
|
2021-08-22 06:06:02 -04:00
|
|
|
raise FetchError.new("bad response #{response.message} #{response.code}", uri)
|
2011-05-31 23:45:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
alias :fetch_https :fetch_http
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
##
|
|
|
|
# Downloads +uri+ and returns it as a String.
|
|
|
|
|
2008-06-25 22:06:00 -04:00
|
|
|
def fetch_path(uri, mtime = nil, head = false)
|
2021-08-24 06:02:29 -04:00
|
|
|
uri = Gem::Uri.new uri
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
unless uri.scheme
|
|
|
|
raise ArgumentError, "uri scheme is invalid: #{uri.scheme.inspect}"
|
|
|
|
end
|
2011-05-31 23:45:05 -04:00
|
|
|
|
|
|
|
data = send "fetch_#{uri.scheme}", uri, mtime, head
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2020-04-19 12:06:02 -04:00
|
|
|
if data and !head and uri.to_s.end_with?(".gz")
|
2012-11-29 01:52:18 -05:00
|
|
|
begin
|
2018-05-17 21:39:13 -04:00
|
|
|
data = Gem::Util.gunzip data
|
2012-11-29 01:52:18 -05:00
|
|
|
rescue Zlib::GzipFile::Error
|
2021-08-22 06:06:02 -04:00
|
|
|
raise FetchError.new("server did not return a valid file", uri)
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-25 22:06:00 -04:00
|
|
|
data
|
2021-04-14 23:47:18 -04:00
|
|
|
rescue Timeout::Error, IOError, SocketError, SystemCallError,
|
2020-12-08 02:33:39 -05:00
|
|
|
*(OpenSSL::SSL::SSLError if Gem::HAVE_OPENSSL) => e
|
2021-08-22 06:06:02 -04:00
|
|
|
raise FetchError.new("#{e.class}: #{e}", uri)
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
def fetch_s3(uri, mtime = nil, head = false)
|
2019-07-03 19:35:59 -04:00
|
|
|
begin
|
|
|
|
public_uri = s3_uri_signer(uri).sign
|
|
|
|
rescue Gem::S3URISigner::ConfigurationError, Gem::S3URISigner::InstanceProfileError => e
|
2021-08-22 06:06:02 -04:00
|
|
|
raise FetchError.new(e.message, "s3://#{uri.host}")
|
2019-07-03 19:35:59 -04:00
|
|
|
end
|
2014-09-13 23:30:02 -04:00
|
|
|
fetch_https public_uri, mtime, head
|
|
|
|
end
|
|
|
|
|
2019-07-03 19:35:59 -04:00
|
|
|
# we have our own signing code here to avoid a dependency on the aws-sdk gem
|
|
|
|
def s3_uri_signer(uri)
|
|
|
|
Gem::S3URISigner.new(uri)
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
##
|
|
|
|
# Downloads +uri+ to +path+ if necessary. If no path is given, it just
|
|
|
|
# passes the data.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def cache_update_path(uri, path = nil, update = true)
|
2012-11-29 01:52:18 -05:00
|
|
|
mtime = path && File.stat(path).mtime rescue nil
|
|
|
|
|
2014-02-03 19:48:31 -05:00
|
|
|
data = fetch_path(uri, mtime)
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2021-02-01 10:17:16 -05:00
|
|
|
if data.nil? # indicates the server returned 304 Not Modified
|
2014-02-03 19:48:31 -05:00
|
|
|
return Gem.read_binary(path)
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2014-02-03 19:48:31 -05:00
|
|
|
if update and path
|
2016-06-18 01:11:55 -04:00
|
|
|
Gem.write_binary(path, data)
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
2014-02-03 19:48:31 -05:00
|
|
|
|
|
|
|
data
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
##
|
|
|
|
# Performs a Net::HTTP request of type +request_class+ on +uri+ returning
|
|
|
|
# a Net::HTTP response object. request maintains a table of persistent
|
|
|
|
# connections to reduce connect overhead.
|
|
|
|
|
2008-06-25 22:06:00 -04:00
|
|
|
def request(uri, request_class, last_modified = nil)
|
2014-09-13 23:30:02 -04:00
|
|
|
proxy = proxy_for @proxy, uri
|
|
|
|
pool = pools_for(proxy).pool_for uri
|
|
|
|
|
|
|
|
request = Gem::Request.new uri, request_class, last_modified, pool
|
2013-07-30 18:11:11 -04:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
request.fetch do |req|
|
|
|
|
yield req if block_given?
|
2013-07-22 18:46:50 -04:00
|
|
|
end
|
2011-05-31 23:45:05 -04:00
|
|
|
end
|
|
|
|
|
2012-04-19 19:07:48 -04:00
|
|
|
def https?(uri)
|
|
|
|
uri.scheme.downcase == 'https'
|
|
|
|
end
|
|
|
|
|
2014-11-16 22:55:02 -05:00
|
|
|
def close_all
|
2020-06-10 13:46:05 -04:00
|
|
|
@pools.each_value {|pool| pool.close_all }
|
2014-11-16 22:55:02 -05:00
|
|
|
end
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
private
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def proxy_for(proxy, uri)
|
2014-09-13 23:30:02 -04:00
|
|
|
Gem::Request.proxy_uri(proxy || Gem::Request.get_proxy_from_env(uri.scheme))
|
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def pools_for(proxy)
|
2014-09-13 23:30:02 -04:00
|
|
|
@pool_lock.synchronize do
|
|
|
|
@pools[proxy] ||= Gem::Request::ConnectionPools.new proxy, @cert_files
|
|
|
|
end
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|