Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions lib/uri/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ def self.scheme_list
end

# :stopdoc:
INITIAL_SCHEMES = scheme_list
# Constant names of the bundled schemes, by lowercase name, to skip Schemes.escape.
INITIAL_SCHEMES = %w[file ftp http https ldap ldaps mailto ws wss].map { |scheme| [scheme, scheme.upcase.to_sym] }.to_h
private_constant :INITIAL_SCHEMES
Ractor.make_shareable(INITIAL_SCHEMES) if defined?(Ractor)
# :startdoc:
Expand All @@ -185,11 +186,8 @@ def self.scheme_list
# # => #<URI::HTTP foo://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top>
#
def self.for(scheme, *arguments, default: Generic)
const_name = Schemes.escape(scheme)

uri_class = INITIAL_SCHEMES[const_name]
uri_class ||= Schemes.find(const_name)
uri_class ||= default
const_name = INITIAL_SCHEMES[scheme] || Schemes.escape(scheme)
uri_class = Schemes.find(const_name) || default

return uri_class.new(scheme, *arguments)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/uri/generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ def password=(password)
# See also URI::Generic.userinfo=.
#
def set_userinfo(user, password = nil)
unless password
if user && !password
user, password = split_userinfo(user)
end
@user = user
Expand Down
9 changes: 9 additions & 0 deletions test/uri/test_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ def test_register_scheme_lowercase
assert_equal(DEFAULT_SCHEMES, URI.scheme_list.keys.sort)
end

def test_register_scheme_overrides_bundled_scheme
http = Class.new(URI::HTTP)
EnvUtil.suppress_warning { URI.register_scheme 'HTTP', http }
assert_equal http, URI.parse('http://localhost').class
assert_equal http, URI.for('http', nil, 'localhost', nil, nil, '/', nil, nil, nil).class
ensure
EnvUtil.suppress_warning { URI.register_scheme 'HTTP', URI::HTTP }
end

def test_register_scheme_with_symbols
# Valid schemes from https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
list = []
Expand Down