2007-11-10 02:48:56 -05:00
|
|
|
require 'rubygems'
|
2013-09-14 04:59:02 -04:00
|
|
|
require 'rubygems/request'
|
|
|
|
require 'rubygems/uri_formatter'
|
2011-01-18 19:08:49 -05:00
|
|
|
require 'rubygems/user_interaction'
|
2014-09-13 23:30:02 -04:00
|
|
|
require 'rubygems/request/connection_pools'
|
2012-11-29 01:52:18 -05:00
|
|
|
require 'resolv'
|
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
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
attr_accessor :uri
|
|
|
|
|
|
|
|
def initialize(message, uri)
|
|
|
|
super message
|
|
|
|
@uri = uri
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
#
|
|
|
|
# +dns+: An object to use for DNS resolution of the API endpoint.
|
|
|
|
# By default, use Resolv::DNS.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def initialize(proxy=nil, dns=Resolv::DNS.new)
|
2011-01-18 19:08:49 -05:00
|
|
|
require 'net/http'
|
|
|
|
require 'stringio'
|
|
|
|
require 'time'
|
|
|
|
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 = {}
|
|
|
|
@pool_lock = Mutex.new
|
|
|
|
@cert_files = Gem::Request.get_cert_files
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
@dns = dns
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Given a source at +uri+, calculate what hostname to actually
|
|
|
|
# connect to query the data for it.
|
|
|
|
|
|
|
|
def api_endpoint(uri)
|
|
|
|
host = uri.host
|
|
|
|
|
|
|
|
begin
|
|
|
|
res = @dns.getresource "_rubygems._tcp.#{host}",
|
|
|
|
Resolv::DNS::Resource::IN::SRV
|
|
|
|
rescue Resolv::ResolvError
|
|
|
|
uri
|
|
|
|
else
|
2013-11-18 19:34:13 -05:00
|
|
|
URI.parse "#{uri.scheme}://#{res.target}#{uri.path}"
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
2007-11-10 02:48:56 -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
|
|
|
|
# larger, more emcompassing effort. -erikh
|
|
|
|
|
|
|
|
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?
|
|
|
|
|
2013-09-24 20:53:19 -04:00
|
|
|
spec, source = found.max_by { |(s,_)| s.version }
|
2011-03-01 04:41:32 -05:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
download spec, source.uri.to_s
|
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)
|
2012-11-29 01:52:18 -05:00
|
|
|
cache_dir =
|
|
|
|
if Dir.pwd == install_dir then # see fetch_command
|
|
|
|
install_dir
|
|
|
|
elsif File.writable? install_dir then
|
|
|
|
File.join install_dir, "cache"
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2014-02-03 19:48:31 -05:00
|
|
|
# Always escape URI's to deal with potential spaces and such
|
|
|
|
# It should also be considered that source_uri may already be
|
|
|
|
# a valid URI with escaped characters. e.g. "{DESede}" is encoded
|
|
|
|
# as "%7BDESede%7D". If this is escaped again the percentage
|
|
|
|
# symbols will be escaped.
|
|
|
|
unless source_uri.is_a?(URI::Generic)
|
|
|
|
begin
|
|
|
|
source_uri = URI.parse(source_uri)
|
|
|
|
rescue
|
|
|
|
source_uri = URI.parse(URI.const_defined?(:DEFAULT_PARSER) ?
|
|
|
|
URI::DEFAULT_PARSER.escape(source_uri.to_s) :
|
|
|
|
URI.escape(source_uri.to_s))
|
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
|
|
|
|
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
|
2008-03-31 18:40:06 -04:00
|
|
|
unless File.exist? local_gem_path then
|
|
|
|
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
|
2008-03-31 18:40:06 -04:00
|
|
|
rescue Gem::RemoteFetcher::FetchError
|
|
|
|
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'
|
|
|
|
|
2011-03-01 04:41:32 -05:00
|
|
|
remote_gem_path = 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 &&
|
|
|
|
!source_uri.path.include?(':') then
|
|
|
|
"#{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.
|
|
|
|
|
|
|
|
def fetch_file uri, *_
|
|
|
|
Gem.read_binary correct_for_windows_path uri.path
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# HTTP Fetcher. Dispatched by +fetch_path+. Use it instead.
|
|
|
|
|
|
|
|
def fetch_http uri, last_modified = nil, head = false, depth = 0
|
|
|
|
fetch_type = head ? Net::HTTP::Head : Net::HTTP::Get
|
|
|
|
response = request uri, fetch_type, last_modified
|
|
|
|
|
|
|
|
case response
|
|
|
|
when Net::HTTPOK, Net::HTTPNotModified then
|
2014-09-13 23:30:02 -04:00
|
|
|
response.uri = uri if response.respond_to? :uri
|
2011-05-31 23: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
|
|
|
|
|
|
|
|
location = URI.parse response['Location']
|
2012-04-19 19:07:48 -04:00
|
|
|
|
|
|
|
if https?(uri) && !https?(location)
|
|
|
|
raise FetchError.new("redirecting to non-https resource: #{location}", uri)
|
|
|
|
end
|
|
|
|
|
2011-05-31 23: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-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)
|
2011-05-31 23:45:05 -04:00
|
|
|
uri = URI.parse uri unless URI::Generic === uri
|
|
|
|
|
|
|
|
raise ArgumentError, "bad uri: #{uri}" unless 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
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
if data and !head and uri.to_s =~ /\.gz$/
|
2012-11-29 01:52:18 -05:00
|
|
|
begin
|
|
|
|
data = Gem.gunzip data
|
|
|
|
rescue Zlib::GzipFile::Error
|
|
|
|
raise FetchError.new("server did not return a valid file", uri.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-25 22:06:00 -04:00
|
|
|
data
|
2008-06-17 18:04:18 -04:00
|
|
|
rescue FetchError
|
|
|
|
raise
|
2007-11-10 02:48:56 -05:00
|
|
|
rescue Timeout::Error
|
2012-11-29 01:52:18 -05:00
|
|
|
raise UnknownHostError.new('timed out', uri.to_s)
|
2007-12-20 03:39:12 -05:00
|
|
|
rescue IOError, SocketError, SystemCallError => e
|
2012-11-29 01:52:18 -05:00
|
|
|
if e.message =~ /getaddrinfo/
|
|
|
|
raise UnknownHostError.new('no such name', uri.to_s)
|
|
|
|
else
|
|
|
|
raise FetchError.new("#{e.class}: #{e}", uri.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
def fetch_s3(uri, mtime = nil, head = false)
|
|
|
|
public_uri = sign_s3_url(uri)
|
|
|
|
fetch_https public_uri, mtime, head
|
|
|
|
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.
|
|
|
|
|
2012-12-06 00:13:08 -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
|
|
|
|
2014-02-03 19:48:31 -05:00
|
|
|
if data == nil # indicates the server returned 304 Not Modified
|
|
|
|
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
|
|
|
|
open(path, 'wb') do |io|
|
|
|
|
io.flock(File::LOCK_EX)
|
|
|
|
io.write data
|
|
|
|
end
|
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
|
|
|
##
|
2007-11-10 02:48:56 -05:00
|
|
|
# Returns the size of +uri+ in bytes.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2008-06-25 22:06:00 -04:00
|
|
|
def fetch_size(uri) # TODO: phase this out
|
|
|
|
response = fetch_path(uri, nil, true)
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-06-25 22:06:00 -04:00
|
|
|
response['content-length'].to_i
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2011-03-01 04:41:32 -05:00
|
|
|
def correct_for_windows_path(path)
|
|
|
|
if path[0].chr == '/' && path[1].chr =~ /[a-z]/i && path[2].chr == ':'
|
|
|
|
path = path[1..-1]
|
|
|
|
else
|
|
|
|
path
|
|
|
|
end
|
|
|
|
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-09-13 23:30:02 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
# we have our own signing code here to avoid a dependency on the aws-sdk gem
|
|
|
|
# fortunately, a simple GET request isn't too complex to sign properly
|
|
|
|
def sign_s3_url(uri, expiration = nil)
|
|
|
|
require 'base64'
|
|
|
|
require 'openssl'
|
|
|
|
|
|
|
|
unless uri.user && uri.password
|
|
|
|
raise FetchError.new("credentials needed in s3 source, like s3://key:secret@bucket-name/", uri.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
expiration ||= s3_expiration
|
|
|
|
canonical_path = "/#{uri.host}#{uri.path}"
|
|
|
|
payload = "GET\n\n\n#{expiration}\n#{canonical_path}"
|
|
|
|
digest = OpenSSL::HMAC.digest('sha1', uri.password, payload)
|
|
|
|
# URI.escape is deprecated, and there isn't yet a replacement that does quite what we want
|
|
|
|
signature = Base64.encode64(digest).gsub("\n", '').gsub(/[\+\/=]/) { |c| BASE64_URI_TRANSLATE[c] }
|
|
|
|
URI.parse("https://#{uri.host}.s3.amazonaws.com#{uri.path}?AWSAccessKeyId=#{uri.user}&Expires=#{expiration}&Signature=#{signature}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def s3_expiration
|
|
|
|
(Time.now + 3600).to_i # one hour from now
|
|
|
|
end
|
|
|
|
|
|
|
|
BASE64_URI_TRANSLATE = { '+' => '%2B', '/' => '%2F', '=' => '%3D' }.freeze
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def proxy_for proxy, uri
|
|
|
|
Gem::Request.proxy_uri(proxy || Gem::Request.get_proxy_from_env(uri.scheme))
|
|
|
|
end
|
|
|
|
|
|
|
|
def pools_for proxy
|
|
|
|
@pool_lock.synchronize do
|
|
|
|
@pools[proxy] ||= Gem::Request::ConnectionPools.new proxy, @cert_files
|
|
|
|
end
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|