Skip to content

Commit

Permalink
Update URI::Generic.build/build2 to use RFC3986_PARSER
Browse files Browse the repository at this point in the history
In bb83f32, a new URI parser was introduced to support RFC3986, where
previously support was limited to RFC2396. The default parser used by
URI() was updated to point to the new parser.

However, the parser used by URI::Generic.build and build2 still points
at the old parser. Among other things this means that hostnames with
underscores can't correctly be parsed by this method.

This commit switches to explicitly use the newer parser for these
methods, and adds a previously failing test that now passes.
  • Loading branch information
gareth committed May 28, 2024
1 parent a0425d9 commit a473de3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/uri/generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def self.build(args)
# +fragment+::
# Part of the URI after '#' character.
# +parser+::
# Parser for internal use [URI::DEFAULT_PARSER by default].
# Parser for internal use [URI::RFC3986_PARSER by default].
# +arg_check+::
# Check arguments [false by default].
#
Expand All @@ -171,7 +171,7 @@ def initialize(scheme,
path, opaque,
query,
fragment,
parser = DEFAULT_PARSER,
parser = RFC3986_PARSER,
arg_check = false)
@scheme = nil
@user = nil
Expand All @@ -182,7 +182,7 @@ def initialize(scheme,
@query = nil
@opaque = nil
@fragment = nil
@parser = parser == DEFAULT_PARSER ? nil : parser
@parser = parser == RFC3986_PARSER ? nil : parser

if arg_check
self.scheme = scheme
Expand Down Expand Up @@ -284,13 +284,13 @@ def registry # :nodoc:

# Returns the parser to be used.
#
# Unless a URI::Parser is defined, DEFAULT_PARSER is used.
# Unless a URI::Parser is defined, RFC3986_PARSER is used.
#
def parser
if !defined?(@parser) || !@parser
DEFAULT_PARSER
RFC3986_PARSER
else
@parser || DEFAULT_PARSER
@parser || RFC3986_PARSER
end
end

Expand Down
4 changes: 4 additions & 0 deletions test/uri/test_generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,10 @@ def test_build
assert_equal(":5432", u.to_s)
assert_equal(5432, u.port)

u = URI::Generic.build(:host => "underscore_host.test")
assert_equal("//underscore_host.test", u.to_s)
assert_equal("underscore_host.test", u.host)

u = URI::Generic.build(:scheme => "http", :host => "::1", :path => "/bar/baz")
assert_equal("http://[::1]/bar/baz", u.to_s)
assert_equal("[::1]", u.host)
Expand Down

0 comments on commit a473de3

Please sign in to comment.