Skip to content

Fix parsing of scheme that are invalid Ruby constant names #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2021
Merged
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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be interesting to add a test for register_scheme with a -, . or +.
Probably ends up in an exception, but better than e.g. allowing to register but then the class is not used when parsing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I wasn't sure if the intent was to have register_scheme be a public API or not. My priority was to fix the parsing of arbitrary schemes.

If register is public API, then I'd rather leave it to you to fix it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I"d consider it public API because it is documented in

uri/lib/uri.rb

Line 33 in 86de79c

# register_scheme 'RSYNC', RSYNC


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