2007-11-10 02:48:56 -05:00
|
|
|
require 'rubygems'
|
2011-01-18 19:08:49 -05:00
|
|
|
require 'rubygems/user_interaction'
|
|
|
|
require 'uri'
|
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
|
|
|
|
|
|
|
|
@connections = {}
|
|
|
|
@requests = Hash.new 0
|
2007-11-10 02:48:56 -05:00
|
|
|
@proxy_uri =
|
|
|
|
case proxy
|
|
|
|
when :no_proxy then nil
|
|
|
|
when nil then get_proxy_from_env
|
|
|
|
when URI::HTTP then proxy
|
|
|
|
else URI.parse(proxy)
|
|
|
|
end
|
2011-05-31 23:45:05 -04:00
|
|
|
@user_agent = user_agent
|
2012-11-29 01:52:18 -05:00
|
|
|
@env_no_proxy = get_no_proxy_from_env
|
|
|
|
|
|
|
|
@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
|
|
|
|
URI.parse "#{res.target}#{uri.path}"
|
|
|
|
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?
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
spec, source = found.sort_by { |(s,_)| s.version }.last
|
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
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
# Always escape URI's to deal with potential spaces and such
|
|
|
|
unless URI::Generic === source_uri
|
2010-04-22 04:24:42 -04:00
|
|
|
source_uri = URI.parse(URI.const_defined?(:DEFAULT_PARSER) ?
|
2011-05-31 23:45:05 -04:00
|
|
|
URI::DEFAULT_PARSER.escape(source_uri.to_s) :
|
|
|
|
URI.escape(source_uri.to_s))
|
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
|
2008-09-25 06:13:50 -04:00
|
|
|
when 'http', 'https' then
|
2008-03-31 18:40:06 -04:00
|
|
|
unless File.exist? local_gem_path then
|
|
|
|
begin
|
|
|
|
say "Downloading gem #{gem_file_name}" if
|
|
|
|
Gem.configuration.really_verbose
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
say "Failed, downloading gem #{alternate_name}" if
|
|
|
|
Gem.configuration.really_verbose
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
say "Using local gem #{local_gem_path}" if
|
|
|
|
Gem.configuration.really_verbose
|
|
|
|
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
|
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
source_path = unescape source_path
|
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
|
|
|
|
|
|
|
|
say "Using local gem #{local_gem_path}" if
|
|
|
|
Gem.configuration.really_verbose
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
if data and !head and uri.to_s =~ /gz$/
|
|
|
|
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
|
|
|
|
|
|
|
|
##
|
|
|
|
# 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
|
|
|
|
|
|
|
|
if mtime && Net::HTTPNotModified === fetch_path(uri, mtime, true)
|
|
|
|
Gem.read_binary(path)
|
|
|
|
else
|
|
|
|
data = fetch_path(uri)
|
|
|
|
|
2012-12-06 00:13:08 -05:00
|
|
|
if update and path then
|
2012-11-29 01:52:18 -05:00
|
|
|
open(path, 'wb') do |io|
|
|
|
|
io.write data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
data
|
|
|
|
end
|
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
|
|
|
|
|
|
|
|
def escape(str)
|
|
|
|
return unless str
|
2011-01-18 19:08:49 -05:00
|
|
|
@uri_parser ||= uri_escaper
|
|
|
|
@uri_parser.escape str
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def unescape(str)
|
|
|
|
return unless str
|
2011-01-18 19:08:49 -05:00
|
|
|
@uri_parser ||= uri_escaper
|
|
|
|
@uri_parser.unescape str
|
|
|
|
end
|
|
|
|
|
|
|
|
def uri_escaper
|
|
|
|
URI::Parser.new
|
|
|
|
rescue NameError
|
|
|
|
URI
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
##
|
|
|
|
# Returns list of no_proxy entries (if any) from the environment
|
|
|
|
|
|
|
|
def get_no_proxy_from_env
|
|
|
|
env_no_proxy = ENV['no_proxy'] || ENV['NO_PROXY']
|
|
|
|
|
|
|
|
return [] if env_no_proxy.nil? or env_no_proxy.empty?
|
|
|
|
|
|
|
|
env_no_proxy.split(/\s*,\s*/)
|
|
|
|
end
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
##
|
2007-11-10 02:48:56 -05:00
|
|
|
# Returns an HTTP proxy URI if one is set in the environment variables.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def get_proxy_from_env
|
|
|
|
env_proxy = ENV['http_proxy'] || ENV['HTTP_PROXY']
|
|
|
|
|
|
|
|
return nil if env_proxy.nil? or env_proxy.empty?
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
uri = URI.parse(normalize_uri(env_proxy))
|
2007-11-10 02:48:56 -05:00
|
|
|
|
|
|
|
if uri and uri.user.nil? and uri.password.nil? then
|
|
|
|
# Probably we have http_proxy_* variables?
|
|
|
|
uri.user = escape(ENV['http_proxy_user'] || ENV['HTTP_PROXY_USER'])
|
|
|
|
uri.password = escape(ENV['http_proxy_pass'] || ENV['HTTP_PROXY_PASS'])
|
|
|
|
end
|
|
|
|
|
|
|
|
uri
|
|
|
|
end
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
##
|
2007-11-10 02:48:56 -05:00
|
|
|
# Normalize the URI by adding "http://" if it is missing.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def normalize_uri(uri)
|
2012-11-29 01:52:18 -05:00
|
|
|
(uri =~ /^(https?|ftp|file):/i) ? uri : "http://#{uri}"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
##
|
|
|
|
# Creates or an HTTP connection based on +uri+, or retrieves an existing
|
|
|
|
# connection, using a proxy if needed.
|
|
|
|
|
|
|
|
def connection_for(uri)
|
|
|
|
net_http_args = [uri.host, uri.port]
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
if @proxy_uri and not no_proxy?(uri.host) then
|
2008-06-17 18:04:18 -04:00
|
|
|
net_http_args += [
|
|
|
|
@proxy_uri.host,
|
|
|
|
@proxy_uri.port,
|
|
|
|
@proxy_uri.user,
|
|
|
|
@proxy_uri.password
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
connection_id = [Thread.current.object_id, *net_http_args].join ':'
|
2008-06-17 18:04:18 -04:00
|
|
|
@connections[connection_id] ||= Net::HTTP.new(*net_http_args)
|
|
|
|
connection = @connections[connection_id]
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
if https?(uri) and not connection.started? then
|
2012-04-19 19:07:48 -04:00
|
|
|
configure_connection_for_https(connection)
|
2012-07-23 20:31:31 -04:00
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
connection.start unless connection.started?
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
connection
|
2013-03-05 17:40:53 -05:00
|
|
|
rescue defined?(OpenSSL::SSL) ? OpenSSL::SSL::SSLError : Errno::EHOSTDOWN,
|
|
|
|
Errno::EHOSTDOWN => e
|
2012-11-29 01:52:18 -05:00
|
|
|
raise FetchError.new(e.message, uri)
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2012-04-19 19:07:48 -04:00
|
|
|
def configure_connection_for_https(connection)
|
|
|
|
require 'net/https'
|
|
|
|
connection.use_ssl = true
|
|
|
|
connection.verify_mode =
|
|
|
|
Gem.configuration.ssl_verify_mode || OpenSSL::SSL::VERIFY_PEER
|
|
|
|
store = OpenSSL::X509::Store.new
|
|
|
|
if Gem.configuration.ssl_ca_cert
|
|
|
|
if File.directory? Gem.configuration.ssl_ca_cert
|
|
|
|
store.add_path Gem.configuration.ssl_ca_cert
|
|
|
|
else
|
|
|
|
store.add_file Gem.configuration.ssl_ca_cert
|
|
|
|
end
|
|
|
|
else
|
|
|
|
store.set_default_paths
|
|
|
|
add_rubygems_trusted_certs(store)
|
|
|
|
end
|
|
|
|
connection.cert_store = store
|
2013-03-05 17:40:53 -05:00
|
|
|
rescue LoadError => e
|
|
|
|
raise unless (e.respond_to?(:path) && e.path == 'openssl') ||
|
|
|
|
e.message =~ / -- openssl$/
|
|
|
|
|
|
|
|
raise Gem::Exception.new(
|
|
|
|
'Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPS sources')
|
2012-04-19 19:07:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_rubygems_trusted_certs(store)
|
2012-11-29 01:52:18 -05:00
|
|
|
pattern = File.expand_path("./ssl_certs/*.pem", File.dirname(__FILE__))
|
|
|
|
Dir.glob(pattern).each do |ssl_cert_file|
|
2012-04-19 19:07:48 -04:00
|
|
|
store.add_file ssl_cert_file
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def no_proxy? host
|
|
|
|
host = host.downcase
|
|
|
|
@env_no_proxy.each do |pattern|
|
|
|
|
pattern = pattern.downcase
|
|
|
|
return true if host[-pattern.length, pattern.length ] == pattern
|
|
|
|
end
|
|
|
|
return false
|
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)
|
2008-06-17 18:04:18 -04:00
|
|
|
request = request_class.new uri.request_uri
|
|
|
|
|
|
|
|
unless uri.nil? || uri.user.nil? || uri.user.empty? then
|
|
|
|
request.basic_auth uri.user, uri.password
|
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
request.add_field 'User-Agent', @user_agent
|
2008-06-17 18:04:18 -04:00
|
|
|
request.add_field 'Connection', 'keep-alive'
|
|
|
|
request.add_field 'Keep-Alive', '30'
|
|
|
|
|
2008-06-25 22:06:00 -04:00
|
|
|
if last_modified then
|
2008-09-25 06:13:50 -04:00
|
|
|
last_modified = last_modified.utc
|
2008-06-25 22:06:00 -04:00
|
|
|
request.add_field 'If-Modified-Since', last_modified.rfc2822
|
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
yield request if block_given?
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
connection = connection_for uri
|
|
|
|
|
|
|
|
retried = false
|
2008-06-25 22:06:00 -04:00
|
|
|
bad_response = false
|
2008-06-17 18:04:18 -04:00
|
|
|
|
|
|
|
begin
|
|
|
|
@requests[connection.object_id] += 1
|
2010-02-21 21:52:35 -05:00
|
|
|
|
|
|
|
say "#{request.method} #{uri}" if
|
|
|
|
Gem.configuration.really_verbose
|
2011-01-18 19:08:49 -05:00
|
|
|
|
|
|
|
file_name = File.basename(uri.path)
|
|
|
|
# perform download progress reporter only for gems
|
|
|
|
if request.response_body_permitted? && file_name =~ /\.gem$/
|
|
|
|
reporter = ui.download_reporter
|
|
|
|
response = connection.request(request) do |incomplete_response|
|
|
|
|
if Net::HTTPOK === incomplete_response
|
|
|
|
reporter.fetch(file_name, incomplete_response.content_length)
|
|
|
|
downloaded = 0
|
|
|
|
data = ''
|
|
|
|
|
|
|
|
incomplete_response.read_body do |segment|
|
|
|
|
data << segment
|
|
|
|
downloaded += segment.length
|
|
|
|
reporter.update(downloaded)
|
|
|
|
end
|
|
|
|
reporter.done
|
|
|
|
if incomplete_response.respond_to? :body=
|
|
|
|
incomplete_response.body = data
|
|
|
|
else
|
|
|
|
incomplete_response.instance_variable_set(:@body, data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
response = connection.request request
|
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
say "#{response.code} #{response.message}" if
|
2008-06-17 18:04:18 -04:00
|
|
|
Gem.configuration.really_verbose
|
2010-02-21 21:52:35 -05:00
|
|
|
|
2008-06-25 22:06:00 -04:00
|
|
|
rescue Net::HTTPBadResponse
|
2010-02-21 21:52:35 -05:00
|
|
|
say "bad response" if Gem.configuration.really_verbose
|
|
|
|
|
2008-06-25 22:06:00 -04:00
|
|
|
reset connection
|
|
|
|
|
|
|
|
raise FetchError.new('too many bad responses', uri) if bad_response
|
|
|
|
|
|
|
|
bad_response = true
|
|
|
|
retry
|
2008-09-25 06:13:50 -04:00
|
|
|
# HACK work around EOFError bug in Net::HTTP
|
|
|
|
# NOTE Errno::ECONNABORTED raised a lot on Windows, and make impossible
|
|
|
|
# to install gems.
|
2010-04-22 04:24:42 -04:00
|
|
|
rescue EOFError, Timeout::Error,
|
|
|
|
Errno::ECONNABORTED, Errno::ECONNRESET, Errno::EPIPE
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
requests = @requests[connection.object_id]
|
|
|
|
say "connection reset after #{requests} requests, retrying" if
|
|
|
|
Gem.configuration.really_verbose
|
|
|
|
|
|
|
|
raise FetchError.new('too many connection resets', uri) if retried
|
|
|
|
|
2008-06-25 22:06:00 -04:00
|
|
|
reset connection
|
2008-06-17 18:04:18 -04:00
|
|
|
|
|
|
|
retried = true
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
2008-06-25 22:06:00 -04:00
|
|
|
##
|
|
|
|
# Resets HTTP connection +connection+.
|
|
|
|
|
|
|
|
def reset(connection)
|
|
|
|
@requests.delete connection.object_id
|
|
|
|
|
|
|
|
connection.finish
|
|
|
|
connection.start
|
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
def user_agent
|
|
|
|
ua = "RubyGems/#{Gem::VERSION} #{Gem::Platform.local}"
|
|
|
|
|
|
|
|
ruby_version = RUBY_VERSION
|
|
|
|
ruby_version += 'dev' if RUBY_PATCHLEVEL == -1
|
|
|
|
|
|
|
|
ua << " Ruby/#{ruby_version} (#{RUBY_RELEASE_DATE}"
|
|
|
|
if RUBY_PATCHLEVEL >= 0 then
|
|
|
|
ua << " patchlevel #{RUBY_PATCHLEVEL}"
|
|
|
|
elsif defined?(RUBY_REVISION) then
|
|
|
|
ua << " revision #{RUBY_REVISION}"
|
|
|
|
end
|
|
|
|
ua << ")"
|
|
|
|
|
|
|
|
ua << " #{RUBY_ENGINE}" if defined?(RUBY_ENGINE) and RUBY_ENGINE != 'ruby'
|
|
|
|
|
|
|
|
ua
|
|
|
|
end
|
|
|
|
|
2012-04-19 19:07:48 -04:00
|
|
|
def https?(uri)
|
|
|
|
uri.scheme.downcase == 'https'
|
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|