Skip to content

Commit

Permalink
Move URI parsing to a separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
deivid-rodriguez committed Dec 8, 2019
1 parent e6ce7b2 commit 361e080
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 18 deletions.
1 change: 1 addition & 0 deletions Manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ lib/rubygems/test_utilities.rb
lib/rubygems/text.rb
lib/rubygems/uninstaller.rb
lib/rubygems/uri_formatter.rb
lib/rubygems/uri_parser.rb
lib/rubygems/user_interaction.rb
lib/rubygems/util.rb
lib/rubygems/util/licenses.rb
Expand Down
43 changes: 25 additions & 18 deletions lib/rubygems/remote_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'rubygems/request/connection_pools'
require 'rubygems/s3_uri_signer'
require 'rubygems/uri_formatter'
require 'rubygems/uri_parser'
require 'rubygems/user_interaction'
require 'resolv'
require 'rubygems/deprecate'
Expand Down Expand Up @@ -31,23 +32,31 @@ class FetchError < Gem::Exception
def initialize(message, uri)
super message

if uri.is_a?(String)
begin
uri = URI.parse(uri)
rescue URI::InvalidURIError
@uri = uri
return
end
end
uri = parse_uri(uri)

uri.password = 'REDACTED' if uri.respond_to?(:password) && uri.password

uri.password = 'REDACTED' if uri.password
@uri = uri.to_s
end

def to_s # :nodoc:
"#{super} (#{uri})"
end

private

def parse_uri(source_uri)
return source_uri unless source_uri.is_a?(String)

uri_parser.parse(source_uri)
end

def uri_parser
require "uri"

Gem::UriParser.new
end

end

##
Expand Down Expand Up @@ -350,15 +359,13 @@ def close_all
def parse_uri(source_uri)
return source_uri unless source_uri.is_a?(String)

# 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(source_uri)
rescue
URI.parse(URI::DEFAULT_PARSER.escape(source_uri))
end
uri_parser.parse!(source_uri)
end

def uri_parser
require "uri"

@uri_parser ||= Gem::UriParser.new
end

def proxy_for(proxy, uri)
Expand Down
36 changes: 36 additions & 0 deletions lib/rubygems/uri_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

##
# The UriParser handles parsing URIs.
#

class Gem::UriParser

##
# Parses the #uri, raising if it's invalid

def parse!(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

0 comments on commit 361e080

Please sign in to comment.