diff options
author | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2019-12-13 20:19:08 +0900 |
---|---|---|
committer | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2019-12-13 20:19:33 +0900 |
commit | 82cc2843a92b286cc13afd0860a4e111d4ea2a0b (patch) | |
tree | a517dedd40d35540930cea7732f5a36e76a549e8 /lib/rubygems/uri_parser.rb | |
parent | 26774351dc5f494253ba031e4bc453dc4dddb2cf (diff) | |
download | ruby-82cc2843a92b286cc13afd0860a4e111d4ea2a0b.tar.gz |
Prepare to release RubyGems 3.1.0 final version.
Diffstat (limited to 'lib/rubygems/uri_parser.rb')
-rw-r--r-- | lib/rubygems/uri_parser.rb | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/rubygems/uri_parser.rb b/lib/rubygems/uri_parser.rb new file mode 100644 index 0000000000..5c7cabc436 --- /dev/null +++ b/lib/rubygems/uri_parser.rb @@ -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 |