Skip to content

Commit

Permalink
Fix parsing of scheme that are invalid Ruby constant names
Browse files Browse the repository at this point in the history
Some symbols such as `-`, `+` or `.` are valid inside URI schemes.
See: https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
  • Loading branch information
byroot authored and eregon committed Jul 28, 2021
1 parent 86de79c commit bc47bf7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/uri/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ def self.for(scheme, *arguments, default: Generic)
const_name = scheme.to_s.upcase

uri_class = INITIAL_SCHEMES[const_name]
if !uri_class && !const_name.empty? && Schemes.const_defined?(const_name, false)
uri_class = Schemes.const_get(const_name, false)
uri_class ||= if /\A[A-Z]\w*\z/.match?(const_name) && Schemes.const_defined?(const_name, false)
Schemes.const_get(const_name, false)
end
uri_class ||= default

Expand Down
7 changes: 7 additions & 0 deletions test/uri/test_generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ def test_parse
assert_equal(nil, url.userinfo)
end

def test_parse_scheme_with_symbols
# Valid schemes from https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
assert_equal 'ms-search', URI.parse('ms-search://localhost').scheme
assert_equal 'microsoft.windows.camera', URI.parse('microsoft.windows.camera://localhost').scheme
assert_equal 'coaps+ws', URI.parse('coaps+ws:localhost').scheme
end

def test_merge
u1 = URI.parse('http://foo')
u2 = URI.parse('http://foo/')
Expand Down

0 comments on commit bc47bf7

Please sign in to comment.