2016-02-01 15:43:26 +03:00
|
|
|
# frozen_string_literal: true
|
2007-11-10 10:48:56 +03:00
|
|
|
require 'rubygems'
|
2013-09-14 12:59:02 +04:00
|
|
|
require 'rubygems/request'
|
2019-07-04 02:35:59 +03:00
|
|
|
require 'rubygems/request/connection_pools'
|
|
|
|
require 'rubygems/s3_uri_signer'
|
2013-09-14 12:59:02 +04:00
|
|
|
require 'rubygems/uri_formatter'
|
2019-12-13 14:19:08 +03:00
|
|
|
require 'rubygems/uri_parsing'
|
2011-01-19 03:08:49 +03:00
|
|
|
require 'rubygems/user_interaction'
|
2012-11-29 10:52:18 +04:00
|
|
|
require 'resolv'
|
2007-11-10 10:48:56 +03:00
|
|
|
|
|
|
|
##
|
|
|
|
# RemoteFetcher handles the details of fetching gems and gem information from
|
|
|
|
# a remote source.
|
|
|
|
|
|
|
|
class Gem::RemoteFetcher
|
2008-04-01 02:40:06 +04:00
|
|
|
include Gem::UserInteraction
|
2019-12-13 14:19:08 +03:00
|
|
|
include Gem::UriParsing
|
|
|
|
|
2008-06-18 02: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
|
2019-12-13 14:19:08 +03:00
|
|
|
include Gem::UriParsing
|
|
|
|
|
2008-06-18 02:04:18 +04:00
|
|
|
##
|
|
|
|
# The URI which was being accessed when the exception happened.
|
|
|
|
|
|
|
|
attr_accessor :uri
|
|
|
|
|
|
|
|
def initialize(message, uri)
|
|
|
|
super message
|
2019-12-13 14:19:08 +03:00
|
|
|
|
|
|
|
uri = parse_uri(uri)
|
|
|
|
|
|
|
|
uri.password = 'REDACTED' if uri.respond_to?(:password) && uri.password
|
|
|
|
|
|
|
|
@uri = uri.to_s
|
2008-06-18 02:04:18 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s # :nodoc:
|
|
|
|
"#{super} (#{uri})"
|
|
|
|
end
|
|
|
|
end
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2012-11-29 10:52:18 +04: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
|
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
@fetcher = nil
|
|
|
|
|
2008-06-18 02:04:18 +04:00
|
|
|
##
|
2007-11-10 10:48:56 +03:00
|
|
|
# Cached RemoteFetcher instance.
|
2008-06-18 02:04:18 +04:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
def self.fetcher
|
2007-12-20 11:39:12 +03:00
|
|
|
@fetcher ||= self.new Gem.configuration[:http_proxy]
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
|
2015-10-30 03:54:12 +03:00
|
|
|
attr_accessor :headers
|
|
|
|
|
2008-06-18 02:04:18 +04:00
|
|
|
##
|
2007-11-10 10:48:56 +03: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 10:52:18 +04:00
|
|
|
#
|
2015-10-30 03:54:12 +03:00
|
|
|
# +headers+: A set of additional HTTP headers to be sent to the server when
|
|
|
|
# fetching the gem.
|
2008-06-18 02:04:18 +04:00
|
|
|
|
2018-10-22 03:27:02 +03:00
|
|
|
def initialize(proxy=nil, dns=nil, headers={})
|
2011-01-19 03:08:49 +03:00
|
|
|
require 'net/http'
|
|
|
|
require 'stringio'
|
|
|
|
require 'uri'
|
|
|
|
|
2008-04-01 02:40:06 +04:00
|
|
|
Socket.do_not_reverse_lookup = true
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
@proxy = proxy
|
2014-09-14 07:30:02 +04:00
|
|
|
@pools = {}
|
|
|
|
@pool_lock = Mutex.new
|
|
|
|
@cert_files = Gem::Request.get_cert_files
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2015-10-30 03:54:12 +03:00
|
|
|
@headers = headers
|
2012-11-29 10:52:18 +04:00
|
|
|
end
|
|
|
|
|
2011-03-01 12:41:32 +03: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 17:20:10 +03:00
|
|
|
# larger, more encompassing effort. -erikh
|
2011-03-01 12:41:32 +03:00
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def download_to_cache(dependency)
|
2012-11-29 10:52:18 +04:00
|
|
|
found, _ = Gem::SpecFetcher.fetcher.spec_for_dependency dependency
|
2011-03-01 12:41:32 +03:00
|
|
|
|
|
|
|
return if found.empty?
|
|
|
|
|
2020-06-10 20:46:05 +03:00
|
|
|
spec, source = found.max_by {|(s,_)| s.version }
|
2011-03-01 12:41:32 +03:00
|
|
|
|
2019-12-13 14:19:08 +03:00
|
|
|
download spec, source.uri
|
2011-03-01 12:41:32 +03:00
|
|
|
end
|
|
|
|
|
2008-04-01 02: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-18 02:04:18 +04:00
|
|
|
|
2008-04-01 02:40:06 +04:00
|
|
|
def download(spec, source_uri, install_dir = Gem.dir)
|
2020-08-04 08:25:59 +03:00
|
|
|
install_cache_dir = File.join install_dir, "cache"
|
2012-11-29 10:52:18 +04:00
|
|
|
cache_dir =
|
2019-05-03 20:56:58 +03:00
|
|
|
if Dir.pwd == install_dir # see fetch_command
|
2012-11-29 10:52:18 +04:00
|
|
|
install_dir
|
2020-08-04 08:25:59 +03:00
|
|
|
elsif File.writable?(install_cache_dir) || (File.writable?(install_dir) && (not File.exist?(install_cache_dir)))
|
|
|
|
install_cache_dir
|
2012-11-29 10:52:18 +04:00
|
|
|
else
|
|
|
|
File.join Gem.user_dir, "cache"
|
|
|
|
end
|
2008-06-26 06:06:00 +04:00
|
|
|
|
2011-06-01 07:45:05 +04:00
|
|
|
gem_file_name = File.basename spec.cache_file
|
2008-06-18 02:04:18 +04:00
|
|
|
local_gem_path = File.join cache_dir, gem_file_name
|
2008-04-01 02:40:06 +04:00
|
|
|
|
2020-07-02 16:11:18 +03:00
|
|
|
require "fileutils"
|
2008-06-18 02:04:18 +04:00
|
|
|
FileUtils.mkdir_p cache_dir rescue nil unless File.exist? cache_dir
|
2008-04-01 02:40:06 +04:00
|
|
|
|
2019-12-13 14:19:08 +03:00
|
|
|
source_uri = parse_uri(source_uri)
|
2009-06-10 01:38:59 +04:00
|
|
|
|
2008-04-01 02: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 10:52:18 +04: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-04-01 02:40:06 +04:00
|
|
|
case scheme
|
2014-09-14 07:30:02 +04:00
|
|
|
when 'http', 'https', 's3' then
|
2018-11-21 13:20:47 +03:00
|
|
|
unless File.exist? local_gem_path
|
2008-04-01 02:40:06 +04:00
|
|
|
begin
|
2014-09-14 07:30:02 +04:00
|
|
|
verbose "Downloading gem #{gem_file_name}"
|
2008-04-01 02:40:06 +04:00
|
|
|
|
|
|
|
remote_gem_path = source_uri + "gems/#{gem_file_name}"
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
self.cache_update_path remote_gem_path, local_gem_path
|
2019-12-13 14:19:08 +03:00
|
|
|
rescue FetchError
|
2008-04-01 02:40:06 +04:00
|
|
|
raise if spec.original_platform == spec.platform
|
|
|
|
|
|
|
|
alternate_name = "#{spec.original_name}.gem"
|
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
verbose "Failed, downloading gem #{alternate_name}"
|
2008-04-01 02:40:06 +04:00
|
|
|
|
|
|
|
remote_gem_path = source_uri + "gems/#{alternate_name}"
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
self.cache_update_path remote_gem_path, local_gem_path
|
2008-04-01 02:40:06 +04:00
|
|
|
end
|
|
|
|
end
|
2009-06-10 01:38:59 +04:00
|
|
|
when 'file' then
|
|
|
|
begin
|
|
|
|
path = source_uri.path
|
|
|
|
path = File.dirname(path) if File.extname(path) == '.gem'
|
|
|
|
|
2019-06-01 12:45:11 +03:00
|
|
|
remote_gem_path = Gem::Util.correct_for_windows_path(File.join(path, 'gems', gem_file_name))
|
2009-06-10 01: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-14 07:30:02 +04:00
|
|
|
verbose "Using local gem #{local_gem_path}"
|
2009-06-10 01:38:59 +04:00
|
|
|
when nil then # TODO test for local overriding cache
|
2010-02-22 05:52:35 +03:00
|
|
|
source_path = if Gem.win_platform? && source_uri.scheme &&
|
2018-11-21 13:20:47 +03:00
|
|
|
!source_uri.path.include?(':')
|
2010-02-22 05:52:35 +03:00
|
|
|
"#{source_uri.scheme}:#{source_uri.path}"
|
|
|
|
else
|
|
|
|
source_uri.path
|
|
|
|
end
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
source_path = Gem::UriFormatter.new(source_path).unescape
|
2010-02-22 05:52:35 +03:00
|
|
|
|
2008-04-01 02:40:06 +04:00
|
|
|
begin
|
2010-02-22 05:52:35 +03:00
|
|
|
FileUtils.cp source_path, local_gem_path unless
|
2012-04-16 04:44:47 +04:00
|
|
|
File.identical?(source_path, local_gem_path)
|
2008-04-01 02:40:06 +04:00
|
|
|
rescue Errno::EACCES
|
|
|
|
local_gem_path = source_uri.to_s
|
|
|
|
end
|
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
verbose "Using local gem #{local_gem_path}"
|
2008-04-01 02:40:06 +04:00
|
|
|
else
|
2012-11-29 10:52:18 +04:00
|
|
|
raise ArgumentError, "unsupported URI scheme #{source_uri.scheme}"
|
2008-04-01 02:40:06 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
local_gem_path
|
|
|
|
end
|
|
|
|
|
2011-06-01 07:45:05 +04:00
|
|
|
##
|
|
|
|
# File Fetcher. Dispatched by +fetch_path+. Use it instead.
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def fetch_file(uri, *_)
|
2019-06-01 12:45:11 +03:00
|
|
|
Gem.read_binary Gem::Util.correct_for_windows_path uri.path
|
2011-06-01 07:45:05 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# HTTP Fetcher. Dispatched by +fetch_path+. Use it instead.
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def fetch_http(uri, last_modified = nil, head = false, depth = 0)
|
2011-06-01 07:45:05 +04:00
|
|
|
fetch_type = head ? Net::HTTP::Head : Net::HTTP::Get
|
2015-10-30 03:54:12 +03:00
|
|
|
response = request uri, fetch_type, last_modified do |req|
|
2020-06-10 20:46:05 +03:00
|
|
|
headers.each {|k,v| req.add_field(k,v) }
|
2015-10-30 03:54:12 +03:00
|
|
|
end
|
2011-06-01 07:45:05 +04:00
|
|
|
|
|
|
|
case response
|
|
|
|
when Net::HTTPOK, Net::HTTPNotModified then
|
2020-12-23 02:45:19 +03:00
|
|
|
response.uri = uri
|
2011-06-01 07:45:05 +04:00
|
|
|
head ? response : response.body
|
|
|
|
when Net::HTTPMovedPermanently, Net::HTTPFound, Net::HTTPSeeOther,
|
|
|
|
Net::HTTPTemporaryRedirect then
|
|
|
|
raise FetchError.new('too many redirects', uri) if depth > 10
|
|
|
|
|
2016-09-28 03:57:53 +03:00
|
|
|
unless location = response['Location']
|
|
|
|
raise FetchError.new("redirecting but no redirect location was given", uri)
|
|
|
|
end
|
2019-12-13 14:19:08 +03:00
|
|
|
location = parse_uri location
|
2012-04-20 03:07:48 +04:00
|
|
|
|
|
|
|
if https?(uri) && !https?(location)
|
|
|
|
raise FetchError.new("redirecting to non-https resource: #{location}", uri)
|
|
|
|
end
|
|
|
|
|
2011-06-01 07:45:05 +04:00
|
|
|
fetch_http(location, last_modified, head, depth + 1)
|
|
|
|
else
|
|
|
|
raise FetchError.new("bad response #{response.message} #{response.code}", uri)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
alias :fetch_https :fetch_http
|
|
|
|
|
2008-06-18 02:04:18 +04:00
|
|
|
##
|
|
|
|
# Downloads +uri+ and returns it as a String.
|
|
|
|
|
2008-06-26 06:06:00 +04:00
|
|
|
def fetch_path(uri, mtime = nil, head = false)
|
2019-12-13 14:19:08 +03:00
|
|
|
uri = parse_uri uri
|
2012-11-29 10:52:18 +04:00
|
|
|
|
|
|
|
unless uri.scheme
|
|
|
|
raise ArgumentError, "uri scheme is invalid: #{uri.scheme.inspect}"
|
|
|
|
end
|
2011-06-01 07:45:05 +04:00
|
|
|
|
|
|
|
data = send "fetch_#{uri.scheme}", uri, mtime, head
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2020-04-19 19:06:02 +03:00
|
|
|
if data and !head and uri.to_s.end_with?(".gz")
|
2012-11-29 10:52:18 +04:00
|
|
|
begin
|
2018-05-18 04:39:13 +03:00
|
|
|
data = Gem::Util.gunzip data
|
2012-11-29 10:52:18 +04:00
|
|
|
rescue Zlib::GzipFile::Error
|
2019-12-13 14:19:08 +03:00
|
|
|
raise FetchError.new("server did not return a valid file", uri)
|
2012-11-29 10:52:18 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-26 06:06:00 +04:00
|
|
|
data
|
2007-11-10 10:48:56 +03:00
|
|
|
rescue Timeout::Error
|
2019-12-13 14:19:08 +03:00
|
|
|
raise UnknownHostError.new('timed out', uri)
|
2018-12-14 11:01:40 +03:00
|
|
|
rescue IOError, SocketError, SystemCallError,
|
2020-12-08 10:33:39 +03:00
|
|
|
*(OpenSSL::SSL::SSLError if Gem::HAVE_OPENSSL) => e
|
2012-11-29 10:52:18 +04:00
|
|
|
if e.message =~ /getaddrinfo/
|
2019-12-13 14:19:08 +03:00
|
|
|
raise UnknownHostError.new('no such name', uri)
|
2012-11-29 10:52:18 +04:00
|
|
|
else
|
2019-12-13 14:19:08 +03:00
|
|
|
raise FetchError.new("#{e.class}: #{e}", uri)
|
2012-11-29 10:52:18 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
def fetch_s3(uri, mtime = nil, head = false)
|
2019-07-04 02:35:59 +03:00
|
|
|
begin
|
|
|
|
public_uri = s3_uri_signer(uri).sign
|
|
|
|
rescue Gem::S3URISigner::ConfigurationError, Gem::S3URISigner::InstanceProfileError => e
|
|
|
|
raise FetchError.new(e.message, "s3://#{uri.host}")
|
|
|
|
end
|
2014-09-14 07:30:02 +04:00
|
|
|
fetch_https public_uri, mtime, head
|
|
|
|
end
|
|
|
|
|
2019-07-04 02:35:59 +03: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 10:52:18 +04:00
|
|
|
##
|
|
|
|
# Downloads +uri+ to +path+ if necessary. If no path is given, it just
|
|
|
|
# passes the data.
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def cache_update_path(uri, path = nil, update = true)
|
2012-11-29 10:52:18 +04:00
|
|
|
mtime = path && File.stat(path).mtime rescue nil
|
|
|
|
|
2014-02-04 04:48:31 +04:00
|
|
|
data = fetch_path(uri, mtime)
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2014-02-04 04:48:31 +04:00
|
|
|
if data == nil # indicates the server returned 304 Not Modified
|
|
|
|
return Gem.read_binary(path)
|
|
|
|
end
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2014-02-04 04:48:31 +04:00
|
|
|
if update and path
|
2016-06-18 08:11:55 +03:00
|
|
|
Gem.write_binary(path, data)
|
2012-11-29 10:52:18 +04:00
|
|
|
end
|
2014-02-04 04:48:31 +04:00
|
|
|
|
|
|
|
data
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
|
2008-06-18 02: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-26 06:06:00 +04:00
|
|
|
def request(uri, request_class, last_modified = nil)
|
2014-09-14 07: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-31 02:11:11 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
request.fetch do |req|
|
|
|
|
yield req if block_given?
|
2013-07-23 02:46:50 +04:00
|
|
|
end
|
2011-06-01 07:45:05 +04:00
|
|
|
end
|
|
|
|
|
2012-04-20 03:07:48 +04:00
|
|
|
def https?(uri)
|
|
|
|
uri.scheme.downcase == 'https'
|
|
|
|
end
|
|
|
|
|
2014-11-17 06:55:02 +03:00
|
|
|
def close_all
|
2020-06-10 20:46:05 +03:00
|
|
|
@pools.each_value {|pool| pool.close_all }
|
2014-11-17 06:55:02 +03:00
|
|
|
end
|
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
private
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def proxy_for(proxy, uri)
|
2014-09-14 07:30:02 +04:00
|
|
|
Gem::Request.proxy_uri(proxy || Gem::Request.get_proxy_from_env(uri.scheme))
|
|
|
|
end
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def pools_for(proxy)
|
2014-09-14 07:30:02 +04:00
|
|
|
@pool_lock.synchronize do
|
|
|
|
@pools[proxy] ||= Gem::Request::ConnectionPools.new proxy, @cert_files
|
|
|
|
end
|
|
|
|
end
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|