summaryrefslogtreecommitdiff
path: root/lib/rubygems/remote_fetcher.rb
diff options
context:
space:
mode:
authorDaniel Niknam <[email protected]>2021-08-22 01:40:21 +1000
committerHiroshi SHIBATA <[email protected]>2021-08-31 19:06:14 +0900
commit3adc141a79cdab83671b7f68301901dd7726e7c4 ()
treef54b06d49939b7fe03358acdca0f5b178a583a12 /lib/rubygems/remote_fetcher.rb
parent19e1d3cdce96b9e58a0947b6fcbabd6da06cbd11 (diff)
[rubygems/rubygems] Refactor `Gem::RemoteFetcher::FetchError` initializer to `build` method
The `initialize` method is already doing a lot and by adding the `Gem::PrintableUri` to redact sensitive information, things are getting complicated and hard to read here. For the start, I have refactored the `initialize` method into a class method called `build`. https://.com/rubygems/rubygems/commit/4312e8fdf5
Notes: Merged: https://.com/ruby/ruby/pull/4789
-rw-r--r--lib/rubygems/remote_fetcher.rb36
1 files changed, 21 insertions, 15 deletions
@@ -5,6 +5,7 @@ require_relative 'request/connection_pools'
require_relative 's3_uri_signer'
require_relative 'uri_formatter'
require_relative 'uri_parser'
require_relative 'user_interaction'
##
@@ -21,19 +22,24 @@ class Gem::RemoteFetcher
class FetchError < Gem::Exception
##
# The URI which was being accessed when the exception happened.
- attr_accessor :uri, :original_uri
-
- def initialize(message, uri)
- super message
- uri = Gem::UriParser.parse_uri(uri)
- @original_uri = uri.dup
- uri.password = 'REDACTED' if uri.respond_to?(:password) && uri.password
- @uri = uri.to_s
end
def to_s # :nodoc:
@@ -219,20 +225,20 @@ class Gem::RemoteFetcher
head ? response : response.body
when Net::HTTPMovedPermanently, Net::HTTPFound, Net::HTTPSeeOther,
Net::HTTPTemporaryRedirect then
- raise FetchError.new('too many redirects', uri) if depth > 10
unless location = response['Location']
- raise FetchError.new("redirecting but no redirect location was given", uri)
end
location = Gem::UriParser.parse_uri location
if https?(uri) && !https?(location)
- raise FetchError.new("redirecting to non-https resource: #{location}", uri)
end
fetch_http(location, last_modified, head, depth + 1)
else
- raise FetchError.new("bad response #{response.message} #{response.code}", uri)
end
end
@@ -254,21 +260,21 @@ class Gem::RemoteFetcher
begin
data = Gem::Util.gunzip data
rescue Zlib::GzipFile::Error
- raise FetchError.new("server did not return a valid file", uri)
end
end
data
rescue Timeout::Error, IOError, SocketError, SystemCallError,
*(OpenSSL::SSL::SSLError if Gem::HAVE_OPENSSL) => e
- raise FetchError.new("#{e.class}: #{e}", uri)
end
def fetch_s3(uri, mtime = nil, head = false)
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
fetch_https public_uri, mtime, head
end