summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDavid Rodríguez <[email protected]>2021-08-24 12:02:29 +0200
committerHiroshi SHIBATA <[email protected]>2021-08-31 19:06:14 +0900
commit1e290c31f4fdfd330b9cd1d5c7fe61efa4ab066c ()
tree0d3e58f3df45c7ab430375fe27550db6f6df6a65 /lib
parentf0c6cc14b10616a61d3113dd5a88291fe915461b (diff)
[rubygems/rubygems] Merge `Gem::UriParser` and `Gem::PrintableUri` into a `Gem::Uri` class
The new class is a wrapper on top of an URI. And then, when you want credentials redacted, you call `#redacted` that returns a copy of itself, but with credentials redacted. https://.com/rubygems/rubygems/commit/9581c2740a
Notes: Merged: https://.com/ruby/ruby/pull/4789
-rw-r--r--lib/rubygems/commands/install_command.rb5
-rw-r--r--lib/rubygems/printable_uri.rb61
-rw-r--r--lib/rubygems/remote_fetcher.rb17
-rw-r--r--lib/rubygems/request.rb3
-rw-r--r--lib/rubygems/uri.rb102
-rw-r--r--lib/rubygems/uri_parser.rb42
6 files changed, 113 insertions, 117 deletions
@@ -5,7 +5,6 @@ require_relative '../dependency_installer'
require_relative '../local_remote_options'
require_relative '../validator'
require_relative '../version_option'
-require_relative '../printable_uri'
##
# Gem installer command line tool
@@ -261,8 +260,8 @@ You can use `i` command instead of `install`.
errors.each do |x|
return unless Gem::SourceFetchProblem === x
- printable_uri = Gem::PrintableUri.parse_uri(x.source.uri.clone)
- msg = "Unable to pull data from '#{printable_uri}': #{x.error.message}"
alert_warning msg
end
@@ -1,61 +0,0 @@
-# frozen_string_literal: true
-
-require_relative 'uri_parser'
-
-class Gem::PrintableUri
- def self.parse_uri(uri)
- printable_uri = new(uri)
- printable_uri.parse_uri
-
- printable_uri
- end
-
- def initialize(original_uri)
- @original_uri = original_uri
- end
-
- def parse_uri
- @original_uri = Gem::UriParser.parse_uri(@original_uri)
- @uri = @original_uri.dup
- redact_credential if valid_uri?
- end
-
- def valid_uri?
- @uri.respond_to?(:user) &&
- @uri.respond_to?(:user=) &&
- @uri.respond_to?(:password) &&
- @uri.respond_to?(:password=)
- end
-
- def original_password
- @original_uri.password
- end
-
- def to_s
- @uri.to_s
- end
-
- private
-
- def redact_credential
- if token?
- @uri.user = 'REDACTED'
- elsif oauth_basic?
- @uri.user = 'REDACTED'
- elsif password?
- @uri.password = 'REDACTED'
- end
- end
-
- def password?
- end
-
- def oauth_basic?
- @uri.password == 'x-oauth-basic'
- end
-
- def token?
- [email protected]? && @uri.password.nil?
- end
-end
@@ -4,8 +4,7 @@ require_relative 'request'
require_relative 'request/connection_pools'
require_relative 's3_uri_signer'
require_relative 'uri_formatter'
-require_relative 'uri_parser'
-require_relative 'printable_uri'
require_relative 'user_interaction'
##
@@ -26,12 +25,12 @@ class Gem::RemoteFetcher
attr_accessor :uri, :original_uri
def initialize(message, uri)
- @original_uri = uri.dup
- uri = Gem::PrintableUri.parse_uri(uri)
- super(uri.valid_uri? && uri.original_password ? message.sub(uri.original_password, 'REDACTED') : message)
- @uri = uri.to_s
end
def to_s # :nodoc:
@@ -127,7 +126,7 @@ class Gem::RemoteFetcher
require "fileutils"
FileUtils.mkdir_p cache_dir rescue nil unless File.exist? cache_dir
- source_uri = Gem::UriParser.parse_uri(source_uri)
scheme = source_uri.scheme
@@ -222,7 +221,7 @@ class Gem::RemoteFetcher
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)
@@ -240,7 +239,7 @@ class Gem::RemoteFetcher
# Downloads +uri+ and returns it as a String.
def fetch_path(uri, mtime = nil, head = false)
- uri = Gem::UriParser.parse_uri uri
unless uri.scheme
raise ArgumentError, "uri scheme is invalid: #{uri.scheme.inspect}"
@@ -184,7 +184,6 @@ class Gem::Request
def perform_request(request) # :nodoc:
connection = connection_for @uri
- uri = Gem::PrintableUri.parse_uri(@uri)
retried = false
bad_response = false
@@ -192,7 +191,7 @@ class Gem::Request
begin
@requests[connection.object_id] += 1
- verbose "#{request.method} #{uri}"
file_name = File.basename(@uri.path)
# perform download progress reporter only for gems
@@ -0,0 +1,102 @@
@@ -1,42 +0,0 @@
-# frozen_string_literal: true
-
-##
-# The UriParser handles parsing URIs.
-#
-
-class Gem::UriParser
- def self.parse_uri(source_uri)
- return source_uri unless source_uri.is_a?(String)
-
- new.parse(source_uri)
- end
-
- ##
- # Parses the #uri, raising if it's invalid
-
- def parse!(uri)
- require "uri"
-
- raise URI::InvalidURIError unless uri
-
- # 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.
- begin
- URI.parse(uri)
- rescue URI::InvalidURIError
- URI.parse(URI::DEFAULT_PARSER.escape(uri))
- end
- end
-
- ##
- # Parses the #uri, returning the original uri if it's invalid
-
- def parse(uri)
- parse!(uri)
- rescue URI::InvalidURIError
- uri
- end
-end