summaryrefslogtreecommitdiff
path: root/lib/rubygems/remote_fetcher.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rubygems/remote_fetcher.rb')
-rw-r--r--lib/rubygems/remote_fetcher.rb127
1 files changed, 62 insertions, 65 deletions
diff --git a/lib/rubygems/remote_fetcher.rb b/lib/rubygems/remote_fetcher.rb
index a14c42f00e..c3a41592f6 100644
--- a/lib/rubygems/remote_fetcher.rb
+++ b/lib/rubygems/remote_fetcher.rb
@@ -1,12 +1,12 @@
# frozen_string_literal: true
-require 'rubygems'
-require 'rubygems/request'
-require 'rubygems/request/connection_pools'
-require 'rubygems/s3_uri_signer'
-require 'rubygems/uri_formatter'
-require 'rubygems/uri_parsing'
-require 'rubygems/user_interaction'
-require 'resolv'
+
+require_relative "../rubygems"
+require_relative "request"
+require_relative "request/connection_pools"
+require_relative "s3_uri_signer"
+require_relative "uri_formatter"
+require_relative "uri"
+require_relative "user_interaction"
##
# RemoteFetcher handles the details of fetching gems and gem information from
@@ -14,30 +14,24 @@ require 'resolv'
class Gem::RemoteFetcher
include Gem::UserInteraction
- include Gem::UriParsing
##
# A FetchError exception wraps up the various possible IO and HTTP failures
# that could happen while downloading from the internet.
class FetchError < Gem::Exception
- include Gem::UriParsing
-
##
# The URI which was being accessed when the exception happened.
attr_accessor :uri, :original_uri
def initialize(message, uri)
- super message
-
- uri = parse_uri(uri)
+ uri = Gem::Uri.new(uri)
- @original_uri = uri.dup
+ super uri.redact_credentials_from(message)
- uri.password = 'REDACTED' if uri.respond_to?(:password) && uri.password
-
- @uri = uri.to_s
+ @original_uri = uri.to_s
+ @uri = uri.redacted.to_s
end
def to_s # :nodoc:
@@ -51,6 +45,7 @@ class Gem::RemoteFetcher
class UnknownHostError < FetchError
end
+ deprecate_constant(:UnknownHostError)
@fetcher = nil
@@ -58,7 +53,7 @@ class Gem::RemoteFetcher
# Cached RemoteFetcher instance.
def self.fetcher
- @fetcher ||= self.new Gem.configuration[:http_proxy]
+ @fetcher ||= new Gem.configuration[:http_proxy]
end
attr_accessor :headers
@@ -78,16 +73,16 @@ class Gem::RemoteFetcher
# fetching the gem.
def initialize(proxy=nil, dns=nil, headers={})
- require 'rubygems/core_ext/tcpsocket_init' if Gem.configuration.ipv4_fallback_enabled
- require 'net/http'
- require 'stringio'
- require 'uri'
+ require_relative "core_ext/tcpsocket_init" if Gem.configuration.ipv4_fallback_enabled
+ require_relative "vendored_net_http"
+ require "stringio"
+ require_relative "vendor/uri/lib/uri"
Socket.do_not_reverse_lookup = true
@proxy = proxy
@pools = {}
- @pool_lock = Mutex.new
+ @pool_lock = Thread::Mutex.new
@cert_files = Gem::Request.get_cert_files
@headers = headers
@@ -120,7 +115,7 @@ class Gem::RemoteFetcher
cache_dir =
if Dir.pwd == install_dir # see fetch_command
install_dir
- elsif File.writable?(install_cache_dir) || (File.writable?(install_dir) && (not File.exist?(install_cache_dir)))
+ elsif File.writable?(install_cache_dir) || (File.writable?(install_dir) && !File.exist?(install_cache_dir))
install_cache_dir
else
File.join Gem.user_dir, "cache"
@@ -130,26 +125,30 @@ class Gem::RemoteFetcher
local_gem_path = File.join cache_dir, gem_file_name
require "fileutils"
- FileUtils.mkdir_p cache_dir rescue nil unless File.exist? cache_dir
+ begin
+ FileUtils.mkdir_p cache_dir
+ rescue StandardError
+ nil
+ end unless File.exist? cache_dir
- source_uri = parse_uri(source_uri)
+ source_uri = Gem::Uri.new(source_uri)
scheme = source_uri.scheme
- # URI.parse gets confused by MS Windows paths with forward slashes.
- scheme = nil if scheme =~ /^[a-z]$/i
+ # Gem::URI.parse gets confused by MS Windows paths with forward slashes.
+ scheme = nil if /^[a-z]$/i.match?(scheme)
# REFACTOR: split this up and dispatch on scheme (eg download_http)
# REFACTOR: be sure to clean up fake fetcher when you do this... cleaner
case scheme
- when 'http', 'https', 's3' then
+ when "http", "https", "s3" then
unless File.exist? local_gem_path
begin
verbose "Downloading gem #{gem_file_name}"
remote_gem_path = source_uri + "gems/#{gem_file_name}"
- self.cache_update_path remote_gem_path, local_gem_path
+ cache_update_path remote_gem_path, local_gem_path
rescue FetchError
raise if spec.original_platform == spec.platform
@@ -159,15 +158,15 @@ class Gem::RemoteFetcher
remote_gem_path = source_uri + "gems/#{alternate_name}"
- self.cache_update_path remote_gem_path, local_gem_path
+ cache_update_path remote_gem_path, local_gem_path
end
end
- when 'file' then
+ when "file" then
begin
path = source_uri.path
- path = File.dirname(path) if File.extname(path) == '.gem'
+ path = File.dirname(path) if File.extname(path) == ".gem"
- remote_gem_path = Gem::Util.correct_for_windows_path(File.join(path, 'gems', gem_file_name))
+ remote_gem_path = Gem::Util.correct_for_windows_path(File.join(path, "gems", gem_file_name))
FileUtils.cp(remote_gem_path, local_gem_path)
rescue Errno::EACCES
@@ -175,13 +174,13 @@ class Gem::RemoteFetcher
end
verbose "Using local gem #{local_gem_path}"
- when nil then # TODO test for local overriding cache
+ when nil then # TODO: test for local overriding cache
source_path = if Gem.win_platform? && source_uri.scheme &&
- !source_uri.path.include?(':')
- "#{source_uri.scheme}:#{source_uri.path}"
- else
- source_uri.path
- end
+ !source_uri.path.include?(":")
+ "#{source_uri.scheme}:#{source_uri.path}"
+ else
+ source_uri.path
+ end
source_path = Gem::UriFormatter.new(source_path).unescape
@@ -211,23 +210,23 @@ class Gem::RemoteFetcher
# 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
+ fetch_type = head ? Gem::Net::HTTP::Head : Gem::Net::HTTP::Get
response = request uri, fetch_type, last_modified do |req|
headers.each {|k,v| req.add_field(k,v) }
end
case response
- when Net::HTTPOK, Net::HTTPNotModified then
+ when Gem::Net::HTTPOK, Gem::Net::HTTPNotModified then
response.uri = uri
head ? response : response.body
- when Net::HTTPMovedPermanently, Net::HTTPFound, Net::HTTPSeeOther,
- Net::HTTPTemporaryRedirect then
- raise FetchError.new('too many redirects', uri) if depth > 10
+ when Gem::Net::HTTPMovedPermanently, Gem::Net::HTTPFound, Gem::Net::HTTPSeeOther,
+ Gem::Net::HTTPTemporaryRedirect then
+ raise FetchError.new("too many redirects", uri) if depth > 10
- unless location = response['Location']
+ unless location = response["Location"]
raise FetchError.new("redirecting but no redirect location was given", uri)
end
- location = parse_uri location
+ location = Gem::Uri.new location
if https?(uri) && !https?(location)
raise FetchError.new("redirecting to non-https resource: #{location}", uri)
@@ -239,13 +238,13 @@ class Gem::RemoteFetcher
end
end
- alias :fetch_https :fetch_http
+ alias_method :fetch_https, :fetch_http
##
# Downloads +uri+ and returns it as a String.
def fetch_path(uri, mtime = nil, head = false)
- uri = parse_uri uri
+ uri = Gem::Uri.new uri
unless uri.scheme
raise ArgumentError, "uri scheme is invalid: #{uri.scheme.inspect}"
@@ -253,7 +252,7 @@ class Gem::RemoteFetcher
data = send "fetch_#{uri.scheme}", uri, mtime, head
- if data and !head and uri.to_s.end_with?(".gz")
+ if data && !head && uri.to_s.end_with?(".gz")
begin
data = Gem::Util.gunzip data
rescue Zlib::GzipFile::Error
@@ -262,15 +261,9 @@ class Gem::RemoteFetcher
end
data
- rescue Timeout::Error
- raise UnknownHostError.new('timed out', uri)
- rescue IOError, SocketError, SystemCallError,
+ rescue Gem::Timeout::Error, IOError, SocketError, SystemCallError,
*(OpenSSL::SSL::SSLError if Gem::HAVE_OPENSSL) => e
- if e.message =~ /getaddrinfo/
- raise UnknownHostError.new('no such name', uri)
- else
- raise FetchError.new("#{e.class}: #{e}", uri)
- end
+ raise FetchError.new("#{e.class}: #{e}", uri)
end
def fetch_s3(uri, mtime = nil, head = false)
@@ -292,7 +285,11 @@ class Gem::RemoteFetcher
# passes the data.
def cache_update_path(uri, path = nil, update = true)
- mtime = path && File.stat(path).mtime rescue nil
+ mtime = begin
+ path && File.stat(path).mtime
+ rescue StandardError
+ nil
+ end
data = fetch_path(uri, mtime)
@@ -300,7 +297,7 @@ class Gem::RemoteFetcher
return Gem.read_binary(path)
end
- if update and path
+ if update && path
Gem.write_binary(path, data)
end
@@ -308,8 +305,8 @@ class Gem::RemoteFetcher
end
##
- # Performs a Net::HTTP request of type +request_class+ on +uri+ returning
- # a Net::HTTP response object. request maintains a table of persistent
+ # Performs a Gem::Net::HTTP request of type +request_class+ on +uri+ returning
+ # a Gem::Net::HTTP response object. request maintains a table of persistent
# connections to reduce connect overhead.
def request(uri, request_class, last_modified = nil)
@@ -324,11 +321,11 @@ class Gem::RemoteFetcher
end
def https?(uri)
- uri.scheme.downcase == 'https'
+ uri.scheme.casecmp("https").zero?
end
def close_all
- @pools.each_value {|pool| pool.close_all }
+ @pools.each_value(&:close_all)
end
private